Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicateItemsAllowed option is now respected by setChoices() method #1198

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features (from 11.0.0)
* Pass `getClassNames` as the 3rd argument to `callbackOnCreateTemplates` callback
* `duplicateItemsAllowed` option is now respected by `setChoices()` method [#855](https://github.com/Choices-js/Choices/issues/855)

### Bug Fixes (from 11.0.0)
* Fix choice disable state wasn't considered when showing the "no choices to choose from" notice
Expand Down
17 changes: 13 additions & 4 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,13 +853,14 @@ class Choices {
return this;
}

clearStore(): this {
clearStore(clearOptions: boolean = true): this {
this._stopSearch();

this.passedElement.element.replaceChildren('');
if (clearOptions) {
this.passedElement.element.replaceChildren('');
}
this.itemList.element.replaceChildren('');
this.choiceList.element.replaceChildren('');
this._stopSearch();
this._store.reset();
this._lastAddedChoiceId = 0;
this._lastAddedGroupId = 0;
Expand Down Expand Up @@ -2078,12 +2079,20 @@ class Choices {
throw new TypeError('Can not re-add a choice which has already been added');
}

const { config } = this;
if (
(this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find((c) => config.valueComparer(c.value, choice.value))
) {
return;
}

// Generate unique id, in-place update is required so chaining _addItem works as expected
this._lastAddedChoiceId++;
choice.id = this._lastAddedChoiceId;
choice.elementId = `${this._baseId}-${this._idNames.itemChoice}-${choice.id}`;

const { prependValue, appendValue } = this.config;
const { prependValue, appendValue } = config;
if (prependValue) {
choice.value = prependValue + choice.value;
}
Expand Down
Loading