Skip to content

Commit 1aeaaf3

Browse files
fix(NavItem): do not include aria-controls if the related element is not mounted (#40)
* test(TabContainerSpec): converted to use @testing-library/react * test(ModalSpec): re-enables 'Focused state' tests * fix(NavItem): do not include aria-controls if the related TabPanel is not mounted because of unmountOnExit When using `unmountOnExit`, the inactive TabPanels are not mounted, therefore the aria-controls in NavItem would refer an element that doesn't exist. * fix(NavItem): include aria-controls only for the active TabPanel when mountOnEnter is enabled
1 parent 9ef5110 commit 1aeaaf3

File tree

4 files changed

+249
-180
lines changed

4 files changed

+249
-180
lines changed

src/NavItem.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import SelectableContext, { makeEventKey } from './SelectableContext';
77
import { EventKey, DynamicRefForwardingComponent } from './types';
88
import Button from './Button';
99
import { dataAttr } from './DataKey';
10+
import TabContext from './TabContext';
1011

1112
export interface NavItemProps extends React.HTMLAttributes<HTMLElement> {
1213
/**
@@ -54,6 +55,7 @@ export function useNavItem({
5455
}: UseNavItemOptions) {
5556
const parentOnSelect = useContext(SelectableContext);
5657
const navContext = useContext(NavContext);
58+
const tabContext = useContext(TabContext);
5759

5860
let isActive = active;
5961
const props = { role } as any;
@@ -68,10 +70,21 @@ export function useNavItem({
6870
props[dataAttr('event-key')] = key;
6971

7072
props.id = contextControllerId || id;
71-
props['aria-controls'] = contextControlledId;
7273

7374
isActive =
7475
active == null && key != null ? navContext.activeKey === key : active;
76+
77+
/**
78+
* Simplified scenario for `mountOnEnter`.
79+
*
80+
* While it would make sense to keep 'aria-controls' for tabs that have been mounted at least
81+
* once, it would also complicate the code quite a bit, for very little gain.
82+
* The following implementation is probably good enough.
83+
*
84+
* @see https://github.com/react-restart/ui/pull/40#issuecomment-1009971561
85+
*/
86+
if (isActive || (!tabContext?.unmountOnExit && !tabContext?.mountOnEnter))
87+
props['aria-controls'] = contextControlledId;
7588
}
7689

7790
if (props.role === 'tab') {

test/ModalSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ describe('<Modal>', () => {
336336
);
337337
});
338338

339-
xdescribe('Focused state', () => {
339+
describe('Focused state', () => {
340340
let focusableContainer = null;
341341

342342
beforeEach(() => {

test/TabContainerSpec.js

Lines changed: 0 additions & 178 deletions
This file was deleted.

0 commit comments

Comments
 (0)