Skip to content

Commit 174c8af

Browse files
authored
feat(RAC): support render props in Breadcrumbs (#6440)
1 parent 0f832b6 commit 174c8af

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

packages/react-aria-components/src/Breadcrumbs.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import {AriaBreadcrumbsProps} from 'react-aria';
1313
import {Collection, Node} from 'react-stately';
1414
import {CollectionProps, useCollection, useSSRCollectionNode} from './Collection';
15-
import {ContextValue, forwardRefType, SlotProps, StyleProps, useContextProps} from './utils';
15+
import {ContextValue, forwardRefType, SlotProps, StyleProps, useContextProps, useRenderProps} from './utils';
1616
import {filterDOMProps} from '@react-aria/utils';
1717
import {Key} from '@react-types/shared';
1818
import {LinkContext} from './Link';
@@ -104,14 +104,22 @@ function BreadcrumbItem({node, isCurrent, isDisabled, onAction}: BreadcrumbItemP
104104
onPress: () => onAction?.(node.key)
105105
};
106106

107+
let renderProps = useRenderProps({
108+
...node.props,
109+
children: node.rendered,
110+
values: {isDisabled: isDisabled || isCurrent, isCurrent},
111+
defaultClassName: 'react-aria-Breadcrumb'
112+
});
113+
107114
return (
108115
<li
109116
{...filterDOMProps(node.props)}
117+
{...renderProps}
110118
ref={node.props.ref}
111-
style={node.props.style}
112-
className={node.props.className ?? 'react-aria-Breadcrumb'}>
119+
data-disabled={isDisabled || isCurrent || undefined}
120+
data-current={isCurrent || undefined}>
113121
<LinkContext.Provider value={linkProps}>
114-
{node.rendered}
122+
{renderProps.children}
115123
</LinkContext.Provider>
116124
</li>
117125
);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2024 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import {Breadcrumb, Breadcrumbs, Link} from 'react-aria-components';
14+
import React from 'react';
15+
16+
export default {
17+
title: 'React Aria Components',
18+
component: Breadcrumbs
19+
};
20+
21+
export const BreadcrumbsExample = (args: any) => (
22+
<Breadcrumbs {...args}>
23+
<Breadcrumb>
24+
<Link href="/">Home</Link>
25+
</Breadcrumb>
26+
<Breadcrumb>
27+
<Link href="/react-aria">React Aria</Link>
28+
</Breadcrumb>
29+
<Breadcrumb>
30+
<Link href="/react-aria">Breadcrumbs</Link>
31+
</Breadcrumb>
32+
</Breadcrumbs>
33+
);

packages/react-aria-components/test/Breadcrumbs.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,20 @@ describe('Breadcrumbs', () => {
103103
let item = getByRole('listitem');
104104
expect(breadcrumbRef.current).toBe(item);
105105
});
106+
107+
it('should support render props', () => {
108+
let items = [
109+
{id: 1, name: 'Item 1'},
110+
{id: 2, name: 'Item 2'},
111+
{id: 3, name: 'Item 3'}
112+
];
113+
114+
let {getAllByRole} = render(
115+
<Breadcrumbs items={items}>
116+
{(item) => <Breadcrumb>{({isCurrent}) => isCurrent ? 'Current' : item.name}</Breadcrumb>}
117+
</Breadcrumbs>
118+
);
119+
120+
expect(getAllByRole('listitem').map((it) => it.textContent)).toEqual(['Item 1', 'Item 2', 'Current']);
121+
});
106122
});

0 commit comments

Comments
 (0)