From 806edca24fb0f2bf5dd6c211b88bb0bf1e63cfe3 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 10 Oct 2017 16:59:49 +0100 Subject: [PATCH] Move templates into own file --- src/scripts/src/choices.js | 227 +--------------------------------- src/scripts/src/templates.js | 229 +++++++++++++++++++++++++++++++++++ 2 files changed, 233 insertions(+), 223 deletions(-) create mode 100644 src/scripts/src/templates.js diff --git a/src/scripts/src/choices.js b/src/scripts/src/choices.js index 43d4435..fafa514 100644 --- a/src/scripts/src/choices.js +++ b/src/scripts/src/choices.js @@ -1,11 +1,11 @@ import Fuse from 'fuse.js'; -import classNames from 'classnames'; import Store from './store/store'; import Dropdown from './components/dropdown'; import Container from './components/container'; import Input from './components/input'; import List from './components/list'; import { DEFAULT_CONFIG, DEFAULT_CLASSNAMES, EVENTS } from './constants'; +import { TEMPLATES } from './templates'; import { addChoice, filterChoices, activateChoices, clearChoices } from './actions/choices'; import { addItem, removeItem, highlightItem } from './actions/items'; import { addGroup } from './actions/groups'; @@ -2247,7 +2247,8 @@ class Choices { return null; } const templates = this.config.templates; - return templates[template](...args); + const globalClasses = this.config.classNames; + return templates[template].call(this, globalClasses, ...args); } /** @@ -2256,226 +2257,6 @@ class Choices { * @private */ _createTemplates() { - const globalClasses = this.config.classNames; - const templates = { - containerOuter: (direction) => { - const tabIndex = this.isSelectOneElement ? 'tabindex="0"' : ''; - let role = this.isSelectElement ? 'role="listbox"' : ''; - let ariaAutoComplete = ''; - - if (this.isSelectElement && this.config.searchEnabled) { - role = 'role="combobox"'; - ariaAutoComplete = 'aria-autocomplete="list"'; - } - - return strToEl(` - - `); - }, - containerInner: () => strToEl(` -
- `), - itemList: () => { - const localClasses = classNames( - globalClasses.list, { - [globalClasses.listSingle]: (this.isSelectOneElement), - [globalClasses.listItems]: (!this.isSelectOneElement), - }, - ); - - return strToEl(` -
- `); - }, - placeholder: value => strToEl(` -
- ${value} -
- `), - item: (data) => { - const ariaSelected = data.active ? 'aria-selected="true"' : ''; - const ariaDisabled = data.disabled ? 'aria-disabled="true"' : ''; - - let localClasses = classNames( - globalClasses.item, { - [globalClasses.highlightedState]: data.highlighted, - [globalClasses.itemSelectable]: !data.highlighted, - [globalClasses.placeholder]: data.placeholder, - }, - ); - - if (this.config.removeItemButton) { - localClasses = classNames( - globalClasses.item, { - [globalClasses.highlightedState]: data.highlighted, - [globalClasses.itemSelectable]: !data.disabled, - [globalClasses.placeholder]: data.placeholder, - }, - ); - - return strToEl(` -
- ${data.label} -
- `); - } - - return strToEl(` -
- ${data.label} -
- `); - }, - choiceList: () => { - const ariaMultiSelectable = !this.isSelectOneElement ? - 'aria-multiselectable="true"' : - ''; - - return strToEl(` -
-
- `); - }, - choiceGroup: (data) => { - const ariaDisabled = data.disabled ? 'aria-disabled="true"' : ''; - const localClasses = classNames( - globalClasses.group, { - [globalClasses.itemDisabled]: data.disabled, - }, - ); - - return strToEl(` -
-
${data.value}
-
- `); - }, - choice: (data) => { - const role = data.groupId > 0 ? 'role="treeitem"' : 'role="option"'; - const localClasses = classNames( - globalClasses.item, - globalClasses.itemChoice, { - [globalClasses.itemDisabled]: data.disabled, - [globalClasses.itemSelectable]: !data.disabled, - [globalClasses.placeholder]: data.placeholder, - }, - ); - - return strToEl(` -
- ${data.label} -
- `); - }, - input: () => { - const localClasses = classNames( - globalClasses.input, - globalClasses.inputCloned, - ); - - return strToEl(` - - `); - }, - dropdown: () => { - const localClasses = classNames( - globalClasses.list, - globalClasses.listDropdown, - ); - - return strToEl(` - - `); - }, - notice: (label, type = '') => { - const localClasses = classNames( - globalClasses.item, - globalClasses.itemChoice, { - [globalClasses.noResults]: (type === 'no-results'), - [globalClasses.noChoices]: (type === 'no-choices'), - }, - ); - - return strToEl(` -
- ${label} -
- `); - }, - option: data => strToEl(` - - `), - }; - // User's custom templates const callbackTemplate = this.config.callbackOnCreateTemplates; let userTemplates = {}; @@ -2483,7 +2264,7 @@ class Choices { userTemplates = callbackTemplate.call(this, strToEl); } - this.config.templates = extend(templates, userTemplates); + this.config.templates = extend(TEMPLATES, userTemplates); } /** diff --git a/src/scripts/src/templates.js b/src/scripts/src/templates.js new file mode 100644 index 0000000..0fe7edc --- /dev/null +++ b/src/scripts/src/templates.js @@ -0,0 +1,229 @@ +import classNames from 'classnames'; +import { strToEl } from './lib/utils'; + +export const TEMPLATES = { + containerOuter(globalClasses, direction) { + const tabIndex = this.isSelectOneElement ? 'tabindex="0"' : ''; + let role = this.isSelectElement ? 'role="listbox"' : ''; + let ariaAutoComplete = ''; + + if (this.isSelectElement && this.config.searchEnabled) { + role = 'role="combobox"'; + ariaAutoComplete = 'aria-autocomplete="list"'; + } + + return strToEl(` + + `); + }, + containerInner(globalClasses) { + return strToEl(` +
+ `); + }, + itemList(globalClasses) { + const localClasses = classNames( + globalClasses.list, { + [globalClasses.listSingle]: (this.isSelectOneElement), + [globalClasses.listItems]: (!this.isSelectOneElement), + }, + ); + + return strToEl(` +
+ `); + }, + placeholder(globalClasses, value) { + return strToEl(` +
+ ${value} +
+ `); + }, + item(globalClasses, data) { + const ariaSelected = data.active ? 'aria-selected="true"' : ''; + const ariaDisabled = data.disabled ? 'aria-disabled="true"' : ''; + + let localClasses = classNames( + globalClasses.item, { + [globalClasses.highlightedState]: data.highlighted, + [globalClasses.itemSelectable]: !data.highlighted, + [globalClasses.placeholder]: data.placeholder, + }, + ); + + if (this.config.removeItemButton) { + localClasses = classNames( + globalClasses.item, { + [globalClasses.highlightedState]: data.highlighted, + [globalClasses.itemSelectable]: !data.disabled, + [globalClasses.placeholder]: data.placeholder, + }, + ); + + return strToEl(` +
+ ${data.label} +
+ `); + } + + return strToEl(` +
+ ${data.label} +
+ `); + }, + choiceList(globalClasses) { + const ariaMultiSelectable = !this.isSelectOneElement ? + 'aria-multiselectable="true"' : + ''; + + return strToEl(` +
+
+ `); + }, + choiceGroup(globalClasses, data) { + const ariaDisabled = data.disabled ? 'aria-disabled="true"' : ''; + const localClasses = classNames( + globalClasses.group, { + [globalClasses.itemDisabled]: data.disabled, + }, + ); + + return strToEl(` +
+
${data.value}
+
+ `); + }, + choice(globalClasses, data) { + const role = data.groupId > 0 ? 'role="treeitem"' : 'role="option"'; + const localClasses = classNames( + globalClasses.item, + globalClasses.itemChoice, { + [globalClasses.itemDisabled]: data.disabled, + [globalClasses.itemSelectable]: !data.disabled, + [globalClasses.placeholder]: data.placeholder, + }, + ); + + return strToEl(` +
+ ${data.label} +
+ `); + }, + input(globalClasses) { + const localClasses = classNames( + globalClasses.input, + globalClasses.inputCloned, + ); + + return strToEl(` + + `); + }, + dropdown(globalClasses) { + const localClasses = classNames( + globalClasses.list, + globalClasses.listDropdown, + ); + + return strToEl(` + + `); + }, + notice(globalClasses, label, type = '') { + const localClasses = classNames( + globalClasses.item, + globalClasses.itemChoice, { + [globalClasses.noResults]: (type === 'no-results'), + [globalClasses.noChoices]: (type === 'no-choices'), + }, + ); + + return strToEl(` +
+ ${label} +
+ `); + }, + option(globalClasses, data) { + return strToEl(` + + `); + }, +}; + +export default TEMPLATES;