Skip to content

Commit

Permalink
Merge pull request #1271 from Xon/fix-1268
Browse files Browse the repository at this point in the history
Fix v11 regression where `duplicateItemsAllowed` option did not work with `select-one`/`select-multiple`
  • Loading branch information
Xon authored Feb 22, 2025
2 parents 3bd4414 + d3756e8 commit 5544387
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ Pass an array of objects:

**Type:** `Boolean` **Default:** `true`

**Input types affected:** `text`
**Input types affected:** `text`, `select-multiple`, `select-one`

**Usage:** Whether duplicate inputted/chosen items are allowed

Expand Down
13 changes: 5 additions & 8 deletions public/assets/scripts/choices.js
Original file line number Diff line number Diff line change
Expand Up @@ -4415,15 +4415,13 @@
}
if (canAddItem) {
var foundChoice = this._store.choices.find(function (choice) { return config.valueComparer(choice.value, value); });
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
if (foundChoice) {
if (foundChoice) {
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
this._displayNotice('', NoticeTypes.addChoice);
return false;
}
}
else if (this._isTextElement && !config.duplicateItemsAllowed) {
if (foundChoice) {
if (!config.duplicateItemsAllowed) {
canAddItem = false;
notice = resolveNoticeFunction(config.uniqueItemText, value);
}
Expand Down Expand Up @@ -5006,8 +5004,7 @@
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
if (!config.duplicateItemsAllowed && this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.min.js

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions public/assets/scripts/choices.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4409,15 +4409,13 @@ var Choices = /** @class */ (function () {
}
if (canAddItem) {
var foundChoice = this._store.choices.find(function (choice) { return config.valueComparer(choice.value, value); });
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
if (foundChoice) {
if (foundChoice) {
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
this._displayNotice('', NoticeTypes.addChoice);
return false;
}
}
else if (this._isTextElement && !config.duplicateItemsAllowed) {
if (foundChoice) {
if (!config.duplicateItemsAllowed) {
canAddItem = false;
notice = resolveNoticeFunction(config.uniqueItemText, value);
}
Expand Down Expand Up @@ -5000,8 +4998,7 @@ var Choices = /** @class */ (function () {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
if (!config.duplicateItemsAllowed && this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
Expand Down
13 changes: 5 additions & 8 deletions public/assets/scripts/choices.search-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3933,15 +3933,13 @@
}
if (canAddItem) {
var foundChoice = this._store.choices.find(function (choice) { return config.valueComparer(choice.value, value); });
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
if (foundChoice) {
if (foundChoice) {
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
this._displayNotice('', NoticeTypes.addChoice);
return false;
}
}
else if (this._isTextElement && !config.duplicateItemsAllowed) {
if (foundChoice) {
if (!config.duplicateItemsAllowed) {
canAddItem = false;
notice = resolveNoticeFunction(config.uniqueItemText, value);
}
Expand Down Expand Up @@ -4524,8 +4522,7 @@
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
if (!config.duplicateItemsAllowed && this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.search-basic.min.js

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions public/assets/scripts/choices.search-basic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3927,15 +3927,13 @@ var Choices = /** @class */ (function () {
}
if (canAddItem) {
var foundChoice = this._store.choices.find(function (choice) { return config.valueComparer(choice.value, value); });
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
if (foundChoice) {
if (foundChoice) {
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
this._displayNotice('', NoticeTypes.addChoice);
return false;
}
}
else if (this._isTextElement && !config.duplicateItemsAllowed) {
if (foundChoice) {
if (!config.duplicateItemsAllowed) {
canAddItem = false;
notice = resolveNoticeFunction(config.uniqueItemText, value);
}
Expand Down Expand Up @@ -4518,8 +4516,7 @@ var Choices = /** @class */ (function () {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
if (!config.duplicateItemsAllowed && this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
Expand Down
13 changes: 5 additions & 8 deletions public/assets/scripts/choices.search-prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -2775,15 +2775,13 @@
}
if (canAddItem) {
var foundChoice = this._store.choices.find(function (choice) { return config.valueComparer(choice.value, value); });
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
if (foundChoice) {
if (foundChoice) {
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
this._displayNotice('', NoticeTypes.addChoice);
return false;
}
}
else if (this._isTextElement && !config.duplicateItemsAllowed) {
if (foundChoice) {
if (!config.duplicateItemsAllowed) {
canAddItem = false;
notice = resolveNoticeFunction(config.uniqueItemText, value);
}
Expand Down Expand Up @@ -3366,8 +3364,7 @@
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
if (!config.duplicateItemsAllowed && this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
Expand Down
2 changes: 1 addition & 1 deletion public/assets/scripts/choices.search-prefix.min.js

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions public/assets/scripts/choices.search-prefix.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2769,15 +2769,13 @@ var Choices = /** @class */ (function () {
}
if (canAddItem) {
var foundChoice = this._store.choices.find(function (choice) { return config.valueComparer(choice.value, value); });
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
if (foundChoice) {
if (foundChoice) {
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
this._displayNotice('', NoticeTypes.addChoice);
return false;
}
}
else if (this._isTextElement && !config.duplicateItemsAllowed) {
if (foundChoice) {
if (!config.duplicateItemsAllowed) {
canAddItem = false;
notice = resolveNoticeFunction(config.uniqueItemText, value);
}
Expand Down Expand Up @@ -3360,8 +3358,7 @@ var Choices = /** @class */ (function () {
throw new TypeError('Can not re-add a choice which has already been added');
}
var config = this.config;
if ((this._isSelectElement || !config.duplicateItemsAllowed) &&
this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
if (!config.duplicateItemsAllowed && this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
return;
}
// Generate unique id, in-place update is required so chaining _addItem works as expected
Expand Down
36 changes: 36 additions & 0 deletions public/test/select-multiple/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,42 @@ <h2>Select multiple inputs</h2>
</script>
</div>

<div data-test-hook="duplicate-items-allowed">
<label for="duplicate-items-allowed">Duplicate items allowed</label>
<select class="form-control" name="no-choices" id="duplicate-items-allowed" multiple>
<option value="call1">Service 1</option>
<option value="call2">Service 2</option>
<option value="call2">Service 3</option>
<option value="call1">Service 4</option>
<option value="call5">Service 5</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#duplicate-items-allowed', {
duplicateItemsAllowed: true,
});
});
</script>
</div>

<div data-test-hook="duplicate-items-disallowed">
<label for="duplicate-items-disallowed">Duplicate items disallowed</label>
<select class="form-control" name="no-choices" id="duplicate-items-disallowed" multiple>
<option value="call1">Service 1</option>
<option value="call2">Service 2</option>
<option value="call2">Service 3</option>
<option value="call1">Service 4</option>
<option value="call5">Service 5</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#duplicate-items-disallowed', {
duplicateItemsAllowed: false,
});
});
</script>
</div>

<div data-test-hook="no-choices">
<label for="no-choices">No choices</label>
<select class="form-control" name="no-choices" id="no-choices" multiple>
Expand Down
36 changes: 36 additions & 0 deletions public/test/select-one/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,42 @@ <h2>Select one inputs</h2>
</script>
</div>

<div data-test-hook="duplicate-items-allowed">
<label for="duplicate-items-allowed">Duplicate items allowed</label>
<select class="form-control" name="no-choices" id="duplicate-items-allowed">
<option value="call1">Service 1</option>
<option value="call2">Service 2</option>
<option value="call2">Service 3</option>
<option value="call1">Service 4</option>
<option value="call5">Service 5</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#duplicate-items-allowed', {
duplicateItemsAllowed: true,
});
});
</script>
</div>

<div data-test-hook="duplicate-items-disallowed">
<label for="duplicate-items-disallowed">Duplicate items disallowed</label>
<select class="form-control" name="no-choices" id="duplicate-items-disallowed">
<option value="call1">Service 1</option>
<option value="call2">Service 2</option>
<option value="call2">Service 3</option>
<option value="call1">Service 4</option>
<option value="call5">Service 5</option>
</select>
<script>
document.addEventListener('DOMContentLoaded', function() {
new Choices('#duplicate-items-disallowed', {
duplicateItemsAllowed: false,
});
});
</script>
</div>

<div data-test-hook="no-choices">
<label for="no-choices">No choices</label>
<select class="form-control" name="no-choices" id="no-choices">
Expand Down
2 changes: 1 addition & 1 deletion public/types/src/scripts/interfaces/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export interface Options {
/**
* Whether each inputted/chosen item should be unique.
*
* **Input types affected:** text
* **Input types affected:** text, `select-multiple`, `select-one`
*
* @default true
*/
Expand Down
14 changes: 5 additions & 9 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,15 +1423,14 @@ class Choices {

if (canAddItem) {
const foundChoice = this._store.choices.find((choice) => config.valueComparer(choice.value, value));
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
if (foundChoice) {
if (foundChoice) {
if (this._isSelectElement) {
// for exact matches, do not prompt to add it as a custom choice
this._displayNotice('', NoticeTypes.addChoice);

return false;
}
} else if (this._isTextElement && !config.duplicateItemsAllowed) {
if (foundChoice) {
if (!config.duplicateItemsAllowed) {
canAddItem = false;
notice = resolveNoticeFunction(config.uniqueItemText, value);
}
Expand Down Expand Up @@ -2105,10 +2104,7 @@ class Choices {
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/interfaces/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export interface Options {
/**
* Whether each inputted/chosen item should be unique.
*
* **Input types affected:** text
* **Input types affected:** text, `select-multiple`, `select-one`
*
* @default true
*/
Expand Down
15 changes: 15 additions & 0 deletions test-e2e/tests/select-multiple.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,21 @@ describe(`Choices - select multiple`, () => {
});
});

describe('duplicate-items', () => {
test('shows all duplicate items', async ({ page, bundle }) => {
const suite = new SelectTestSuit(page, bundle, testUrl, 'duplicate-items-allowed');
await suite.startWithClick();

await expect(suite.selectableChoices).toHaveCount(5);
});
test('shows unique items', async ({ page, bundle }) => {
const suite = new SelectTestSuit(page, bundle, testUrl, 'duplicate-items-disallowed');
await suite.startWithClick();

await expect(suite.selectableChoices).toHaveCount(3);
});
});

describe('no choices', () => {
const testId = 'no-choices';
test('shows no choices banner', async ({ page, bundle }) => {
Expand Down
15 changes: 15 additions & 0 deletions test-e2e/tests/select-one.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ describe(`Choices - select one`, () => {
});
});

describe('duplicate-items', () => {
test('shows all duplicate items', async ({ page, bundle }) => {
const suite = new SelectTestSuit(page, bundle, testUrl, 'duplicate-items-allowed');
await suite.startWithClick();

await expect(suite.selectableChoices).toHaveCount(5);
});
test('shows unique items', async ({ page, bundle }) => {
const suite = new SelectTestSuit(page, bundle, testUrl, 'duplicate-items-disallowed');
await suite.startWithClick();

await expect(suite.selectableChoices).toHaveCount(3);
});
});

describe('no choices', () => {
const testId = 'no-choices';
test('shows no choices banner', async ({ page, bundle }) => {
Expand Down

0 comments on commit 5544387

Please sign in to comment.