diff --git a/README.md b/README.md index 0b47cae..dc7f5a2 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,36 @@ # Choices.js [![Actions Status](https://github.com/jshjohnson/Choices/workflows/Unit%20Tests/badge.svg)](https://github.com/jshjohnson/Choices/actions) [![npm](https://img.shields.io/npm/v/choices.js.svg)](https://www.npmjs.com/package/choices.js) [![codebeat badge](https://codebeat.co/badges/55120150-5866-42d8-8010-6aaaff5d3fa1)](https://codebeat.co/projects/github-com-jshjohnson-choices-master) + A vanilla, lightweight (~19kb gzipped 🎉), configurable select box/text input plugin. Similar to Select2 and Selectize but without the jQuery dependency. [Demo](https://joshuajohnson.co.uk/Choices/) ## TL;DR -* Lightweight -* No jQuery dependency -* Configurable sorting -* Flexible styling -* Fast search/filtering -* Clean API -* Right-to-left support -* Custom templates ----- +- Lightweight +- No jQuery dependency +- Configurable sorting +- Flexible styling +- Fast search/filtering +- Clean API +- Right-to-left support +- Custom templates + +--- + ### Interested in writing your own ES6 JavaScript plugins? Check out [ES6.io](https://ES6.io/friend/JOHNSON) for great tutorials! 💪🏼 ----- + +--- ## Installation + With [NPM](https://www.npmjs.com/package/choices.js): + ```zsh npm install choices.js ``` With [Yarn](https://yarnpkg.com/): + ```zsh yarn add choices.js ``` @@ -34,9 +41,15 @@ From a [CDN](https://www.jsdelivr.com/package/npm/choices.js): ```html - + - + ``` @@ -45,12 +58,13 @@ Or include Choices directly: ```html - + - + ``` + ## Setup If you pass a selector which targets multiple elements, an array of Choices instances @@ -152,24 +166,26 @@ will be returned. If you target one element, that instance will be returned. ``` ## Terminology + | Word | Definition | | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Choice | A choice is a value a user can select. A choice would be equivalent to the `` element within a select input. | | Group | A group is a collection of choices. A group should be seen as equivalent to a `` element within a select input. | | Item | An item is an inputted value (text input) or a selected choice (select element). In the context of a select element, an item is equivalent to a selected option element: `` whereas in the context of a text input an item is equivalent to `` | - ## Configuration options + ### silent -**Type:** `Boolean` **Default:** `false` + +**Type:** `Boolean` **Default:** `false` **Input types affected:** `text`, `select-single`, `select-multiple` **Usage:** Optionally suppress console errors and warnings. - ### items -**Type:** `Array` **Default:** `[]` + +**Type:** `Array` **Default:** `[]` **Input types affected:** `text` @@ -198,7 +214,8 @@ Pass an array of objects: ``` ### choices -**Type:** `Array` **Default:** `[]` + +**Type:** `Array` **Default:** `[]` **Input types affected:** `select-one`, `select-multiple` @@ -226,6 +243,7 @@ Pass an array of objects: ``` ### renderChoiceLimit + **Type:** `Number` **Default:** `-1` **Input types affected:** `select-one`, `select-multiple` @@ -233,6 +251,7 @@ Pass an array of objects: **Usage:** The amount of choices to be rendered within the dropdown list ("-1" indicates no limit). This is useful if you have a lot of choices where it is easier for a user to use the search area to find a choice. ### maxItemCount + **Type:** `Number` **Default:** `-1` **Input types affected:** `text`, `select-multiple` @@ -240,6 +259,7 @@ Pass an array of objects: **Usage:** The amount of items a user can input/select ("-1" indicates no limit). ### addItems + **Type:** `Boolean` **Default:** `true` **Input types affected:** `text` @@ -247,6 +267,7 @@ Pass an array of objects: **Usage:** Whether a user can add items. ### removeItems + **Type:** `Boolean` **Default:** `true` **Input types affected:** `text`, `select-multiple` @@ -254,6 +275,7 @@ Pass an array of objects: **Usage:** Whether a user can remove items. ### removeItemButton + **Type:** `Boolean` **Default:** `false` **Input types affected:** `text`, `select-one`, `select-multiple` @@ -261,6 +283,7 @@ Pass an array of objects: **Usage:** Whether each item should have a remove button. ### editItems + **Type:** `Boolean` **Default:** `false` **Input types affected:** `text` @@ -268,6 +291,7 @@ Pass an array of objects: **Usage:** Whether a user can edit items. An item's value can be edited by pressing the backspace. ### duplicateItemsAllowed + **Type:** `Boolean` **Default:** `true` **Input types affected:** `text`, `select-multiple` @@ -275,6 +299,7 @@ Pass an array of objects: **Usage:** Whether duplicate inputted/chosen items are allowed ### delimiter + **Type:** `String` **Default:** `,` **Input types affected:** `text` @@ -282,6 +307,7 @@ Pass an array of objects: **Usage:** What divides each value. The default delimiter seperates each value with a comma: `"Value 1, Value 2, Value 3"`. ### paste + **Type:** `Boolean` **Default:** `true` **Input types affected:** `text`, `select-multiple` @@ -289,21 +315,23 @@ Pass an array of objects: **Usage:** Whether a user can paste into the input. ### searchEnabled + **Type:** `Boolean` **Default:** `true` **Input types affected:** `select-one` -**Usage:** Whether a search area should be shown. **Note:** Multiple select boxes will *always* show search areas. +**Usage:** Whether a search area should be shown. **Note:** Multiple select boxes will _always_ show search areas. ### searchChoices + **Type:** `Boolean` **Default:** `true` **Input types affected:** `select-one` **Usage:** Whether choices should be filtered by input or not. If `false`, the search event will still emit, but choices will not be filtered. - ### searchFields + **Type:** `Array/String` **Default:** `['label', 'value']` **Input types affected:**`select-one`, `select-multiple` @@ -311,6 +339,7 @@ Pass an array of objects: **Usage:** Specify which fields should be used when a user is searching. If you have added custom properties to your choices, you can add these values thus: `['label', 'value', 'customProperties.example']`. ### searchFloor + **Type:** `Number` **Default:** `1` **Input types affected:** `select-one`, `select-multiple` @@ -318,6 +347,7 @@ Pass an array of objects: **Usage:** The minimum length a search value should be before choices are searched. ### searchResultLimit: 4, + **Type:** `Number` **Default:** `4` **Input types affected:** `select-one`, `select-multiple` @@ -325,6 +355,7 @@ Pass an array of objects: **Usage:** The maximum amount of search results to show. ### position + **Type:** `String` **Default:** `auto` **Input types affected:** `select-one`, `select-multiple` @@ -332,6 +363,7 @@ Pass an array of objects: **Usage:** Whether the dropdown should appear above (`top`) or below (`bottom`) the input. By default, if there is not enough space within the window the dropdown will appear above the input, otherwise below it. ### resetScrollPosition + **Type:** `Boolean` **Default:** `true` **Input types affected:** `select-multiple` @@ -339,6 +371,7 @@ Pass an array of objects: **Usage:** Whether the scroll position should reset after adding an item. ### addItemFilterFn + **Type:** `Function` **Default:** `null` **Input types affected:** `text` @@ -357,6 +390,7 @@ new Choices(element, { ``` ### shouldSort + **Type:** `Boolean` **Default:** `true` **Input types affected:** `select-one`, `select-multiple` @@ -364,6 +398,7 @@ new Choices(element, { **Usage:** Whether choices and groups should be sorted. If false, choices/groups will appear in the order they were given. ### shouldSortItems + **Type:** `Boolean` **Default:** `false` **Input types affected:** `text`, `select-multiple` @@ -371,6 +406,7 @@ new Choices(element, { **Usage:** Whether items should be sorted. If false, items will appear in the order they were selected. ### sortFn + **Type:** `Function` **Default:** sortByAlpha **Input types affected:** `select-one`, `select-multiple` @@ -389,11 +425,12 @@ const example = new Choices(element, { ``` ### placeholder + **Type:** `Boolean` **Default:** `true` **Input types affected:** `text`, `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. +**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. **Note:** For single select boxes, the recommended way of adding a placeholder is as follows: @@ -409,6 +446,7 @@ const example = new Choices(element, { For backward compatibility, `` is also supported. ### placeholderValue + **Type:** `String` **Default:** `null` **Input types affected:** `text`, `select-multiple` @@ -416,6 +454,7 @@ For backward compatibility, `` **Usage:** The value of the inputs placeholder. ### searchPlaceholderValue + **Type:** `String` **Default:** `null` **Input types affected:** `select-one` @@ -423,6 +462,7 @@ For backward compatibility, `` **Usage:** The value of the search inputs placeholder. ### prependValue + **Type:** `String` **Default:** `null` **Input types affected:** `text`, `select-one`, `select-multiple` @@ -430,6 +470,7 @@ For backward compatibility, `` **Usage:** Prepend a value to each item added/selected. ### appendValue + **Type:** `String` **Default:** `null` **Input types affected:** `text`, `select-one`, `select-multiple` @@ -437,6 +478,7 @@ For backward compatibility, `` **Usage:** Append a value to each item added/selected. ### renderSelectedChoices + **Type:** `String` **Default:** `auto` **Input types affected:** `select-multiple` @@ -444,6 +486,7 @@ For backward compatibility, `` **Usage:** Whether selected choices should be removed from the list. By default choices are removed when they are selected in multiple select box. To always render choices pass `always`. ### loadingText + **Type:** `String` **Default:** `Loading...` **Input types affected:** `select-one`, `select-multiple` @@ -451,6 +494,7 @@ For backward compatibility, `` **Usage:** The text that is shown whilst choices are being populated via AJAX. ### noResultsText + **Type:** `String/Function` **Default:** `No results found` **Input types affected:** `select-one`, `select-multiple` @@ -458,6 +502,7 @@ For backward compatibility, `` **Usage:** The text that is shown when a user's search has returned no results. Optionally pass a function returning a string. ### noChoicesText + **Type:** `String/Function` **Default:** `No choices to choose from` **Input types affected:** `select-multiple` @@ -465,6 +510,7 @@ For backward compatibility, `` **Usage:** The text that is shown when a user has selected all possible choices. Optionally pass a function returning a string. ### itemSelectText + **Type:** `String` **Default:** `Press to select` **Input types affected:** `select-multiple`, `select-one` @@ -472,6 +518,7 @@ For backward compatibility, `` **Usage:** The text that is shown when a user hovers over a selectable choice. ### addItemText + **Type:** `String/Function` **Default:** `Press Enter to add "${value}"` **Input types affected:** `text` @@ -479,6 +526,7 @@ For backward compatibility, `` **Usage:** The text that is shown when a user has inputted a new item but has not pressed the enter key. To access the current input value, pass a function with a `value` argument (see the [default config](https://github.com/jshjohnson/Choices#setup) for an example), otherwise pass a string. ### maxItemText + **Type:** `String/Function` **Default:** `Only ${maxItemCount} values can be added` **Input types affected:** `text` @@ -486,6 +534,7 @@ For backward compatibility, `` **Usage:** The text that is shown when a user has focus on the input but has already reached the [max item count](https://github.com/jshjohnson/Choices#maxitemcount). To access the max item count, pass a function with a `maxItemCount` argument (see the [default config](https://github.com/jshjohnson/Choices#setup) for an example), otherwise pass a string. ### itemComparer + **Type:** `Function` **Default:** `strict equality` **Input types affected:** `select-one`, `select-multiple` @@ -493,6 +542,7 @@ For backward compatibility, `` **Usage:** Compare choice and value in appropriate way (e.g. deep equality for objects). To compare choice and value, pass a function with a `itemComparer` argument (see the [default config](https://github.com/jshjohnson/Choices#setup) for an example). ### classNames + **Type:** `Object` **Default:** ``` @@ -527,9 +577,11 @@ classNames: { **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. ## Callbacks + **Note:** For each callback, `this` refers to the current instance of Choices. This can be useful if you need access to methods (`this.disable()`) or the config object (`this.config`). ### callbackOnInit + **Type:** `Function` **Default:** `null` **Input types affected:** `text`, `select-one`, `select-multiple` @@ -537,6 +589,7 @@ classNames: { **Usage:** Function to run once Choices initialises. ### callbackOnCreateTemplates + **Type:** `Function` **Default:** `null` **Arguments:** `template` **Input types affected:** `text`, `select-one`, `select-multiple` @@ -547,28 +600,45 @@ classNames: { ```js const example = new Choices(element, { - callbackOnCreateTemplates: function (template) { + callbackOnCreateTemplates: function(template) { return { item: (classNames, data) => { return template(` -