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).
### setChoices(choices[, value[, label[, replaceChoices[, replaceItems]]]]);
### setChoices(choices[, value[, label[, replaceChoices]]]);
**Input types affected:** `select-one`, `select-multiple`
@ -902,7 +902,6 @@ example.setChoices(
'value',
'label',
false,
true,
);
```
@ -960,7 +959,6 @@ example.setChoices(
'value',
'label',
false,
true,
);
```

View file

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

View file

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

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