Skip to content

fix: Key text children of SpaceBetween, Grid, and ColumnLayout by position - #5340

Open
TrevorBurnham wants to merge 3 commits into
cloudscape-design:mainfrom
TrevorBurnham:fix/flattened-primitive-child-keys
Open

TrevorBurnham wants to merge 3 commits into
cloudscape-design:mainfrom
TrevorBurnham:fix/flattened-primitive-child-keys

Conversation

@TrevorBurnham

@TrevorBurnham TrevorBurnham commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

SpaceBetween, Grid, and flexible ColumnLayout wrap each flattened child in a <div> keyed by the child's key. flattenChildren passes strings and numbers through without a key, so their wrappers got key={undefined}. Any of these components with a text child next to an element, such as <SpaceBetween><Box>...</Box>{t('...')}</SpaceBetween>, logged "Each child in a list should have a unique key prop".

This adds a shared getFlattenedChildKey helper. Elements keep the key from flattening. Primitives are keyed by position as primitive-${index}, which can't collide with element keys: those always contain a . (.0, .$custom, 0..1) on both the React 16–18 and React 19+ flattening paths. ControlGroup already fell back to the index and now uses the same helper. Grid-variant ColumnLayout renders through Grid, so it's covered too.

Related links, issue #, if available: AWSUI-62564

How has this been tested?

  • Unit tests for the helper on both flattening paths, checking that keys are unique for mixed element, string, and number children.
  • Regression tests for SpaceBetween and Grid that assert nothing is logged to console.error. Both fail without the fix.
Review checklist

The following items are to be evaluated by the author(s) and the reviewer(s).

Correctness

  • Changes include appropriate documentation updates.
  • Changes are backward-compatible if not indicated, see CONTRIBUTING.md.
  • Changes do not include unsupported browser features, see CONTRIBUTING.md.
  • Changes were manually tested for accessibility, see accessibility guidelines.

Security

Testing

  • Changes are covered with new/existing unit tests?
  • Changes are covered with new/existing integration tests?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@TrevorBurnham
TrevorBurnham marked this pull request as ready for review November 21, 2026 22:47
@TrevorBurnham
TrevorBurnham requested a review from a team as a code owner November 21, 2026 22:47
@TrevorBurnham
TrevorBurnham requested review from pan-kot and removed request for a team November 21, 2026 22:47
@github-actions
github-actions Bot temporarily deployed to fork-dev-pages-react18 November 24, 2026 09:24 Inactive
@github-actions
github-actions Bot temporarily deployed to fork-dev-pages-react16 November 24, 2026 09:24 Inactive
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.10%. Comparing base (8ad64bc) to head (87a512c).
⚠️ Report is 23 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5340      +/-   ##
==========================================
- Coverage   97.87%   94.10%   -3.77%     
==========================================
  Files        1016     1017       +1     
  Lines       32917    28563    -4354     
  Branches    12211     9888    -2323     
==========================================
- Hits        32216    26880    -5336     
- Misses        695      759      +64     
- Partials        6      924     +918     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

'more text',
];

it.each(['18.2.0', '19.0.0'])('returns unique keys for mixed children in React %s', version => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not an actual React 19 test - why are we doing that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point: overriding React.version only switches the flattenChildren branch, and the test still runs React 16 or 18's Children.toArray, so it isn't testing React 19. I dropped the version loop, so the helper test no longer overrides React.version. The SpaceBetween and Grid tests cover the component behavior.

@github-actions
github-actions Bot temporarily deployed to fork-dev-pages-react16 November 25, 2026 17:26 Inactive
@github-actions
github-actions Bot temporarily deployed to fork-dev-pages-react18 November 25, 2026 17:26 Inactive
@pan-kot
pan-kot requested a balanced review from Copilot November 26, 2026 05:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Keyed portals receive unstable positional keys on the React 19+ path, so reordering or inserting siblings can remount their contents and lose state or focus.

1 open issue
What changed in this PR

This PR addresses React key warnings for mixed text and element children in Cloudscape layout components by sharing wrapper-key generation.

Changes:

  • Adds getFlattenedChildKey to preserve element keys and assign positional keys to primitives.
  • Uses the helper in SpaceBetween, Grid, flexible ColumnLayout, and ControlGroup.
  • Adds helper uniqueness tests and key-warning regressions for SpaceBetween and Grid.
File Description
src/​space-between/​internal.tsx Uses shared wrapper-key generation.
src/​space-between/​__tests__/​space-between.test.tsx Tests mixed children without key warnings.
src/​internal/​utils/​flatten-children.ts Adds the shared child-key helper.
src/​internal/​utils/​__tests__/​flatten-children.test.tsx Tests key uniqueness for mixed children.
src/​grid/​internal.tsx Uses shared wrapper-key generation.
src/​grid/​__tests__/​grid.test.tsx Adds a mixed-child key-warning regression.
src/​control-group/​internal.tsx Replaces its key fallback with the shared helper.
src/​column-layout/​flexible-column-layout/​index.tsx Uses the helper for flexible-layout child wrappers.

Review effort: Balanced

Comment thread src/internal/utils/flatten-children.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The mixed-child key test inherits React 18.2.0 and exercises only the legacy flattening path, leaving the React 19+ Children.toArray path untested.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Low severity Test mixed-child keys on the React 19+ flattening path

src/​internal/​utils/​__tests__/​flatten-children.test.tsx:117

This test only exercises the legacy flattening path: the preceding React 16–18 suite leaves React.version set to 18.2.0, and this separate suite never overrides it. The mixed-child key behavior on the React 19+ Children.toArray path therefore remains untested, despite the stated coverage of both paths. Parameterize this test over the two mocked versions and restore the version afterward; no React 19 installation is needed.

1 resolved since last review

Review effort: Balanced

This branch was successfully deployed

2 active deployments
fork-dev-pages-react18 — 87a512c3 Deployed Nov 28, 2026 by github-actions[bot]
fork-dev-pages-react16 — 87a512c3 Deployed Nov 28, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants