Skip to content

Commit

Permalink
fix(Dropdown): change active item on keyboard up/down (#1735)
Browse files Browse the repository at this point in the history
* fix(Dropdown): change active item on keyboard up/down

* fix(Dropdown): change active item on keyboard up/down

* refactor(Dropdown): simplify move constant
  • Loading branch information
Bebersohl authored and levithomason committed Jun 15, 2017
1 parent 553facc commit c15fc00
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,20 +514,19 @@ export default class Dropdown extends Component {
}

moveSelectionOnKeyDown = (e) => {
debug('moveSelectionOnKeyDown()')
debug(keyboardKey.getName(e))
switch (keyboardKey.getCode(e)) {
case keyboardKey.ArrowDown:
e.preventDefault()
this.moveSelectionBy(1)
break
case keyboardKey.ArrowUp:
e.preventDefault()
this.moveSelectionBy(-1)
break
default:
break
debug('moveSelectionOnKeyDown()', keyboardKey.getName(e))

const { multiple } = this.props
const moves = {
[keyboardKey.ArrowDown]: 1,
[keyboardKey.ArrowUp]: -1,
}
const move = moves[keyboardKey.getCode(e)]

if (move === undefined) return
e.preventDefault()
this.moveSelectionBy(move)
if (!multiple) this.makeSelectedItemActive(e)
}

openOnSpace = (e) => {
Expand Down
18 changes: 18 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,24 @@ describe('Dropdown', () => {
.find('div.text')
.should.contain.text(nextItem.text)
})

it('updates value on down arrow', () => {
wrapperMount(<Dropdown options={options} selection />)

wrapper.simulate('click')
domEvent.keyDown(document, { key: 'ArrowDown' })

wrapper.should.have.state('value', options[1].value)
})

it('updates value on up arrow', () => {
wrapperMount(<Dropdown options={options} selection />)

wrapper.simulate('click')
domEvent.keyDown(document, { key: 'ArrowUp' })

wrapper.should.have.state('value', options[4].value)
})
})

describe('text', () => {
Expand Down

0 comments on commit c15fc00

Please sign in to comment.