Skip to content

Commit cb531c0

Browse files
committed
PR feedback
1 parent 1bdf50e commit cb531c0

File tree

4 files changed

+32
-46
lines changed

4 files changed

+32
-46
lines changed

packages/react/slot/src/Slot.stories.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@ export const ButtonAsLink = () => (
5757
<>
5858
<h1>Button with left/right icons</h1>
5959
<Button
60-
iconLeft={<FakeIcon color="tomato" />}
61-
iconRight={<FakeIcon color="royalblue" />}
60+
iconLeft={<MockIcon color="tomato" />}
61+
iconRight={<MockIcon color="royalblue" />}
6262
ref={console.log}
6363
>
64-
Click <em>me</em>!
64+
Button <em>text</em>
6565
</Button>
6666

6767
<h1>Button with left/right icons as link (asChild)</h1>
6868
<Button
6969
asChild
70-
iconLeft={<FakeIcon color="tomato" />}
71-
iconRight={<FakeIcon color="royalblue" />}
70+
iconLeft={<MockIcon color="tomato" />}
71+
iconRight={<MockIcon color="royalblue" />}
7272
ref={console.log}
7373
>
7474
<a href="https://radix-ui.com">
75-
Click <em>me</em>!
75+
Button <em>text</em>
7676
</a>
7777
</Button>
7878
</>
@@ -211,18 +211,18 @@ export const Chromatic = () => (
211211
</ErrorBoundary>
212212

213213
<h2>Button with left/right icons</h2>
214-
<Button iconLeft={<FakeIcon color="tomato" />} iconRight={<FakeIcon color="royalblue" />}>
215-
Click <em>me</em>!
214+
<Button iconLeft={<MockIcon color="tomato" />} iconRight={<MockIcon color="royalblue" />}>
215+
Button <em>text</em>
216216
</Button>
217217

218218
<h2>Button with left/right icons as link (asChild)</h2>
219219
<Button
220220
asChild
221-
iconLeft={<FakeIcon color="tomato" />}
222-
iconRight={<FakeIcon color="royalblue" />}
221+
iconLeft={<MockIcon color="tomato" />}
222+
iconRight={<MockIcon color="royalblue" />}
223223
>
224224
<a href="https://radix-ui.com">
225-
Click <em>me</em>!
225+
Button <em>text</em>
226226
</a>
227227
</Button>
228228
</>
@@ -325,7 +325,7 @@ const Button = React.forwardRef<
325325
);
326326
});
327327

328-
const FakeIcon = React.forwardRef<React.ElementRef<'span'>, React.ComponentProps<'span'>>(
328+
const MockIcon = React.forwardRef<React.ElementRef<'span'>, React.ComponentProps<'span'>>(
329329
({ color = 'tomato', ...props }, forwardedRef) => (
330330
<span
331331
ref={forwardedRef}

packages/react/slot/src/Slot.test.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,8 @@ describe('given a Button with Slottable', () => {
112112
describe('without asChild', () => {
113113
it('should render a button with icon on the left/right', async () => {
114114
const tree = render(
115-
<Button
116-
iconLeft={<span data-testid="left">left</span>}
117-
iconRight={<span data-testid="right">right</span>}
118-
>
119-
Click <em>me</em>!
115+
<Button iconLeft={<span>left</span>} iconRight={<span>right</span>}>
116+
Button <em>text</em>
120117
</Button>
121118
);
122119

@@ -127,13 +124,9 @@ describe('given a Button with Slottable', () => {
127124
describe('with asChild', () => {
128125
it('should render a link with icon on the left/right', async () => {
129126
const tree = render(
130-
<Button
131-
iconLeft={<span data-testid="left">left</span>}
132-
iconRight={<span data-testid="right">right</span>}
133-
asChild
134-
>
127+
<Button iconLeft={<span>left</span>} iconRight={<span>right</span>} asChild>
135128
<a href="https://radix-ui.com">
136-
Click <em>me</em>!
129+
Button <em>text</em>
137130
</a>
138131
</Button>
139132
);

packages/react/slot/src/Slot.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ const Slot = React.forwardRef<HTMLElement, SlotProps>((props, forwardedRef) => {
1515
const slottable = childrenArray.find(isSlottable);
1616

1717
if (slottable) {
18-
const newElement = slottable.props.children;
18+
// the new element to render is the one passed as a child of `Slottable`
19+
const newElement = slottable.props.children as React.ReactNode;
20+
1921
const newChildren = childrenArray.map((child) => {
2022
if (child === slottable) {
21-
const slottableChildren = slottable.props.children as React.ReactNode;
22-
if (React.Children.count(slottableChildren) > 1) return React.Children.only(null);
23-
return React.isValidElement(slottableChildren)
24-
? (slottableChildren.props.children as React.ReactNode)
23+
// because the new element will be the one rendered, we are only interested
24+
// in grabbing its children (`newElement.props.children`)
25+
if (React.Children.count(newElement) > 1) return React.Children.only(null);
26+
return React.isValidElement(newElement)
27+
? (newElement.props.children as React.ReactNode)
2528
: null;
2629
} else {
2730
return child;

packages/react/slot/src/__snapshots__/Slot.test.tsx.snap

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@ exports[`given a Button with Slottable with asChild should render a link with ic
55
<a
66
href="https://radix-ui.com"
77
>
8-
<span
9-
data-testid="left"
10-
>
8+
<span>
119
left
1210
</span>
13-
Click
11+
Button
1412
<em>
15-
me
13+
text
1614
</em>
17-
!
18-
<span
19-
data-testid="right"
20-
>
15+
<span>
2116
right
2217
</span>
2318
</a>
@@ -27,19 +22,14 @@ exports[`given a Button with Slottable with asChild should render a link with ic
2722
exports[`given a Button with Slottable without asChild should render a button with icon on the left/right 1`] = `
2823
<div>
2924
<button>
30-
<span
31-
data-testid="left"
32-
>
25+
<span>
3326
left
3427
</span>
35-
Click
28+
Button
3629
<em>
37-
me
30+
text
3831
</em>
39-
!
40-
<span
41-
data-testid="right"
42-
>
32+
<span>
4333
right
4434
</span>
4535
</button>

0 commit comments

Comments
 (0)