@vaadin/multi-select-combo-box 24.6.0-alpha9 → 24.6.0-beta1

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.
Files changed (26) hide show
  1. package/package.json +16 -15
  2. package/src/vaadin-lit-multi-select-combo-box-chip.js +79 -0
  3. package/src/vaadin-lit-multi-select-combo-box-container.js +66 -0
  4. package/src/vaadin-lit-multi-select-combo-box-internal.js +56 -0
  5. package/src/vaadin-lit-multi-select-combo-box-item.js +50 -0
  6. package/src/vaadin-lit-multi-select-combo-box-overlay.js +64 -0
  7. package/src/vaadin-lit-multi-select-combo-box-scroller.js +96 -0
  8. package/src/vaadin-lit-multi-select-combo-box.d.ts +1 -0
  9. package/src/vaadin-lit-multi-select-combo-box.js +146 -0
  10. package/src/vaadin-multi-select-combo-box-chip.js +6 -27
  11. package/src/vaadin-multi-select-combo-box-internal-mixin.js +425 -0
  12. package/src/vaadin-multi-select-combo-box-internal.js +3 -399
  13. package/src/vaadin-multi-select-combo-box-mixin.d.ts +253 -0
  14. package/src/vaadin-multi-select-combo-box-mixin.js +1150 -0
  15. package/src/vaadin-multi-select-combo-box-styles.d.ts +10 -0
  16. package/src/vaadin-multi-select-combo-box-styles.js +73 -0
  17. package/src/vaadin-multi-select-combo-box.d.ts +5 -213
  18. package/src/vaadin-multi-select-combo-box.js +5 -1139
  19. package/theme/lumo/vaadin-lit-multi-select-combo-box.d.ts +3 -0
  20. package/theme/lumo/vaadin-lit-multi-select-combo-box.js +3 -0
  21. package/theme/material/vaadin-lit-multi-select-combo-box.d.ts +3 -0
  22. package/theme/material/vaadin-lit-multi-select-combo-box.js +3 -0
  23. package/vaadin-lit-multi-select-combo-box.d.ts +1 -0
  24. package/vaadin-lit-multi-select-combo-box.js +2 -0
  25. package/web-types.json +5 -5
  26. package/web-types.lit.json +8 -8
@@ -0,0 +1,1150 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 - 2024 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { announce } from '@vaadin/a11y-base/src/announce.js';
7
+ import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js';
8
+ import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
9
+ import { processTemplates } from '@vaadin/component-base/src/templates.js';
10
+ import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
11
+ import { InputControlMixin } from '@vaadin/field-base/src/input-control-mixin.js';
12
+ import { InputController } from '@vaadin/field-base/src/input-controller.js';
13
+ import { LabelledInputController } from '@vaadin/field-base/src/labelled-input-controller.js';
14
+
15
+ /**
16
+ * @polymerMixin
17
+ * @mixes InputControlMixin
18
+ * @mixes ResizeMixin
19
+ */
20
+ export const MultiSelectComboBoxMixin = (superClass) =>
21
+ class MultiSelectComboBoxMixinClass extends InputControlMixin(ResizeMixin(superClass)) {
22
+ static get properties() {
23
+ return {
24
+ /**
25
+ * Set to true to auto expand horizontally, causing input field to
26
+ * grow until max width is reached.
27
+ * @attr {boolean} auto-expand-horizontally
28
+ */
29
+ autoExpandHorizontally: {
30
+ type: Boolean,
31
+ value: false,
32
+ reflectToAttribute: true,
33
+ observer: '_autoExpandHorizontallyChanged',
34
+ sync: true,
35
+ },
36
+
37
+ /**
38
+ * Set to true to not collapse selected items chips into the overflow
39
+ * chip and instead always expand vertically, causing input field to
40
+ * wrap into multiple lines when width is limited.
41
+ * @attr {boolean} auto-expand-vertically
42
+ */
43
+ autoExpandVertically: {
44
+ type: Boolean,
45
+ value: false,
46
+ reflectToAttribute: true,
47
+ observer: '_autoExpandVerticallyChanged',
48
+ sync: true,
49
+ },
50
+
51
+ /**
52
+ * Set true to prevent the overlay from opening automatically.
53
+ * @attr {boolean} auto-open-disabled
54
+ */
55
+ autoOpenDisabled: {
56
+ type: Boolean,
57
+ sync: true,
58
+ },
59
+
60
+ /**
61
+ * Set to true to display the clear icon which clears the input.
62
+ * @attr {boolean} clear-button-visible
63
+ */
64
+ clearButtonVisible: {
65
+ type: Boolean,
66
+ reflectToAttribute: true,
67
+ observer: '_clearButtonVisibleChanged',
68
+ value: false,
69
+ sync: true,
70
+ },
71
+
72
+ /**
73
+ * A full set of items to filter the visible options from.
74
+ * The items can be of either `String` or `Object` type.
75
+ */
76
+ items: {
77
+ type: Array,
78
+ sync: true,
79
+ },
80
+
81
+ /**
82
+ * A function used to generate CSS class names for dropdown
83
+ * items and selected chips based on the item. The return
84
+ * value should be the generated class name as a string, or
85
+ * multiple class names separated by whitespace characters.
86
+ */
87
+ itemClassNameGenerator: {
88
+ type: Object,
89
+ observer: '__itemClassNameGeneratorChanged',
90
+ sync: true,
91
+ },
92
+
93
+ /**
94
+ * The item property used for a visual representation of the item.
95
+ * @attr {string} item-label-path
96
+ */
97
+ itemLabelPath: {
98
+ type: String,
99
+ value: 'label',
100
+ sync: true,
101
+ },
102
+
103
+ /**
104
+ * Path for the value of the item. If `items` is an array of objects,
105
+ * this property is used as a string value for the selected item.
106
+ * @attr {string} item-value-path
107
+ */
108
+ itemValuePath: {
109
+ type: String,
110
+ value: 'value',
111
+ sync: true,
112
+ },
113
+
114
+ /**
115
+ * Path for the id of the item, used to detect whether the item is selected.
116
+ * @attr {string} item-id-path
117
+ */
118
+ itemIdPath: {
119
+ type: String,
120
+ sync: true,
121
+ },
122
+
123
+ /**
124
+ * The object used to localize this component.
125
+ * To change the default localization, replace the entire
126
+ * _i18n_ object or just the property you want to modify.
127
+ *
128
+ * The object has the following JSON structure and default values:
129
+ * ```
130
+ * {
131
+ * // Screen reader announcement on clear button click.
132
+ * cleared: 'Selection cleared',
133
+ * // Screen reader announcement when a chip is focused.
134
+ * focused: ' focused. Press Backspace to remove',
135
+ * // Screen reader announcement when item is selected.
136
+ * selected: 'added to selection',
137
+ * // Screen reader announcement when item is deselected.
138
+ * deselected: 'removed from selection',
139
+ * // Screen reader announcement of the selected items count.
140
+ * // {count} is replaced with the actual count of items.
141
+ * total: '{count} items selected',
142
+ * }
143
+ * ```
144
+ * @type {!MultiSelectComboBoxI18n}
145
+ * @default {English/US}
146
+ */
147
+ i18n: {
148
+ type: Object,
149
+ value: () => {
150
+ return {
151
+ cleared: 'Selection cleared',
152
+ focused: 'focused. Press Backspace to remove',
153
+ selected: 'added to selection',
154
+ deselected: 'removed from selection',
155
+ total: '{count} items selected',
156
+ };
157
+ },
158
+ },
159
+
160
+ /**
161
+ * When true, filter string isn't cleared after selecting an item.
162
+ */
163
+ keepFilter: {
164
+ type: Boolean,
165
+ value: false,
166
+ },
167
+
168
+ /**
169
+ * True when loading items from the data provider, false otherwise.
170
+ */
171
+ loading: {
172
+ type: Boolean,
173
+ value: false,
174
+ reflectToAttribute: true,
175
+ sync: true,
176
+ },
177
+
178
+ /**
179
+ * A space-delimited list of CSS class names to set on the overlay element.
180
+ *
181
+ * @attr {string} overlay-class
182
+ */
183
+ overlayClass: {
184
+ type: String,
185
+ sync: true,
186
+ },
187
+
188
+ /**
189
+ * When present, it specifies that the field is read-only.
190
+ */
191
+ readonly: {
192
+ type: Boolean,
193
+ value: false,
194
+ observer: '_readonlyChanged',
195
+ reflectToAttribute: true,
196
+ sync: true,
197
+ },
198
+
199
+ /**
200
+ * The list of selected items.
201
+ * Note: modifying the selected items creates a new array each time.
202
+ */
203
+ selectedItems: {
204
+ type: Array,
205
+ value: () => [],
206
+ notify: true,
207
+ sync: true,
208
+ },
209
+
210
+ /**
211
+ * True if the dropdown is open, false otherwise.
212
+ */
213
+ opened: {
214
+ type: Boolean,
215
+ notify: true,
216
+ value: false,
217
+ reflectToAttribute: true,
218
+ sync: true,
219
+ },
220
+
221
+ /**
222
+ * Total number of items.
223
+ */
224
+ size: {
225
+ type: Number,
226
+ sync: true,
227
+ },
228
+
229
+ /**
230
+ * Number of items fetched at a time from the data provider.
231
+ * @attr {number} page-size
232
+ */
233
+ pageSize: {
234
+ type: Number,
235
+ value: 50,
236
+ observer: '_pageSizeChanged',
237
+ sync: true,
238
+ },
239
+
240
+ /**
241
+ * Function that provides items lazily. Receives two arguments:
242
+ *
243
+ * - `params` - Object with the following properties:
244
+ * - `params.page` Requested page index
245
+ * - `params.pageSize` Current page size
246
+ * - `params.filter` Currently applied filter
247
+ *
248
+ * - `callback(items, size)` - Callback function with arguments:
249
+ * - `items` Current page of items
250
+ * - `size` Total number of items.
251
+ */
252
+ dataProvider: {
253
+ type: Object,
254
+ sync: true,
255
+ },
256
+
257
+ /**
258
+ * When true, the user can input a value that is not present in the items list.
259
+ * @attr {boolean} allow-custom-value
260
+ */
261
+ allowCustomValue: {
262
+ type: Boolean,
263
+ value: false,
264
+ },
265
+
266
+ /**
267
+ * A hint to the user of what can be entered in the control.
268
+ * The placeholder will be only displayed in the case when
269
+ * there is no item selected.
270
+ */
271
+ placeholder: {
272
+ type: String,
273
+ observer: '_placeholderChanged',
274
+ sync: true,
275
+ },
276
+
277
+ /**
278
+ * Custom function for rendering the content of every item.
279
+ * Receives three arguments:
280
+ *
281
+ * - `root` The `<vaadin-multi-select-combo-box-item>` internal container DOM element.
282
+ * - `comboBox` The reference to the `<vaadin-multi-select-combo-box>` element.
283
+ * - `model` The object with the properties related with the rendered
284
+ * item, contains:
285
+ * - `model.index` The index of the rendered item.
286
+ * - `model.item` The item.
287
+ */
288
+ renderer: {
289
+ type: Function,
290
+ sync: true,
291
+ },
292
+
293
+ /**
294
+ * Filtering string the user has typed into the input field.
295
+ */
296
+ filter: {
297
+ type: String,
298
+ value: '',
299
+ notify: true,
300
+ sync: true,
301
+ },
302
+
303
+ /**
304
+ * A subset of items, filtered based on the user input. Filtered items
305
+ * can be assigned directly to omit the internal filtering functionality.
306
+ * The items can be of either `String` or `Object` type.
307
+ */
308
+ filteredItems: {
309
+ type: Array,
310
+ sync: true,
311
+ },
312
+
313
+ /**
314
+ * Set to true to group selected items at the top of the overlay.
315
+ * @attr {boolean} selected-items-on-top
316
+ */
317
+ selectedItemsOnTop: {
318
+ type: Boolean,
319
+ value: false,
320
+ sync: true,
321
+ },
322
+
323
+ /** @private */
324
+ value: {
325
+ type: String,
326
+ },
327
+
328
+ /** @private */
329
+ _overflowItems: {
330
+ type: Array,
331
+ value: () => [],
332
+ sync: true,
333
+ },
334
+
335
+ /** @private */
336
+ _focusedChipIndex: {
337
+ type: Number,
338
+ value: -1,
339
+ observer: '_focusedChipIndexChanged',
340
+ },
341
+
342
+ /** @private */
343
+ _lastFilter: {
344
+ type: String,
345
+ sync: true,
346
+ },
347
+
348
+ /** @private */
349
+ _topGroup: {
350
+ type: Array,
351
+ },
352
+ };
353
+ }
354
+
355
+ static get observers() {
356
+ return [
357
+ '_selectedItemsChanged(selectedItems)',
358
+ '__updateOverflowChip(_overflow, _overflowItems, disabled, readonly)',
359
+ '__updateTopGroup(selectedItemsOnTop, selectedItems, opened)',
360
+ ];
361
+ }
362
+
363
+ /** @protected */
364
+ get slotStyles() {
365
+ const tag = this.localName;
366
+ return [
367
+ ...super.slotStyles,
368
+ `
369
+ ${tag}[has-value] input::placeholder {
370
+ color: transparent !important;
371
+ forced-color-adjust: none;
372
+ }
373
+ `,
374
+ ];
375
+ }
376
+
377
+ /**
378
+ * Used by `InputControlMixin` as a reference to the clear button element.
379
+ * @protected
380
+ * @return {!HTMLElement}
381
+ */
382
+ get clearElement() {
383
+ return this.$.clearButton;
384
+ }
385
+
386
+ /** @protected */
387
+ get _chips() {
388
+ return [...this.querySelectorAll('[slot="chip"]')];
389
+ }
390
+
391
+ /**
392
+ * Override a getter from `InputMixin` to compute
393
+ * the presence of value based on `selectedItems`.
394
+ *
395
+ * @protected
396
+ * @override
397
+ */
398
+ get _hasValue() {
399
+ return this.selectedItems && this.selectedItems.length > 0;
400
+ }
401
+
402
+ /** @protected */
403
+ ready() {
404
+ super.ready();
405
+
406
+ this.addController(
407
+ new InputController(this, (input) => {
408
+ this._setInputElement(input);
409
+ this._setFocusElement(input);
410
+ this.stateTarget = input;
411
+ this.ariaTarget = input;
412
+ }),
413
+ );
414
+ this.addController(new LabelledInputController(this.inputElement, this._labelController));
415
+
416
+ this._tooltipController = new TooltipController(this);
417
+ this.addController(this._tooltipController);
418
+ this._tooltipController.setPosition('top');
419
+ this._tooltipController.setAriaTarget(this.inputElement);
420
+ this._tooltipController.setShouldShow((target) => !target.opened);
421
+
422
+ this._inputField = this.shadowRoot.querySelector('[part="input-field"]');
423
+
424
+ this._overflowController = new SlotController(this, 'overflow', 'vaadin-multi-select-combo-box-chip', {
425
+ initializer: (chip) => {
426
+ chip.addEventListener('mousedown', (e) => this._preventBlur(e));
427
+ this._overflow = chip;
428
+ },
429
+ });
430
+ this.addController(this._overflowController);
431
+
432
+ this.__updateChips();
433
+
434
+ processTemplates(this);
435
+ }
436
+
437
+ /**
438
+ * Returns true if the current input value satisfies all constraints (if any).
439
+ * @return {boolean}
440
+ */
441
+ checkValidity() {
442
+ return this.required && !this.readonly ? this._hasValue : true;
443
+ }
444
+
445
+ /**
446
+ * Clears the selected items.
447
+ */
448
+ clear() {
449
+ this.__updateSelection([]);
450
+
451
+ announce(this.i18n.cleared);
452
+ }
453
+
454
+ /**
455
+ * Clears the cached pages and reloads data from data provider when needed.
456
+ */
457
+ clearCache() {
458
+ if (this.$ && this.$.comboBox) {
459
+ this.$.comboBox.clearCache();
460
+ }
461
+ }
462
+
463
+ /**
464
+ * Requests an update for the content of items.
465
+ * While performing the update, it invokes the renderer (passed in the `renderer` property) once an item.
466
+ *
467
+ * It is not guaranteed that the update happens immediately (synchronously) after it is requested.
468
+ */
469
+ requestContentUpdate() {
470
+ if (this.$ && this.$.comboBox) {
471
+ this.$.comboBox.requestContentUpdate();
472
+ }
473
+ }
474
+
475
+ /**
476
+ * Override method inherited from `DisabledMixin` to forward disabled to chips.
477
+ * @protected
478
+ * @override
479
+ */
480
+ _disabledChanged(disabled, oldDisabled) {
481
+ super._disabledChanged(disabled, oldDisabled);
482
+
483
+ if (disabled || oldDisabled) {
484
+ this.__updateChips();
485
+ }
486
+ }
487
+
488
+ /**
489
+ * Override method inherited from `InputMixin` to forward the input to combo-box.
490
+ * @protected
491
+ * @override
492
+ */
493
+ _inputElementChanged(input) {
494
+ super._inputElementChanged(input);
495
+
496
+ if (input) {
497
+ this.$.comboBox._setInputElement(input);
498
+ }
499
+ }
500
+
501
+ /**
502
+ * Override method inherited from `FocusMixin` to validate on blur.
503
+ * @param {boolean} focused
504
+ * @protected
505
+ */
506
+ _setFocused(focused) {
507
+ super._setFocused(focused);
508
+
509
+ // Do not validate when focusout is caused by document
510
+ // losing focus, which happens on browser tab switch.
511
+ if (!focused && document.hasFocus()) {
512
+ this._focusedChipIndex = -1;
513
+ this._requestValidation();
514
+ }
515
+ }
516
+
517
+ /**
518
+ * Implement callback from `ResizeMixin` to update chips.
519
+ * @protected
520
+ * @override
521
+ */
522
+ _onResize() {
523
+ this.__updateChips();
524
+ }
525
+
526
+ /**
527
+ * Override method from `DelegateStateMixin` to set required state
528
+ * using `aria-required` attribute instead of `required`, in order
529
+ * to prevent screen readers from announcing "invalid entry".
530
+ * @protected
531
+ * @override
532
+ */
533
+ _delegateAttribute(name, value) {
534
+ if (!this.stateTarget) {
535
+ return;
536
+ }
537
+
538
+ if (name === 'required') {
539
+ this._delegateAttribute('aria-required', value ? 'true' : false);
540
+ return;
541
+ }
542
+
543
+ super._delegateAttribute(name, value);
544
+ }
545
+
546
+ /** @private */
547
+ _autoExpandHorizontallyChanged(autoExpand, oldAutoExpand) {
548
+ if (autoExpand || oldAutoExpand) {
549
+ this.__updateChips();
550
+ }
551
+ }
552
+
553
+ /** @private */
554
+ _autoExpandVerticallyChanged(autoExpand, oldAutoExpand) {
555
+ if (autoExpand || oldAutoExpand) {
556
+ this.__updateChips();
557
+ }
558
+ }
559
+
560
+ /**
561
+ * Setting clear button visible reduces total space available
562
+ * for rendering chips, and making it hidden increases it.
563
+ * @private
564
+ */
565
+ _clearButtonVisibleChanged(visible, oldVisible) {
566
+ if (visible || oldVisible) {
567
+ this.__updateChips();
568
+ }
569
+ }
570
+
571
+ /**
572
+ * Implement two-way binding for the `filteredItems` property
573
+ * that can be set on the internal combo-box element.
574
+ *
575
+ * @param {CustomEvent} event
576
+ * @private
577
+ */
578
+ _onFilteredItemsChanged(event) {
579
+ const { value } = event.detail;
580
+ if (Array.isArray(value) || value == null) {
581
+ this.filteredItems = value;
582
+ }
583
+ }
584
+
585
+ /** @private */
586
+ _readonlyChanged(readonly, oldReadonly) {
587
+ if (readonly || oldReadonly) {
588
+ this.__updateChips();
589
+ }
590
+
591
+ if (this.dataProvider) {
592
+ this.clearCache();
593
+ }
594
+ }
595
+
596
+ /** @private */
597
+ __itemClassNameGeneratorChanged(generator, oldGenerator) {
598
+ if (generator || oldGenerator) {
599
+ this.__updateChips();
600
+ }
601
+ }
602
+
603
+ /** @private */
604
+ _pageSizeChanged(pageSize, oldPageSize) {
605
+ if (Math.floor(pageSize) !== pageSize || pageSize <= 0) {
606
+ this.pageSize = oldPageSize;
607
+ console.error('"pageSize" value must be an integer > 0');
608
+ }
609
+
610
+ this.$.comboBox.pageSize = this.pageSize;
611
+ }
612
+
613
+ /** @private */
614
+ _placeholderChanged(placeholder) {
615
+ const tmpPlaceholder = this.__tmpA11yPlaceholder;
616
+ // Do not store temporary placeholder
617
+ if (tmpPlaceholder !== placeholder) {
618
+ this.__savedPlaceholder = placeholder;
619
+
620
+ if (tmpPlaceholder) {
621
+ this.placeholder = tmpPlaceholder;
622
+ }
623
+ }
624
+ }
625
+
626
+ /** @private */
627
+ _selectedItemsChanged(selectedItems) {
628
+ this._toggleHasValue(this._hasValue);
629
+
630
+ // Use placeholder for announcing items
631
+ if (this._hasValue) {
632
+ const tmpPlaceholder = this._mergeItemLabels(selectedItems);
633
+ if (this.__tmpA11yPlaceholder === undefined) {
634
+ this.__savedPlaceholder = this.placeholder;
635
+ }
636
+ this.__tmpA11yPlaceholder = tmpPlaceholder;
637
+ this.placeholder = tmpPlaceholder;
638
+ } else if (this.__tmpA11yPlaceholder !== undefined) {
639
+ delete this.__tmpA11yPlaceholder;
640
+ this.placeholder = this.__savedPlaceholder;
641
+ }
642
+
643
+ // Re-render chips
644
+ this.__updateChips();
645
+
646
+ // Update selected for dropdown items
647
+ this.requestContentUpdate();
648
+
649
+ if (this.opened) {
650
+ this.$.comboBox._updateOverlayWidth();
651
+ }
652
+ }
653
+
654
+ /** @private */
655
+ _getItemLabel(item) {
656
+ return this.$.comboBox._getItemLabel(item);
657
+ }
658
+
659
+ /** @private */
660
+ _mergeItemLabels(items) {
661
+ return items.map((item) => this._getItemLabel(item)).join(', ');
662
+ }
663
+
664
+ /** @private */
665
+ _findIndex(item, selectedItems, itemIdPath) {
666
+ if (itemIdPath && item) {
667
+ for (let index = 0; index < selectedItems.length; index++) {
668
+ if (selectedItems[index] && selectedItems[index][itemIdPath] === item[itemIdPath]) {
669
+ return index;
670
+ }
671
+ }
672
+ return -1;
673
+ }
674
+
675
+ return selectedItems.indexOf(item);
676
+ }
677
+
678
+ /**
679
+ * Clear the internal combo box value and filter. Filter will not be cleared
680
+ * when the `keepFilter` option is enabled. Using `force` can enforce clearing
681
+ * the filter.
682
+ * @param {boolean} force overrides the keepFilter option
683
+ * @private
684
+ */
685
+ __clearInternalValue(force = false) {
686
+ if (!this.keepFilter || force) {
687
+ // Clear both combo box value and filter.
688
+ this.filter = '';
689
+ this.$.comboBox.clear();
690
+ } else {
691
+ // Only clear combo box value. This effectively resets _lastCommittedValue
692
+ // which allows toggling the same item multiple times via keyboard.
693
+ this.$.comboBox.clear();
694
+ // Restore input to the filter value. Needed when items are
695
+ // navigated with keyboard, which overrides the input value
696
+ // with the item label.
697
+ this._inputElementValue = this.filter;
698
+ }
699
+ }
700
+
701
+ /** @private */
702
+ __announceItem(itemLabel, isSelected, itemCount) {
703
+ const state = isSelected ? 'selected' : 'deselected';
704
+ const total = this.i18n.total.replace('{count}', itemCount || 0);
705
+ announce(`${itemLabel} ${this.i18n[state]} ${total}`);
706
+ }
707
+
708
+ /** @private */
709
+ __removeItem(item) {
710
+ const itemsCopy = [...this.selectedItems];
711
+ itemsCopy.splice(itemsCopy.indexOf(item), 1);
712
+ this.__updateSelection(itemsCopy);
713
+ const itemLabel = this._getItemLabel(item);
714
+ this.__announceItem(itemLabel, false, itemsCopy.length);
715
+ }
716
+
717
+ /** @private */
718
+ __selectItem(item) {
719
+ const itemsCopy = [...this.selectedItems];
720
+
721
+ const index = this._findIndex(item, itemsCopy, this.itemIdPath);
722
+ const itemLabel = this._getItemLabel(item);
723
+
724
+ let isSelected = false;
725
+
726
+ if (index !== -1) {
727
+ const lastFilter = this._lastFilter;
728
+ // Do not unselect when manually typing and committing an already selected item.
729
+ if (lastFilter && lastFilter.toLowerCase() === itemLabel.toLowerCase()) {
730
+ this.__clearInternalValue();
731
+ return;
732
+ }
733
+
734
+ itemsCopy.splice(index, 1);
735
+ } else {
736
+ itemsCopy.push(item);
737
+ isSelected = true;
738
+ }
739
+
740
+ this.__updateSelection(itemsCopy);
741
+
742
+ // Suppress `value-changed` event.
743
+ this.__clearInternalValue();
744
+
745
+ this.__announceItem(itemLabel, isSelected, itemsCopy.length);
746
+ }
747
+
748
+ /** @private */
749
+ __updateSelection(selectedItems) {
750
+ this.selectedItems = selectedItems;
751
+
752
+ this._requestValidation();
753
+
754
+ this.dispatchEvent(new CustomEvent('change', { bubbles: true }));
755
+ }
756
+
757
+ /** @private */
758
+ __updateTopGroup(selectedItemsOnTop, selectedItems, opened) {
759
+ if (!selectedItemsOnTop) {
760
+ this._topGroup = [];
761
+ } else if (!opened) {
762
+ this._topGroup = [...selectedItems];
763
+ }
764
+ }
765
+
766
+ /** @private */
767
+ __createChip(item) {
768
+ const chip = document.createElement('vaadin-multi-select-combo-box-chip');
769
+ chip.setAttribute('slot', 'chip');
770
+
771
+ chip.item = item;
772
+ chip.disabled = this.disabled;
773
+ chip.readonly = this.readonly;
774
+
775
+ const label = this._getItemLabel(item);
776
+ chip.label = label;
777
+ chip.setAttribute('title', label);
778
+
779
+ if (typeof this.itemClassNameGenerator === 'function') {
780
+ chip.className = this.itemClassNameGenerator(item);
781
+ }
782
+
783
+ chip.addEventListener('item-removed', (e) => this._onItemRemoved(e));
784
+ chip.addEventListener('mousedown', (e) => this._preventBlur(e));
785
+
786
+ return chip;
787
+ }
788
+
789
+ /** @private */
790
+ __getOverflowWidth() {
791
+ const chip = this._overflow;
792
+
793
+ chip.style.visibility = 'hidden';
794
+ chip.removeAttribute('hidden');
795
+
796
+ const count = chip.getAttribute('count');
797
+
798
+ // Detect max possible width of the overflow chip
799
+ // by measuring it with widest number (2 digits)
800
+ chip.setAttribute('count', '99');
801
+ const overflowStyle = getComputedStyle(chip);
802
+ const overflowWidth = chip.clientWidth + parseInt(overflowStyle.marginInlineStart);
803
+
804
+ chip.setAttribute('count', count);
805
+ chip.setAttribute('hidden', '');
806
+ chip.style.visibility = '';
807
+
808
+ return overflowWidth;
809
+ }
810
+
811
+ /** @private */
812
+ async __updateChips() {
813
+ if (!this._inputField || !this.inputElement) {
814
+ return;
815
+ }
816
+
817
+ if (!this._inputField.$) {
818
+ await this._inputField.updateComplete;
819
+ }
820
+
821
+ // Clear all chips except the overflow
822
+ this._chips.forEach((chip) => {
823
+ chip.remove();
824
+ });
825
+
826
+ const items = [...this.selectedItems];
827
+
828
+ // Detect available remaining width for chips
829
+ const totalWidth = this._inputField.$.wrapper.clientWidth;
830
+ const inputWidth = parseInt(getComputedStyle(this.inputElement).flexBasis);
831
+
832
+ let remainingWidth = totalWidth - inputWidth;
833
+
834
+ if (items.length > 1) {
835
+ remainingWidth -= this.__getOverflowWidth();
836
+ }
837
+
838
+ const chipMinWidth = parseInt(getComputedStyle(this).getPropertyValue('--_chip-min-width'));
839
+
840
+ if (this.autoExpandHorizontally) {
841
+ const chips = [];
842
+
843
+ // First, add all chips to make the field fully expand
844
+ for (let i = items.length - 1, refNode = null; i >= 0; i--) {
845
+ const chip = this.__createChip(items[i]);
846
+ this.insertBefore(chip, refNode);
847
+ // Render Lit based chip
848
+ if (chip.performUpdate) {
849
+ chip.performUpdate();
850
+ }
851
+ refNode = chip;
852
+ chips.unshift(chip);
853
+ }
854
+
855
+ const overflowItems = [];
856
+ const availableWidth = this._inputField.$.wrapper.clientWidth - this.$.chips.clientWidth;
857
+
858
+ // When auto expanding vertically, no need to measure width
859
+ if (!this.autoExpandVertically && availableWidth < inputWidth) {
860
+ // Always show at least last item as a chip
861
+ while (chips.length > 1) {
862
+ const lastChip = chips.pop();
863
+ lastChip.remove();
864
+ overflowItems.unshift(items.pop());
865
+
866
+ // Remove chips until there is enough width for the input element to fit
867
+ const neededWidth = overflowItems.length > 0 ? inputWidth + this.__getOverflowWidth() : inputWidth;
868
+ if (this._inputField.$.wrapper.clientWidth - this.$.chips.clientWidth >= neededWidth) {
869
+ break;
870
+ }
871
+ }
872
+
873
+ if (chips.length === 1) {
874
+ chips[0].style.maxWidth = `${Math.max(chipMinWidth, remainingWidth)}px`;
875
+ }
876
+ }
877
+
878
+ this._overflowItems = overflowItems;
879
+ return;
880
+ }
881
+
882
+ // Add chips until remaining width is exceeded
883
+ for (let i = items.length - 1, refNode = null; i >= 0; i--) {
884
+ const chip = this.__createChip(items[i]);
885
+ this.insertBefore(chip, refNode);
886
+ // Render Lit based chip
887
+ if (chip.performUpdate) {
888
+ chip.performUpdate();
889
+ }
890
+
891
+ // When auto expanding vertically, no need to measure remaining width
892
+ if (!this.autoExpandVertically && this.$.chips.clientWidth > remainingWidth) {
893
+ // Always show at least last selected item as a chip
894
+ if (refNode === null) {
895
+ chip.style.maxWidth = `${Math.max(chipMinWidth, remainingWidth)}px`;
896
+ } else {
897
+ chip.remove();
898
+ break;
899
+ }
900
+ }
901
+
902
+ items.pop();
903
+ refNode = chip;
904
+ }
905
+
906
+ this._overflowItems = items;
907
+ }
908
+
909
+ /** @private */
910
+ __updateOverflowChip(overflow, items, disabled, readonly) {
911
+ if (overflow) {
912
+ const count = items.length;
913
+
914
+ overflow.label = `${count}`;
915
+ overflow.setAttribute('count', `${count}`);
916
+ overflow.setAttribute('title', this._mergeItemLabels(items));
917
+ overflow.toggleAttribute('hidden', count === 0);
918
+
919
+ overflow.disabled = disabled;
920
+ overflow.readonly = readonly;
921
+ }
922
+ }
923
+
924
+ /** @private */
925
+ _onClearButtonTouchend(event) {
926
+ // Cancel the following click and focus events
927
+ event.preventDefault();
928
+ // Prevent default combo box behavior which can otherwise unnecessarily
929
+ // clear the input and filter
930
+ event.stopPropagation();
931
+
932
+ this.clear();
933
+ }
934
+
935
+ /**
936
+ * Override method inherited from `InputControlMixin` and clear items.
937
+ * @protected
938
+ * @override
939
+ */
940
+ _onClearButtonClick(event) {
941
+ event.stopPropagation();
942
+
943
+ this.clear();
944
+ }
945
+
946
+ /**
947
+ * Override an event listener from `InputControlMixin` to
948
+ * stop the change event re-targeted from the input.
949
+ *
950
+ * @param {!Event} event
951
+ * @protected
952
+ * @override
953
+ */
954
+ _onChange(event) {
955
+ event.stopPropagation();
956
+ }
957
+
958
+ /**
959
+ * Override an event listener from `KeyboardMixin`.
960
+ * Do not call `super` in order to override clear
961
+ * button logic defined in `InputControlMixin`.
962
+ *
963
+ * @param {!KeyboardEvent} event
964
+ * @protected
965
+ * @override
966
+ */
967
+ _onEscape(event) {
968
+ if (this.clearButtonVisible && this.selectedItems && this.selectedItems.length) {
969
+ event.stopPropagation();
970
+ this.selectedItems = [];
971
+ }
972
+ }
973
+
974
+ /**
975
+ * Override an event listener from `KeyboardMixin`.
976
+ * @param {KeyboardEvent} event
977
+ * @protected
978
+ * @override
979
+ */
980
+ _onKeyDown(event) {
981
+ super._onKeyDown(event);
982
+
983
+ const chips = this._chips;
984
+
985
+ if (!this.readonly && chips.length > 0) {
986
+ switch (event.key) {
987
+ case 'Backspace':
988
+ this._onBackSpace(chips);
989
+ break;
990
+ case 'ArrowLeft':
991
+ this._onArrowLeft(chips, event);
992
+ break;
993
+ case 'ArrowRight':
994
+ this._onArrowRight(chips, event);
995
+ break;
996
+ default:
997
+ this._focusedChipIndex = -1;
998
+ break;
999
+ }
1000
+ }
1001
+ }
1002
+
1003
+ /** @private */
1004
+ _onArrowLeft(chips, event) {
1005
+ if (this.inputElement.selectionStart !== 0) {
1006
+ return;
1007
+ }
1008
+
1009
+ const idx = this._focusedChipIndex;
1010
+ if (idx !== -1) {
1011
+ event.preventDefault();
1012
+ }
1013
+ let newIdx;
1014
+
1015
+ if (!this.__isRTL) {
1016
+ if (idx === -1) {
1017
+ // Focus last chip
1018
+ newIdx = chips.length - 1;
1019
+ } else if (idx > 0) {
1020
+ // Focus prev chip
1021
+ newIdx = idx - 1;
1022
+ }
1023
+ } else if (idx === chips.length - 1) {
1024
+ // Blur last chip
1025
+ newIdx = -1;
1026
+ } else if (idx > -1) {
1027
+ // Focus next chip
1028
+ newIdx = idx + 1;
1029
+ }
1030
+
1031
+ if (newIdx !== undefined) {
1032
+ this._focusedChipIndex = newIdx;
1033
+ }
1034
+ }
1035
+
1036
+ /** @private */
1037
+ _onArrowRight(chips, event) {
1038
+ if (this.inputElement.selectionStart !== 0) {
1039
+ return;
1040
+ }
1041
+
1042
+ const idx = this._focusedChipIndex;
1043
+ if (idx !== -1) {
1044
+ event.preventDefault();
1045
+ }
1046
+ let newIdx;
1047
+
1048
+ if (this.__isRTL) {
1049
+ if (idx === -1) {
1050
+ // Focus last chip
1051
+ newIdx = chips.length - 1;
1052
+ } else if (idx > 0) {
1053
+ // Focus prev chip
1054
+ newIdx = idx - 1;
1055
+ }
1056
+ } else if (idx === chips.length - 1) {
1057
+ // Blur last chip
1058
+ newIdx = -1;
1059
+ } else if (idx > -1) {
1060
+ // Focus next chip
1061
+ newIdx = idx + 1;
1062
+ }
1063
+
1064
+ if (newIdx !== undefined) {
1065
+ this._focusedChipIndex = newIdx;
1066
+ }
1067
+ }
1068
+
1069
+ /** @private */
1070
+ _onBackSpace(chips) {
1071
+ if (this.inputElement.selectionStart !== 0) {
1072
+ return;
1073
+ }
1074
+
1075
+ const idx = this._focusedChipIndex;
1076
+ if (idx === -1) {
1077
+ this._focusedChipIndex = chips.length - 1;
1078
+ } else {
1079
+ this.__removeItem(chips[idx].item);
1080
+ this._focusedChipIndex = -1;
1081
+ }
1082
+ }
1083
+
1084
+ /** @private */
1085
+ _focusedChipIndexChanged(focusedIndex, oldFocusedIndex) {
1086
+ if (focusedIndex > -1 || oldFocusedIndex > -1) {
1087
+ const chips = this._chips;
1088
+ chips.forEach((chip, index) => {
1089
+ chip.toggleAttribute('focused', index === focusedIndex);
1090
+ });
1091
+
1092
+ // Announce focused chip
1093
+ if (focusedIndex > -1) {
1094
+ const item = chips[focusedIndex].item;
1095
+ const itemLabel = this._getItemLabel(item);
1096
+ announce(`${itemLabel} ${this.i18n.focused}`);
1097
+ }
1098
+ }
1099
+ }
1100
+
1101
+ /** @private */
1102
+ _onComboBoxChange() {
1103
+ const item = this.$.comboBox.selectedItem;
1104
+ if (item) {
1105
+ this.__selectItem(item);
1106
+ }
1107
+ }
1108
+
1109
+ /** @private */
1110
+ _onComboBoxItemSelected(event) {
1111
+ this.__selectItem(event.detail.item);
1112
+ }
1113
+
1114
+ /** @private */
1115
+ _onCustomValueSet(event) {
1116
+ // Do not set combo-box value
1117
+ event.preventDefault();
1118
+
1119
+ // Stop the original event
1120
+ event.stopPropagation();
1121
+
1122
+ this.__clearInternalValue(true);
1123
+
1124
+ this.dispatchEvent(
1125
+ new CustomEvent('custom-value-set', {
1126
+ detail: event.detail,
1127
+ composed: true,
1128
+ bubbles: true,
1129
+ }),
1130
+ );
1131
+ }
1132
+
1133
+ /** @private */
1134
+ _onItemRemoved(event) {
1135
+ this.__removeItem(event.detail.item);
1136
+ }
1137
+
1138
+ /** @private */
1139
+ _preventBlur(event) {
1140
+ // Prevent mousedown event to keep the input focused
1141
+ // and keep the overlay opened when clicking a chip.
1142
+ event.preventDefault();
1143
+ }
1144
+
1145
+ /**
1146
+ * Fired when the user sets a custom value.
1147
+ * @event custom-value-set
1148
+ * @param {string} detail the custom value
1149
+ */
1150
+ };