From c64a7ca04ca97c197f01f9c3c029a9fc57dfd7e8 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Thu, 28 Nov 2019 21:37:55 +0000 Subject: [PATCH] Clear items by default instead --- README.md | 4 +--- src/scripts/choices.js | 9 +-------- src/scripts/choices.test.js | 38 +++++++++++++++++++++---------------- types/index.d.ts | 2 -- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index c2c04d4..faa77ff 100644 --- a/README.md +++ b/README.md @@ -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, ); ``` diff --git a/src/scripts/choices.js b/src/scripts/choices.js index e47485d..1f1fec2 100644 --- a/src/scripts/choices.js +++ b/src/scripts/choices.js @@ -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} * * @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); diff --git a/src/scripts/choices.test.js b/src/scripts/choices.test.js index 73c75b4..e472ead 100644 --- a/src/scripts/choices.test.js +++ b/src/scripts/choices.test.js @@ -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); }); }); diff --git a/types/index.d.ts b/types/index.d.ts index 942cb4f..5e0f94e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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; /**