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

Add: value property #16

Merged
merged 1 commit into from
Feb 5, 2016
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: 21 additions & 3 deletions examples/src/components/Tags.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import React, { Component } from 'react';
import Select2 from '../../../src/components/Select2';
import '../../../css/select2.css';

import $ from 'jquery';
/* eslint react/jsx-no-bind: 0, arrow-spacing: 0 */
export default class Tags extends Component {
constructor(props) {
super(props);
this.state = {
value1: ['feature'],
value2: null,
};
}
render() {
const { value1, value2 } = this.state;
return (
<div>
Basic usage<br/>
<Select2
multiple
data={['bug', 'feature', 'documents', 'discussion']}
value={ value1 }
onChange={(e)=> {
this.setState({ value1: $(e.target).val() });
}}
options={
{
placeholder: 'search by tags',
}
}
/>

<button onClick={ ()=> this.setState({ value1: ['bug', 'discussion'] }) }>Set 'bug' 'discussion' value</button>
<br/><br/>

With data as an object<br/>
Expand All @@ -28,11 +41,16 @@ export default class Tags extends Component {
{ text: 'documents', id: 3 },
{ text: 'discussion', id: 4 },
]}
defaultValue={ 1 }
value={ value2 }
onChange={ (e)=> {
this.setState({ value2: +$(e.target).val() });
}}
options={{
placeholder: 'search by tags',
}}
/>

<button onClick={ ()=> this.setState({ value2: 3 }) }>Set 'documents' value</button>
<br/><br/>

With callbacks<br/>
Expand Down
2 changes: 2 additions & 0 deletions examples/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module.exports = {
noParse: [],
},

devtool: 'eval-source-map',

plugins: [
new ExtractTextPlugin('[name].css'),
new webpack.optimize.DedupePlugin(),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"input"
],
"dependencies": {
"fbjs": "^0.7.0",
"jquery": "^2.1.4",
"react": "^0.14.0",
"react-dom": "^0.14.1",
Expand Down
26 changes: 24 additions & 2 deletions src/components/Select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
Expand Down Expand Up @@ -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)) {
Copy link
Contributor Author

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

this.setValue(value);
}
}

componentWillReceiveProps(nextProps) {
if (this.el && nextProps.value !== this.props.value) {
this.setValue(nextProps.value);
}
}

componentWillUnmount() {
Expand All @@ -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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem with React warning was here, because select receive property value, but we not need it

return (
<select {...params}>
{data.map((item, k) => {
Expand Down