Skip to content

Commit 863b97c

Browse files
committed
refactor: update chips measuring logic when autoExpandHorizontally
1 parent 9d7f905 commit 863b97c

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

packages/multi-select-combo-box/src/vaadin-multi-select-combo-box.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -969,10 +969,42 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
969969

970970
const chipMinWidth = parseInt(getComputedStyle(this).getPropertyValue('--_chip-min-width'));
971971

972-
// Detect if the field can expand
973972
if (this.autoExpandHorizontally) {
974-
const extraWidth = this.__getExtraWidth();
975-
remainingWidth += Math.max(0, extraWidth);
973+
const chips = [];
974+
975+
// First, add all chips to make the field fully expand
976+
for (let i = items.length - 1, refNode = null; i >= 0; i--) {
977+
const chip = this.__createChip(items[i]);
978+
this.insertBefore(chip, refNode);
979+
refNode = chip;
980+
chips.unshift(chip);
981+
}
982+
983+
const overflowItems = [];
984+
const availableWidth = this._inputField.$.wrapper.clientWidth - this.$.chips.clientWidth;
985+
986+
// When auto expanding vertically, no need to measure width
987+
if (!this.autoExpandVertically && availableWidth < inputWidth) {
988+
// Always show at least last item as a chip
989+
while (chips.length > 1) {
990+
const lastChip = chips.pop();
991+
lastChip.remove();
992+
overflowItems.unshift(items.pop());
993+
994+
// Remove chips until there is enough width for the input element to fit
995+
const neededWidth = overflowItems.length > 0 ? inputWidth + this.__getOverflowWidth() : inputWidth;
996+
if (this._inputField.$.wrapper.clientWidth - this.$.chips.clientWidth >= neededWidth) {
997+
break;
998+
}
999+
}
1000+
1001+
if (chips.length === 1) {
1002+
chips[0].style.maxWidth = `${Math.max(chipMinWidth, remainingWidth)}px`;
1003+
}
1004+
}
1005+
1006+
this._overflowItems = overflowItems;
1007+
return;
9761008
}
9771009

9781010
// Add chips until remaining width is exceeded
@@ -998,27 +1030,6 @@ class MultiSelectComboBox extends ResizeMixin(InputControlMixin(ThemableMixin(El
9981030
this._overflowItems = items;
9991031
}
10001032

1001-
/** @private */
1002-
__getExtraWidth() {
1003-
// Check if max-width is set on the host
1004-
const maxWidth = parseInt(getComputedStyle(this).maxWidth);
1005-
if (!isNaN(maxWidth)) {
1006-
return maxWidth - this.clientWidth;
1007-
}
1008-
1009-
// Check if width is set using style attribute
1010-
const width = parseInt(this.style.width);
1011-
if (!isNaN(width)) {
1012-
return width - this.clientWidth;
1013-
}
1014-
1015-
// Check if parent element allows to expand
1016-
const parent = this.parentNode;
1017-
const host = parent instanceof ShadowRoot ? parent.host : parent;
1018-
1019-
return host.clientWidth - this.clientWidth;
1020-
}
1021-
10221033
/** @private */
10231034
__updateOverflowChip(overflow, items, disabled, readonly) {
10241035
if (overflow) {

0 commit comments

Comments
 (0)