1
|
|
|
var React = require("react"); |
2
|
|
|
|
3
|
|
|
var Dropdown = React.createClass({ |
4
|
|
|
propTypes: { |
5
|
|
|
options: React.PropTypes.arrayOf(React.PropTypes.shape({ |
6
|
|
|
id: React.PropTypes.oneOfType([ |
|
|
|
|
7
|
|
|
React.PropTypes.string, |
8
|
|
|
React.PropTypes.number |
9
|
|
|
]).isRequired, |
10
|
|
|
title: React.PropTypes.string.isRequired |
|
|
|
|
11
|
|
|
}).isRequired), |
12
|
|
|
defaultValue: React.PropTypes.oneOfType([ |
|
|
|
|
13
|
|
|
React.PropTypes.string, |
14
|
|
|
React.PropTypes.number, |
15
|
|
|
React.PropTypes.array |
16
|
|
|
]), |
17
|
|
|
value: React.PropTypes.oneOfType([ |
18
|
|
|
React.PropTypes.string, |
19
|
|
|
React.PropTypes.number, |
20
|
|
|
React.PropTypes.array |
21
|
|
|
]), |
22
|
|
|
disabled: React.PropTypes.bool, |
|
|
|
|
23
|
|
|
onSelect: React.PropTypes.func.isRequired |
24
|
|
|
}, |
25
|
|
|
|
26
|
|
|
getDefaultProps: function() { |
27
|
|
|
return { |
28
|
|
|
defaultText: 'Select an option' |
29
|
|
|
}; |
30
|
|
|
}, |
31
|
|
|
|
32
|
|
|
componentDidMount: function() { |
33
|
|
|
this.initSelectize(); |
34
|
|
|
}, |
35
|
|
|
|
36
|
|
|
// When props get updated we re-initialise the selector |
37
|
|
|
componentDidUpdate: function() { |
38
|
|
|
this.removeSelectize(); |
39
|
|
|
this.initSelectize(); |
40
|
|
|
}, |
41
|
|
|
|
42
|
|
|
componentWillUnmount: function() { |
43
|
|
|
// we want to destroy selectize to prevent |
44
|
|
|
// onUpdate events to hang around after this |
45
|
|
|
// component have been unmounted. |
46
|
|
|
this.removeSelectize(); |
47
|
|
|
}, |
48
|
|
|
|
49
|
|
|
selector: null, |
50
|
|
|
|
51
|
|
|
// We are using the selectize library instead of select2 because |
52
|
|
|
// select2 causes a lot of bugs and the hacks didn't do it anymore. |
53
|
|
|
// The biggest problem is displaying a select2 dropdown in a bootstrap |
54
|
|
|
// modal that has overflow scrolling will break the scrolling. |
55
|
|
|
initSelectize: function() { |
56
|
|
|
$(this.selector).selectize({ |
57
|
|
|
valueField: 'id', |
58
|
|
|
labelField: 'title', |
59
|
|
|
searchField: 'title', |
60
|
|
|
options: this.props.options |
61
|
|
|
}); |
62
|
|
|
// push the set the default value from react to selectize |
63
|
|
|
if (typeof this.props.value !== 'undefined') { |
64
|
|
|
$(this.selector)[0].selectize.setValue(this.props.value); |
65
|
|
|
} |
66
|
|
|
// make sure that react get notified when values are picked |
67
|
|
|
$(this.selector)[0].selectize.on("change", function(value) { |
68
|
|
|
this.props.onSelect(value); |
69
|
|
|
}.bind(this)); |
70
|
|
|
}, |
71
|
|
|
|
72
|
|
|
removeSelectize: function() { |
73
|
|
|
if ($(this.selector)[0].selectize) { |
74
|
|
|
$(this.selector)[0].selectize.destroy(); |
75
|
|
|
} |
76
|
|
|
}, |
77
|
|
|
|
78
|
|
|
render: function() { |
79
|
|
|
var props = this.props; |
80
|
|
|
var options = []; |
81
|
|
|
var idx = 0; |
82
|
|
|
|
83
|
|
|
if (props.options) { |
84
|
|
|
options = Object.keys(props.options).map(function(index) { |
85
|
|
|
idx += 1; |
86
|
|
|
return ( |
87
|
|
|
<option |
88
|
|
|
key={idx} |
89
|
|
|
value={props.options[index].id} |
90
|
|
|
> |
91
|
|
|
{props.options[index].title} |
92
|
|
|
</option> |
93
|
|
|
); |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
options.unshift(<option key={"default"} value={""} disabled>{props.defaultText}</option>); |
98
|
|
|
return ( |
99
|
|
|
<select |
100
|
|
|
ref={function(node) { this.selector = node; }.bind(this)} |
101
|
|
|
id={props.name} |
102
|
|
|
className='form-control disable-select2' |
103
|
|
|
name={props.name} |
104
|
|
|
onChange={props.onSelect} |
105
|
|
|
value={props.value} |
106
|
|
|
disabled={props.disabled} |
107
|
|
|
> |
108
|
|
|
{options} |
109
|
|
|
</select> |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
|
module.exports = Dropdown; |
115
|
|
|
|