Skip to content

Commit 32f21c7

Browse files
authored
fix(TabPanel): export role from useTabPanel hook (#45)
1 parent adf9d2d commit 32f21c7

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/TabPanel.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ export function useTabPanel({
6060
mountOnEnter,
6161
transition,
6262
unmountOnExit,
63+
role = 'tabpanel',
6364
...props
6465
}: TabPanelProps): [any, TabPanelMetadata] {
6566
const context = useContext(TabContext);
6667

6768
if (!context)
6869
return [
69-
props,
70+
{
71+
...props,
72+
role,
73+
},
7074
{
7175
eventKey,
7276
isActive: active,
@@ -82,6 +86,7 @@ export function useTabPanel({
8286
return [
8387
{
8488
...props,
89+
role,
8590
id: getControlledId(eventKey!),
8691
'aria-labelledby': getControllerId(eventKey!),
8792
},
@@ -136,7 +141,6 @@ const TabPanel: DynamicRefForwardingComponent<'div', TabPanelProps> =
136141
<Component
137142
{...tabPanelProps}
138143
ref={ref}
139-
role="tabpanel"
140144
hidden={!isActive}
141145
aria-hidden={!isActive}
142146
/>

test/TabPanelSpec.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ describe('<TabPanel>', () => {
1212
getByText('test').should.exist;
1313
});
1414

15+
it('should render a TabPanel with role tabpanel', () => {
16+
const { getByRole } = render(<TabPanel active>test</TabPanel>);
17+
18+
getByRole('tabpanel').should.exist;
19+
});
20+
1521
it('should not render if not active and mountOnEnter=true', () => {
1622
const { queryByText } = render(<TabPanel mountOnEnter>test</TabPanel>);
1723

@@ -82,6 +88,44 @@ describe('<TabPanel>', () => {
8288
});
8389

8490
describe('useTabPanel', () => {
91+
it('should have role set to tabpanel', () => {
92+
let props: any;
93+
function Wrapper(wrapperProps: any) {
94+
const [_props] = useTabPanel(wrapperProps);
95+
props = _props;
96+
return null;
97+
}
98+
99+
render(<Wrapper />);
100+
101+
props.role.should.equal('tabpanel');
102+
});
103+
104+
it('should have role tabpanel also within a context', () => {
105+
let props: any;
106+
function Wrapper(wrapperProps: any) {
107+
const [_props] = useTabPanel(wrapperProps);
108+
props = _props;
109+
return null;
110+
}
111+
112+
render(
113+
<TabContext.Provider
114+
value={{
115+
onSelect: sinon.spy(),
116+
mountOnEnter: true,
117+
unmountOnExit: false,
118+
getControlledId: sinon.spy(),
119+
getControllerId: sinon.spy(),
120+
}}
121+
>
122+
<Wrapper />
123+
</TabContext.Provider>,
124+
);
125+
126+
props.role.should.equal('tabpanel');
127+
});
128+
85129
it('should use mountOnEnter from props if provided', () => {
86130
let meta: any;
87131
function Wrapper(wrapperProps: any) {

0 commit comments

Comments
 (0)