Allow passing an empty array to setChoices

This commit is contained in:
Hans Lemuet 2019-03-13 10:34:06 +01:00
parent fc9bca2ece
commit 5c17250e20
No known key found for this signature in database
GPG key ID: 6EABF1FFDDFA8032
3 changed files with 23 additions and 11 deletions

View file

@ -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`

View file

@ -411,7 +411,7 @@ class Choices {
}
setChoices(choices = [], value = '', label = '', replaceChoices = false) {
if (!this._isSelectElement || !choices.length || !value) {
if (!this._isSelectElement || !value) {
return this;
}

View file

@ -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);