mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-17 05:05:08 +02:00
Allow passing an empty array to setChoices
This commit is contained in:
parent
fc9bca2ece
commit
5c17250e20
|
@ -747,7 +747,7 @@ choices.disable();
|
||||||
### setChoices(choices, value, label, replaceChoices);
|
### setChoices(choices, value, label, replaceChoices);
|
||||||
**Input types affected:** `select-one`, `select-multiple`
|
**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:**
|
**Example 1:**
|
||||||
|
|
||||||
|
@ -791,6 +791,11 @@ example.setChoices([{
|
||||||
}], 'value', 'label', false);
|
}], 'value', 'label', false);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### clearChoices();
|
||||||
|
**Input types affected:** `select-one`, `select-multiple`
|
||||||
|
|
||||||
|
**Usage:** Clear all choices from select
|
||||||
|
|
||||||
### getValue(valueOnly)
|
### getValue(valueOnly)
|
||||||
**Input types affected:** `text`, `select-one`, `select-multiple`
|
**Input types affected:** `text`, `select-one`, `select-multiple`
|
||||||
|
|
||||||
|
|
|
@ -411,7 +411,7 @@ class Choices {
|
||||||
}
|
}
|
||||||
|
|
||||||
setChoices(choices = [], value = '', label = '', replaceChoices = false) {
|
setChoices(choices = [], value = '', label = '', replaceChoices = false) {
|
||||||
if (!this._isSelectElement || !choices.length || !value) {
|
if (!this._isSelectElement || !value) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1370,15 +1370,6 @@ describe('choices', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('passing invalid arguments', () => {
|
describe('passing invalid arguments', () => {
|
||||||
describe('passing an empty array', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
instance._isSelectElement = true;
|
|
||||||
instance.setChoices([], value, label, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
returnsEarly();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('passing no value', () => {
|
describe('passing no value', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
instance._isSelectElement = true;
|
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', () => {
|
describe('passing true replaceChoices flag', () => {
|
||||||
it('choices are cleared', () => {
|
it('choices are cleared', () => {
|
||||||
instance.setChoices(choices, value, label, true);
|
instance.setChoices(choices, value, label, true);
|
||||||
|
|
Loading…
Reference in a new issue