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): fix call order of onAddItem and onChange handlers #2113

Merged
merged 2 commits into from
Sep 24, 2017
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
24 changes: 13 additions & 11 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ export default class Dropdown extends Component {

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

const item = this.getSelectedItem()
const value = _.get(item, 'value')
Expand All @@ -588,21 +588,22 @@ export default class Dropdown extends Component {
// prevent selecting duplicate items when the dropdown is closed
if (_.isNil(value) || !open) return

// notify the onAddItem prop if this is a new value
if (onAddItem && item['data-additional']) onAddItem(e, { ...this.props, value })

// 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 })
}

selectItemOnEnter = (e) => {
debug('selectItemOnEnter()', keyboardKey.getName(e))
const { onAddItem, search } = this.props
const { search } = this.props

if (keyboardKey.getCode(e) !== keyboardKey.Enter) return
e.preventDefault()
Expand All @@ -611,7 +612,7 @@ export default class Dropdown extends Component {
if (search && optionSize === 0) return

const item = this.getSelectedItem()
const isAdditionItem = onAddItem && item['data-additional']
const isAdditionItem = item['data-additional']

this.makeSelectedItemActive(e)
this.closeOnChange(e)
Expand Down Expand Up @@ -700,7 +701,7 @@ export default class Dropdown extends Component {
handleItemClick = (e, item) => {
debug('handleItemClick()', item)

const { multiple, onAddItem, search } = this.props
const { multiple, search } = this.props
const { value } = item

// prevent toggle() in handleClick()
Expand All @@ -709,10 +710,7 @@ export default class Dropdown extends Component {
if (multiple || item.disabled) e.nativeEvent.stopImmediatePropagation()
if (item.disabled) return

// notify the onAddItem prop if this is a new value
const isAdditionItem = onAddItem && item['data-additional']
if (isAdditionItem) onAddItem(e, { ...this.props, value })

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

// notify the onChange prop that the user is trying to change value
Expand All @@ -725,6 +723,10 @@ export default class Dropdown extends Component {
this.handleChange(e, newValue)
this.closeOnChange(e)

// Heads up! This event handler should be called after `onChange`
// Notify the onAddItem prop if this is a new value
if (isAdditionItem) _.invoke(this.props, 'onAddItem', e, { ...this.props, value })

if (multiple && search && this.searchRef) this.searchRef.focus()
}

Expand Down
36 changes: 28 additions & 8 deletions test/specs/modules/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2291,9 +2291,17 @@ describe('Dropdown', () => {
})

it('calls onAddItem prop when clicking new value', () => {
const spy = sandbox.spy()
const onAddItem = sandbox.spy()
const onChange = sandbox.spy()
const search = wrapperMount(
<Dropdown options={customOptions} selection search allowAdditions onAddItem={spy} />,
<Dropdown
allowAdditions
onAddItem={onAddItem}
onChange={onChange}
options={customOptions}
search
selection
/>,
)
.find('input.search')

Expand All @@ -2304,22 +2312,34 @@ describe('Dropdown', () => {
.first()
.simulate('click')

spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch({}, { value: 'boo' })
onChange.should.have.been.calledOnce()
onAddItem.should.have.been.calledOnce()
onAddItem.should.have.been.calledWithMatch({}, { value: 'boo' })
onAddItem.should.have.been.calledImmediatelyAfter(onChange)
})

it('calls onAddItem prop when pressing enter on new value', () => {
const spy = sandbox.spy()
const onAddItem = sandbox.spy()
const onChange = sandbox.spy()
const search = wrapperMount(
<Dropdown options={customOptions} selection search allowAdditions onAddItem={spy} />,
<Dropdown
allowAdditions
onAddItem={onAddItem}
onChange={onChange}
options={customOptions}
search
selection
/>,
)
.find('input.search')

search.simulate('change', { target: { value: 'boo' } })
domEvent.keyDown(document, { key: 'Enter' })

spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch({}, { value: 'boo' })
onChange.should.have.been.calledOnce()
onAddItem.should.have.been.calledOnce()
onAddItem.should.have.been.calledWithMatch({}, { value: 'boo' })
onAddItem.should.have.been.calledImmediatelyAfter(onChange)
})

it('clears value of the searchQuery when selection is only option', () => {
Expand Down