|
1 | | -import { Children, FC, ReactNode } from 'react'; |
| 1 | +import { FC, Fragment, isValidElement, ReactNode } from 'react'; |
2 | 2 |
|
3 | 3 | import { spacings } from 'src/atoms/style'; |
4 | 4 |
|
@@ -57,28 +57,68 @@ type Primitive = string | number; |
57 | 57 | const isPrimitive = (child: unknown): child is Primitive => |
58 | 58 | typeof child === 'string' || typeof child === 'number'; |
59 | 59 |
|
| 60 | +const flattenChildren = (children: ReactNode): ReactNode[] => { |
| 61 | + const result: ReactNode[] = []; |
| 62 | + |
| 63 | + const visit = (node: ReactNode) => { |
| 64 | + if ( |
| 65 | + node === null || |
| 66 | + node === undefined || |
| 67 | + node === true || |
| 68 | + node === false |
| 69 | + ) { |
| 70 | + return; |
| 71 | + } |
| 72 | + if (Array.isArray(node)) { |
| 73 | + node.forEach(visit); |
| 74 | + return; |
| 75 | + } |
| 76 | + if (isValidElement(node) && node.type === Fragment) { |
| 77 | + visit((node.props as { children?: ReactNode }).children); |
| 78 | + return; |
| 79 | + } |
| 80 | + result.push(node); |
| 81 | + }; |
| 82 | + |
| 83 | + visit(children); |
| 84 | + return result; |
| 85 | +}; |
| 86 | + |
60 | 87 | const coalesceChildren = ( |
61 | 88 | children: ReactNode, |
62 | 89 | Wrapper: FC<{ children: ReactNode }> |
63 | 90 | ): ReactNode[] => { |
64 | | - const all = Children.toArray(children); |
| 91 | + const all = flattenChildren(children); |
65 | 92 | const result: ReactNode[] = []; |
66 | 93 | let buffer: Primitive[] = []; |
67 | 94 |
|
| 95 | + let primitiveGroupCount = 0; |
| 96 | + |
68 | 97 | const flush = () => { |
69 | 98 | if (buffer.length === 0) return; |
70 | 99 | result.push( |
71 | | - <Wrapper key={`primitive-${result.length}`}>{buffer.join('')}</Wrapper> |
| 100 | + <Wrapper key={`primitive-${primitiveGroupCount++}`}> |
| 101 | + {buffer.join('')} |
| 102 | + </Wrapper> |
72 | 103 | ); |
73 | 104 | buffer = []; |
74 | 105 | }; |
75 | 106 |
|
| 107 | + let elementCount = 0; |
| 108 | + |
76 | 109 | all.forEach((child) => { |
77 | 110 | if (isPrimitive(child)) { |
78 | 111 | buffer.push(child); |
79 | 112 | } else { |
80 | 113 | flush(); |
81 | | - result.push(child); |
| 114 | + if (isValidElement(child) && child.key == null) { |
| 115 | + result.push( |
| 116 | + <Fragment key={`node-${elementCount++}`}>{child}</Fragment> |
| 117 | + ); |
| 118 | + } else { |
| 119 | + elementCount++; |
| 120 | + result.push(child); |
| 121 | + } |
82 | 122 | } |
83 | 123 | }); |
84 | 124 | flush(); |
|
0 commit comments