Skip to content

Commit 1c23c7d

Browse files
authored
fix(Dropdown): avoid calling onToggle when tabbing if menu ref not set (#959)
1 parent b0363c0 commit 1c23c7d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Dropdown.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ function Dropdown({
248248
return;
249249
}
250250

251+
if (!menuRef.current && key === 'Tab') {
252+
return;
253+
}
254+
251255
lastSourceEvent.current = event.type;
252256

253257
switch (key) {

test/DropdownSpec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,26 @@ describe('<Dropdown>', () => {
335335
});
336336
});
337337

338+
it('should not call onToggle if the menu is not open and "tab" is pressed', () => {
339+
const onToggleSpy = sinon.spy();
340+
const wrapper = mount(<SimpleDropdown onToggle={onToggleSpy} />, {
341+
attachTo: focusableContainer,
342+
});
343+
344+
const toggle = wrapper.find('.toggle').getDOMNode();
345+
toggle.focus();
346+
347+
simulant.fire(toggle, 'keydown', {
348+
key: 'Tab',
349+
});
350+
351+
simulant.fire(document, 'keyup', {
352+
key: 'Tab',
353+
});
354+
355+
onToggleSpy.should.not.be.called;
356+
});
357+
338358
describe('popper config', () => {
339359
it('can add modifiers', (done) => {
340360
const spy = sinon.spy();

0 commit comments

Comments
 (0)