mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-21 23:16:38 +02:00
Add ability to configure the item comparer function.
This commit is contained in:
parent
5048379e68
commit
1e6ae4e3f9
10
README.md
10
README.md
|
@ -99,6 +99,9 @@ Or include Choices directly:
|
|||
maxItemText: (maxItemCount) => {
|
||||
return `Only ${maxItemCount} values can be added.`;
|
||||
},
|
||||
itemComparer: (choice, item) => {
|
||||
return choice === item;
|
||||
},
|
||||
classNames: {
|
||||
containerOuter: 'choices',
|
||||
containerInner: 'choices__inner',
|
||||
|
@ -458,6 +461,13 @@ const example = new Choices(element, {
|
|||
|
||||
**Usage:** The text that is shown when a user has focus on the input but has already reached the [max item count](https://github.com/jshjohnson/Choices#maxitemcount). To access the max item count, pass a function with a `maxItemCount` argument (see the [default config](https://github.com/jshjohnson/Choices#setup) for an example), otherwise pass a string.
|
||||
|
||||
### itemComparer
|
||||
**Type:** `Function` **Default:** `strict equality`
|
||||
|
||||
**Input types affected:** `select-one`, `select-multiple`
|
||||
|
||||
**Usage:** Compare choice and value in appropriate way (e.g. deep equality for objects). To compare choice and value, pass a function with a `itemComparer` argument (see the [default config](https://github.com/jshjohnson/Choices#setup) for an example).
|
||||
|
||||
### classNames
|
||||
**Type:** `Object` **Default:**
|
||||
|
||||
|
|
|
@ -2369,7 +2369,7 @@ class Choices {
|
|||
_findAndSelectChoiceByValue(val) {
|
||||
const choices = this.store.getChoices();
|
||||
// Check 'value' property exists and the choice isn't already selected
|
||||
const foundChoice = choices.find(choice => choice.value === val);
|
||||
const foundChoice = choices.find(choice => this.config.itemComparer(choice.value, val));
|
||||
|
||||
if (foundChoice && !foundChoice.selected) {
|
||||
this._addItem(
|
||||
|
|
|
@ -64,6 +64,7 @@ export const DEFAULT_CONFIG = {
|
|||
uniqueItemText: 'Only unique values can be added.',
|
||||
addItemText: value => `Press Enter to add <b>"${value}"</b>`,
|
||||
maxItemText: maxItemCount => `Only ${maxItemCount} values can be added.`,
|
||||
itemComparer: (choice, item) => (choice === item),
|
||||
fuseOptions: {
|
||||
includeScore: true,
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue