Skip to content

Commit

Permalink
fix(Dropdown): prevent overzealous onAddItem calls (#1071)
Browse files Browse the repository at this point in the history
* fix(Dropdown): Fixed onAddItem to compare values to values instead of values to text.

* fix(Dropdown): Fixed Dropdown to identify new items by a property instead of comparing values.
  • Loading branch information
keeslinp authored and levithomason committed Dec 27, 2016
1 parent bcdf523 commit 9281947
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,18 +514,18 @@ export default class Dropdown extends Component {

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

// prevent selecting null if there was no selected item value
// 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 })) {
if (onAddItem && item.additional) {
onAddItem(e, { ...this.props, value })
}

// notify the onChange prop that the user is trying to change value
if (multiple) {
// state value may be undefined
Expand Down Expand Up @@ -611,7 +611,7 @@ export default class Dropdown extends Component {
handleItemClick = (e, item) => {
debug('handleItemClick()')
debug(item)
const { multiple, onAddItem, options } = this.props
const { multiple, onAddItem } = this.props
const { value } = item

// prevent toggle() in handleClick()
Expand All @@ -624,7 +624,7 @@ export default class Dropdown extends Component {
if (item.disabled) return

// notify the onAddItem prop if this is a new value
if (onAddItem && !_.some(options, { text: value })) {
if (onAddItem && item.additional) {
onAddItem(e, { ...this.props, value })
}

Expand Down Expand Up @@ -719,6 +719,7 @@ export default class Dropdown extends Component {
],
value: searchQuery,
className: 'addition',
additional: true,
}
if (additionPosition === 'top') filteredOptions.unshift(addItem)
else filteredOptions.push(addItem)
Expand Down

0 comments on commit 9281947

Please sign in to comment.