Skip to content

Commit

Permalink
fix unique dropdown for multi-select select boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
devkral committed Apr 20, 2020
1 parent ac30fd2 commit 72ca731
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
35 changes: 35 additions & 0 deletions cypress/integration/select-multiple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,41 @@ describe('Choices - select multiple', () => {
});
});

describe('unique values only', () => {
describe('unique values', () => {
beforeEach(() => {
cy.get('[data-test-hook=unique-values]')
.find('.choices__input--cloned')
.type('Choice 1')
.type('{enter}');
});

it('only allows me to input unique values', () => {
cy.get('[data-test-hook=unique-values]')
.find('.choices__list--multiple')
.first()
.children()
.should($items => {
expect($items.length).to.equal(1);
});
});

describe('inputting a non-unique value', () => {
it('displays dropdown prompt', () => {
cy.get('[data-test-hook=unique-values]')
.find('.choices__list--dropdown')
.should('be.visible')
.should($dropdown => {
const dropdownText = $dropdown.text().trim();
expect(dropdownText).to.equal(
'Only unique values can be added',
);
});
});
});
});
});

describe('disabled choice', () => {
describe('selecting a disabled choice', () => {
beforeEach(() => {
Expand Down
20 changes: 20 additions & 0 deletions public/test/select-multiple/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ <h2>Select multiple inputs</h2>
</select>
</div>

<div data-test-hook="unique-values">
<label for="choices-unique-values">Unique values</label>
<select
class="form-control"
name="choices-unique-values"
id="choices-unique-values"
multiple
>
<option value="Choice 1" selected>Choice 1</option>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
</select>
</div>

<div data-test-hook="disabled-choice">
<label for="choices-disabled-choice">Disabled choice</label>
<select
Expand Down Expand Up @@ -381,6 +397,10 @@ <h2>Select multiple inputs</h2>
removeItemButton: true,
});

new Choices('#choices-unique-values', {
duplicateItemsAllowed: false,
});

new Choices('#choices-disabled-choice');

new Choices('#choices-add-items', {
Expand Down
7 changes: 3 additions & 4 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1516,13 +1516,12 @@ class Choices {
const { activeItems } = this._store;
const canAddItem = this._canAddItem(activeItems, value);
const { BACK_KEY: backKey, DELETE_KEY: deleteKey } = KEY_CODES;
const canShowDropdownNotice =
this.config.addItems && canAddItem.notice && value;

// We are typing into a text input and have a value, we want to show a dropdown
// notice. Otherwise hide the dropdown
if (this._isTextElement) {
const canShowDropdownNotice =
this.config.addItems && canAddItem.notice && value;

if (canShowDropdownNotice) {
const dropdownItem = this._getTemplate('notice', canAddItem.notice);
this.dropdown.element.innerHTML = dropdownItem.outerHTML;
Expand All @@ -1540,7 +1539,7 @@ class Choices {
if (userHasRemovedValue && canReactivateChoices) {
this._isSearching = false;
this._store.dispatch(activateChoices(true));
} else if (canSearch) {
} else if (canSearch || canShowDropdownNotice) {
this._handleSearch(this.input.value);
}
}
Expand Down

0 comments on commit 72ca731

Please sign in to comment.