From f30b9764242fc7bafe637c2e36f43dd2d3813b7f Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Sun, 3 Nov 2019 18:12:47 +0000 Subject: [PATCH] Resolve bug 533 (#736) * Add placeholder options to demo page * Use constant types in components * Refactor adding predefined groups/items/choices * Add 'highlighted' flag to Item type * Fix dispatch param type * Build * Add jsdoc comments to utils * Remove unused file * Add default values to js doc comments * Use Redux Action type * Housekeeping * Increase utils coverage * Apply suggestions from code review * Add _getTemplate unit tests * Resolve #533 * Add tests --- src/scripts/choices.js | 36 +++++++++++++++++++----------------- src/scripts/choices.test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/scripts/choices.js b/src/scripts/choices.js index 3ff3314..f81800c 100644 --- a/src/scripts/choices.js +++ b/src/scripts/choices.js @@ -82,19 +82,6 @@ class Choices { { arrayMerge: (_, sourceArray) => [...sourceArray] }, ); - // Convert addItemFilter to function - if ( - userConfig.addItemFilter && - typeof userConfig.addItemFilter !== 'function' - ) { - const re = - userConfig.addItemFilter instanceof RegExp - ? userConfig.addItemFilter - : new RegExp(userConfig.addItemFilter); - - this.config.addItemFilter = re.test.bind(re); - } - const invalidConfigOptions = diff(this.config, DEFAULT_CONFIG); if (invalidConfigOptions.length) { console.warn( @@ -103,10 +90,6 @@ class Choices { ); } - if (!['auto', 'always'].includes(this.config.renderSelectedChoices)) { - this.config.renderSelectedChoices = 'auto'; - } - const passedElement = typeof element === 'string' ? document.querySelector(element) : element; @@ -127,6 +110,25 @@ class Choices { this._isSelectElement = this._isSelectOneElement || this._isSelectMultipleElement; + this.config.searchEnabled = + this._isSelectMultipleElement || this.config.searchEnabled; + + if (!['auto', 'always'].includes(this.config.renderSelectedChoices)) { + this.config.renderSelectedChoices = 'auto'; + } + + if ( + userConfig.addItemFilter && + typeof userConfig.addItemFilter !== 'function' + ) { + const re = + userConfig.addItemFilter instanceof RegExp + ? userConfig.addItemFilter + : new RegExp(userConfig.addItemFilter); + + this.config.addItemFilter = re.test.bind(re); + } + if (this._isTextElement) { this.passedElement = new WrappedInput({ element: passedElement, diff --git a/src/scripts/choices.test.js b/src/scripts/choices.test.js index 3e5bb34..06f4f96 100644 --- a/src/scripts/choices.test.js +++ b/src/scripts/choices.test.js @@ -63,6 +63,36 @@ describe('choices', () => { ...config, }); }); + + describe('passing the searchEnabled config option with a value of false', () => { + describe('passing a select-multiple element', () => { + it('sets searchEnabled to true', () => { + document.body.innerHTML = ` + + `; + + instance = new Choices('[data-choice]', { + searchEnabled: false, + }); + + expect(instance.config.searchEnabled).to.equal(true); + }); + }); + }); + + describe('passing the renderSelectedChoices config option with an unexpected value', () => { + it('sets renderSelectedChoices to "auto"', () => { + document.body.innerHTML = ` + + `; + + instance = new Choices('[data-choice]', { + renderSelectedChoices: 'test', + }); + + expect(instance.config.renderSelectedChoices).to.equal('auto'); + }); + }); }); });