From d95408cc260897de800c27db6b11953cdf6b699c Mon Sep 17 00:00:00 2001 From: Levi Thomason Date: Sun, 18 Sep 2016 13:38:48 -0700 Subject: [PATCH] fix(Dropdown): skip select item on blur when closed (#508) * fix(Dropdown): skip select item on blur when closed * fix(Dropdown-test): update select on blur test --- src/modules/Dropdown/Dropdown.js | 4 +++- test/specs/modules/Dropdown/Dropdown-test.js | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/modules/Dropdown/Dropdown.js b/src/modules/Dropdown/Dropdown.js index b15a2bec82..8519dd1929 100644 --- a/src/modules/Dropdown/Dropdown.js +++ b/src/modules/Dropdown/Dropdown.js @@ -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) diff --git a/test/specs/modules/Dropdown/Dropdown-test.js b/test/specs/modules/Dropdown/Dropdown-test.js index 44c18000de..aca8af7fb7 100644 --- a/test/specs/modules/Dropdown/Dropdown-test.js +++ b/test/specs/modules/Dropdown/Dropdown-test.js @@ -840,14 +840,19 @@ describe('Dropdown Component', () => { it('is called with event and value when blurring', () => { const firstValue = options[0].value wrapperMount() - .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() + .simulate('focus') + .simulate('blur') + + spy.should.not.have.been.called() + }) it('is not called on blur when selectOnBlur is false', () => { wrapperMount() .simulate('focus')