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): not calls onChange when value is not changed on item click #3482

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
14 changes: 10 additions & 4 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ export default class Dropdown extends Component {
debug('handleItemClick()', item)

const { multiple, search } = this.props
const { value: currentValue } = this.state
const { value } = item

// prevent toggle() in handleClick()
Expand All @@ -686,14 +687,19 @@ export default class Dropdown extends Component {

const isAdditionItem = item['data-additional']
const newValue = multiple ? _.union(this.state.value, [value]) : value
const valueHasChanged = multiple
? !!_.difference(newValue, currentValue).length
: newValue !== currentValue

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

this.clearSearchQuery()
this.handleChange(e, newValue)
}

this.handleChange(e, newValue)
this.clearSearchQuery()
this.closeOnChange(e)

// Heads up! This event handler should be called after `onChange`
Expand Down
19 changes: 19 additions & 0 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,25 @@ describe('Dropdown', () => {
spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch({}, { value: randomValue })
})
it('is not called when value is not changed on item click', () => {
wrapperMount(<Dropdown options={options} selection onChange={spy} />)

wrapper
.simulate('click')
.find('DropdownItem')
.at(0)
.simulate('click')
spy.should.have.been.calledOnce()
dropdownMenuIsClosed()

wrapper
.simulate('click')
.find('DropdownItem')
.at(0)
.simulate('click')
spy.should.have.been.calledOnce()
dropdownMenuIsClosed()
})
it('is called with event and value when pressing enter on a selected item', () => {
const firstValue = options[0].value
wrapperMount(<Dropdown options={options} selection onChange={spy} />).simulate('click')
Expand Down