forked from dmwyatt/reactselect2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactselect2.js
192 lines (158 loc) · 5.08 KB
/
reactselect2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* @jsx React.DOM
*/
var Select2Component = React.createClass({
propTypes: {
// Id to apply to hidden input element
id: React.PropTypes.string.isRequired,
// Data for Select2,
dataSet: React.PropTypes.array.isRequired,
// If true apply the prop `errorClass`
hasError: React.PropTypes.bool,
// Class to apply if hasError is true.
errorClass: React.PropTypes.string,
// Function to call with currently selected elements
onSelection: React.PropTypes.func,
// Allow multiple selections
multiple: React.PropTypes.bool,
// Placeholder to display in select
placeholder: React.PropTypes.string,
// Initial selection. If not `multiple` then will use first element.
// Provide a list of ids, no need to provide whole objects as provided to `dataSet`
val: React.PropTypes.arrayOf(React.PropTypes.number),
// inline style for select
styleWidth: React.PropTypes.string,
// enable or disable the input
enabled: React.PropTypes.bool
},
getDefaultProps: function () {
return {
hasError: false,
errorClass: "has-error", // default to Bootstrap 3's error class
multiple: false,
placeholder: "Make selection",
val: [],
styleWidth: "100%",
enabled: true,
dataSet: []
};
},
///////////////////////////////
// Lifecycle
///////////////////////////////
componentDidUpdate: function (prevProps, prevState) {
if (this._isDataUpdated(prevProps.dataSet)) {
// "brute-force" new data into our Select2 widget since Select2 doesn't
// have a method for changng data on already-existing widgets
this.createSelect2();
} else {
// Change placeholder
if (prevProps.placeholder !== this.props.placeholder) {
this.setPlaceholderTo(this.getInputElem(), this.props.placeholder);
}
// Handle val prop
var updateVal = false;
// ...if length of old val prop and new val prop arrays are the same,
// we'll need to check the elements
if (prevProps.val.length == this.props.val.length) {
$.each(prevProps.val, function (index, value) {
if (this.props.val[index] != value) {
updateVal = true;
}
}.bind(this));
} else {
updateVal = true;
}
// ...update our val if we need to
if (updateVal) {
this.getInputElem().select2("val", this.props.val);
}
// Enable/disable
if (prevProps.enabled != this.props.enabled) {
this.getInputElem().select2("enable", this.props.enabled);
}
}
},
componentDidMount: function () {
// Set up Select2
var $node = this.createSelect2();
},
///////////////////////////////
// Manipulate Select2
///////////////////////////////
setPlaceholderTo: function($elem, placeholder) {
if (!placeholder) {
placeholder = "";
}
var currData = $elem.select2("data");
// Set placeholder to new placeholder
$elem.attr("placeholder", placeholder);
// Now workaround the fact that Select2 doesn't pick up on this
// ..First assign null
$elem.select2("data", null);
// ..Then assign dummy value in case that currData is null since
// that won't do anything.
$elem.select2("data", {});
// ..Then put original data back
$elem.select2("data", currData);
},
createSelect2: function () {
// Get inital value
var val = null;
if (this.props.val.length > 0) {
val = this.props.multiple ? this.props.val : this.props.val[0];
}
var options = {
data: this.props.dataSet,
multiple: this.props.multiple,
val: val
};
var $node = this.getInputElem();
$node.
val(val).
select2(options).
on("change", this.handleChange).
on("select2-open", this.handleErrorState).
select2("enable", this.props.enabled);
this.setPlaceholderTo($node, this.props.placeholder);
},
///////////////////////////////
// Helpers
///////////////////////////////
handleErrorState: function () {
var $dropNode = $('#select2-drop');
var classNames = $dropNode[0].className.split(/\s+/);
if (this.props.hasError) {
var hasErrorClass = $.inArray(this.props.errorClass, classNames);
if (hasErrorClass == -1) {
$dropNode.addClass(this.props.errorClass);
}
} else {
$dropNode.removeClass(this.props.errorClass);
}
},
handleChange: function (e) {
if (this.props.onSelection) {
this.props.onSelection(e, this.getInputElem().select2("data"));
}
},
getInputElem: function () {
return $("#" + this.props.id);
},
_isDataUpdated: function (oldData) {
// TODO: More robust dataSet checker. Need to recurse and
// check all elements of data set.
if (oldData.length != this.props.dataSet.length) {
return true;
}
return false;
},
render: function () {
var style = {width: this.props.styleWidth};
return (
<div className={this.props.hasError ? this.props.errorClass : ""}>
<input type='hidden' style={style} id={this.props.id}/>
</div>
);
}
});