Skip to content

Commit a81ec97

Browse files
authored
fix: issue 5711 from React-Bootstrap (#53)
1 parent 1aeaaf3 commit a81ec97

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Nav.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const Nav: DynamicRefForwardingComponent<'div', NavProps> = React.forwardRef<
8787
const activeChild = currentListNode.querySelector<HTMLElement>(
8888
'[aria-selected=true]',
8989
);
90-
if (!activeChild) return null;
90+
if (!activeChild || activeChild !== document.activeElement) return null;
9191

9292
const index = items.indexOf(activeChild);
9393
if (index === -1) return null;

test/NavSpec.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-disable mocha/no-hooks-for-single-case */
2+
3+
import { mount } from 'enzyme';
4+
import Tabs from '../src/Tabs';
5+
import Nav from '../src/Nav';
6+
import NavItem from '../src/NavItem';
7+
8+
describe('<Nav>', () => {
9+
let focusableContainer;
10+
11+
beforeEach(() => {
12+
focusableContainer = document.createElement('div');
13+
document.body.appendChild(focusableContainer);
14+
});
15+
16+
afterEach(() => {
17+
document.body.removeChild(focusableContainer);
18+
focusableContainer = null;
19+
});
20+
21+
it('When Arrow key is pressed and a nom NavItem element is the activeElement, then the activeElement keeps the same element', () => {
22+
const wrapper = mount(
23+
<Tabs defaultActiveKey="1">
24+
<Nav>
25+
<NavItem eventKey="1">One</NavItem>
26+
<NavItem eventKey="2">Two</NavItem>
27+
<input type="text" autoFocus />
28+
</Nav>
29+
</Tabs>,
30+
{ attachTo: focusableContainer },
31+
);
32+
wrapper.find(NavItem).at(0).simulate('keydown', { key: 'ArrowRight' });
33+
expect(document.activeElement).to.equal(wrapper.find('input').getDOMNode());
34+
});
35+
});

0 commit comments

Comments
 (0)