-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathvaadin-combo-box-mixin.js
More file actions
555 lines (488 loc) · 16.6 KB
/
vaadin-combo-box-mixin.js
File metadata and controls
555 lines (488 loc) · 16.6 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* @license
* Copyright (c) 2015 - 2026 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { ValidateMixin } from '@vaadin/field-base/src/validate-mixin.js';
import { ComboBoxItemsMixin } from './vaadin-combo-box-items-mixin.js';
/**
* Checks if the value is supported as an item value in this control.
*
* @param {unknown} value
* @return {boolean}
*/
function isValidValue(value) {
return value !== undefined && value !== null;
}
/**
* @polymerMixin
* @mixes ComboBoxItemsMixin
* @mixes ValidateMixin
* @param {function(new:HTMLElement)} superClass
*/
export const ComboBoxMixin = (superClass) =>
class ComboBoxMixinClass extends ValidateMixin(ComboBoxItemsMixin(superClass)) {
static get properties() {
return {
/**
* Custom function for rendering the content of every item.
* Receives three arguments:
*
* - `root` The `<vaadin-combo-box-item>` internal container DOM element.
* - `comboBox` The reference to the `<vaadin-combo-box>` element.
* - `model` The object with the properties related with the rendered
* item, contains:
* - `model.index` The index of the rendered item.
* - `model.item` The item.
* @type {ComboBoxRenderer | undefined}
*/
renderer: {
type: Object,
sync: true,
},
/**
* If `true`, the user can input a value that is not present in the items list.
* `value` property will be set to the input value in this case.
* Also, when `value` is set programmatically, the input value will be set
* to reflect that value.
* @attr {boolean} allow-custom-value
*/
allowCustomValue: {
type: Boolean,
value: false,
},
/**
* When set to `true`, "loading" attribute is added to host and the overlay element.
*/
loading: {
type: Boolean,
value: false,
reflectToAttribute: true,
sync: true,
},
/**
* The selected item from the `items` array.
* @type {ComboBoxItem | string | undefined}
*/
selectedItem: {
type: Object,
notify: true,
sync: true,
},
/**
* A function used to generate CSS class names for dropdown
* items based on the item. The return value should be the
* generated class name as a string, or multiple class names
* separated by whitespace characters.
*/
itemClassNameGenerator: {
type: Object,
},
/**
* Path for the id of the item. If `items` is an array of objects,
* the `itemIdPath` is used to compare and identify the same item
* in `selectedItem` and `filteredItems` (items given by the
* `dataProvider` callback).
* @attr {string} item-id-path
*/
itemIdPath: {
type: String,
sync: true,
},
/** @private */
__keepOverlayOpened: {
type: Boolean,
sync: true,
},
};
}
static get observers() {
return [
'_openedOrItemsChanged(opened, _dropdownItems, loading, __keepOverlayOpened)',
'_selectedItemChanged(selectedItem, itemValuePath, itemLabelPath)',
'_updateScroller(opened, _dropdownItems, _focusedIndex, _theme)',
];
}
/** @protected */
ready() {
super.ready();
/**
* Used to detect user value changes and fire `change` events.
* Do not define in `properties` to avoid triggering updates.
* @type {string}
* @protected
*/
this._lastCommittedValue = this.value;
}
/**
* Requests an update for the content of items.
* While performing the update, it invokes the renderer (passed in the `renderer` property) once an item.
*
* It is not guaranteed that the update happens immediately (synchronously) after it is requested.
*/
requestContentUpdate() {
if (!this._scroller) {
return;
}
this._scroller.requestContentUpdate();
this._getItemElements().forEach((item) => {
item.requestContentUpdate();
});
}
/**
* @param {Object} props
* @protected
*/
updated(props) {
super.updated(props);
['loading', 'itemIdPath', 'itemClassNameGenerator', 'renderer', 'selectedItem'].forEach((prop) => {
if (props.has(prop)) {
this._scroller[prop] = this[prop];
}
});
}
/** @private */
_updateScroller(opened, items, focusedIndex, theme) {
if (opened) {
this._scroller.style.maxHeight =
getComputedStyle(this).getPropertyValue(`--${this._tagNamePrefix}-overlay-max-height`) || '65vh';
}
this._scroller.setProperties({
items: opened ? items : [],
opened,
focusedIndex,
theme,
});
}
/** @private */
_openedOrItemsChanged(opened, items, loading, keepOverlayOpened) {
// Close the overlay if there are no items to display.
// See https://github.com/vaadin/vaadin-combo-box/pull/964
this._overlayOpened = opened && (keepOverlayOpened || loading || !!(items && items.length));
}
/**
* Override method from `ComboBoxBaseMixin` to deselect
* dropdown item by requesting content update on clear.
* @param {Event} event
* @protected
*/
_onClearButtonClick(event) {
super._onClearButtonClick(event);
if (this.opened) {
this.requestContentUpdate();
}
}
/**
* Override method inherited from `InputMixin`
* to revert the input value to value.
* @protected
* @override
*/
_inputElementChanged(input) {
super._inputElementChanged(input);
if (input) {
this._revertInputValueToValue();
}
}
/**
* Override method from `ComboBoxBaseMixin` to handle loading.
* @protected
* @override
*/
_closeOrCommit() {
if (!this.opened && !this.loading) {
this._commitValue();
} else {
this.close();
}
}
/**
* Override method from `ComboBoxBaseMixin` to handle valid value.
* @protected
* @override
*/
_hasValidInputValue() {
const hasInvalidOption =
this._focusedIndex < 0 &&
this._inputElementValue !== '' &&
this._getItemLabel(this.selectedItem) !== this._inputElementValue;
return this.allowCustomValue || !hasInvalidOption;
}
/**
* Override method from `ComboBoxBaseMixin`.
* @protected
* @override
*/
_onEscapeCancel() {
this.cancel();
}
/**
* Override method from `ComboBoxBaseMixin` to reset selected item.
* @protected
* @override
*/
_onClearAction() {
this.selectedItem = null;
if (this.allowCustomValue) {
this.value = '';
}
this._detectAndDispatchChange();
}
/**
* Clears the current filter. Should be used instead of setting the property
* directly in order to allow overriding this in multi-select combo box.
* @protected
*/
_clearFilter() {
this.filter = '';
}
/**
* Reverts back to original value.
*/
cancel() {
this._revertInputValueToValue();
// In the next _detectAndDispatchChange() call, the change detection should not pass
this._lastCommittedValue = this.value;
this._closeOrCommit();
}
/**
* Override method from `ComboBoxBaseMixin` to store last committed value.
* @protected
* @override
*/
_onOpened() {
this.dispatchEvent(new CustomEvent('vaadin-combo-box-dropdown-opened', { bubbles: true, composed: true }));
// _detectAndDispatchChange() should not consider value changes done before opening
this._lastCommittedValue = this.value;
}
/**
* Override method from `ComboBoxBaseMixin` to dispatch an event.
* @protected
* @override
*/
_onOverlayClosed() {
this.dispatchEvent(new CustomEvent('vaadin-combo-box-dropdown-closed', { bubbles: true, composed: true }));
}
/**
* Override method from `ComboBoxBaseMixin` to commit value on overlay closing.
* @protected
* @override
*/
_onClosed() {
if (!this.loading || this.allowCustomValue) {
this._commitValue();
}
}
/**
* Override method from `ComboBoxBaseMixin` to implement value commit logic.
* @protected
* @override
*/
_commitValue() {
if (this._focusedIndex > -1) {
const focusedItem = this._dropdownItems[this._focusedIndex];
if (this.selectedItem !== focusedItem) {
this.selectedItem = focusedItem;
}
// Make sure input field is updated in case value doesn't change (i.e. FOO -> foo)
this._inputElementValue = this._getItemLabel(this.selectedItem);
this._focusedIndex = -1;
} else if (this._inputElementValue === '' || this._inputElementValue === undefined) {
this.selectedItem = null;
if (this.allowCustomValue) {
this.value = '';
}
} else {
// Try to find an item which label matches the input value.
const items = [this.selectedItem, ...(this._dropdownItems || [])];
const itemMatchingInputValue = items[this.__getItemIndexByLabel(items, this._inputElementValue)];
if (
this.allowCustomValue &&
// To prevent a repetitive input value being saved after pressing ESC and Tab.
!itemMatchingInputValue
) {
const customValue = this._inputElementValue;
// Store reference to the last custom value for checking it on focusout.
this._lastCustomValue = customValue;
// An item matching by label was not found, but custom values are allowed.
// Dispatch a custom-value-set event with the input value.
const e = new CustomEvent('custom-value-set', {
detail: customValue,
composed: true,
cancelable: true,
bubbles: true,
});
this.dispatchEvent(e);
if (!e.defaultPrevented) {
this.value = customValue;
}
} else if (!this.allowCustomValue && !this.opened && itemMatchingInputValue) {
// An item matching by label was found, select it.
this.value = this._getItemValue(itemMatchingInputValue);
} else {
// Revert the input value
this._revertInputValueToValue();
}
}
this._detectAndDispatchChange();
this._clearSelectionRange();
this._clearFilter();
}
/**
* Override an event listener from `InputMixin`.
* @param {!Event} event
* @protected
* @override
*/
_onChange(event) {
// Suppress the native change event fired on the native input.
// We use `_detectAndDispatchChange` to fire a custom event.
event.stopPropagation();
}
/**
* Override method from `ComboBoxBaseMixin` to handle reverting value.
* @protected
* @override
*/
_revertInputValue() {
if (this.filter !== '') {
this._inputElementValue = this.filter;
} else {
this._revertInputValueToValue();
}
this._clearSelectionRange();
}
/** @private */
_revertInputValueToValue() {
if (this.allowCustomValue && !this.selectedItem) {
this._inputElementValue = this.value;
} else {
this._inputElementValue = this._getItemLabel(this.selectedItem);
}
}
/** @private */
_selectedItemChanged(selectedItem) {
if (selectedItem === null || selectedItem === undefined) {
if (this.filteredItems) {
if (!this.allowCustomValue) {
this.value = '';
}
this._toggleHasValue(this._hasValue);
this._inputElementValue = this.value;
}
} else {
const value = this._getItemValue(selectedItem);
if (this.value !== value) {
this.value = value;
if (this.value !== value) {
// The value was changed to something else in value-changed listener,
// so prevent from resetting it to the previous value.
return;
}
}
this._toggleHasValue(true);
this._inputElementValue = this._getItemLabel(selectedItem);
}
}
/**
* Override an observer from `InputMixin`.
* @protected
* @override
*/
_valueChanged(value, oldVal) {
if (value === '' && oldVal === undefined) {
// Initializing, no need to do anything
// See https://github.com/vaadin/vaadin-combo-box/issues/554
return;
}
if (isValidValue(value)) {
if (this._getItemValue(this.selectedItem) !== value) {
this._selectItemForValue(value);
}
if (!this.selectedItem && this.allowCustomValue) {
this._inputElementValue = value;
}
this._toggleHasValue(this._hasValue);
} else {
this.selectedItem = null;
}
this._clearFilter();
// In the next _detectAndDispatchChange() call, the change detection should pass
this._lastCommittedValue = undefined;
}
/** @private */
_detectAndDispatchChange() {
// Do not validate when focusout is caused by document
// losing focus, which happens on browser tab switch.
if (document.hasFocus()) {
this._requestValidation();
}
if (this.value !== this._lastCommittedValue) {
this.dispatchEvent(new CustomEvent('change', { bubbles: true }));
this._lastCommittedValue = this.value;
}
}
/** @private */
_selectItemForValue(value) {
const valueIndex = this.__getItemIndexByValue(this.filteredItems, value);
const previouslySelectedItem = this.selectedItem;
if (valueIndex >= 0) {
this.selectedItem = this.filteredItems[valueIndex];
} else if (this.dataProvider && this.selectedItem === undefined) {
this.selectedItem = undefined;
} else {
this.selectedItem = null;
}
if (this.selectedItem === null && previouslySelectedItem === null) {
this._selectedItemChanged(this.selectedItem);
}
}
/**
* Provide items to be rendered in the dropdown.
* Override this method to show custom items.
*
* @protected
* @override
*/
_setDropdownItems(newItems) {
const oldItems = this._dropdownItems;
this._dropdownItems = newItems;
// Store the currently focused item if any. The focused index preserves
// in the case when more filtered items are loading but it is reset
// when the user types in a filter query.
const focusedItem = oldItems ? oldItems[this._focusedIndex] : null;
// Try to sync `selectedItem` based on `value` once a new set of `filteredItems` is available
// (as a result of external filtering or when they have been loaded by the data provider).
// When `value` is specified but `selectedItem` is not, it means that there was no item
// matching `value` at the moment `value` was set, so `selectedItem` has remained unsynced.
const valueIndex = this.__getItemIndexByValue(newItems, this.value);
if ((this.selectedItem === null || this.selectedItem === undefined) && valueIndex >= 0) {
this.selectedItem = newItems[valueIndex];
}
// Try to first set focus on the item that had been focused before `newItems` were updated
// if it is still present in the `newItems` array. Otherwise, set the focused index
// depending on the selected item or the filter query.
const focusedItemIndex = this.__getItemIndexByValue(newItems, this._getItemValue(focusedItem));
if (focusedItemIndex > -1) {
this._focusedIndex = focusedItemIndex;
} else {
// When the user filled in something that is different from the current value = filtering is enabled,
// set the focused index to the item that matches the filter query.
this._focusedIndex = this.__getItemIndexByLabel(newItems, this.filter);
}
}
/**
* Override method from `ComboBoxBaseMixin`.
* @protected
* @override
*/
_handleFocusOut() {
// User's logic in `custom-value-set` event listener might cause input to blur,
// which will result in attempting to commit the same custom value once again.
if (!this.opened && this.allowCustomValue && this._inputElementValue === this._lastCustomValue) {
delete this._lastCustomValue;
return;
}
super._handleFocusOut();
}
};