diff --git a/README.md b/README.md index f36218d..a2970de 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ A vanilla, lightweight (~15kb gzipped 🎉), configurable select box/text input callbackOnRemoveItem: null, callbackOnHighlightItem: null, callbackOnUnhighlightItem: null, + callbackOnCreateTemplates: null, callbackOnChange: null, callbackOnSearch: null, }); @@ -413,6 +414,39 @@ const example = new Choices(element, { **Usage:** Function to run each time an item is unhighlighted. +### callbackOnCreateTemplates +**Type:** `Function` **Default:** `null` **Arguments:** `instance, template` + +**Input types affected:** `text`, `select-one`, `select-multiple` + +**Usage:** Function to run on template creation. Through this callback it is possible to provide custom templates for the various components of Choices (see glossary). For Choices to work with custom templates, it is important you maintain the various attributes defined [here](https://github.com/jshjohnson/Choices/blob/master/assets/scripts/src/choices.js#L1946-L2030). + +**Example:** + +```js +const example = new Choices(element, { + callbackOnCreateTemplates: function (instance, template) { + var classNames = instance.config.classNames; + return { + item: (data) => { + return template(` +
+ ${data.label} +
+ `); + }, + choice: (data) => { + return template(` +
0 ? 'role="treeitem"' : 'role="option"'}> + ${data.label} +
+ `); + }, + }; + } +}); +``` + ### callbackOnChange **Type:** `Function` **Default:** `null` **Arguments:** `value, passedInput` diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index a4919ed..5330454 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -2030,10 +2030,8 @@ export default class Choices { // User's custom templates const callbackTemplate = this.config.callbackOnCreateTemplates; let userTemplates = {}; - if (callbackTemplate) { - if (isType('Function', callbackTemplate)) { - userTemplates = callbackTemplate(this, strToEl); - } + if (callbackTemplate && isType('Function', callbackTemplate)) { + userTemplates = callbackTemplate(this, strToEl); } this.config.templates = extend(templates, userTemplates); } diff --git a/index.html b/index.html index 32619b4..8403dc4 100644 --- a/index.html +++ b/index.html @@ -219,6 +219,14 @@ + + +

Below is an example of how you could have two select inputs depend on eachother. 'Boroughs' will only be enabled if the value of 'States' is 'New York'

- - -