Skip to content

Commit

Permalink
feat(Dropdown): allow selecting an item when pressing the spacebar (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
nelsonleite authored and layershifter committed Aug 7, 2019
1 parent f595fc2 commit 9bcc76a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,11 @@ export default class Dropdown extends Component {
debug('selectItemOnEnter()', keyboardKey.getKey(e))
const { search } = this.props

if (keyboardKey.getCode(e) !== keyboardKey.Enter) return
if (
keyboardKey.getCode(e) !== keyboardKey.Enter &&
keyboardKey.getCode(e) !== keyboardKey.Spacebar
)
return
e.preventDefault()

const optionSize = _.size(this.getMenuOptions())
Expand Down
20 changes: 20 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,26 @@ describe('Dropdown', () => {
.at(1)
.should.have.props({ selected: true, active: true })
})
it('becomes active on spacebar when open', () => {
wrapperMount(<Dropdown options={options} selection />)
wrapper.simulate('click')

// initial item props
wrapper
.find('DropdownItem')
.at(1)
.should.have.props({ selected: false, active: false })

// select and make active
domEvent.keyDown(document, { key: 'ArrowDown' })
domEvent.keyDown(document, { key: 'Spacebar' })
wrapper.update()

wrapper
.find('DropdownItem')
.at(1)
.should.have.props({ selected: true, active: true })
})
it('closes the menu', () => {
wrapperMount(<Dropdown options={options} selection />).simulate('click')

Expand Down

0 comments on commit 9bcc76a

Please sign in to comment.