Merge branch 'Spone-improve-clear-choices'

This commit is contained in:
Josh Johnson 2019-03-29 08:56:05 +00:00
commit fee575d6d9
3 changed files with 30 additions and 18 deletions

View file

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

View file

@ -411,13 +411,13 @@ 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;
} }
// Clear choices if needed // Clear choices if needed
if (replaceChoices) { if (replaceChoices) {
this._clearChoices(); this.clearChoices();
} }
this.containerOuter.removeLoadingState(); this.containerOuter.removeLoadingState();
@ -448,6 +448,10 @@ class Choices {
return this; return this;
} }
clearChoices() {
this._store.dispatch(clearChoices());
}
clearStore() { clearStore() {
this._store.dispatch(clearAll()); this._store.dispatch(clearAll());
return this; return this;
@ -1729,10 +1733,6 @@ class Choices {
} }
} }
_clearChoices() {
this._store.dispatch(clearChoices());
}
_addGroup({ group, id, valueKey = 'value', labelKey = 'label' }) { _addGroup({ group, id, valueKey = 'value', labelKey = 'label' }) {
const groupChoices = isType('Object', group) const groupChoices = isType('Object', group)
? group.choices ? group.choices

View file

@ -1339,14 +1339,14 @@ describe('choices', () => {
addChoiceStub = stub(); addChoiceStub = stub();
containerOuterRemoveLoadingStateStub = stub(); containerOuterRemoveLoadingStateStub = stub();
instance._clearChoices = clearChoicesStub; instance.clearChoices = clearChoicesStub;
instance._addGroup = addGroupStub; instance._addGroup = addGroupStub;
instance._addChoice = addChoiceStub; instance._addChoice = addChoiceStub;
instance.containerOuter.removeLoadingState = containerOuterRemoveLoadingStateStub; instance.containerOuter.removeLoadingState = containerOuterRemoveLoadingStateStub;
}); });
afterEach(() => { afterEach(() => {
instance._clearChoices.reset(); instance.clearChoices.reset();
instance._addGroup.reset(); instance._addGroup.reset();
instance._addChoice.reset(); instance._addChoice.reset();
instance.containerOuter.removeLoadingState.reset(); instance.containerOuter.removeLoadingState.reset();
@ -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);