Skip to content

Commit

Permalink
use deepEqual to test selectedValue and return null instead of undefi…
Browse files Browse the repository at this point in the history
…ned in onChange when you unselect
  • Loading branch information
wcastand committed Oct 16, 2017
1 parent 4e1f00e commit 2749ad4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
},
"dependencies": {
"debounce": "1.0.2",
"lodash.isequal": "4.5.0",
"recompose": "0.25.0"
},
"lint-staged": {
Expand Down
10 changes: 6 additions & 4 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import isEqual from 'lodash.isequal'

class SyncSelect extends Component {
state = {
Expand All @@ -16,13 +17,14 @@ class SyncSelect extends Component {
}
componentWillUpdate(nextProps, nextState) {
if (
nextState.selectedValue !== this.state.selectedValue &&
!isEqual(nextState.selectedValue, this.state.selectedValue) &&
typeof this.props.onChange !== 'undefined' &&
!nextProps.disabled
) {
this.props.onChange(
nextProps.multi ? nextState.selectedValue : nextState.selectedValue[0]
)
const val = nextProps.multi
? nextState.selectedValue
: nextState.selectedValue[0]
this.props.onChange(typeof val === 'undefined' ? null : val)
}
if (nextProps.disabled && !this.props.disabled) {
this.setState(() => ({opened: false}))
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4452,6 +4452,10 @@ lodash.isarray@^3.0.0:
version "3.0.4"
resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-3.0.4.tgz#79e4eb88c36a8122af86f844aa9bcd851b5fbb55"

[email protected]:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"

lodash.isnil@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/lodash.isnil/-/lodash.isnil-4.0.0.tgz#49e28cd559013458c814c5479d3c663a21bfaa6c"
Expand Down

0 comments on commit 2749ad4

Please sign in to comment.