Skip to content

Commit cb1aeb0

Browse files
authored
fix: collapse all chips to overflow if there is not enough width (#9177)
1 parent 5000efb commit cb1aeb0

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -877,14 +877,17 @@ export const MultiSelectComboBoxMixin = (superClass) =>
877877
this.insertBefore(chip, refNode);
878878

879879
// When auto expanding vertically, no need to measure remaining width
880-
if (!this.autoExpandVertically && this.$.chips.clientWidth > remainingWidth) {
881-
// Always show at least last selected item as a chip
882-
if (refNode === null) {
883-
chip.style.maxWidth = `${Math.max(chipMinWidth, remainingWidth)}px`;
884-
} else {
885-
chip.remove();
886-
break;
880+
if (!this.autoExpandVertically) {
881+
if (this.$.chips.clientWidth > remainingWidth) {
882+
// If there is no more space for chips, or if there is at least one
883+
// chip already shown, collapse all remaining chips to the overflow
884+
if (remainingWidth < chipMinWidth || refNode !== null) {
885+
chip.remove();
886+
break;
887+
}
887888
}
889+
890+
chip.style.maxWidth = `${remainingWidth}px`;
888891
}
889892

890893
items.pop();

packages/multi-select-combo-box/test/chips.test.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('chips', () => {
193193
expect(overflow.hasAttribute('hidden')).to.be.true;
194194
});
195195

196-
it('should always show at least one chip in addition to overflow', async () => {
196+
it('should always show at least one chip in addition to overflow if there is enough space', async () => {
197197
comboBox.style.width = 'auto';
198198
await nextResize(comboBox);
199199

@@ -205,9 +205,20 @@ describe('chips', () => {
205205
expect(getChipContent(chips[1])).to.equal('orange');
206206
});
207207

208-
it('should set show max width on the chip based on CSS property', async () => {
208+
it('should move all chips to the overflow if there is not enough space for a single chip', async () => {
209+
comboBox.style.width = '100px';
210+
await nextResize(comboBox);
211+
212+
comboBox.selectedItems = ['apple', 'orange'];
213+
await nextRender();
214+
215+
const chips = getChips(comboBox);
216+
expect(chips.length).to.equal(1);
217+
expect(overflow.hasAttribute('hidden')).to.be.false;
218+
});
219+
220+
it('should set max width on the chip based on the remaining space', async () => {
209221
comboBox.style.width = 'auto';
210-
comboBox.clearButtonVisible = true;
211222
await nextResize(comboBox);
212223

213224
comboBox.selectedItems = ['apple', 'orange'];
@@ -216,7 +227,7 @@ describe('chips', () => {
216227
const chips = getChips(comboBox);
217228
const minWidth = getComputedStyle(comboBox).getPropertyValue('--_chip-min-width');
218229
expect(minWidth).to.be.ok;
219-
expect(chips[1].style.maxWidth).to.equal(minWidth);
230+
expect(parseInt(chips[1].style.maxWidth)).to.be.greaterThan(parseInt(minWidth));
220231
});
221232
});
222233
});

0 commit comments

Comments
 (0)