diff --git a/THIRD-PARTY-LICENSES b/THIRD-PARTY-LICENSES index 5bfc45cee7..48f408bc43 100644 --- a/THIRD-PARTY-LICENSES +++ b/THIRD-PARTY-LICENSES @@ -277,3 +277,38 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +--- + +Name: react-keyed-flatten-children +Version: 3.2.0 +License: MIT +Private: false +Description: Flattens React children and fragments to an array with predictable and stable keys +Repository: git+https://github.com/grrowl/react-keyed-flatten-children.git +Homepage: https://github.com/grrowl/react-keyed-flatten-children +Author: Tom McKenzie +License Copyright: +=== + +MIT License + +Copyright (c) 2019 Tom McKenzie + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/package-lock.json b/package-lock.json index 380c4b26b7..8f889fab23 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "date-fns": "^2.25.0", "intl-messageformat": "^10.3.1", "mnth": "^2.0.0", - "react-keyed-flatten-children": "^2.2.1", + "react-is": ">=16.8.0", "react-transition-group": "^4.4.2", "tslib": "^2.4.0", "weekstart": "^1.1.0" @@ -52,6 +52,7 @@ "@types/node": "^20.17.14", "@types/react": "^16.14.20", "@types/react-dom": "^16.9.14", + "@types/react-is": "^19.2.0", "@types/react-router": "^5.1.18", "@types/react-router-dom": "^5.3.2", "@types/react-test-renderer": "^16.9.12", @@ -4590,6 +4591,16 @@ "@types/react": "^16" } }, + "node_modules/@types/react-is": { + "version": "19.2.0", + "resolved": "https://registry.npmjs.org/@types/react-is/-/react-is-19.2.0.tgz", + "integrity": "sha512-NP2xtcjZfORsOa4g2JwdseyEnF+wUCx25fTdG/J/HIY6yKga6+NozRBg2xR2gyh7kKYyd6DXndbq0YbQuTJ7Ew==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/react": "*" + } + }, "node_modules/@types/react-router": { "version": "5.1.20", "dev": true, @@ -17371,21 +17382,6 @@ }, "node_modules/react-is": { "version": "17.0.2", - "dev": true, - "license": "MIT" - }, - "node_modules/react-keyed-flatten-children": { - "version": "2.2.1", - "license": "MIT", - "dependencies": { - "react-is": "^18.2.0" - }, - "peerDependencies": { - "react": ">=15.0.0" - } - }, - "node_modules/react-keyed-flatten-children/node_modules/react-is": { - "version": "18.3.1", "license": "MIT" }, "node_modules/react-router": { diff --git a/package.json b/package.json index 336ecde7f4..95b118b646 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "date-fns": "^2.25.0", "intl-messageformat": "^10.3.1", "mnth": "^2.0.0", - "react-keyed-flatten-children": "^2.2.1", + "react-is": ">=16.8.0", "react-transition-group": "^4.4.2", "tslib": "^2.4.0", "weekstart": "^1.1.0" diff --git a/pages/flashbar/runtime-content.page.tsx b/pages/flashbar/runtime-content.page.tsx index d25d1e8e80..85497f2781 100644 --- a/pages/flashbar/runtime-content.page.tsx +++ b/pages/flashbar/runtime-content.page.tsx @@ -1,8 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import React, { ReactNode, useContext, useState } from 'react'; -import flattenChildren from 'react-keyed-flatten-children'; +import React, { isValidElement, ReactNode, useContext, useState } from 'react'; import { Box, @@ -16,6 +15,7 @@ import { SpaceBetween, } from '~components'; import awsuiPlugins from '~components/internal/plugins'; +import flattenChildren from '~components/internal/vendor/react-keyed-flatten-children'; import { mount, unmount } from '~mount'; import AppContext, { AppContextType } from '../app/app-context'; @@ -27,7 +27,7 @@ type PageContext = React.Context< const nodeAsString = (node: ReactNode) => flattenChildren(node) - .map(node => (typeof node === 'object' ? node.props.children : node)) + .map(node => (isValidElement(node) ? node.props.children : node)) .filter(node => typeof node === 'string') .join(''); diff --git a/src/column-layout/flexible-column-layout/index.tsx b/src/column-layout/flexible-column-layout/index.tsx index c80c38f2d0..f2ca4fa174 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/vendor/react-keyed-flatten-children'; import { InternalColumnLayoutProps } from '../interfaces'; import styles from './styles.css.js'; diff --git a/src/column-layout/grid-column-layout.tsx b/src/column-layout/grid-column-layout.tsx index 067c725df8..3cf9bbb976 100644 --- a/src/column-layout/grid-column-layout.tsx +++ b/src/column-layout/grid-column-layout.tsx @@ -1,12 +1,12 @@ // 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 { GridProps } from '../grid/interfaces'; import InternalGrid from '../grid/internal'; import { useContainerBreakpoints } from '../internal/hooks/container-queries'; +import flattenChildren from '../internal/vendor/react-keyed-flatten-children'; import { InternalColumnLayoutProps } from './interfaces'; import { COLUMN_TRIGGERS, ColumnLayoutBreakpoint } from './internal'; import { repeat } from './util'; diff --git a/src/grid/internal.tsx b/src/grid/internal.tsx index cc7c05881c..6ea3849735 100644 --- a/src/grid/internal.tsx +++ b/src/grid/internal.tsx @@ -1,7 +1,6 @@ // 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, { ClassValue } from 'clsx'; import { useMergeRefs, warnOnce } from '@cloudscape-design/component-toolkit/internal'; @@ -11,6 +10,7 @@ import { Breakpoint, matchBreakpointMapping } from '../internal/breakpoints'; import { useContainerBreakpoints } from '../internal/hooks/container-queries'; import { InternalBaseComponentProps } from '../internal/hooks/use-base-component'; import { isDevelopment } from '../internal/is-development'; +import flattenChildren from '../internal/vendor/react-keyed-flatten-children'; import { GridProps } from './interfaces'; import styles from './styles.css.js'; diff --git a/src/internal/vendor/react-keyed-flatten-children/index.ts b/src/internal/vendor/react-keyed-flatten-children/index.ts new file mode 100644 index 0000000000..38774dc398 --- /dev/null +++ b/src/internal/vendor/react-keyed-flatten-children/index.ts @@ -0,0 +1,62 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Vendored from react-keyed-flatten-children@3.2.0 (MIT License) +// Original source: https://github.com/grrowl/react-keyed-flatten-children/tree/120a286144c7fdc76b64daadaeae0df7d255b497 + +import { Children, cloneElement, isValidElement, ReactElement, ReactNode } from 'react'; + +// React 19 renamed the element symbol to detect version mismatches and prevent subtle bugs from +// inlined JSX. We check both symbols to support React <19 (legacy) and >=19 (transitional). +// The name "transitional" indicates this will change again in future versions. +// Reference: https://github.com/facebook/react/pull/28813 +const REACT_LEGACY_ELEMENT_TYPE: symbol = Symbol.for('react.element'); +const REACT_ELEMENT_TYPE: symbol = Symbol.for('react.transitional.element'); + +const REACT_FRAGMENT_TYPE: symbol = Symbol.for('react.fragment'); + +// Simplified version of react-is typeOf that checks for fragment types only +// Reference: https://github.com/facebook/react/blob/main/packages/react-is/src/ReactIs.js#L40 +function typeOfFragment(object: any): symbol | undefined { + if (typeof object === 'object' && object !== null) { + const $$typeof = object.$$typeof; + if ($$typeof === REACT_ELEMENT_TYPE || $$typeof === REACT_LEGACY_ELEMENT_TYPE) { + const type = object.type; + if (type === REACT_FRAGMENT_TYPE) { + return type; + } + } + } + return undefined; +} + +function isFragment(object: any): boolean { + return typeOfFragment(object) === REACT_FRAGMENT_TYPE; +} + +/* Returns React children into an array, flattening fragments. */ + +function isFragmentWithChildren(node: unknown): node is ReactElement<{ children: ReactNode }> { + return isFragment(node); +} + +export default function flattenChildren( + children: ReactNode, + depth: number = 0, + keys: (string | number)[] = [] +): ReactNode[] { + return Children.toArray(children).reduce((acc: ReactNode[], node, nodeIndex) => { + if (isFragmentWithChildren(node)) { + acc.push(...flattenChildren(node.props.children, depth + 1, keys.concat(node.key || nodeIndex))); + } else if (isValidElement(node)) { + acc.push( + cloneElement(node, { + key: keys.concat(String(node.key)).join('.'), + }) + ); + } else if (typeof node === 'string' || typeof node === 'number' || typeof node === 'bigint') { + acc.push(node); + } + return acc; + }, []); +} diff --git a/src/space-between/internal.tsx b/src/space-between/internal.tsx index f15e362fb2..ea7a4f6037 100644 --- a/src/space-between/internal.tsx +++ b/src/space-between/internal.tsx @@ -1,7 +1,6 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -import React, { forwardRef } from 'react'; -import flattenChildren from 'react-keyed-flatten-children'; +import React, { forwardRef, isValidElement } from 'react'; import clsx from 'clsx'; import { useMergeRefs } from '@cloudscape-design/component-toolkit/internal'; @@ -9,6 +8,7 @@ import { useMergeRefs } from '@cloudscape-design/component-toolkit/internal'; import { getBaseProps } from '../internal/base-component'; import { InternalBaseComponentProps } from '../internal/hooks/use-base-component'; import WithNativeAttributes from '../internal/utils/with-native-attributes'; +import flattenChildren from '../internal/vendor/react-keyed-flatten-children'; import { SpaceBetweenProps } from './interfaces'; import styles from './styles.css.js'; @@ -52,7 +52,7 @@ const InternalSpaceBetween = forwardRef( ref={mergedRef} > {flattenedChildren.map(child => { - const key = typeof child === 'object' ? child.key : undefined; + const key = isValidElement(child) ? child.key : undefined; return (