Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions THIRD-PARTY-LICENSES
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tom@chillidonut.com>
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.
28 changes: 12 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions pages/flashbar/runtime-content.page.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand All @@ -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('');

Expand Down
2 changes: 1 addition & 1 deletion src/column-layout/flexible-column-layout/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/column-layout/grid-column-layout.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/grid/internal.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down
62 changes: 62 additions & 0 deletions src/internal/vendor/react-keyed-flatten-children/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}, []);
}
6 changes: 3 additions & 3 deletions src/space-between/internal.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// 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';

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';
Expand Down Expand Up @@ -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 (
<div key={key} className={styles.child}>
Expand Down
Loading