Use deepmerge lib + store all defaults in DEFAULT_CONFIG

This commit is contained in:
Josh Johnson 2018-10-09 12:43:25 +01:00
parent 4824958e4d
commit 4431a0ec23
3 changed files with 8 additions and 21 deletions

View file

@ -77,6 +77,7 @@
"classnames": "^2.2.5",
"core-js": "^2.5.6",
"custom-event-polyfill": "^0.3.0",
"deepmerge": "^2.2.1",
"fuse.js": "^3.1.0",
"opn": "^5.1.0",
"redux": "^3.3.1"

View file

@ -1,4 +1,5 @@
import Fuse from 'fuse.js';
import merge from 'deepmerge';
import './lib/polyfills';
import Store from './store/store';
@ -10,12 +11,7 @@ import {
WrappedInput,
WrappedSelect,
} from './components';
import {
DEFAULT_CONFIG,
DEFAULT_CLASSNAMES,
EVENTS,
KEY_CODES,
} from './constants';
import { DEFAULT_CONFIG, EVENTS, KEY_CODES } from './constants';
import { TEMPLATES } from './templates';
import {
addChoice,
@ -33,7 +29,6 @@ import {
isType,
strToEl,
extend,
sortByAlpha,
sortByScore,
generateId,
findAncestorByAttrName,
@ -60,7 +55,7 @@ class Choices {
}
}
this.config = Choices._generateConfig(userConfig);
this.config = merge.all([DEFAULT_CONFIG, Choices.userDefaults, userConfig]);
if (!['auto', 'always'].includes(this.config.renderSelectedChoices)) {
this.config.renderSelectedChoices = 'auto';
@ -173,6 +168,7 @@ class Choices {
this._store.subscribe(this._render);
this._render();
this._addEventListeners();
this.initialised = true;
const { callbackOnInit } = this.config;
@ -2029,18 +2025,6 @@ class Choices {
);
}
static _generateConfig(userConfig) {
const defaultConfig = {
...DEFAULT_CONFIG,
items: [],
choices: [],
classNames: DEFAULT_CLASSNAMES,
sortFn: sortByAlpha,
};
return extend(defaultConfig, Choices.userDefaults, userConfig);
}
_generatePlaceholderValue() {
if (this._isSelectOneElement) {
return false;

View file

@ -1,4 +1,4 @@
import { stripHTML } from './lib/utils';
import { stripHTML, sortByAlpha } from './lib/utils';
export const DEFAULT_CLASSNAMES = {
containerOuter: 'choices',
@ -52,6 +52,7 @@ export const DEFAULT_CONFIG = {
regexFilter: null,
shouldSort: true,
shouldSortItems: false,
sortFn: sortByAlpha,
placeholder: true,
placeholderValue: null,
searchPlaceholderValue: null,
@ -71,6 +72,7 @@ export const DEFAULT_CONFIG = {
},
callbackOnInit: null,
callbackOnCreateTemplates: null,
classNames: DEFAULT_CLASSNAMES,
};
export const EVENTS = {