Skip to content

Commit

Permalink
duplicateItemsAllowed option is now respected by setChoices() met…
Browse files Browse the repository at this point in the history
…hod. Resolves #855
  • Loading branch information
Xon committed Sep 4, 2024
1 parent a941e55 commit 0864f2d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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
10 changes: 9 additions & 1 deletion src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2078,12 +2078,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

0 comments on commit 0864f2d

Please sign in to comment.