A vanilla JS customisable select box/text input plugin ⚡️
A vanilla JS customisable select box/text input plugin ⚡️
A vanilla, lightweight (~20kb gzipped ), configurable select box/text input plugin. Similar to Select2 and Selectize but without the jQuery dependency.
With NPM:
npm install choices.jsWith Yarn:
yarn add choices.jsFrom a CDN:
Notes:
…Or include Choices directly:
<link rel="stylesheet" href="public/assets/styles/base.min.css" />
<link rel="stylesheet" href="public/assets/styles/choices.min.css" />
<script src="/public/assets/scripts/choices.min.js"></script>The use of import of css/scss is supported from webpack.
In .scss:
@import "choices.js/src/styles/choices";In .js/.ts:
import "choices.js/public/assets/styles/choices.css";Note: If you pass a selector which targets multiple elements, the first matching element will be used. Versions prior to 8.x.x would return multiple Choices instances.
…| Word | Definition |
|---|---|
| Choice | A choice is a value a user can select. A choice would be equivalent to the <option></option> element within a select input. |
| Group | A group is a collection of choices. A group should be seen as equivalent to a <optgroup></optgroup> 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: <option value="Hello" selected></option> whereas in the context of a text input an item is equivalent to <input type="text" value="Hello"> |
Choices works with the following input types, referenced in the documentation as noted.
| HTML Element | Documentation "Input Type" |
|---|---|
<input type="text"> |
text |
<select> |
select-one |
<select multiple> |
select-multiple |
Type: Boolean Default: false
Input types affected: text, select-one, select-multiple
Usage: Optionally suppress console errors and warnings.
Type: Array Default: []
Input types affected: text
Usage: Add pre-selected items (see terminology) to text input.
Pass an array of strings:
['value 1', 'value 2', 'value 3']
Pass an array of objects:
[{
value: 'Value 1',
label: 'Label 1',
id: 1
},
{
value: 'Value 2',
label: 'Label 2',
id: 2,
customProperties: {
random: 'I am a custom property'
}
}]Type: Array Default: []
Input types affected: select-one, select-multiple
Usage: Add choices (see terminology) to select input.
Pass an array of objects:
…Type: Number Default: -1
Input types affected: select-one, select-multiple
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.
Type: Number Default: -1
Input types affected: text, select-multiple
Usage: The amount of items a user can input/select ("-1" indicates no limit).
Type: Boolean | 'auto' Default: auto
Input types affected: select-one, select-multiple
Usage: Control how the dropdown closes after making a selection for select-one or select-multiple.
Type: Boolean Default: false
Input types affected: select-one, select-multiple
Usage: Make select-multiple with a max item count of 1 work similar to select-one does. Selecting an item will auto-close the dropdown and swap any existing item for the just selected choice. If applied to a select-one, it functions as above and not the standard select-one.
Type: Boolean Default: false
Input types affected: select-multiple, select-one
Usage: Whether a user can add choices dynamically.
Note: addItems must also be true
Type: Boolean Default: true
Input types affected: text
Usage: Whether a user can add items.
Type: Boolean Default: true
Input types affected: text, select-multiple
Usage: Whether a user can remove items.
Type: Boolean Default: false
Input types affected: text, select-one, select-multiple
Usage: Whether each item should have a remove button.
Type: Boolean Default: false
Input types affected: text, select-one, select-multiple
Usage: Align item remove button left vs right
Type: Boolean Default: false
Input types affected: text
Usage: Whether a user can edit items. An item's value can be edited by pressing the backspace.
Type: Boolean Default: false
Input types affected: text, select-one, select-multiple
Usage: Whether HTML should be rendered in all Choices elements. If false, all elements (placeholder, items, etc.) will be treated as plain text. If true, this can be used to perform XSS scripting attacks if you load choices from a remote source.
Type: Boolean Default: false
Input types affected: text, select-one, select-multiple
Usage: Whether HTML should be escaped on input when addItems or addChoices is true. If false, user input will be treated as plain text. If true, this can be used to perform XSS scripting attacks if you load choices from a remote source.
Type: Boolean Default: true
Input types affected: text, select-multiple, select-one
Usage: Whether duplicate inputted/chosen items are allowed
Type: String Default: ,
Input types affected: text
Usage: What divides each value. The default delimiter separates each value with a comma: "Value 1, Value 2, Value 3".
Type: Boolean Default: true
Input types affected: text, select-multiple
Usage: Whether a user can paste into the input.
Type: Boolean Default: true
Input types affected: select-one, select-multiple
Usage: Whether a search area should be shown.
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.
Type: Boolean Default: false
Input types affected: select-one, select-multiple
Usage: Whether disabled choices should be included in search results. If true, disabled choices will appear in search results but still cannot be selected. This is useful when you want users to see what options exist but are currently unavailable. Placeholders are always excluded from search results regardless of this setting.
Type: Array/String Default: ['label', 'value']
Input types affected:select-one, select-multiple
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'].
Type: Number Default: 1
Input types affected: select-one, select-multiple
Usage: The minimum length a search value should be before choices are searched.
Type: Number Default: 4
Input types affected: select-one, select-multiple
Usage: The maximum amount of search results to show ("-1" indicates no limit).
Type: Document Element Default: null
Input types affected: select-one, select-multiple
Usage: You can pass along the shadowRoot from your application like so.
var shadowRoot = document
.getElementById('wrapper')
.attachShadow({ mode: 'open' });
...
var el = shadowRoot.querySelector(...);
var choices = new Choices(el, {
shadowRoot: shadowRoot,
});Type: String Default: auto
Input types affected: select-one, select-multiple
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.
Type: Boolean Default: true
Input types affected: select-multiple
Usage: Whether the scroll position should reset after adding an item.
Type: `string | RegExp |
[BUG] alt+tab breaks focus
F5 and browser reload shortcuts briefly open a focused closed select
[Feature Request] Add `tabIndex` option to configure `tabindex` on the outer container or input
CSS for multi-selects uses opacity var, single-selects doesn't
Select does not work with VoiceOver
Show groupHeading when searching/filtering
Expose Fuse to be used independently
Simple single-input mode
Combobox behavior
Dropdown always scrolls to selected item on open, hiding options above it (regression from v10)