Clear items by default instead

This commit is contained in:
Josh Johnson 2019-11-28 21:37:55 +00:00
parent 35cabbe985
commit c64a7ca04c
4 changed files with 24 additions and 29 deletions

View file

@ -880,7 +880,7 @@ choices.disable();
**Usage:** Hide option list dropdown (only affects select inputs). **Usage:** Hide option list dropdown (only affects select inputs).
### setChoices(choices[, value[, label[, replaceChoices[, replaceItems]]]]); ### setChoices(choices[, value[, label[, replaceChoices]]]);
**Input types affected:** `select-one`, `select-multiple` **Input types affected:** `select-one`, `select-multiple`
@ -902,7 +902,6 @@ example.setChoices(
'value', 'value',
'label', 'label',
false, false,
true,
); );
``` ```
@ -960,7 +959,6 @@ example.setChoices(
'value', 'value',
'label', 'label',
false, false,
true,
); );
``` ```

View file

@ -502,7 +502,6 @@ class Choices {
* @param {string} [value = 'value'] - name of `value` field * @param {string} [value = 'value'] - name of `value` field
* @param {string} [label = 'label'] - name of 'label' field * @param {string} [label = 'label'] - name of 'label' field
* @param {boolean} [replaceChoices = false] - whether to clear existing choices * @param {boolean} [replaceChoices = false] - whether to clear existing choices
* @param {boolean} [replaceItems = false] - whether to clear existing items
* @returns {this | Promise<this>} * @returns {this | Promise<this>}
* *
* @example * @example
@ -564,7 +563,6 @@ class Choices {
value = 'value', value = 'value',
label = 'label', label = 'label',
replaceChoices = false, replaceChoices = false,
replaceItems = false,
) { ) {
if (!this.initialised) { if (!this.initialised) {
throw new ReferenceError( throw new ReferenceError(
@ -583,9 +581,6 @@ class Choices {
if (replaceChoices) { if (replaceChoices) {
this.clearChoices(); this.clearChoices();
}
if (replaceItems) {
this.clearItems(); this.clearItems();
} }
@ -599,9 +594,7 @@ class Choices {
return new Promise(resolve => requestAnimationFrame(resolve)) return new Promise(resolve => requestAnimationFrame(resolve))
.then(() => this._handleLoadingState(true)) .then(() => this._handleLoadingState(true))
.then(() => fetcher) .then(() => fetcher)
.then(data => .then(data => this.setChoices(data, value, label, replaceChoices))
this.setChoices(data, value, label, replaceChoices, replaceItems),
)
.catch(err => { .catch(err => {
if (!this.config.silent) { if (!this.config.silent) {
console.error(err); console.error(err);

View file

@ -1716,37 +1716,43 @@ describe('choices', () => {
}); });
describe('passing an empty array with a false replaceChoices flag', () => { describe('passing an empty array with a false replaceChoices flag', () => {
it('does not clear existing choices', () => { beforeEach(() => {
instance._isSelectElement = true;
instance.setChoices([], value, label, false); instance.setChoices([], value, label, false);
});
it('does not clear existing choices', () => {
expect(clearChoicesStub.called).to.equal(false); expect(clearChoicesStub.called).to.equal(false);
}); });
it('does not clear existing items', () => {
expect(clearItemsStub.called).to.equal(false);
});
}); });
describe('passing true replaceChoices flag', () => { describe('passing true replaceChoices flag', () => {
it('clears existing choices', () => { beforeEach(() => {
instance.setChoices(choices, value, label, true); instance.setChoices(choices, value, label, true);
});
it('clears existing choices', () => {
expect(clearChoicesStub.called).to.equal(true); expect(clearChoicesStub.called).to.equal(true);
}); });
});
describe('passing false replaceChoices flag', () => {
it('clears existing choices are not cleared', () => {
instance.setChoices(choices, value, label, false);
expect(clearChoicesStub.called).to.equal(false);
});
});
describe('passing true replaceItems flag', () => {
it('clears existing items', () => { it('clears existing items', () => {
instance.setChoices(choices, value, label, true, true);
expect(clearItemsStub.called).to.equal(true); expect(clearItemsStub.called).to.equal(true);
}); });
}); });
describe('passing false replaceItems flag', () => { describe('passing false replaceChoices flag', () => {
it('does not clears existing items', () => { beforeEach(() => {
instance.setChoices(choices, value, label, true, false); instance.setChoices(choices, value, label, false);
});
it('does not clear existing choices', () => {
expect(clearChoicesStub.called).to.equal(false);
});
it('does not clear existing items', () => {
expect(clearItemsStub.called).to.equal(false); expect(clearItemsStub.called).to.equal(false);
}); });
}); });

2
types/index.d.ts vendored
View file

@ -934,7 +934,6 @@ export default class Choices {
* @param {string} [value = 'value'] - name of `value` field * @param {string} [value = 'value'] - name of `value` field
* @param {string} [label = 'label'] - name of 'label' field * @param {string} [label = 'label'] - name of 'label' field
* @param {boolean} [replaceChoices = false] - whether to clear existing choices * @param {boolean} [replaceChoices = false] - whether to clear existing choices
* @param {boolean} [replaceItems = false] - whether to clear existing items
* *
* @example * @example
* ```js * ```js
@ -997,7 +996,6 @@ export default class Choices {
value?: string, value?: string,
label?: string, label?: string,
replaceChoices?: boolean, replaceChoices?: boolean,
replaceItems?: boolean,
): T extends object[] ? this : Promise<this>; ): T extends object[] ? this : Promise<this>;
/** /**