Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Dropdown): prevent calling onChange unless value changed #3391

Merged
merged 2 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,27 +576,32 @@ export default class Dropdown extends Component {
}

makeSelectedItemActive = (e) => {
const { open } = this.state
const { open, value } = this.state
const { multiple } = this.props

const item = this.getSelectedItem()
const value = _.get(item, 'value')
const selectedValue = _.get(item, 'value')

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

// state value may be undefined
const newValue = multiple ? _.union(this.state.value, [value]) : value

// notify the onChange prop that the user is trying to change value
this.setValue(newValue)
this.setSelectedIndex(newValue)
this.handleChange(e, newValue)

// Heads up! This event handler should be called after `onChange`
// Notify the onAddItem prop if this is a new value
if (item['data-additional']) _.invoke(this.props, 'onAddItem', e, { ...this.props, value })
const newValue = multiple ? _.union(this.state.value, [selectedValue]) : selectedValue
const valueHasChanged = multiple ? !!_.difference(newValue, value).length : newValue !== value

if (valueHasChanged) {
// notify the onChange prop that the user is trying to change value
this.setValue(newValue)
this.setSelectedIndex(newValue)
this.handleChange(e, newValue)

// Heads up! This event handler should be called after `onChange`
// Notify the onAddItem prop if this is a new value
if (item['data-additional']) {
_.invoke(this.props, 'onAddItem', e, { ...this.props, value: selectedValue })
}
}
}

selectItemOnEnter = (e) => {
Expand Down
25 changes: 21 additions & 4 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,32 @@ describe('Dropdown', () => {
spy.should.have.been.calledWithMatch(event)
})

it('calls makeSelectedItemActive', () => {
wrapperShallow(<Dropdown selectOnBlur />)
it('calls handleChange with the selected option on blur', () => {
wrapperShallow(<Dropdown selectOnBlur options={options} />)

const instance = wrapper.instance()
sandbox.spy(instance, 'makeSelectedItemActive')
wrapper.simulate('click', { stopPropagation: _.noop })
dropdownMenuIsOpen()
sandbox.spy(instance, 'handleChange')

const event = { stopPropagation: _.noop }
wrapper.simulate('blur', event)

instance.handleChange.should.have.been.calledWithMatch(event, options[0].value)
})

it('does not call handleChange if the value has not changed', () => {
wrapperShallow(<Dropdown selectOnBlur options={options} />)

const instance = wrapper.instance()
wrapper.setState({ selectedIndex: 2, value: options[2].value })
wrapper.simulate('click', { stopPropagation: _.noop })
dropdownMenuIsOpen()
sandbox.spy(instance, 'handleChange')

wrapper.simulate('blur')

instance.makeSelectedItemActive.should.have.been.calledOnce()
instance.handleChange.should.not.have.been.called()
})

it('sets focus state to false', () => {
Expand Down