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'); + }); + }); }); });