/
Select.js
330 lines (302 loc) 路 7.26 KB
/
Select.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import {core, array} from 'metal';
import dom from 'metal-dom';
import Component from 'metal-component';
import Soy from 'metal-soy';
import 'marble-dropdown';
import templates from './Select.soy.js';
/**
* Responsible for rendering and handling a custom select component, based
* on `Dropdown`.
*/
class Select extends Component {
/**
* @inheritDoc
*/
attached() {
this.on('itemsChanged', this.handleItemsChanged_);
}
/**
* Finds the index of the given element in the items collection.
* @param {!Element} element
* @return {number}
* @protected
*/
findItemIndex_(element) {
const items = this.element.querySelectorAll('li');
for (const [index, item] of items.entries()) {
if (item === element) {
return index;
}
}
}
/**
* Focuses the option at the given index.
* @param {number} index
* @protected
*/
focusIndex_(index) {
const option = this.element.querySelector(
`.select-option:nth-child(${index + 1}) a`
);
if (option) {
this.focusedIndex_ = index;
option.focus();
}
}
/**
* Gets the `Dropdown` instance used by this `Select`.
* @return {!Dropdown}
*/
getDropdown() {
return this.components.dropdown;
}
/**
* Gets `seletectIndex`'s default value.
* @return {!Dropdown}
*/
getSelectedIndexDefaultValue_() {
return this.label || !this.items.length ? -1 : 0;
}
/**
* Handles a `stateSynced` event for the dropdown.
* @param {!Object} data
* @protected
*/
handleDropdownStateSynced_(data) {
if (this.openedWithKeyboard_) {
// This is done on `stateSynced` because the items need to have already
// been made visible before we try focusing them.
this.focusIndex_(0);
this.openedWithKeyboard_ = false;
} else if (this.closedWithKeyboard_) {
this.element.querySelector('.dropdown-select').focus();
this.closedWithKeyboard_ = false;
} else if (data.changes.expanded) {
this.focusedIndex_ = null;
}
this.expanded_ = this.getDropdown().expanded;
}
/**
* Handles the `itemsChanged` event. Sets the default value to the `selectedIndex`.
* @param {!Object} data
* @protected
*/
handleItemsChanged_(event) {
if (event.prevVal && !array.equal(event.newVal, event.prevVal)) {
this.selectedIndex = this.getSelectedIndexDefaultValue_();
}
}
/**
* Handles a `click` event on one of the items. Updates `selectedIndex`
* accordingly.
* @param {!Event} event
* @protected
*/
handleItemClick_(event) {
this.selectItem_(event.delegateTarget);
event.preventDefault();
}
/**
* Handles a `keydown` event on one of the items. Updates `selectedIndex`
* accordingly.
* @param {!Event} event
* @protected
*/
handleItemKeyDown_(event) {
const {keyCode} = event;
if (keyCode === 13 || keyCode === 32) {
this.closedWithKeyboard_ = true;
this.selectItem_(event.delegateTarget);
event.preventDefault();
}
}
/**
* Handles a `keydown` event on this component. Handles keyboard controls.
* @param {!Event} event
* @protected
*/
handleKeyDown_(event) {
const {keyCode} = event;
if (this.expanded_) {
switch (keyCode) {
case 27:
this.closedWithKeyboard_ = true;
this.expanded_ = false;
break;
case 38:
this.focusedIndex_ = core.isDefAndNotNull(this.focusedIndex_)
? this.focusedIndex_
: 1;
this.focusIndex_(
this.focusedIndex_ === 0
? this.items.length - 1
: this.focusedIndex_ - 1
);
event.preventDefault();
break;
case 40:
this.focusedIndex_ = core.isDefAndNotNull(this.focusedIndex_)
? this.focusedIndex_
: -1;
this.focusIndex_(
this.focusedIndex_ === this.items.length - 1
? 0
: this.focusedIndex_ + 1
);
event.preventDefault();
break;
}
} else if (
(keyCode === 13 || keyCode === 32) &&
dom.hasClass(event.target, 'dropdown-select')
) {
this.openedWithKeyboard_ = true;
this.expanded_ = true;
event.preventDefault();
return;
}
}
/**
* @inheritDoc
*/
prepareStateForRender(data) {
data.items = this.items.map(item => Soy.toIncDom(item));
return data;
}
/**
* Selects the item for the given element, and closes the dropdown.
* @param {!Element} itemElement
* @protected
*/
selectItem_(itemElement) {
this.selectedIndex = this.findItemIndex_(itemElement);
this.expanded_ = false;
}
/**
* Syncs disabled closing dropdown when select is changed to disabled
* and updates internal state expanded_ to be false.
* @param {boolean} disabled
*/
syncDisabled(disabled) {
if (disabled) {
this.expanded_ = false;
if (this.getDropdown()) {
this.getDropdown().close();
}
}
}
/**
* Syncs internal state expanded_ and toggle dropdown.
* @param {boolean} expanded
*/
syncExpanded_(expanded) {
if (expanded) {
if (this.getDropdown()) {
this.getDropdown().open();
}
} else {
if (this.getDropdown()) {
this.getDropdown().close();
}
}
}
}
Soy.register(Select, templates);
/**
* State definition.
* @type {!Object}
* @static
*/
Select.STATE = {
/**
* The CSS class used by the select menu arrow.
* @type {string}
* @default 'caret'
*/
arrowClass: {
value: 'caret',
},
/**
* The CSS class used by the select menu button.
* @type {string}
* @default 'btn btn-default'
*/
buttonClass: {
validator: core.isString,
value: 'btn btn-default',
},
/**
* The caret icon.
* @type {*}
*/
caret: {},
/**
* Block or unblock the component behaviours.
* @type {boolean}
* @default false
*/
disabled: {
validator: core.isBoolean,
value: false,
},
/**
* Flag indicating if the select dropdown is currently expanded.
* @type {boolean}
*/
expanded_: {
setter: function(value) {
return !this.disabled ? value : false;
},
validator: core.isBoolean,
value: false,
internal: true,
},
/**
* The name of the hidden input field
* @type {string}
*/
hiddenInputName: {
validator: core.isString,
},
/**
* A public list representing the select dropdown items. Its value is synced
* with the `internaItems` attribute for internal manipulation.
* @type {!Array<string>}
* @default []
*/
items: {
validator: val => val instanceof Array,
value: [],
},
/**
* The label that should be used for the select menu when no item is
* selected. If not set, the first item will be selected automatically.
* @type {string}
*/
label: {
validator: core.isString,
},
/**
* The index of the currently selected item, or -1 if none is selected.
* @type {number}
*/
selectedIndex: {
validator: core.isNumber,
valueFn: 'getSelectedIndexDefaultValue_',
},
/**
* A list representing the select dropdown values.
* @type {Array<string>=}
*/
values: {
validator: val => val instanceof Array,
},
};
/**
* Default element classes.
* @type {string}
* @static
*/
Select.ELEMENT_CLASSES = 'select';
export {Select};
export default Select;