Skip to content

Commit 9a9327f

Browse files
andrewchoughAndrew ChoughLFDanLu
authored
Pass in ID into generateId function if it exists, add test to validate new prop (#4855)
Co-authored-by: Andrew Chough <[email protected]> Co-authored-by: Daniel Lu <[email protected]>
1 parent 3aa6dbe commit 9a9327f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

packages/@react-aria/tabs/src/useTabPanel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function useTabPanel<T>(props: AriaTabPanelProps, state: TabListState<T>,
3434
// within the tabpanel.
3535
let tabIndex = useHasTabbableChild(ref) ? undefined : 0;
3636

37-
const id = generateId(state, state?.selectedKey, 'tabpanel');
37+
const id = generateId(state, props.id ?? state?.selectedKey, 'tabpanel');
3838
const tabPanelProps = useLabels({...props, id, 'aria-labelledby': generateId(state, state?.selectedKey, 'tab')});
3939

4040
return {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,34 @@ describe('Tabs', () => {
181181
expect(tabs[0]).toHaveClass('selected');
182182
});
183183

184+
it('should update TabPanel ID when current tab is changed', () => {
185+
let onSelectionChange = jest.fn();
186+
let {getByRole, getAllByRole} = render(
187+
<Tabs onSelectionChange={onSelectionChange}>
188+
<TabList>
189+
<Tab id="first-element">First</Tab>
190+
<Tab id="second-element">Second</Tab>
191+
<Tab id="third-element">Third</Tab>
192+
</TabList>
193+
<TabPanel id="first-element">First</TabPanel>
194+
<TabPanel id="second-element">Second</TabPanel>
195+
<TabPanel id="third-element">Third</TabPanel>
196+
</Tabs>
197+
);
198+
199+
expect(getByRole('tabpanel').getAttribute('id')).toContain('first-element');
200+
let tabs = getAllByRole('tab');
201+
202+
userEvent.click(tabs[1]);
203+
expect(onSelectionChange).toHaveBeenCalled();
204+
expect(getByRole('tabpanel').getAttribute('id')).toContain('second-element');
205+
206+
userEvent.click(tabs[2]);
207+
expect(onSelectionChange).toHaveBeenCalled();
208+
expect(getByRole('tabpanel').getAttribute('id')).toContain('third-element');
209+
210+
});
211+
184212
it('should support orientation', () => {
185213
let className = ({orientation}) => orientation;
186214
let {getByRole} = renderTabs({orientation: 'vertical', className}, {className});

0 commit comments

Comments
 (0)