Skip to content

Commit 41a33c1

Browse files
committed
Merge pull request #2 from brookslyrette/master
Expanded for object based data [{id: x, text: 'label'}]
2 parents d452aeb + 404288b commit 41a33c1

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ import Select2 from 'react-select2-wrapper';
2121
}} />
2222
```
2323

24+
With Object Data
25+
```js
26+
import Select2 from 'react-select2-wrapper';
27+
28+
<Select2
29+
multiple={false}
30+
data={[{text: 'Bug', id: 0}, {text: 'Feature', id: 1}, {text: 'Documents', id: 2}, {text: 'Discussion', id: 'UUID'}]}
31+
options={{
32+
placeholder: 'search by tags',
33+
}} />
34+
```
35+
2436
With callbacks
2537

2638
```js

examples/src/components/Tags.js

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default class Tags extends Component {
66
render() {
77
return (
88
<div>
9+
Using ['value', 'value2']
910
<Select2
1011
multiple={true}
1112
data={['bug', 'feature', 'documents', 'discussion']}
@@ -17,6 +18,25 @@ export default class Tags extends Component {
1718
options={{
1819
placeholder: 'search by tags',
1920
}} />
21+
<br/>
22+
<br/>
23+
Using {'{'}id: 'x', text:'foo'{'}'}
24+
<Select2
25+
multiple={false}
26+
data={[
27+
{text: 'Bug', id: 0},
28+
{text: 'Feature', id: 1},
29+
{text: 'Documents', id: 2},
30+
{text: 'Discussion', id: 'UUID'}
31+
]}
32+
onOpen={() => { console.log('onOpen'); } }
33+
onClose={() => { console.log('onClose'); } }
34+
onSelect={() => { console.log('onSelect'); } }
35+
onChange={() => { console.log('onChange'); } }
36+
onUnselect={() => { console.log('onUnselect'); } }
37+
options={{
38+
placeholder: 'search by tags',
39+
}} />
2040
</div>
2141
);
2242
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-select2-wrapper",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "React component for Select2",
55
"main": "lib/components/Select2.js",
66
"scripts": {

src/components/Select2.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ export default class Select2 extends Component {
5050
return (
5151
<select multiple={this.props.multiple}>
5252
{this.props.data.map((item, k) => {
53-
return (<option key={'option-' + k}>{item}</option>);
53+
if (typeof item === 'string' ||
54+
((!!item && typeof item === 'object') && Object.prototype.toString.call(item) === '[object String]')) {
55+
return (<option key={'option-' + k}>{item}</option>);
56+
}
57+
return (<option key={'option-' + k} value={item.id}>{item.text}</option>);
5458
})}
5559
</select>
5660
);

0 commit comments

Comments
 (0)