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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TabPanel): export role from useTabPanel hook #45

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/TabPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ export function useTabPanel({
mountOnEnter,
transition,
unmountOnExit,
role = 'tabpanel',
...props
}: TabPanelProps): [any, TabPanelMetadata] {
const context = useContext(TabContext);

if (!context)
return [
props,
{
...props,
role,
},
{
eventKey,
isActive: active,
Expand All @@ -82,6 +86,7 @@ export function useTabPanel({
return [
{
...props,
role,
id: getControlledId(eventKey!),
'aria-labelledby': getControllerId(eventKey!),
},
Expand Down Expand Up @@ -136,7 +141,6 @@ const TabPanel: DynamicRefForwardingComponent<'div', TabPanelProps> =
<Component
{...tabPanelProps}
ref={ref}
role="tabpanel"
hidden={!isActive}
aria-hidden={!isActive}
/>
Expand Down
44 changes: 44 additions & 0 deletions test/TabPanelSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ describe('<TabPanel>', () => {
getByText('test').should.exist;
});

it('should render a TabPanel with role tabpanel', () => {
const { getByRole } = render(<TabPanel active>test</TabPanel>);

getByRole('tabpanel').should.exist;
});

it('should not render if not active and mountOnEnter=true', () => {
const { queryByText } = render(<TabPanel mountOnEnter>test</TabPanel>);

Expand Down Expand Up @@ -82,6 +88,44 @@ describe('<TabPanel>', () => {
});

describe('useTabPanel', () => {
it('should have role set to tabpanel', () => {
let props: any;
function Wrapper(wrapperProps: any) {
const [_props] = useTabPanel(wrapperProps);
props = _props;
return null;
}

render(<Wrapper />);

props.role.should.equal('tabpanel');
});

it('should have role tabpanel also within a context', () => {
let props: any;
function Wrapper(wrapperProps: any) {
const [_props] = useTabPanel(wrapperProps);
props = _props;
return null;
}

render(
<TabContext.Provider
value={{
onSelect: sinon.spy(),
mountOnEnter: true,
unmountOnExit: false,
getControlledId: sinon.spy(),
getControllerId: sinon.spy(),
}}
>
<Wrapper />
</TabContext.Provider>,
);

props.role.should.equal('tabpanel');
});

it('should use mountOnEnter from props if provided', () => {
let meta: any;
function Wrapper(wrapperProps: any) {
Expand Down