Skip to content

Commit 04e1ad2

Browse files
khoadLFDanLu
andauthored
issue-2429: Pass item data attributes to DOM (#2655)
* issue-2429: Pass item data attributes to DOM * PR review: extract id from domProps Co-authored-by: Daniel Lu <[email protected]>
1 parent 4a5fa5f commit 04e1ad2

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

packages/@react-spectrum/tabs/src/Tabs.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,13 @@ function Tab<T>(props: TabProps<T>) {
165165
...props
166166
});
167167
let isSelected = state.selectedKey === key;
168+
let domProps = filterDOMProps(item.props);
169+
delete domProps.id;
168170

169171
return (
170172
<FocusRing focusRingClass={classNames(styles, 'focus-ring')}>
171173
<div
172-
{...mergeProps(tabProps, hoverProps)}
174+
{...mergeProps(tabProps, hoverProps, domProps)}
173175
ref={ref}
174176
className={classNames(
175177
styles,

packages/@react-spectrum/tabs/test/Tabs.test.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ let items = [
2424
{name: 'Tab 3', children: 'Tab 3 body'}
2525
];
2626

27-
function renderComponent(props) {
27+
function renderComponent(props, itemProps) {
2828
return render(
2929
<Provider theme={theme}>
3030
<Tabs {...props} items={items}>
3131
<TabList>
3232
{item => (
33-
<Item key={item.name} title={item.name || item.children} />
33+
<Item {...itemProps} key={item.name} title={item.name || item.children} />
3434
)}
3535
</TabList>
3636
<TabPanels>
@@ -638,12 +638,27 @@ describe('Tabs', function () {
638638
expect(tabPanelInput.value).toBe('');
639639
});
640640

641-
it('supports custom props', function () {
641+
it('supports custom props for parent tabs element', function () {
642642
let {getByTestId} = renderComponent({'data-testid': 'tabs1'});
643643
let tabs = getByTestId('tabs1');
644644
expect(tabs).toBeInTheDocument();
645645
});
646646

647+
it('supports custom props for tab items', function () {
648+
let {getAllByTestId} = renderComponent({}, {
649+
'data-testid': 'tabItems',
650+
'data-instance-id': 'instance-id',
651+
'id': 'id-1'
652+
});
653+
let tabItems = getAllByTestId('tabItems');
654+
expect(tabItems).toHaveLength(3);
655+
for (let tabItem of tabItems) {
656+
expect(tabItem).toHaveAttribute('data-instance-id', 'instance-id');
657+
expect(tabItem).not.toHaveAttribute('id', 'id-1');
658+
expect(tabItem).toBeInTheDocument();
659+
}
660+
});
661+
647662
it('fires onSelectionChange when clicking on the current tab', function () {
648663
let container = renderComponent({defaultSelectedKey: items[0].name, onSelectionChange});
649664
let tablist = container.getByRole('tablist');

0 commit comments

Comments
 (0)