Skip to content

Commit

Permalink
Use deepmerge lib + store all defaults in DEFAULT_CONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
jshjohnson committed Oct 9, 2018
1 parent 4824958 commit 4431a0e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 4 additions & 20 deletions src/scripts/choices.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Fuse from 'fuse.js';
import merge from 'deepmerge';

import './lib/polyfills';
import Store from './store/store';
Expand All @@ -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,
Expand All @@ -33,7 +29,6 @@ import {
isType,
strToEl,
extend,
sortByAlpha,
sortByScore,
generateId,
findAncestorByAttrName,
Expand All @@ -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';
Expand Down Expand Up @@ -173,6 +168,7 @@ class Choices {
this._store.subscribe(this._render);
this._render();
this._addEventListeners();

this.initialised = true;

const { callbackOnInit } = this.config;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stripHTML } from './lib/utils';
import { stripHTML, sortByAlpha } from './lib/utils';

export const DEFAULT_CLASSNAMES = {
containerOuter: 'choices',
Expand Down Expand Up @@ -52,6 +52,7 @@ export const DEFAULT_CONFIG = {
regexFilter: null,
shouldSort: true,
shouldSortItems: false,
sortFn: sortByAlpha,
placeholder: true,
placeholderValue: null,
searchPlaceholderValue: null,
Expand All @@ -71,6 +72,7 @@ export const DEFAULT_CONFIG = {
},
callbackOnInit: null,
callbackOnCreateTemplates: null,
classNames: DEFAULT_CLASSNAMES,
};

export const EVENTS = {
Expand Down

0 comments on commit 4431a0e

Please sign in to comment.