Skip to content

Commit e50fa98

Browse files
ticktackkXon
authored andcommitted
Fix: Unselecting option after reaching maximum item limit does not remove the notice Choices-js#1249
1 parent 897d309 commit e50fa98

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

public/test/select-multiple/index.html

+25
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,31 @@ <h2>Select multiple inputs</h2>
335335
</script>
336336
</div>
337337

338+
<div data-test-hook="selection-limit-note-after-unselecting-choice">
339+
<label for="choices-selection-limit-note-after-unselecting-choice">Input limit note after unselecting choice</label>
340+
<select
341+
class="form-control"
342+
name="choices-selection-limit-note-after-unselecting-choice"
343+
id="choices-selection-limit-note-after-unselecting-choice"
344+
multiple
345+
>
346+
<option value="Choice 1">Choice 1</option>
347+
<option value="Choice 2">Choice 2</option>
348+
<option value="Choice 3">Choice 3</option>
349+
<option value="Choice 4">Choice 4</option>
350+
<option value="Choice 5">Choice 5</option>
351+
</select>
352+
<script>
353+
document.addEventListener('DOMContentLoaded', function() {
354+
new Choices('#choices-selection-limit-note-after-unselecting-choice', {
355+
allowHTML: true,
356+
maxItemCount: 5,
357+
removeItemButton: true
358+
});
359+
});
360+
</script>
361+
</div>
362+
338363
<div data-test-hook="prepend-append">
339364
<label for="choices-prepend-append">Prepend/append</label>
340365
<select

src/scripts/choices.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,10 @@ class Choices {
14041404
return false;
14051405
}
14061406

1407+
if (this._notice && this._notice.type === NoticeTypes.addChoice) {
1408+
this._clearNotice();
1409+
}
1410+
14071411
return true;
14081412
}
14091413

test-e2e/test-suit.ts

+9
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ export class TestSuit {
165165
await expect(this.dropdown).toBeHidden();
166166
}
167167

168+
async expectHiddenNotice(singleItem: boolean = false): Promise<void> {
169+
await this.advanceClock();
170+
171+
if (singleItem) {
172+
await expect(this.dropdown.locator('> *:not(input)')).toHaveCount(0);
173+
}
174+
await expect(this.dropdown.locator('.choices__notice')).toBeHidden();
175+
}
176+
168177
// eslint-disable-next-line class-methods-use-this
169178
getWrappedElement(): Locator {
170179
throw new Error('Not implemented');

test-e2e/tests/select-multiple.spec.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,24 @@ describe(`Choices - select multiple`, () => {
478478
});
479479

480480
describe('selection limit', () => {
481-
const testId = 'selection-limit';
481+
const displaysTestId = 'selection-limit';
482482
const selectionLimit = 5;
483483

484484
test('displays "limit reached" prompt', async ({ page, bundle }) => {
485-
const suite = new SelectTestSuit(page, bundle, testUrl, testId);
485+
const suite = new SelectTestSuit(page, bundle, testUrl, displaysTestId);
486+
await suite.startWithClick();
487+
488+
for (let index = 0; index < selectionLimit; index++) {
489+
await suite.selectableChoices.first().click();
490+
await suite.advanceClock();
491+
}
492+
493+
await suite.expectVisibleNoticeHtml(`Only ${selectionLimit} values can be added`);
494+
});
495+
496+
const hidesTestId = 'selection-limit-note-after-unselecting-choice';
497+
test('hides "limit reached" prompt', async ({ page, bundle }) => {
498+
const suite = new SelectTestSuit(page, bundle, testUrl, hidesTestId);
486499
await suite.startWithClick();
487500

488501
for (let index = 0; index < selectionLimit; index++) {
@@ -491,6 +504,11 @@ describe(`Choices - select multiple`, () => {
491504
}
492505

493506
await suite.expectVisibleNoticeHtml(`Only ${selectionLimit} values can be added`);
507+
508+
await suite.items.getByRole('button', { name: 'Remove item' }).last().click();
509+
await suite.advanceClock();
510+
511+
await suite.expectHiddenNotice();
494512
});
495513
});
496514

0 commit comments

Comments
 (0)