@vaadin/combo-box 25.0.0-alpha1 → 25.0.0-alpha10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,17 +3,8 @@
3
3
  * Copyright (c) 2015 - 2025 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
- import { DisabledMixin } from '@vaadin/a11y-base/src/disabled-mixin.js';
7
- import { FocusMixin } from '@vaadin/a11y-base/src/focus-mixin.js';
8
- import { isElementFocused, isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
9
- import { KeyboardMixin } from '@vaadin/a11y-base/src/keyboard-mixin.js';
10
- import { isTouch } from '@vaadin/component-base/src/browser-utils.js';
11
- import { OverlayClassMixin } from '@vaadin/component-base/src/overlay-class-mixin.js';
12
- import { get } from '@vaadin/component-base/src/path-utils.js';
13
- import { InputMixin } from '@vaadin/field-base/src/input-mixin.js';
14
6
  import { ValidateMixin } from '@vaadin/field-base/src/validate-mixin.js';
15
- import { VirtualKeyboardController } from '@vaadin/field-base/src/virtual-keyboard-controller.js';
16
- import { ComboBoxPlaceholder } from './vaadin-combo-box-placeholder.js';
7
+ import { ComboBoxItemsMixin } from './vaadin-combo-box-items-mixin.js';
17
8
 
18
9
  /**
19
10
  * Checks if the value is supported as an item value in this control.
@@ -25,72 +16,16 @@ function isValidValue(value) {
25
16
  return value !== undefined && value !== null;
26
17
  }
27
18
 
28
- /**
29
- * Returns the index of the first item that satisfies the provided testing function
30
- * ignoring placeholder items.
31
- *
32
- * @param {Array<ComboBoxItem | string>} items
33
- * @param {Function} callback
34
- * @return {number}
35
- */
36
- function findItemIndex(items, callback) {
37
- return items.findIndex((item) => {
38
- if (item instanceof ComboBoxPlaceholder) {
39
- return false;
40
- }
41
-
42
- return callback(item);
43
- });
44
- }
45
-
46
19
  /**
47
20
  * @polymerMixin
21
+ * @mixes ComboBoxItemsMixin
48
22
  * @mixes ValidateMixin
49
- * @mixes DisabledMixin
50
- * @mixes InputMixin
51
- * @mixes KeyboardMixin
52
- * @mixes FocusMixin
53
- * @mixes OverlayClassMixin
54
- * @param {function(new:HTMLElement)} subclass
23
+ * @param {function(new:HTMLElement)} superClass
55
24
  */
56
- export const ComboBoxMixin = (subclass) =>
57
- class ComboBoxMixinClass extends OverlayClassMixin(
58
- ValidateMixin(FocusMixin(KeyboardMixin(InputMixin(DisabledMixin(subclass))))),
59
- ) {
25
+ export const ComboBoxMixin = (superClass) =>
26
+ class ComboBoxMixinClass extends ValidateMixin(ComboBoxItemsMixin(superClass)) {
60
27
  static get properties() {
61
28
  return {
62
- /**
63
- * True if the dropdown is open, false otherwise.
64
- * @type {boolean}
65
- */
66
- opened: {
67
- type: Boolean,
68
- notify: true,
69
- value: false,
70
- reflectToAttribute: true,
71
- sync: true,
72
- observer: '_openedChanged',
73
- },
74
-
75
- /**
76
- * Set true to prevent the overlay from opening automatically.
77
- * @attr {boolean} auto-open-disabled
78
- */
79
- autoOpenDisabled: {
80
- type: Boolean,
81
- sync: true,
82
- },
83
-
84
- /**
85
- * When present, it specifies that the field is read-only.
86
- * @type {boolean}
87
- */
88
- readonly: {
89
- type: Boolean,
90
- value: false,
91
- reflectToAttribute: true,
92
- },
93
-
94
29
  /**
95
30
  * Custom function for rendering the content of every item.
96
31
  * Receives three arguments:
@@ -108,17 +43,6 @@ export const ComboBoxMixin = (subclass) =>
108
43
  sync: true,
109
44
  },
110
45
 
111
- /**
112
- * A full set of items to filter the visible options from.
113
- * The items can be of either `String` or `Object` type.
114
- * @type {!Array<!ComboBoxItem | string> | undefined}
115
- */
116
- items: {
117
- type: Array,
118
- sync: true,
119
- observer: '_itemsChanged',
120
- },
121
-
122
46
  /**
123
47
  * If `true`, the user can input a value that is not present in the items list.
124
48
  * `value` property will be set to the input value in this case.
@@ -132,24 +56,6 @@ export const ComboBoxMixin = (subclass) =>
132
56
  value: false,
133
57
  },
134
58
 
135
- /**
136
- * A subset of items, filtered based on the user input. Filtered items
137
- * can be assigned directly to omit the internal filtering functionality.
138
- * The items can be of either `String` or `Object` type.
139
- * @type {!Array<!ComboBoxItem | string> | undefined}
140
- */
141
- filteredItems: {
142
- type: Array,
143
- observer: '_filteredItemsChanged',
144
- sync: true,
145
- },
146
-
147
- /**
148
- * Used to detect user value changes and fire `change` events.
149
- * @private
150
- */
151
- _lastCommittedValue: String,
152
-
153
59
  /**
154
60
  * When set to `true`, "loading" attribute is added to host and the overlay element.
155
61
  * @type {boolean}
@@ -161,28 +67,6 @@ export const ComboBoxMixin = (subclass) =>
161
67
  sync: true,
162
68
  },
163
69
 
164
- /**
165
- * @type {number}
166
- * @protected
167
- */
168
- _focusedIndex: {
169
- type: Number,
170
- observer: '_focusedIndexChanged',
171
- value: -1,
172
- sync: true,
173
- },
174
-
175
- /**
176
- * Filtering string the user has typed into the input field.
177
- * @type {string}
178
- */
179
- filter: {
180
- type: String,
181
- value: '',
182
- notify: true,
183
- sync: true,
184
- },
185
-
186
70
  /**
187
71
  * The selected item from the `items` array.
188
72
  * @type {ComboBoxItem | string | undefined}
@@ -203,39 +87,6 @@ export const ComboBoxMixin = (subclass) =>
203
87
  type: Object,
204
88
  },
205
89
 
206
- /**
207
- * Path for label of the item. If `items` is an array of objects, the
208
- * `itemLabelPath` is used to fetch the displayed string label for each
209
- * item.
210
- *
211
- * The item label is also used for matching items when processing user
212
- * input, i.e., for filtering and selecting items.
213
- * @attr {string} item-label-path
214
- * @type {string}
215
- */
216
- itemLabelPath: {
217
- type: String,
218
- value: 'label',
219
- observer: '_itemLabelPathChanged',
220
- sync: true,
221
- },
222
-
223
- /**
224
- * Path for the value of the item. If `items` is an array of objects, the
225
- * `itemValuePath:` is used to fetch the string value for the selected
226
- * item.
227
- *
228
- * The item value is used in the `value` property of the combo box,
229
- * to provide the form value.
230
- * @attr {string} item-value-path
231
- * @type {string}
232
- */
233
- itemValuePath: {
234
- type: String,
235
- value: 'value',
236
- sync: true,
237
- },
238
-
239
90
  /**
240
91
  * Path for the id of the item. If `items` is an array of objects,
241
92
  * the `itemIdPath` is used to compare and identify the same item
@@ -248,40 +99,6 @@ export const ComboBoxMixin = (subclass) =>
248
99
  sync: true,
249
100
  },
250
101
 
251
- /**
252
- * @type {!HTMLElement | undefined}
253
- * @protected
254
- */
255
- _toggleElement: {
256
- type: Object,
257
- observer: '_toggleElementChanged',
258
- },
259
-
260
- /**
261
- * Set of items to be rendered in the dropdown.
262
- * @protected
263
- */
264
- _dropdownItems: {
265
- type: Array,
266
- sync: true,
267
- },
268
-
269
- /** @private */
270
- _closeOnBlurIsPrevented: Boolean,
271
-
272
- /** @private */
273
- _scroller: {
274
- type: Object,
275
- sync: true,
276
- },
277
-
278
- /** @private */
279
- _overlayOpened: {
280
- type: Boolean,
281
- sync: true,
282
- observer: '_overlayOpenedChanged',
283
- },
284
-
285
102
  /** @private */
286
103
  __keepOverlayOpened: {
287
104
  type: Boolean,
@@ -292,103 +109,23 @@ export const ComboBoxMixin = (subclass) =>
292
109
 
293
110
  static get observers() {
294
111
  return [
295
- '_selectedItemChanged(selectedItem, itemValuePath, itemLabelPath)',
296
112
  '_openedOrItemsChanged(opened, _dropdownItems, loading, __keepOverlayOpened)',
297
- '_updateScroller(_scroller, _dropdownItems, opened, loading, selectedItem, itemIdPath, _focusedIndex, renderer, _theme, itemClassNameGenerator)',
113
+ '_selectedItemChanged(selectedItem, itemValuePath, itemLabelPath)',
114
+ '_updateScroller(opened, _dropdownItems, _focusedIndex, _theme)',
298
115
  ];
299
116
  }
300
117
 
301
- constructor() {
302
- super();
303
- this._boundOverlaySelectedItemChanged = this._overlaySelectedItemChanged.bind(this);
304
- this._boundOnClearButtonMouseDown = this.__onClearButtonMouseDown.bind(this);
305
- this._boundOnClick = this._onClick.bind(this);
306
- this._boundOnOverlayTouchAction = this._onOverlayTouchAction.bind(this);
307
- this._boundOnTouchend = this._onTouchend.bind(this);
308
- }
309
-
310
- /**
311
- * Tag name prefix used by scroller and items.
312
- * @protected
313
- * @return {string}
314
- */
315
- get _tagNamePrefix() {
316
- return 'vaadin-combo-box';
317
- }
318
-
319
- /**
320
- * Get a reference to the native `<input>` element.
321
- * Override to provide a custom input.
322
- * @protected
323
- * @return {HTMLInputElement | undefined}
324
- */
325
- get _nativeInput() {
326
- return this.inputElement;
327
- }
328
-
329
- /**
330
- * Override method inherited from `InputMixin`
331
- * to customize the input element.
332
- * @protected
333
- * @override
334
- */
335
- _inputElementChanged(inputElement) {
336
- super._inputElementChanged(inputElement);
337
-
338
- const input = this._nativeInput;
339
-
340
- if (input) {
341
- input.autocomplete = 'off';
342
- input.autocapitalize = 'off';
343
-
344
- input.setAttribute('role', 'combobox');
345
- input.setAttribute('aria-autocomplete', 'list');
346
- input.setAttribute('aria-expanded', !!this.opened);
347
-
348
- // Disable the macOS Safari spell check auto corrections.
349
- input.setAttribute('spellcheck', 'false');
350
-
351
- // Disable iOS autocorrect suggestions.
352
- input.setAttribute('autocorrect', 'off');
353
-
354
- this._revertInputValueToValue();
355
-
356
- if (this.clearElement) {
357
- this.clearElement.addEventListener('mousedown', this._boundOnClearButtonMouseDown);
358
- }
359
- }
360
- }
361
-
362
118
  /** @protected */
363
119
  ready() {
364
120
  super.ready();
365
121
 
366
- this._initOverlay();
367
- this._initScroller();
368
-
122
+ /**
123
+ * Used to detect user value changes and fire `change` events.
124
+ * Do not define in `properties` to avoid triggering updates.
125
+ * @type {string}
126
+ * @protected
127
+ */
369
128
  this._lastCommittedValue = this.value;
370
-
371
- this.addEventListener('click', this._boundOnClick);
372
- this.addEventListener('touchend', this._boundOnTouchend);
373
-
374
- const bringToFrontListener = () => {
375
- requestAnimationFrame(() => {
376
- this._overlayElement.bringToFront();
377
- });
378
- };
379
-
380
- this.addEventListener('mousedown', bringToFrontListener);
381
- this.addEventListener('touchstart', bringToFrontListener);
382
-
383
- this.addController(new VirtualKeyboardController(this));
384
- }
385
-
386
- /** @protected */
387
- disconnectedCallback() {
388
- super.disconnectedCallback();
389
-
390
- // Close the overlay on detach
391
- this.close();
392
129
  }
393
130
 
394
131
  /**
@@ -410,116 +147,32 @@ export const ComboBoxMixin = (subclass) =>
410
147
  }
411
148
 
412
149
  /**
413
- * Opens the dropdown list.
414
- */
415
- open() {
416
- // Prevent _open() being called when input is disabled or read-only
417
- if (!this.disabled && !this.readonly) {
418
- this.opened = true;
419
- }
420
- }
421
-
422
- /**
423
- * Closes the dropdown list.
424
- */
425
- close() {
426
- this.opened = false;
427
- }
428
-
429
- /**
430
- * Override LitElement lifecycle callback to handle filter property change.
431
150
  * @param {Object} props
432
151
  * @protected
433
152
  */
434
153
  updated(props) {
435
154
  super.updated(props);
436
155
 
437
- if (props.has('filter')) {
438
- this._filterChanged(this.filter);
439
- }
440
- }
441
-
442
- /** @private */
443
- _initOverlay() {
444
- const overlay = this.$.overlay;
445
-
446
- // Store instance for detecting "dir" attribute on opening
447
- overlay._comboBox = this;
448
-
449
- overlay.addEventListener('touchend', this._boundOnOverlayTouchAction);
450
- overlay.addEventListener('touchmove', this._boundOnOverlayTouchAction);
451
-
452
- // Prevent blurring the input when clicking inside the overlay
453
- overlay.addEventListener('mousedown', (e) => e.preventDefault());
454
-
455
- // Manual two-way binding for the overlay "opened" property
456
- overlay.addEventListener('opened-changed', (e) => {
457
- this._overlayOpened = e.detail.value;
458
- });
459
-
460
- this._overlayElement = overlay;
461
- }
462
-
463
- /**
464
- * Create and initialize the scroller element.
465
- * Override to provide custom host reference.
466
- *
467
- * @protected
468
- */
469
- _initScroller(host) {
470
- const scroller = document.createElement(`${this._tagNamePrefix}-scroller`);
471
-
472
- scroller.owner = host || this;
473
- scroller.getItemLabel = this._getItemLabel.bind(this);
474
- scroller.addEventListener('selection-changed', this._boundOverlaySelectedItemChanged);
475
-
476
- const overlay = this._overlayElement;
477
-
478
- overlay.renderer = (root) => {
479
- if (!root.innerHTML) {
480
- root.appendChild(scroller);
156
+ ['loading', 'itemIdPath', 'itemClassNameGenerator', 'renderer', 'selectedItem'].forEach((prop) => {
157
+ if (props.has(prop)) {
158
+ this._scroller[prop] = this[prop];
481
159
  }
482
- };
483
-
484
- // Ensure the scroller is rendered
485
- overlay.requestContentUpdate();
486
-
487
- // Trigger the observer to set properties
488
- this._scroller = scroller;
160
+ });
489
161
  }
490
162
 
491
163
  /** @private */
492
- // eslint-disable-next-line @typescript-eslint/max-params
493
- _updateScroller(
494
- scroller,
495
- items,
496
- opened,
497
- loading,
498
- selectedItem,
499
- itemIdPath,
500
- focusedIndex,
501
- renderer,
502
- theme,
503
- itemClassNameGenerator,
504
- ) {
505
- if (scroller) {
506
- if (opened) {
507
- scroller.style.maxHeight =
508
- getComputedStyle(this).getPropertyValue(`--${this._tagNamePrefix}-overlay-max-height`) || '65vh';
509
- }
510
-
511
- scroller.setProperties({
512
- items: opened ? items : [],
513
- opened,
514
- loading,
515
- selectedItem,
516
- itemIdPath,
517
- focusedIndex,
518
- renderer,
519
- theme,
520
- itemClassNameGenerator,
521
- });
164
+ _updateScroller(opened, items, focusedIndex, theme) {
165
+ if (opened) {
166
+ this._scroller.style.maxHeight =
167
+ getComputedStyle(this).getPropertyValue(`--${this._tagNamePrefix}-overlay-max-height`) || '65vh';
522
168
  }
169
+
170
+ this._scroller.setProperties({
171
+ items: opened ? items : [],
172
+ opened,
173
+ focusedIndex,
174
+ theme,
175
+ });
523
176
  }
524
177
 
525
178
  /** @private */
@@ -529,258 +182,39 @@ export const ComboBoxMixin = (subclass) =>
529
182
  this._overlayOpened = opened && (keepOverlayOpened || loading || !!(items && items.length));
530
183
  }
531
184
 
532
- /** @private */
533
- _overlayOpenedChanged(opened, wasOpened) {
534
- if (opened) {
535
- this.dispatchEvent(new CustomEvent('vaadin-combo-box-dropdown-opened', { bubbles: true, composed: true }));
536
-
537
- this._onOpened();
538
- } else if (wasOpened && this._dropdownItems && this._dropdownItems.length) {
539
- this.close();
540
-
541
- this.dispatchEvent(new CustomEvent('vaadin-combo-box-dropdown-closed', { bubbles: true, composed: true }));
542
- }
543
- }
544
-
545
- /** @private */
546
- _focusedIndexChanged(index, oldIndex) {
547
- if (oldIndex === undefined) {
548
- return;
549
- }
550
- this._updateActiveDescendant(index);
551
- }
552
-
553
- /** @protected */
554
- _isInputFocused() {
555
- return this.inputElement && isElementFocused(this.inputElement);
556
- }
557
-
558
- /** @private */
559
- _updateActiveDescendant(index) {
560
- const input = this._nativeInput;
561
- if (!input) {
562
- return;
563
- }
564
-
565
- const item = this._getItemElements().find((el) => el.index === index);
566
- if (item) {
567
- input.setAttribute('aria-activedescendant', item.id);
568
- } else {
569
- input.removeAttribute('aria-activedescendant');
570
- }
571
- }
572
-
573
- /** @private */
574
- _openedChanged(opened, wasOpened) {
575
- // Prevent _close() being called when opened is set to its default value (false).
576
- if (wasOpened === undefined) {
577
- return;
578
- }
579
-
580
- if (opened) {
581
- // For touch devices, we don't want to popup virtual keyboard
582
- // unless input element is explicitly focused by the user.
583
- if (!this._isInputFocused() && !isTouch) {
584
- if (this.inputElement) {
585
- this.inputElement.focus();
586
- }
587
- }
588
- } else {
589
- this._onClosed();
590
- }
591
-
592
- const input = this._nativeInput;
593
- if (input) {
594
- input.setAttribute('aria-expanded', !!opened);
595
-
596
- if (opened) {
597
- input.setAttribute('aria-controls', this._scroller.id);
598
- } else {
599
- input.removeAttribute('aria-controls');
600
- }
601
- }
602
- }
603
-
604
- /** @private */
605
- _onOverlayTouchAction() {
606
- // On touch devices, blur the input on touch start inside the overlay, in order to hide
607
- // the virtual keyboard. But don't close the overlay on this blur.
608
- this._closeOnBlurIsPrevented = true;
609
- this.inputElement.blur();
610
- this._closeOnBlurIsPrevented = false;
611
- }
612
-
613
- /** @protected */
614
- _isClearButton(event) {
615
- return event.composedPath()[0] === this.clearElement;
616
- }
617
-
618
- /** @private */
619
- __onClearButtonMouseDown(event) {
620
- event.preventDefault(); // Prevent native focusout event
621
- this.inputElement.focus();
622
- }
623
-
624
185
  /**
186
+ * Override method from `ComboBoxBaseMixin` to deselect
187
+ * dropdown item by requesting content update on clear.
625
188
  * @param {Event} event
626
189
  * @protected
627
190
  */
628
191
  _onClearButtonClick(event) {
629
- event.preventDefault();
630
- this._onClearAction();
192
+ super._onClearButtonClick(event);
631
193
 
632
- // De-select dropdown item
633
194
  if (this.opened) {
634
195
  this.requestContentUpdate();
635
196
  }
636
197
  }
637
198
 
638
199
  /**
639
- * @param {Event} event
640
- * @private
641
- */
642
- _onToggleButtonClick(event) {
643
- // Prevent parent components such as `vaadin-grid`
644
- // from handling the click event after it bubbles.
645
- event.preventDefault();
646
-
647
- if (this.opened) {
648
- this.close();
649
- } else {
650
- this.open();
651
- }
652
- }
653
-
654
- /**
655
- * @param {Event} event
200
+ * Override method inherited from `InputMixin`
201
+ * to revert the input value to value.
656
202
  * @protected
203
+ * @override
657
204
  */
658
- _onHostClick(event) {
659
- if (!this.autoOpenDisabled) {
660
- event.preventDefault();
661
- this.open();
662
- }
663
- }
205
+ _inputElementChanged(input) {
206
+ super._inputElementChanged(input);
664
207
 
665
- /** @private */
666
- _onClick(event) {
667
- if (this._isClearButton(event)) {
668
- this._onClearButtonClick(event);
669
- } else if (event.composedPath().includes(this._toggleElement)) {
670
- this._onToggleButtonClick(event);
671
- } else {
672
- this._onHostClick(event);
208
+ if (input) {
209
+ this._revertInputValueToValue();
673
210
  }
674
211
  }
675
212
 
676
213
  /**
677
- * Override an event listener from `KeyboardMixin`.
678
- *
679
- * @param {KeyboardEvent} e
214
+ * Override method from `ComboBoxBaseMixin` to handle loading.
680
215
  * @protected
681
216
  * @override
682
217
  */
683
- _onKeyDown(e) {
684
- super._onKeyDown(e);
685
-
686
- if (e.key === 'ArrowDown') {
687
- this._onArrowDown();
688
-
689
- // Prevent caret from moving
690
- e.preventDefault();
691
- } else if (e.key === 'ArrowUp') {
692
- this._onArrowUp();
693
-
694
- // Prevent caret from moving
695
- e.preventDefault();
696
- }
697
- }
698
-
699
- /** @private */
700
- _getItemLabel(item) {
701
- let label = item && this.itemLabelPath ? get(this.itemLabelPath, item) : undefined;
702
- if (label === undefined || label === null) {
703
- label = item ? item.toString() : '';
704
- }
705
- return label;
706
- }
707
-
708
- /** @private */
709
- _getItemValue(item) {
710
- let value = item && this.itemValuePath ? get(this.itemValuePath, item) : undefined;
711
- if (value === undefined) {
712
- value = item ? item.toString() : '';
713
- }
714
- return value;
715
- }
716
-
717
- /** @private */
718
- _onArrowDown() {
719
- if (this.opened) {
720
- const items = this._dropdownItems;
721
- if (items) {
722
- this._focusedIndex = Math.min(items.length - 1, this._focusedIndex + 1);
723
- this._prefillFocusedItemLabel();
724
- }
725
- } else {
726
- this.open();
727
- }
728
- }
729
-
730
- /** @private */
731
- _onArrowUp() {
732
- if (this.opened) {
733
- if (this._focusedIndex > -1) {
734
- this._focusedIndex = Math.max(0, this._focusedIndex - 1);
735
- } else {
736
- const items = this._dropdownItems;
737
- if (items) {
738
- this._focusedIndex = items.length - 1;
739
- }
740
- }
741
-
742
- this._prefillFocusedItemLabel();
743
- } else {
744
- this.open();
745
- }
746
- }
747
-
748
- /** @private */
749
- _prefillFocusedItemLabel() {
750
- if (this._focusedIndex > -1) {
751
- const focusedItem = this._dropdownItems[this._focusedIndex];
752
- this._inputElementValue = this._getItemLabel(focusedItem);
753
- this._markAllSelectionRange();
754
- }
755
- }
756
-
757
- /** @private */
758
- _setSelectionRange(start, end) {
759
- // Setting selection range focuses and/or moves the caret in some browsers,
760
- // and there's no need to modify the selection range if the input isn't focused anyway.
761
- // This affects Safari. When the overlay is open, and then hitting tab, browser should focus
762
- // the next focusable element instead of the combo-box itself.
763
- if (this._isInputFocused() && this.inputElement.setSelectionRange) {
764
- this.inputElement.setSelectionRange(start, end);
765
- }
766
- }
767
-
768
- /** @private */
769
- _markAllSelectionRange() {
770
- if (this._inputElementValue !== undefined) {
771
- this._setSelectionRange(0, this._inputElementValue.length);
772
- }
773
- }
774
-
775
- /** @private */
776
- _clearSelectionRange() {
777
- if (this._inputElementValue !== undefined) {
778
- const pos = this._inputElementValue ? this._inputElementValue.length : 0;
779
- this._setSelectionRange(pos, pos);
780
- }
781
- }
782
-
783
- /** @private */
784
218
  _closeOrCommit() {
785
219
  if (!this.opened && !this.loading) {
786
220
  this._commitValue();
@@ -790,39 +224,10 @@ export const ComboBoxMixin = (subclass) =>
790
224
  }
791
225
 
792
226
  /**
793
- * Override an event listener from `KeyboardMixin`.
794
- *
795
- * @param {KeyboardEvent} e
227
+ * Override method from `ComboBoxBaseMixin` to handle valid value.
796
228
  * @protected
797
229
  * @override
798
230
  */
799
- _onEnter(e) {
800
- // Do not commit value when custom values are disallowed and input value is not a valid option
801
- // also stop propagation of the event, otherwise the user could submit a form while the input
802
- // still contains an invalid value
803
- if (!this._hasValidInputValue()) {
804
- // Do not submit the surrounding form.
805
- e.preventDefault();
806
- // Do not trigger global listeners
807
- e.stopPropagation();
808
- return;
809
- }
810
-
811
- // Stop propagation of the enter event only if the dropdown is opened, this
812
- // "consumes" the enter event for the action of closing the dropdown
813
- if (this.opened) {
814
- // Do not submit the surrounding form.
815
- e.preventDefault();
816
- // Do not trigger global listeners
817
- e.stopPropagation();
818
- }
819
-
820
- this._closeOrCommit();
821
- }
822
-
823
- /**
824
- * @protected
825
- */
826
231
  _hasValidInputValue() {
827
232
  const hasInvalidOption =
828
233
  this._focusedIndex < 0 &&
@@ -833,62 +238,18 @@ export const ComboBoxMixin = (subclass) =>
833
238
  }
834
239
 
835
240
  /**
836
- * Override an event listener from `KeyboardMixin`.
837
- * Do not call `super` in order to override clear
838
- * button logic defined in `InputControlMixin`.
839
- *
840
- * @param {!KeyboardEvent} e
241
+ * Override method from `ComboBoxBaseMixin`.
841
242
  * @protected
842
243
  * @override
843
244
  */
844
- _onEscape(e) {
845
- if (
846
- this.autoOpenDisabled &&
847
- (this.opened || (this.value !== this._inputElementValue && this._inputElementValue.length > 0))
848
- ) {
849
- // Auto-open is disabled
850
- // The overlay is open or
851
- // The input value has changed but the change hasn't been committed, so cancel it.
852
- e.stopPropagation();
853
- this._focusedIndex = -1;
854
- this.cancel();
855
- } else if (this.opened) {
856
- // Auto-open is enabled
857
- // The overlay is open
858
- e.stopPropagation();
859
-
860
- if (this._focusedIndex > -1) {
861
- // An item is focused, revert the input to the filtered value
862
- this._focusedIndex = -1;
863
- this._revertInputValue();
864
- } else {
865
- // No item is focused, cancel the change and close the overlay
866
- this.cancel();
867
- }
868
- } else if (this.clearButtonVisible && !!this.value && !this.readonly) {
869
- e.stopPropagation();
870
- // The clear button is visible and the overlay is closed, so clear the value.
871
- this._onClearAction();
872
- }
873
- }
874
-
875
- /** @private */
876
- _toggleElementChanged(toggleElement) {
877
- if (toggleElement) {
878
- // Don't blur the input on toggle mousedown
879
- toggleElement.addEventListener('mousedown', (e) => e.preventDefault());
880
- // Unfocus previously focused element if focus is not inside combo box (on touch devices)
881
- toggleElement.addEventListener('click', () => {
882
- if (isTouch && !this._isInputFocused()) {
883
- document.activeElement.blur();
884
- }
885
- });
886
- }
245
+ _onEscapeCancel() {
246
+ this.cancel();
887
247
  }
888
248
 
889
249
  /**
890
- * Clears the current value.
250
+ * Override method from `ComboBoxBaseMixin` to reset selected item.
891
251
  * @protected
252
+ * @override
892
253
  */
893
254
  _onClearAction() {
894
255
  this.selectedItem = null;
@@ -919,20 +280,43 @@ export const ComboBoxMixin = (subclass) =>
919
280
  this._closeOrCommit();
920
281
  }
921
282
 
922
- /** @private */
283
+ /**
284
+ * Override method from `ComboBoxBaseMixin` to store last committed value.
285
+ * @protected
286
+ * @override
287
+ */
923
288
  _onOpened() {
289
+ this.dispatchEvent(new CustomEvent('vaadin-combo-box-dropdown-opened', { bubbles: true, composed: true }));
290
+
924
291
  // _detectAndDispatchChange() should not consider value changes done before opening
925
292
  this._lastCommittedValue = this.value;
926
293
  }
927
294
 
928
- /** @private */
295
+ /**
296
+ * Override method from `ComboBoxBaseMixin` to dispatch an event.
297
+ * @protected
298
+ * @override
299
+ */
300
+ _onOverlayClosed() {
301
+ this.dispatchEvent(new CustomEvent('vaadin-combo-box-dropdown-closed', { bubbles: true, composed: true }));
302
+ }
303
+
304
+ /**
305
+ * Override method from `ComboBoxBaseMixin` to commit value on overlay closing.
306
+ * @protected
307
+ * @override
308
+ */
929
309
  _onClosed() {
930
310
  if (!this.loading || this.allowCustomValue) {
931
311
  this._commitValue();
932
312
  }
933
313
  }
934
314
 
935
- /** @private */
315
+ /**
316
+ * Override method from `ComboBoxBaseMixin` to implement value commit logic.
317
+ * @protected
318
+ * @override
319
+ */
936
320
  _commitValue() {
937
321
  if (this._focusedIndex > -1) {
938
322
  const focusedItem = this._dropdownItems[this._focusedIndex];
@@ -991,36 +375,6 @@ export const ComboBoxMixin = (subclass) =>
991
375
  this._clearFilter();
992
376
  }
993
377
 
994
- /**
995
- * Override an event listener from `InputMixin`.
996
- * @param {!Event} event
997
- * @protected
998
- * @override
999
- */
1000
- _onInput(event) {
1001
- const filter = this._inputElementValue;
1002
-
1003
- // When opening dropdown on user input, both `opened` and `filter` properties are set.
1004
- // Perform a batched property update instead of relying on sync property observers.
1005
- // This is necessary to avoid an extra data-provider request for loading first page.
1006
- const props = {};
1007
-
1008
- if (this.filter === filter) {
1009
- // Filter and input value might get out of sync, while keyboard navigating for example.
1010
- // Afterwards, input value might be changed to the same value as used in filtering.
1011
- // In situation like these, we need to make sure all the filter changes handlers are run.
1012
- this._filterChanged(this.filter);
1013
- } else {
1014
- props.filter = filter;
1015
- }
1016
-
1017
- if (!this.opened && !this._isClearButton(event) && !this.autoOpenDisabled) {
1018
- props.opened = true;
1019
- }
1020
-
1021
- this.setProperties(props);
1022
- }
1023
-
1024
378
  /**
1025
379
  * Override an event listener from `InputMixin`.
1026
380
  * @param {!Event} event
@@ -1033,31 +387,11 @@ export const ComboBoxMixin = (subclass) =>
1033
387
  event.stopPropagation();
1034
388
  }
1035
389
 
1036
- /** @private */
1037
- _itemLabelPathChanged(itemLabelPath) {
1038
- if (typeof itemLabelPath !== 'string') {
1039
- console.error('You should set itemLabelPath to a valid string');
1040
- }
1041
- }
1042
-
1043
- /** @private */
1044
- _filterChanged(filter) {
1045
- // Scroll to the top of the list whenever the filter changes.
1046
- this._scrollIntoView(0);
1047
-
1048
- this._focusedIndex = -1;
1049
-
1050
- if (this.items) {
1051
- this.filteredItems = this._filterItems(this.items, filter);
1052
- } else {
1053
- // With certain use cases (e. g., external filtering), `items` are
1054
- // undefined. Filtering is unnecessary per se, but the filteredItems
1055
- // observer should still be invoked to update focused item.
1056
- this._filteredItemsChanged(this.filteredItems);
1057
- }
1058
- }
1059
-
1060
- /** @protected */
390
+ /**
391
+ * Override method from `ComboBoxBaseMixin` to handle reverting value.
392
+ * @protected
393
+ * @override
394
+ */
1061
395
  _revertInputValue() {
1062
396
  if (this.filter !== '') {
1063
397
  this._inputElementValue = this.filter;
@@ -1149,40 +483,6 @@ export const ComboBoxMixin = (subclass) =>
1149
483
  }
1150
484
  }
1151
485
 
1152
- /** @private */
1153
- _itemsChanged(items, oldItems) {
1154
- this._ensureItemsOrDataProvider(() => {
1155
- this.items = oldItems;
1156
- });
1157
-
1158
- if (items) {
1159
- this.filteredItems = items.slice(0);
1160
- } else if (oldItems) {
1161
- // Only clear filteredItems if the component had items previously but got cleared
1162
- this.filteredItems = null;
1163
- }
1164
- }
1165
-
1166
- /** @private */
1167
- _filteredItemsChanged(filteredItems) {
1168
- this._setDropdownItems(filteredItems);
1169
- }
1170
-
1171
- /** @private */
1172
- _filterItems(arr, filter) {
1173
- if (!arr) {
1174
- return arr;
1175
- }
1176
-
1177
- const filteredItems = arr.filter((item) => {
1178
- filter = filter ? filter.toString().toLowerCase() : '';
1179
- // Check if item contains input value.
1180
- return this._getItemLabel(item).toString().toLowerCase().indexOf(filter) > -1;
1181
- });
1182
-
1183
- return filteredItems;
1184
- }
1185
-
1186
486
  /** @private */
1187
487
  _selectItemForValue(value) {
1188
488
  const valueIndex = this.__getItemIndexByValue(this.filteredItems, value);
@@ -1206,6 +506,7 @@ export const ComboBoxMixin = (subclass) =>
1206
506
  * Override this method to show custom items.
1207
507
  *
1208
508
  * @protected
509
+ * @override
1209
510
  */
1210
511
  _setDropdownItems(newItems) {
1211
512
  const oldItems = this._dropdownItems;
@@ -1238,135 +539,20 @@ export const ComboBoxMixin = (subclass) =>
1238
539
  }
1239
540
  }
1240
541
 
1241
- /** @private */
1242
- _getItemElements() {
1243
- return Array.from(this._scroller.querySelectorAll(`${this._tagNamePrefix}-item`));
1244
- }
1245
-
1246
- /** @private */
1247
- _scrollIntoView(index) {
1248
- if (!this._scroller) {
1249
- return;
1250
- }
1251
- this._scroller.scrollIntoView(index);
1252
- }
1253
-
1254
- /**
1255
- * Returns the first item that matches the provided value.
1256
- *
1257
- * @private
1258
- */
1259
- __getItemIndexByValue(items, value) {
1260
- if (!items || !isValidValue(value)) {
1261
- return -1;
1262
- }
1263
-
1264
- return findItemIndex(items, (item) => {
1265
- return this._getItemValue(item) === value;
1266
- });
1267
- }
1268
-
1269
- /**
1270
- * Returns the first item that matches the provided label.
1271
- * Labels are matched against each other case insensitively.
1272
- *
1273
- * @private
1274
- */
1275
- __getItemIndexByLabel(items, label) {
1276
- if (!items || !label) {
1277
- return -1;
1278
- }
1279
-
1280
- return findItemIndex(items, (item) => {
1281
- return this._getItemLabel(item).toString().toLowerCase() === label.toString().toLowerCase();
1282
- });
1283
- }
1284
-
1285
- /** @private */
1286
- _overlaySelectedItemChanged(e) {
1287
- // Stop this private event from leaking outside.
1288
- e.stopPropagation();
1289
-
1290
- if (e.detail.item instanceof ComboBoxPlaceholder) {
1291
- // Placeholder items should not be selectable.
1292
- return;
1293
- }
1294
-
1295
- if (this.opened) {
1296
- this._focusedIndex = this.filteredItems.indexOf(e.detail.item);
1297
- this.close();
1298
- }
1299
- }
1300
-
1301
542
  /**
1302
- * Override method inherited from `FocusMixin`
1303
- * to close the overlay on blur and commit the value.
1304
- *
1305
- * @param {boolean} focused
543
+ * Override method from `ComboBoxBaseMixin`.
1306
544
  * @protected
1307
545
  * @override
1308
546
  */
1309
- _setFocused(focused) {
1310
- super._setFocused(focused);
1311
-
1312
- if (!focused && !this.readonly && !this._closeOnBlurIsPrevented) {
1313
- // User's logic in `custom-value-set` event listener might cause input to blur,
1314
- // which will result in attempting to commit the same custom value once again.
1315
- if (!this.opened && this.allowCustomValue && this._inputElementValue === this._lastCustomValue) {
1316
- delete this._lastCustomValue;
1317
- return;
1318
- }
1319
-
1320
- if (isKeyboardActive()) {
1321
- // Close on Tab key causing blur. With mouse, close on outside click instead.
1322
- this._closeOrCommit();
1323
- return;
1324
- }
1325
-
1326
- if (!this.opened) {
1327
- this._commitValue();
1328
- } else if (!this._overlayOpened) {
1329
- // Combo-box is opened, but overlay is not visible -> custom value was entered.
1330
- // Make sure we close here as there won't be an "outside click" in this case.
1331
- this.close();
1332
- }
1333
- }
1334
- }
1335
-
1336
- /**
1337
- * Override method inherited from `FocusMixin` to not remove focused
1338
- * state when focus moves to the overlay.
1339
- *
1340
- * @param {FocusEvent} event
1341
- * @return {boolean}
1342
- * @protected
1343
- * @override
1344
- */
1345
- _shouldRemoveFocus(event) {
1346
- // VoiceOver on iOS fires `focusout` event when moving focus to the item in the dropdown.
1347
- // Do not focus the input in this case, because it would break announcement for the item.
1348
- if (event.relatedTarget && event.relatedTarget.localName === `${this._tagNamePrefix}-item`) {
1349
- return false;
1350
- }
1351
-
1352
- // Do not blur when focus moves to the overlay
1353
- // Also, fixes the problem with `focusout` happening when clicking on the scroll bar on Edge
1354
- if (event.relatedTarget === this._overlayElement) {
1355
- event.composedPath()[0].focus();
1356
- return false;
1357
- }
1358
-
1359
- return true;
1360
- }
1361
-
1362
- /** @private */
1363
- _onTouchend(event) {
1364
- if (!this.clearElement || event.composedPath()[0] !== this.clearElement) {
547
+ _handleFocusOut() {
548
+ // User's logic in `custom-value-set` event listener might cause input to blur,
549
+ // which will result in attempting to commit the same custom value once again.
550
+ if (!this.opened && this.allowCustomValue && this._inputElementValue === this._lastCustomValue) {
551
+ delete this._lastCustomValue;
1365
552
  return;
1366
553
  }
1367
554
 
1368
- event.preventDefault();
1369
- this._onClearAction();
555
+ super._handleFocusOut();
1370
556
  }
1371
557
 
1372
558
  /**