From 5c17250e20f93a2e87367c805d754b91b95afa20 Mon Sep 17 00:00:00 2001 From: Hans Lemuet Date: Wed, 13 Mar 2019 10:34:06 +0100 Subject: [PATCH] Allow passing an empty array to setChoices --- README.md | 7 ++++++- src/scripts/choices.js | 2 +- src/scripts/choices.test.js | 25 ++++++++++++++++--------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5fd0edb..696e0ae 100644 --- a/README.md +++ b/README.md @@ -747,7 +747,7 @@ choices.disable(); ### setChoices(choices, value, label, replaceChoices); **Input types affected:** `select-one`, `select-multiple` -**Usage:** Set choices of select input via an array of objects, a value name and a label name. This behaves the same as passing items via the `choices` option but can be called after initialising Choices. This can also be used to add groups of choices (see example 2); Optionally pass a true `replaceChoices` value to remove any existing choices. Optionally pass a `customProperties` object to add additional data to your choices (useful when searching/filtering etc). +**Usage:** Set choices of select input via an array of objects, a value name and a label name. This behaves the same as passing items via the `choices` option but can be called after initialising Choices. This can also be used to add groups of choices (see example 2); Optionally pass a true `replaceChoices` value to remove any existing choices. Optionally pass a `customProperties` object to add additional data to your choices (useful when searching/filtering etc). Passing an empty array as the first parameter, and a true `replaceChoices` is the same as calling `clearChoices` (see below). **Example 1:** @@ -791,6 +791,11 @@ example.setChoices([{ }], 'value', 'label', false); ``` +### clearChoices(); +**Input types affected:** `select-one`, `select-multiple` + +**Usage:** Clear all choices from select + ### getValue(valueOnly) **Input types affected:** `text`, `select-one`, `select-multiple` diff --git a/src/scripts/choices.js b/src/scripts/choices.js index 503810c..fa0d200 100644 --- a/src/scripts/choices.js +++ b/src/scripts/choices.js @@ -411,7 +411,7 @@ class Choices { } setChoices(choices = [], value = '', label = '', replaceChoices = false) { - if (!this._isSelectElement || !choices.length || !value) { + if (!this._isSelectElement || !value) { return this; } diff --git a/src/scripts/choices.test.js b/src/scripts/choices.test.js index bc7943b..c805e79 100644 --- a/src/scripts/choices.test.js +++ b/src/scripts/choices.test.js @@ -1370,15 +1370,6 @@ describe('choices', () => { }); describe('passing invalid arguments', () => { - describe('passing an empty array', () => { - beforeEach(() => { - instance._isSelectElement = true; - instance.setChoices([], value, label, false); - }); - - returnsEarly(); - }); - describe('passing no value', () => { beforeEach(() => { instance._isSelectElement = true; @@ -1429,6 +1420,22 @@ describe('choices', () => { }); }); + describe('passing an empty array with a true replaceChoices flag', () => { + it('choices are cleared', () => { + instance._isSelectElement = true; + instance.setChoices([], value, label, true); + expect(clearChoicesStub.called).to.equal(true); + }); + }); + + describe('passing an empty array with a false replaceChoices flag', () => { + it('choices stay the same', () => { + instance._isSelectElement = true; + instance.setChoices([], value, label, false); + expect(clearChoicesStub.called).to.equal(false); + }); + }); + describe('passing true replaceChoices flag', () => { it('choices are cleared', () => { instance.setChoices(choices, value, label, true);