From 400a70e5cb1df2a6c5e5229c593c6698450b0af3 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Sat, 30 Jul 2016 15:12:22 +0100 Subject: [PATCH] Options array in config --- README.md | 106 ++++++++++++++++++++-- assets/scripts/src/choices.js | 24 ++++- index.html | 163 ++++++++++++++++++---------------- 3 files changed, 208 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index 76c1791..11da943 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ A lightweight, configurable select box/text input plugin. Similar to Select2 and // Passing options (with default options) const choices = new Choices(elements, { items: [], + options: [], maxItemCount: -1, addItems: true, removeItems: true, @@ -87,8 +88,11 @@ A lightweight, configurable select box/text input plugin. Similar to Select2 and ## Configuration options ### items -Type: Default: `[]` -Usage: Add pre-selected items to input. +Type: `Array` Default: `[]` + +Usage: Add pre-selected items to text input. + +Input types affected: `text` Pass an array of strings: @@ -109,29 +113,63 @@ Pass an array of objects: }] ``` +### options +Type: `Array` Default: `[]` + +Usage: Add options to select input. + +Input types affected: `select-one`, `select-multiple` + +Pass an array of objects: + +``` +[{ + value: 'Option 1', + label: 'Option 1', + selected: true, + disabled: false, +}, +{ + value: 'Option 2', + label: 'Option 2', + selected: false, + disabled: true, +}] +``` + ### maxItemCount Type: `Number` Default:`-1` +Input types affected: `text`, `select-multiple` + Usage: The amount of items a user can input/select ("-1" indicates no limit). ### addItems Type: `Boolean` Default:`true` +Input types affected: `text` + Usage: Whether a user can add items to the passed input's value. ### removeItems Type: `Boolean` Default:`true` +Input types affected: `text`, `select-multiple` + Usage: Whether a user can remove items (only affects text and multiple select input types). ### removeButton Type: `Boolean` Default:`false` +Input types affected: `text`, `select-multiple` + Usage: Whether a button should show that, when clicked, will remove an item (only affects text and multiple select input types). ### editItems Type: `Boolean` Default:`false` +Input types affected: `text` + Usage: Whether a user can edit selected items (only affects text input types). Usage: Optionally set an item limit (`-1` indicates no limit). @@ -139,51 +177,71 @@ Pass an array of objects: ### delimiter Type: `String` Default:`,` -Usage: What divides each value (only affects text input types). +Input types affected: `text` + +Usage: What divides each value. ### duplicates Type: `Boolean` Default:`true` -Usage: Whether a user can input a duplicate item (only affects text input types). +Input types affected: `text` + +Usage: Whether a user can input a duplicate item. ### paste Type: `Boolean` Default:`true` +Input types affected: `text`, `select-multiple`. + Usage: Whether a user can paste into the input. ### search Type: `Boolean` Default:`true` +Input types affected: `select-one`, `select-multiple`. + Usage: Whether a user can filter options by searching (only affects select input types). ### regexFilter Type: `Regex` Default:`null` -Usage: A filter that will need to pass for a user to successfully add an item (only affects text input types). +Input types affected: `text` + +Usage: A filter that will need to pass for a user to successfully add an item. ### placeholder Type: `Boolean` Default:`true` +Input types affected: `text`, `select-one`, `select-multiple` + Usage: Whether the input should show a placeholder. Used in conjunction with `placeholderValue`. If `placeholder` is set to true and no value is passed to `placeholderValue`, the passed input's placeholder attribute will be used as the placeholder value. ### placeholderValue Type: `String` Default:`null` +Input types affected: `text`, `select-one`, `select-multiple` + Usage: The value of the inputs placeholder. ### prependValue Type: `String` Default:`null` +Input types affected: `text`, `select-one`, `select-multiple` + Usage: Prepend a value to each item added to input (only affects text input types). ### appendValue Type: `String` Default:`null` +Input types affected: `text`, `select-one`, `select-multiple` + Usage: Append a value to each item added to input (only affects text input types). ### loadingText Type: `String` Default:`Loading...` +Input types affected: `select-one`, `select-multiple` + Usage: The loading text that is shown when options are populated via an AJAX callback. ### classNames @@ -217,21 +275,29 @@ classNames: { } ``` +Input types affected: `text`, `select-one`, `select-multiple` + Usage: Classes added to HTML generated by Choices. By default classnames follow the [BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/) notation. ### callbackOnInit Type: `Function` Default:`() => {}` +Input types affected: `text`, `select-one`, `select-multiple` + Usage: Function to run once Choices initialises. ### callbackOnAddItem Type: `Function` Default:`(id, value, passedInput) => {}` +Input types affected: `text`, `select-one`, `select-multiple` + Usage: Function to run each time an item is added. ### callbackOnRemoveItem Type: `Function` Default:`(id, value, passedInput) => {}` +Input types affected: `text`, `select-one`, `select-multiple` + Usage: Function to run each time an item is removed. @@ -256,54 +322,80 @@ choices.disable(); ``` ### highlightAll(); +Input types affected: `text`, `select-multiple` + Usage: Highlight each chosen item (selected items can be removed). ### unhighlightAll(); +Input types affected: `text`, `select-multiple` + Usage: Un-highlight each chosen item. ### removeItemsByValue(value); +Input types affected: `text`, `select-multiple` + Usage: Remove each item by a given value. ### removeActiveItems(excludedId); +Input types affected: `text`, `select-multiple` + Usage: Remove each selectable item. ### removeHighlightedItems(); +Input types affected: `text`, `select-multiple` + Usage: Remove each item the user has selected. ### showDropdown(); +Input types affected: `select-one`, `select-multiple` + Usage: Show option list dropdown (only affects select inputs). ### hideDropdown(); +Input types affected: `text`, `select-multiple` + Usage: Hide option list dropdown (only affects select inputs). ### toggleDropdown(); +Input types affected: `text`, `select-multiple` + Usage: Toggle dropdown between showing/hidden. ### setValue(args); -Usage: Set value of input based on an array of objects or strings. This behaves exactly the same as passing items via the `items` option but can be called after initialising Choices on an text input (only affects text inputs). +Input types affected: `text` + +Usage: Set value of input based on an array of objects or strings. This behaves exactly the same as passing items via the `items` option but can be called after initialising Choices on an text input. ### clearValue(); +Input types affected: `text` + Usage: Clear value of input. ### clearInput(); -Usage: Clear input of any user inputted text (only affects text inputs). +Input types affected: `text` + +Usage: Clear input of any user inputted text. ### disable(); +Input types affected: `text`, `select-one`, `select-multiple` + Usage: Disable input from selecting further options. ### ajax(fn); +Input types affected: `select-one`, `select-multiple` + Usage: Populate options via a callback. diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index 1276734..38dcdd7 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -26,6 +26,7 @@ export class Choices { const defaultConfig = { items: [], + options: [], maxItemCount: -1, addItems: true, removeItems: true, @@ -90,8 +91,12 @@ export class Choices { this.highlightPosition = 0; this.canSearch = this.config.search; + // Assing preset optiosn from passed object + this.presetOptions = this.config.options; + // Assign preset items from passed object first this.presetItems = this.config.items; + // Then add any values passed from attribute if(this.passedElement.value) { this.presetItems = this.presetItems.concat(this.passedElement.value.split(this.config.delimiter)); @@ -1261,9 +1266,22 @@ export class Choices { }); } else { const passedOptions = Array.from(this.passedElement.options); - passedOptions.forEach((option) => { - const isDisabled = option.disabled || option.parentNode.disabled; - this._addChoice(option.selected, isDisabled, option.value, option.innerHTML); + let allOptions = []; + + // Create array of options from option elements + passedOptions.forEach((o) => { + allOptions.push({ + value: o.value, + label: o.innerHTML, + selected: o.selected, + disabled: o.disabled || o.parentNode.disabled + }); + }); + + allOptions = allOptions.concat(this.presetOptions); + + allOptions.forEach((o) => { + this._addChoice(o.selected ? o.selected : false, o.disabled ? o.disabled : false, o.value, o.label); }); } } else if(this.passedElement.type === 'text') { diff --git a/index.html b/index.html index 838abc9..15ca427 100644 --- a/index.html +++ b/index.html @@ -11,7 +11,7 @@
-

Choices

+ + +