Skip to content

Commit 3bc83a2

Browse files
ryo-manbasnowystingeryihuiliao
authored
fix: labelable option to Tab props for aria-label support (#5408)
* fix: labelable option to Tab props for aria-label support * Add test for RSP component as well --------- Co-authored-by: Robert Snow <rsnow@adobe.com> Co-authored-by: Yihui Liao <44729383+yihuiliao@users.noreply.github.com>
1 parent 9e6513b commit 3bc83a2

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function useTab<T>(
5858
let {tabIndex} = itemProps;
5959

6060
let item = state.collection.getItem(key);
61-
let domProps = filterDOMProps(item?.props, {isLink: !!item?.props?.href});
61+
let domProps = filterDOMProps(item?.props, {isLink: !!item?.props?.href, labelable: true});
6262
delete domProps.id;
6363

6464
return {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,30 @@ describe('Tabs', function () {
782782
expect(tabPanelInput.value).toBe('');
783783
});
784784

785+
it('Tabs can be aria-labelled', () => {
786+
let {getAllByRole, getByLabelText} = render(
787+
<Provider theme={theme}>
788+
<Tabs aria-label="Tab Example" maxWidth={500}>
789+
<TabList>
790+
<Item aria-label="Foo">Tab 1</Item>
791+
<Item>Tab 2</Item>
792+
</TabList>
793+
<TabPanels>
794+
<Item>
795+
<input data-testid="panel1_input" />
796+
</Item>
797+
<Item>
798+
<input disabled data-testid="panel2_input" />
799+
</Item>
800+
</TabPanels>
801+
</Tabs>
802+
</Provider>
803+
);
804+
805+
let tab = getByLabelText('Foo');
806+
expect(tab).toBe(getAllByRole('tab')[0]);
807+
});
808+
785809
it('supports custom props for parent tabs element', function () {
786810
let {getByTestId} = renderComponent({'data-testid': 'tabs1'});
787811
let tabs = getByTestId('tabs1');

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,24 @@ describe('Tabs', () => {
313313
fireEvent.keyDown(tabs[1], {key: 'ArrowRight'});
314314
expect(tabs[2]).toHaveAttribute('aria-selected', 'true');
315315
});
316+
317+
it('should render tab with aria-label', () => {
318+
let {getAllByRole} = render(
319+
<Tabs>
320+
<TabList>
321+
<Tab id="a" aria-label="Tab A">A</Tab>
322+
<Tab id="b" aria-label="Tab B">B</Tab>
323+
<Tab id="c" aria-label="Tab C">C</Tab>
324+
</TabList>
325+
<TabPanel id="a">A</TabPanel>
326+
<TabPanel id="b">B</TabPanel>
327+
<TabPanel id="c">C</TabPanel>
328+
</Tabs>
329+
);
330+
331+
let tabs = getAllByRole('tab');
332+
expect(tabs[0]).toHaveAttribute('aria-label', 'Tab A');
333+
expect(tabs[1]).toHaveAttribute('aria-label', 'Tab B');
334+
expect(tabs[2]).toHaveAttribute('aria-label', 'Tab C');
335+
});
316336
});

0 commit comments

Comments
 (0)