Skip to content

Commit af5b9ae

Browse files
committed
🚧 Requested changes
1 parent b831c94 commit af5b9ae

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

src/molecules/Button/Button.stories.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,18 @@ export const Introduction: Story = {
6161
render: (args) => <Button {...args}>You can control me</Button>,
6262
};
6363

64+
const test = 'test';
65+
6466
export const Basic: Story = {
6567
render: () => (
6668
<div
6769
style={{ display: 'flex', flexDirection: 'column', gap: spacings.medium }}
6870
>
6971
<div style={{ display: 'flex', gap: spacings.medium }}>
70-
<Button>Filled</Button>
72+
<Button>
73+
<Icon data={save} />
74+
Filled {test} text
75+
</Button>
7176
<Button variant="outlined">Outlined</Button>
7277
<Button variant="ghost">Ghost</Button>
7378
</div>

src/molecules/Button/ContentWrapper.tsx

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Children, FC, ReactNode } from 'react';
1+
import { FC, Fragment, isValidElement, ReactNode } from 'react';
22

33
import { spacings } from 'src/atoms/style';
44

@@ -57,28 +57,68 @@ type Primitive = string | number;
5757
const isPrimitive = (child: unknown): child is Primitive =>
5858
typeof child === 'string' || typeof child === 'number';
5959

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+
6087
const coalesceChildren = (
6188
children: ReactNode,
6289
Wrapper: FC<{ children: ReactNode }>
6390
): ReactNode[] => {
64-
const all = Children.toArray(children);
91+
const all = flattenChildren(children);
6592
const result: ReactNode[] = [];
6693
let buffer: Primitive[] = [];
6794

95+
let primitiveGroupCount = 0;
96+
6897
const flush = () => {
6998
if (buffer.length === 0) return;
7099
result.push(
71-
<Wrapper key={`primitive-${result.length}`}>{buffer.join('')}</Wrapper>
100+
<Wrapper key={`primitive-${primitiveGroupCount++}`}>
101+
{buffer.join('')}
102+
</Wrapper>
72103
);
73104
buffer = [];
74105
};
75106

107+
let elementCount = 0;
108+
76109
all.forEach((child) => {
77110
if (isPrimitive(child)) {
78111
buffer.push(child);
79112
} else {
80113
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+
}
82122
}
83123
});
84124
flush();

0 commit comments

Comments
 (0)