-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add: value property #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import React, { Component, PropTypes } from 'react'; | |
import ReactDOM from 'react-dom'; | ||
import $ from 'jquery'; | ||
import 'select2'; | ||
import shallowEqual from 'fbjs/lib/shallowEqual'; | ||
|
||
export default class Select2 extends Component { | ||
static propTypes = { | ||
|
@@ -10,6 +11,11 @@ export default class Select2 extends Component { | |
PropTypes.array, | ||
PropTypes.string, | ||
]), | ||
value: PropTypes.oneOfType([ | ||
PropTypes.number, | ||
PropTypes.array, | ||
PropTypes.string, | ||
]), | ||
data: PropTypes.array, | ||
events: PropTypes.array, | ||
options: PropTypes.object, | ||
|
@@ -45,6 +51,16 @@ export default class Select2 extends Component { | |
this.props.events.forEach(event => { | ||
this.el.on(event[0], this.props[event[1]]); | ||
}); | ||
const { defaultValue, value } = this.props; | ||
if (defaultValue === (void 0) && value !== (void 0)) { | ||
this.setValue(value); | ||
} | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
if (this.el && nextProps.value !== this.props.value) { | ||
this.setValue(nextProps.value); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
|
@@ -54,9 +70,15 @@ export default class Select2 extends Component { | |
this.el.select2('destroy'); | ||
} | ||
|
||
render() { | ||
const { data, ...params } = this.props; | ||
setValue(value) { | ||
const elVal = this.el.val(); | ||
if (!shallowEqual(elVal, value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I add shallowEqual here. It's nessasary for multiple value, when we need to compare 2 arrays. for example [1,2] !== [1,2] but shallowEqual([1,2], [1,2]) === true. |
||
this.el.val(value).trigger('change'); | ||
} | ||
} | ||
|
||
render() { | ||
const { data, value, ...params } = this.props; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Problem with React warning was here, because select receive property |
||
return ( | ||
<select {...params}> | ||
{data.map((item, k) => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rkit Fix: setValue if only defaultValue isn't defined