Skip to content

Commit b2c8ae7

Browse files
committed
Add ability to specify a default value
1 parent 57188cb commit b2c8ae7

File tree

4 files changed

+110
-32
lines changed

4 files changed

+110
-32
lines changed

README.md

+48-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ npm install react-select2-wrapper --save
1212

1313
```js
1414
import Select2 from 'react-select2-wrapper';
15+
1516

1617
<Select2
1718
multiple={true}
@@ -23,18 +24,20 @@ import Select2 from 'react-select2-wrapper';
2324

2425
With data as an object
2526
```js
26-
import Select2 from 'react-select2-wrapper';
27-
2827
<Select2
2928
multiple={false}
30-
data={[{text: 'bug', id: 1}, {text: 'feature', id: 2}, {text: 'documents', id: 3}, {text: 'discussion', id: 4}]}
29+
data={[
30+
{ text: 'bug', id: 1 },
31+
{ text: 'feature', id: 2 },
32+
{ text: 'documents', id: 3 },
33+
{ text: 'discussion', id: 4 },
34+
]}
3135
options={{
3236
placeholder: 'search by tags',
3337
}} />
3438
```
3539

3640
With callbacks
37-
3841
```js
3942
<Select2
4043
multiple={true}
@@ -49,6 +52,47 @@ With callbacks
4952
}} />
5053
```
5154

55+
With default value
56+
```js
57+
<Select2
58+
multiple={false}
59+
defaultValue={2}
60+
data={[
61+
{ text: 'bug', id: 1 },
62+
{ text: 'feature', id: 2 },
63+
{ text: 'documents', id: 3 },
64+
{ text: 'discussion', id: 4 },
65+
]}
66+
options={{
67+
placeholder: 'search by tags',
68+
}} />
69+
```
70+
71+
With default multiple value
72+
73+
```js
74+
<Select2
75+
multiple={true}
76+
defaultValue={[1, 4]}
77+
data={[
78+
{ text: 'bug', id: 1 },
79+
{ text: 'feature', id: 2 },
80+
{ text: 'documents', id: 3 },
81+
{ text: 'discussion', id: 4 },
82+
]}
83+
options={{
84+
placeholder: 'search by tags',
85+
}} />
86+
```
87+
88+
You can access to select2 as follows
89+
```js
90+
// assign a ref attribute
91+
<Select2 ref="tags" />
92+
// somewhere in your code, access via `this.refs`
93+
this.refs.tags.el
94+
```
95+
5296
## Themes
5397

5498
Default theme in [css/select2.css](css/select2.css)

examples/src/components/Tags.js

+56-26
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,67 @@ export default class Tags extends Component {
66
render() {
77
return (
88
<div>
9-
Using ['value', 'value2']
9+
Basic usage<br/>
10+
<Select2
11+
multiple={true}
12+
data={['bug', 'feature', 'documents', 'discussion']}
13+
options={{
14+
placeholder: 'search by tags',
15+
}} />
16+
<br/><br/>
17+
With data as an object<br/>
18+
<Select2
19+
multiple={false}
20+
data={[
21+
{ text: 'bug', id: 1 },
22+
{ text: 'feature', id: 2 },
23+
{ text: 'documents', id: 3 },
24+
{ text: 'discussion', id: 4 },
25+
]}
26+
options={{
27+
placeholder: 'search by tags',
28+
}} />
29+
<br/><br/>
30+
With callbacks<br/>
31+
<Select2
32+
multiple={true}
33+
data={['bug', 'feature', 'documents', 'discussion']}
34+
onOpen={() => { console.log('onOpen'); } }
35+
onClose={() => { console.log('onClose'); } }
36+
onSelect={() => { console.log('onSelect'); } }
37+
onChange={() => { console.log('onChange'); } }
38+
onUnselect={() => { console.log('onUnselect'); } }
39+
options={{
40+
placeholder: 'search by tags',
41+
}} />
42+
<br/><br/>
43+
With default value<br/>
44+
<Select2
45+
multiple={false}
46+
defaultValue={2}
47+
data={[
48+
{ text: 'bug', id: 1 },
49+
{ text: 'feature', id: 2 },
50+
{ text: 'documents', id: 3 },
51+
{ text: 'discussion', id: 4 },
52+
]}
53+
options={{
54+
placeholder: 'search by tags',
55+
}} />
56+
<br/><br/>
57+
With default multiple value<br/>
1058
<Select2
1159
multiple={true}
12-
data={['bug', 'feature', 'documents', 'discussion']}
13-
onOpen={() => { console.log('onOpen'); } }
14-
onClose={() => { console.log('onClose'); } }
15-
onSelect={() => { console.log('onSelect'); } }
16-
onChange={() => { console.log('onChange'); } }
17-
onUnselect={() => { console.log('onUnselect'); } }
60+
defaultValue={[1, 4]}
61+
data={[
62+
{ text: 'bug', id: 1 },
63+
{ text: 'feature', id: 2 },
64+
{ text: 'documents', id: 3 },
65+
{ text: 'discussion', id: 4 },
66+
]}
1867
options={{
1968
placeholder: 'search by tags',
2069
}} />
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-
}} />
4070
</div>
4171
);
4272
}

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.10",
3+
"version": "0.1.0",
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
@@ -5,6 +5,10 @@ import 'select2';
55

66
export default class Select2 extends Component {
77
static propTypes = {
8+
defaultValue: PropTypes.oneOfType([
9+
PropTypes.number,
10+
PropTypes.array,
11+
]),
812
data: PropTypes.array,
913
events: PropTypes.array,
1014
options: PropTypes.object,
@@ -49,7 +53,7 @@ export default class Select2 extends Component {
4953

5054
render() {
5155
return (
52-
<select multiple={this.props.multiple}>
56+
<select multiple={this.props.multiple} defaultValue={this.props.defaultValue}>
5357
{this.props.data.map((item, k) => {
5458
if (typeof item === 'string' ||
5559
((!!item && typeof item === 'object') && Object.prototype.toString.call(item) === '[object String]')) {

0 commit comments

Comments
 (0)