Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tabs] Fix Tabs accessibility issue #1869

Merged
merged 1 commit into from Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions UNRELEASED.md
Expand Up @@ -15,6 +15,7 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f
### Bug fixes

- Fixed accessibility issue with ChoiceList errors not being correctly connected to the inputs ([#1824](https://github.com/Shopify/polaris-react/pull/1824));
- Fixed `Tab` `aria-controls` pointing to a non-existent `Panel` `id` ([#1869](https://github.com/Shopify/polaris-react/pull/1869))

### Documentation

Expand Down
4 changes: 4 additions & 0 deletions src/components/Tabs/Tabs.scss
Expand Up @@ -116,6 +116,10 @@ $focus-state-box-shadow-color: rgba(92, 106, 196, 0.8);
}
}

.Panel-hidden {
dleroux marked this conversation as resolved.
Show resolved Hide resolved
display: none;
}

.List {
list-style: none;
margin: 0;
Expand Down
28 changes: 20 additions & 8 deletions src/components/Tabs/Tabs.tsx
Expand Up @@ -79,14 +79,26 @@ class Tabs extends React.PureComponent<CombinedProps, State> {
const {tabToFocus, visibleTabs, hiddenTabs, showDisclosure} = this.state;
const disclosureTabs = hiddenTabs.map((tabIndex) => tabs[tabIndex]);

const panelMarkup = children ? (
<Panel
id={tabs[selected].panelID || `${tabs[selected].id}-panel`}
tabID={tabs[selected].id}
>
{children}
</Panel>
) : null;
const panelMarkup = children
? tabs.map((_tab, index) => {
return selected === index ? (
<Panel
id={tabs[index].panelID || `${tabs[index].id}-panel`}
tabID={tabs[index].id}
key={tabs[index].id}
>
{children}
</Panel>
) : (
<Panel
id={tabs[index].panelID || `${tabs[index].id}-panel`}
tabID={tabs[index].id}
key={tabs[index].id}
hidden
/>
);
})
: null;

const tabsMarkup = visibleTabs
.sort((tabA, tabB) => tabA - tabB)
Expand Down
7 changes: 5 additions & 2 deletions src/components/Tabs/components/Panel/Panel.tsx
@@ -1,16 +1,19 @@
import * as React from 'react';
import {classNames} from '@shopify/css-utilities';
import styles from '../../Tabs.scss';

export interface Props {
hidden?: boolean;
id: string;
tabID: string;
children?: React.ReactNode;
}

export default function Panel({id, tabID, children}: Props) {
export default function Panel({hidden, id, tabID, children}: Props) {
const className = classNames(styles.Panel, hidden && styles['Panel-hidden']);
return (
<div
className={styles.Panel}
className={className}
id={id}
role="tabpanel"
aria-labelledby={tabID}
Expand Down
25 changes: 22 additions & 3 deletions src/components/Tabs/tests/Tabs.test.tsx
Expand Up @@ -168,15 +168,34 @@ describe('<Tabs />', () => {
});
});

describe('children', () => {
describe('panel', () => {
it('renders a Panel for each of the Tabs', () => {
const content = <p>Tab content</p>;
const wrapper = mountWithAppProvider(
<Tabs {...mockProps}>{content}</Tabs>,
);
const panel = wrapper.find(Panel);
expect(panel).toHaveLength(2);
});

it('renders a Panel with a hidden prop for the non selected tabs', () => {
const content = <p>Tab content</p>;
const wrapper = mountWithAppProvider(
<Tabs {...mockProps}>{content}</Tabs>,
);

const nonSelectedPanel = wrapper.find(Panel).at(1);
expect(nonSelectedPanel.prop('hidden')).toBe(true);
});

it('wraps the children in a Panel with matching aria attributes to the tab', () => {
const content = <p>Tab content</p>;
const wrapper = mountWithAppProvider(
<Tabs {...mockProps}>{content}</Tabs>,
);

const selectedTab = wrapper.find(Tab).at(0);
const panel = wrapper.find(Panel);
const panel = wrapper.find(Panel).at(0);
expect(panel.exists()).toBe(true);
expect(panel.contains(content)).toBe(true);
expect(panel.prop('id')).toBeTruthy();
Expand All @@ -195,7 +214,7 @@ describe('<Tabs />', () => {
</Tabs>,
);

const panel = wrapper.find(Panel);
const panel = wrapper.find(Panel).at(0);
const selectedTab = wrapper.find(Tab).at(0);
expect(panel.prop('id')).toBe(selectedTab.prop('panelID'));
});
Expand Down