Created
April 15, 2015 21:03
-
-
Save jquense/aafc98519417a1d2aa97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyComboBox extends React.Component { | |
constructor(props, context){ | |
super(props, context) | |
this.state = { value: props.value } | |
} | |
componentWillReceiveProps(props){ | |
this.setState({ value: props.value }) | |
} | |
render(){ | |
var value = this.state.value | |
return <Combobox {...this.props} | |
onChange={ v => this._change(v) } | |
onBlur={ e => this._blur(e) }/> | |
} | |
_change(value){ | |
var isListItem = this.props.data.indexOf(value) !== -1 // this _should_ work, but you can always just check by id | |
if ( isListItem) | |
return this.props.onChange(value) | |
this._needsValueFlush = true | |
this.setState({ value }) | |
} | |
_blur(){ | |
// the user moved away from the widget and its still not a list item | |
if (this._needsValueFlush) { | |
this._needsValueFlush = false | |
this.props.onChange(this.props.value) // reset the value, you can do whatever you like here | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment