Skip to content

Commit 9bcc76a

Browse files
nelsonleitelayershifter
authored andcommitted
feat(Dropdown): allow selecting an item when pressing the spacebar (#3702)
Due to accessibility matters (and similarity to the native <select>), this adds the possibility to select an item via Spacebar and not only with Enter
1 parent f595fc2 commit 9bcc76a

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/modules/Dropdown/Dropdown.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,11 @@ export default class Dropdown extends Component {
579579
debug('selectItemOnEnter()', keyboardKey.getKey(e))
580580
const { search } = this.props
581581

582-
if (keyboardKey.getCode(e) !== keyboardKey.Enter) return
582+
if (
583+
keyboardKey.getCode(e) !== keyboardKey.Enter &&
584+
keyboardKey.getCode(e) !== keyboardKey.Spacebar
585+
)
586+
return
583587
e.preventDefault()
584588

585589
const optionSize = _.size(this.getMenuOptions())

test/specs/modules/Dropdown/Dropdown-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,26 @@ describe('Dropdown', () => {
10581058
.at(1)
10591059
.should.have.props({ selected: true, active: true })
10601060
})
1061+
it('becomes active on spacebar when open', () => {
1062+
wrapperMount(<Dropdown options={options} selection />)
1063+
wrapper.simulate('click')
1064+
1065+
// initial item props
1066+
wrapper
1067+
.find('DropdownItem')
1068+
.at(1)
1069+
.should.have.props({ selected: false, active: false })
1070+
1071+
// select and make active
1072+
domEvent.keyDown(document, { key: 'ArrowDown' })
1073+
domEvent.keyDown(document, { key: 'Spacebar' })
1074+
wrapper.update()
1075+
1076+
wrapper
1077+
.find('DropdownItem')
1078+
.at(1)
1079+
.should.have.props({ selected: true, active: true })
1080+
})
10611081
it('closes the menu', () => {
10621082
wrapperMount(<Dropdown options={options} selection />).simulate('click')
10631083

0 commit comments

Comments
 (0)