Skip to content

Commit 1403cd3

Browse files
committed
Merge pull request react-bootstrap#1375 from taion/dropdown-focus
Use role to determine dropdown open focus behavior
2 parents a38bed7 + 4fe8565 commit 1403cd3

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/Dropdown.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class Dropdown extends React.Component {
4848
this.state = {};
4949

5050
this.lastOpenEventType = null;
51-
this.isKeyboardClick = false;
5251
}
5352

5453
componentDidMount() {
@@ -83,7 +82,7 @@ class Dropdown extends React.Component {
8382
let children = this.extractChildren();
8483
let Component = this.props.componentClass;
8584

86-
let props = omit(this.props, ['id']);
85+
let props = omit(this.props, ['id', 'role']);
8786

8887
const rootClasses = {
8988
open: this.props.open,
@@ -120,8 +119,7 @@ class Dropdown extends React.Component {
120119
return;
121120
}
122121

123-
this.toggleOpen(this.isKeyboardClick ? 'keydown' : 'click');
124-
this.isKeyboardClick = false;
122+
this.toggleOpen('click');
125123
}
126124

127125
handleKeyDown(event) {
@@ -142,10 +140,6 @@ class Dropdown extends React.Component {
142140
case keycode.codes.tab:
143141
this.handleClose(event);
144142
break;
145-
case keycode.codes.space:
146-
case keycode.codes.enter:
147-
this.isKeyboardClick = true;
148-
break;
149143
default:
150144
}
151145
}
@@ -166,7 +160,7 @@ class Dropdown extends React.Component {
166160

167161
if (
168162
this.lastOpenEventType === 'keydown' ||
169-
this.props.alwaysFocusNextOnOpen
163+
this.props.role === 'menuitem'
170164
) {
171165
menu.focusNext();
172166
}
@@ -227,7 +221,8 @@ class Dropdown extends React.Component {
227221
let toggleProps = {
228222
open,
229223
id: this.props.id,
230-
ref: TOGGLE_REF
224+
ref: TOGGLE_REF,
225+
role: this.props.role
231226
};
232227

233228
toggleProps.onClick = createChainedFunction(
@@ -327,9 +322,10 @@ Dropdown.propTypes = {
327322
onSelect: React.PropTypes.func,
328323

329324
/**
330-
* Focus first menu item on menu open on all events, not just keydown events.
325+
* If `'menuitem'`, causes the dropdown to behave like a menu item rather than
326+
* a menu button.
331327
*/
332-
alwaysFocusNextOnOpen: React.PropTypes.bool
328+
role: React.PropTypes.string
333329
};
334330

335331
Dropdown = uncontrollable(Dropdown, { open: 'onToggle' });

test/DropdownSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ describe('Dropdown', () => {
416416
// I am fairly confident that the failure is due to a test specific conflict and not an actual bug.
417417
it('when open and the key "esc" is pressed the menu is closed and focus is returned to the button', () => {
418418
const instance = React.render(
419-
<Dropdown defaultOpen alwaysFocusNextOnOpen id='test-id'>
419+
<Dropdown defaultOpen role="menuitem" id="test-id">
420420
{dropdownChildren}
421421
</Dropdown>
422422
, focusableContainer);

0 commit comments

Comments
 (0)