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
This commit is contained in:
Josh Johnson 2019-11-03 18:12:47 +00:00 committed by GitHub
parent ab22347d7b
commit f30b976424
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 17 deletions

View File

@ -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,

View File

@ -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 = `
<select data-choice multiple></select>
`;
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 = `
<select data-choice multiple></select>
`;
instance = new Choices('[data-choice]', {
renderSelectedChoices: 'test',
});
expect(instance.config.renderSelectedChoices).to.equal('auto');
});
});
});
});