From 0178ba039c7a1019e261edc021c474d9737dc846 Mon Sep 17 00:00:00 2001 From: Amanuel Sisay Date: Mon, 16 Feb 2026 08:20:21 +0100 Subject: [PATCH 1/2] fix: use chilren.toArray() for the react 19+ --- .../flexible-column-layout/index.tsx | 4 ++-- src/column-layout/grid-column-layout.tsx | 2 +- src/grid/internal.tsx | 4 ++-- src/internal/utils/flatten-children.ts | 17 +++++++++++++++++ src/space-between/internal.tsx | 7 ++++--- 5 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 src/internal/utils/flatten-children.ts diff --git a/src/column-layout/flexible-column-layout/index.tsx b/src/column-layout/flexible-column-layout/index.tsx index c80c38f2d0..270b60a98a 100644 --- a/src/column-layout/flexible-column-layout/index.tsx +++ b/src/column-layout/flexible-column-layout/index.tsx @@ -1,11 +1,11 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import React from 'react'; -import flattenChildren from 'react-keyed-flatten-children'; import clsx from 'clsx'; import { useContainerQuery } from '@cloudscape-design/component-toolkit'; +import { flattenChildren } from '../../internal/utils/flatten-children'; import { InternalColumnLayoutProps } from '../interfaces'; import styles from './styles.css.js'; @@ -68,7 +68,7 @@ export default function FlexibleColumnLayout({ > {flattenedChildren.map((child, i) => { // If this react child is a primitive value, the key will be undefined - const key = (child as Record<'key', unknown>).key; + const key = child && typeof child === 'object' ? (child as Record<'key', unknown>).key : undefined; return (
{flattenedChildren.map((child, i) => { // If this react child is a primitive value, the key will be undefined - const key = (child as Record<'key', unknown>).key; + const key = child && typeof child === 'object' ? (child as Record<'key', unknown>).key : undefined; return (
{flattenedChildren.map(child => { - const key = typeof child === 'object' ? child.key : undefined; + // If this react child is a primitive value, the key will be undefined + const key = child && typeof child === 'object' ? (child as Record<'key', unknown>).key : undefined; return ( -
+
{child}
); From 55f3fb74fa5c22bffcd6df6f02bfa4b8fc091b54 Mon Sep 17 00:00:00 2001 From: Amanuel Sisay Date: Mon, 16 Feb 2026 08:20:46 +0100 Subject: [PATCH 2/2] test: add tests for flattenChildren --- .../utils/__tests__/flatten-children.test.tsx | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/internal/utils/__tests__/flatten-children.test.tsx diff --git a/src/internal/utils/__tests__/flatten-children.test.tsx b/src/internal/utils/__tests__/flatten-children.test.tsx new file mode 100644 index 0000000000..8e4a13a173 --- /dev/null +++ b/src/internal/utils/__tests__/flatten-children.test.tsx @@ -0,0 +1,70 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +import React, { Fragment } from 'react'; + +import { flattenChildren } from '../flatten-children'; + +describe('flattenChildren', () => { + const nestedArrayChildren = [ +
Item 1
, + [
Item 2
,
Item 3
], +
Item 4
, + ]; + + const fragmentChildren = [ + + A + B + , + C, + ]; + + const singleFragment = ( + + A + B + + ); + + // Tests React 19+ behavior: uses Children.toArray() which does NOT flatten fragments + describe('React 19+', () => { + beforeEach(() => { + // Mock React.version to trigger Children.toArray() code path + Object.defineProperty(React, 'version', { + value: '19.0.0', + writable: true, + configurable: true, + }); + }); + + it('flattens nested arrays', () => { + expect(flattenChildren(nestedArrayChildren)).toHaveLength(4); + }); + + it('does NOT flatten fragments', () => { + expect(flattenChildren(fragmentChildren)).toHaveLength(2); + expect(flattenChildren(singleFragment)).toHaveLength(1); + }); + }); + + // Tests React 16-18 behavior: uses react-keyed-flatten-children which DOES flatten fragments + describe('React 16-18', () => { + beforeEach(() => { + // Mock React.version to trigger react-keyed-flatten-children code path + Object.defineProperty(React, 'version', { + value: '18.2.0', + writable: true, + configurable: true, + }); + }); + + it('flattens nested arrays', () => { + expect(flattenChildren(nestedArrayChildren)).toHaveLength(4); + }); + + it('flattens fragments', () => { + expect(flattenChildren(fragmentChildren)).toHaveLength(3); + expect(flattenChildren(singleFragment)).toHaveLength(2); + }); + }); +});