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 24b028b..fa0d200 100644 --- a/src/scripts/choices.js +++ b/src/scripts/choices.js @@ -411,13 +411,13 @@ class Choices { } setChoices(choices = [], value = '', label = '', replaceChoices = false) { - if (!this._isSelectElement || !choices.length || !value) { + if (!this._isSelectElement || !value) { return this; } // Clear choices if needed if (replaceChoices) { - this._clearChoices(); + this.clearChoices(); } this.containerOuter.removeLoadingState(); @@ -448,6 +448,10 @@ class Choices { return this; } + clearChoices() { + this._store.dispatch(clearChoices()); + } + clearStore() { this._store.dispatch(clearAll()); return this; @@ -1729,10 +1733,6 @@ class Choices { } } - _clearChoices() { - this._store.dispatch(clearChoices()); - } - _addGroup({ group, id, valueKey = 'value', labelKey = 'label' }) { const groupChoices = isType('Object', group) ? group.choices diff --git a/src/scripts/choices.test.js b/src/scripts/choices.test.js index 12c21fa..c805e79 100644 --- a/src/scripts/choices.test.js +++ b/src/scripts/choices.test.js @@ -1339,14 +1339,14 @@ describe('choices', () => { addChoiceStub = stub(); containerOuterRemoveLoadingStateStub = stub(); - instance._clearChoices = clearChoicesStub; + instance.clearChoices = clearChoicesStub; instance._addGroup = addGroupStub; instance._addChoice = addChoiceStub; instance.containerOuter.removeLoadingState = containerOuterRemoveLoadingStateStub; }); afterEach(() => { - instance._clearChoices.reset(); + instance.clearChoices.reset(); instance._addGroup.reset(); instance._addChoice.reset(); instance.containerOuter.removeLoadingState.reset(); @@ -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);