-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti-select.js
More file actions
221 lines (213 loc) · 8.38 KB
/
multi-select.js
File metadata and controls
221 lines (213 loc) · 8.38 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
var _elemental = require('elemental');
var _ = require('lodash');
var React = require('react');
var classNames = require('classnames');
module.exports = React.createClass({
displayName: 'exports',
propTypes: {
clearable: React.PropTypes.bool,
searchable: React.PropTypes.bool,
initialValues: React.PropTypes.arrayOf(React.PropTypes.object),
items: React.PropTypes.arrayOf(React.PropTypes.object),
hiddenKey: React.PropTypes.string,
labelKey: React.PropTypes.string,
valueKey: React.PropTypes.string
},
_ariaGetListId: function _ariaGetListId() {
return this.state.controlId + '_list';
},
getDefaultProps: function getDefaultProps() {
return {
items: [],
initialValues: [],
onChange: function onChange() {},
valueKey: 'value',
labelKey: 'label',
hiddenKey: 'hidden',
clearable: true,
searchable: false,
type: 'bootstrap'
};
},
getInitialState: function getInitialState() {
return {
controlId: _.uniqueId('rss_'),
options: [],
selections: [],
title: this.props.placeholder,
listOpen: false
};
},
componentWillReceiveProps: function componentWillReceiveProps(newProps) {
var data = newProps.items.map(function (item) {
var newItem = {};
newItem[newProps.valueKey] = item[newProps.valueKey];
newItem[newProps.labelKey] = item[newProps.labelKey];
newItem[newProps.hiddenKey] = item[newProps.hiddenKey];
return newItem;
});
var selections = newProps.initialValues.map(function (val) {
var index = _.findIndex(data, newProps.valueKey, val[newProps.valueKey]);
data[index].selected = true;
return data[index];
});
this.setState({ options: data, selections: selections });
},
valueClicked: function valueClicked(option) {
var index = _.findIndex(this.state.options, this.props.valueKey, option[this.props.valueKey]);
var selIndex = _.findIndex(this.state.selections, this.props.valueKey, option[this.props.valueKey]);
var selected = selIndex === -1;
if (!selected) {
this.state.selections.splice(selIndex, 1);
} else {
this.state.selections.push(option);
}
this.state.options[index].selected = selected;
this.setState({
selections: this.state.selections,
options: this.state.options
});
this.props.onChange(option, this.state.selections);
},
renderOption: function renderOption(dataOption, index) {
var itemKey = "drop_li_" + dataOption[this.props.valueKey],
indexRef = 'option_' + index,
ariaDescendantId = this.state.controlId + '_aria_' + indexRef,
classes = classNames('r-ss-dropdown-option', {
'r-ss-selected': dataOption.selected
});
if (dataOption[this.props.hiddenKey]) {
classes += ' hidden-li';
}
return React.createElement(
'li',
{ ref: indexRef,
id: ariaDescendantId,
tabIndex: '0',
'data-option-index': index,
className: classes,
'aria-selected': dataOption.selected,
key: itemKey,
'data-option-value': dataOption[this.props.valueKey],
onClick: this.valueClicked.bind(null, dataOption),
role: 'option' },
dataOption[this.props.labelKey]
);
},
buildOptions: function buildOptions() {
var ops = this.state.options.map(this.renderOption);
return React.createElement(
'div',
{ ref: 'dropdownContent', className: 'r-ss-dropdown', onKeyDown: this._handleKeyDown },
React.createElement(
'div',
{ ref: 'scrollWrap', className: 'r-ss-options-wrap' },
React.createElement(
'ul',
{ className: 'r-ss-dropdown-options',
ref: 'dropdownOptionsList',
tabIndex: '-1',
id: this._ariaGetListId(),
role: 'listbox',
'aria-expanded': this.state.isOpen },
ops
)
)
);
},
toggleList: function toggleList() {
this.setState({ listOpen: !this.state.listOpen });
},
clearSelected: function clearSelected() {
var data = this.state.options;
var i = 0;
for (i = 0; i < data.length; i++) {
data[i].selected = false;
}
this.setState({ selections: [], options: data });
this.props.onChange(null, []);
},
render: function render() {
if (this.props.type === 'elemental') {
return this.render_elemental();
} else if (this.props.type === 'bootstrap') {
return this.render_bootstrap();
}
return this.render_bootstrap();
},
render_bootstrap: function render_bootstrap() {
var menu = this.state.listOpen ? this.buildOptions() : null;
return React.createElement(
'div',
null,
React.createElement(
'div',
{ className: 'input-group' },
React.createElement('input', { type: 'text',
className: 'form-control',
autofocus: true,
onChange: this.handleFilterChange,
readOnly: true,
value: this.state.selections.length + ' selected' }),
this.props.clearable && this.state.selections.length > 0 ? React.createElement(
'span',
{ className: 'input-group-btn' },
React.createElement(
'button',
{ className: 'btn btn-default', type: 'button', onClick: this.clearSelected },
React.createElement('span', { className: 'glyphicon glyphicon-remove', 'aria-hidden': 'true' })
)
) : null,
React.createElement(
'span',
{ className: 'input-group-btn' },
React.createElement(
'button',
{ className: 'btn btn-default', onClick: this.toggleList, type: 'button' },
React.createElement('span', { 'aria-hidden': 'true', className: "glyphicon glyphicon-triangle-" + (this.state.listOpen ? 'top' : 'bottom') })
)
)
),
menu
);
},
render_elemental: function render_elemental() {
var menu = this.state.listOpen ? this.buildOptions() : null;
return React.createElement(
'div',
null,
React.createElement(
_elemental.InputGroup,
{ contiguous: true },
React.createElement(
_elemental.InputGroup.Section,
{ grow: true },
React.createElement(_elemental.FormInput, { autofocus: true,
onChange: this.handleFilterChange,
readOnly: true,
value: this.state.selections.length + ' selected' })
),
this.props.clearable && this.state.selections.length > 0 ? React.createElement(
_elemental.InputGroup.Section,
null,
React.createElement(
_elemental.Button,
{ type: 'hollow-danger', onClick: this.clearSelected },
React.createElement(_elemental.Glyph, { icon: 'circle-slash' })
)
) : null,
React.createElement(
_elemental.InputGroup.Section,
null,
React.createElement(
_elemental.Button,
{ onClick: this.toggleList },
React.createElement(_elemental.Glyph, { icon: "triangle-" + (this.state.listOpen ? 'up' : 'down') })
)
)
),
menu
);
}
});