Skip to content

Commit

Permalink
fix(Dropdown): skip select item on blur when closed (#508)
Browse files Browse the repository at this point in the history
* fix(Dropdown): skip select item on blur when closed

* fix(Dropdown-test): update select on blur test
  • Loading branch information
levithomason committed Sep 18, 2016
1 parent 2b84844 commit d95408c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,13 @@ export default class Dropdown extends Component {
}

selectHighlightedItem = (e) => {
const { open } = this.state
const { multiple, onAddItem, options } = this.props
const value = _.get(this.getSelectedItem(), 'value')

// prevent selecting null if there was no selected item value
if (!value) return
// prevent selecting duplicate items when the dropdown is closed
if (!value || !open) return

// notify the onAddItem prop if this is a new value
if (onAddItem && !_.some(options, { text: value })) onAddItem(value)
Expand Down
13 changes: 9 additions & 4 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,14 +840,19 @@ describe('Dropdown Component', () => {
it('is called with event and value when blurring', () => {
const firstValue = options[0].value
wrapperMount(<Dropdown options={options} selection onChange={spy} />)
.simulate('focus')
.simulate('click')

wrapper.simulate('blur')
.simulate('focus') // open, highlights first item
.simulate('blur') // blur should activate selected item

spy.should.have.been.calledOnce()
spy.firstCall.args[1].should.deep.equal(firstValue)
})
it('is not called on blur when closed', () => {
wrapperMount(<Dropdown options={options} selection open={false} onChange={spy} />)
.simulate('focus')
.simulate('blur')

spy.should.not.have.been.called()
})
it('is not called on blur when selectOnBlur is false', () => {
wrapperMount(<Dropdown options={options} selection onChange={spy} selectOnBlur={false} />)
.simulate('focus')
Expand Down

0 comments on commit d95408c

Please sign in to comment.