Skip to content

Commit

Permalink
Add: support of value props for select2 with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lexich committed Feb 3, 2016
1 parent a85ad15 commit d7146f4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
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 && value !== (void 0)) {
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)) {
this.el.val(value).trigger('change');
}
}

render() {
const { data, value, ...params } = this.props;
return (
<select {...params}>
{data.map((item, k) => {
Expand Down

0 comments on commit d7146f4

Please sign in to comment.