govuk_publishing_components 58.1.1 → 58.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/images/select-with-search/cross-icon.svg +6 -0
  3. data/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-search-tracker.js +4 -0
  4. data/app/assets/javascripts/govuk_publishing_components/components/select-with-search.js +57 -0
  5. data/app/assets/stylesheets/govuk_publishing_components/_all_components.scss +1 -0
  6. data/app/assets/stylesheets/govuk_publishing_components/components/_select-with-search.scss +168 -0
  7. data/app/assets/stylesheets/govuk_publishing_components/components/_select.scss +6 -0
  8. data/app/views/govuk_publishing_components/components/_select.html.erb +22 -23
  9. data/app/views/govuk_publishing_components/components/_select_with_search.html.erb +14 -0
  10. data/app/views/govuk_publishing_components/components/docs/select.yml +11 -0
  11. data/app/views/govuk_publishing_components/components/docs/select_with_search.yml +196 -0
  12. data/lib/govuk_publishing_components/presenters/select_helper.rb +8 -5
  13. data/lib/govuk_publishing_components/presenters/select_with_search_helper.rb +92 -0
  14. data/lib/govuk_publishing_components/version.rb +1 -1
  15. data/lib/govuk_publishing_components.rb +1 -0
  16. data/node_modules/choices.js/LICENSE +21 -0
  17. data/node_modules/choices.js/README.md +1360 -0
  18. data/node_modules/choices.js/package.json +173 -0
  19. data/node_modules/choices.js/public/assets/scripts/choices.js +5230 -0
  20. data/node_modules/choices.js/public/assets/scripts/choices.min.js +2 -0
  21. data/node_modules/choices.js/public/assets/scripts/choices.mjs +5222 -0
  22. data/node_modules/choices.js/public/assets/scripts/choices.search-basic.js +4748 -0
  23. data/node_modules/choices.js/public/assets/scripts/choices.search-basic.min.js +2 -0
  24. data/node_modules/choices.js/public/assets/scripts/choices.search-basic.mjs +4740 -0
  25. data/node_modules/choices.js/public/assets/scripts/choices.search-kmp.js +3631 -0
  26. data/node_modules/choices.js/public/assets/scripts/choices.search-kmp.min.js +2 -0
  27. data/node_modules/choices.js/public/assets/scripts/choices.search-kmp.mjs +3623 -0
  28. data/node_modules/choices.js/public/assets/scripts/choices.search-prefix.js +3590 -0
  29. data/node_modules/choices.js/public/assets/scripts/choices.search-prefix.min.js +2 -0
  30. data/node_modules/choices.js/public/assets/scripts/choices.search-prefix.mjs +3582 -0
  31. data/node_modules/choices.js/public/assets/styles/base.css +180 -0
  32. data/node_modules/choices.js/public/assets/styles/base.css.map +1 -0
  33. data/node_modules/choices.js/public/assets/styles/base.min.css +1 -0
  34. data/node_modules/choices.js/public/assets/styles/choices.css +338 -0
  35. data/node_modules/choices.js/public/assets/styles/choices.css.map +1 -0
  36. data/node_modules/choices.js/public/assets/styles/choices.min.css +1 -0
  37. data/node_modules/choices.js/public/types/src/index.d.ts +6 -0
  38. data/node_modules/choices.js/public/types/src/scripts/actions/choices.d.ts +30 -0
  39. data/node_modules/choices.js/public/types/src/scripts/actions/groups.d.ts +8 -0
  40. data/node_modules/choices.js/public/types/src/scripts/actions/items.d.ts +17 -0
  41. data/node_modules/choices.js/public/types/src/scripts/choices.d.ts +210 -0
  42. data/node_modules/choices.js/public/types/src/scripts/components/container.d.ts +36 -0
  43. data/node_modules/choices.js/public/types/src/scripts/components/dropdown.d.ts +21 -0
  44. data/node_modules/choices.js/public/types/src/scripts/components/index.d.ts +7 -0
  45. data/node_modules/choices.js/public/types/src/scripts/components/input.d.ts +37 -0
  46. data/node_modules/choices.js/public/types/src/scripts/components/list.d.ts +14 -0
  47. data/node_modules/choices.js/public/types/src/scripts/components/wrapped-element.d.ts +21 -0
  48. data/node_modules/choices.js/public/types/src/scripts/components/wrapped-input.d.ts +3 -0
  49. data/node_modules/choices.js/public/types/src/scripts/components/wrapped-select.d.ts +20 -0
  50. data/node_modules/choices.js/public/types/src/scripts/constants.d.ts +1 -0
  51. data/node_modules/choices.js/public/types/src/scripts/defaults.d.ts +4 -0
  52. data/node_modules/choices.js/public/types/src/scripts/interfaces/action-type.d.ts +13 -0
  53. data/node_modules/choices.js/public/types/src/scripts/interfaces/build-flags.d.ts +11 -0
  54. data/node_modules/choices.js/public/types/src/scripts/interfaces/choice-full.d.ts +23 -0
  55. data/node_modules/choices.js/public/types/src/scripts/interfaces/class-names.d.ts +61 -0
  56. data/node_modules/choices.js/public/types/src/scripts/interfaces/event-choice.d.ts +7 -0
  57. data/node_modules/choices.js/public/types/src/scripts/interfaces/event-type.d.ts +14 -0
  58. data/node_modules/choices.js/public/types/src/scripts/interfaces/group-full.d.ts +10 -0
  59. data/node_modules/choices.js/public/types/src/scripts/interfaces/index.d.ts +14 -0
  60. data/node_modules/choices.js/public/types/src/scripts/interfaces/input-choice.d.ts +15 -0
  61. data/node_modules/choices.js/public/types/src/scripts/interfaces/input-group.d.ts +10 -0
  62. data/node_modules/choices.js/public/types/src/scripts/interfaces/item.d.ts +17 -0
  63. data/node_modules/choices.js/public/types/src/scripts/interfaces/keycode-map.d.ts +13 -0
  64. data/node_modules/choices.js/public/types/src/scripts/interfaces/options.d.ts +566 -0
  65. data/node_modules/choices.js/public/types/src/scripts/interfaces/passed-element-type.d.ts +7 -0
  66. data/node_modules/choices.js/public/types/src/scripts/interfaces/passed-element.d.ts +95 -0
  67. data/node_modules/choices.js/public/types/src/scripts/interfaces/position-options-type.d.ts +1 -0
  68. data/node_modules/choices.js/public/types/src/scripts/interfaces/search.d.ts +11 -0
  69. data/node_modules/choices.js/public/types/src/scripts/interfaces/state.d.ts +10 -0
  70. data/node_modules/choices.js/public/types/src/scripts/interfaces/store.d.ts +64 -0
  71. data/node_modules/choices.js/public/types/src/scripts/interfaces/string-pre-escaped.d.ts +3 -0
  72. data/node_modules/choices.js/public/types/src/scripts/interfaces/string-untrusted.d.ts +4 -0
  73. data/node_modules/choices.js/public/types/src/scripts/interfaces/templates.d.ts +29 -0
  74. data/node_modules/choices.js/public/types/src/scripts/interfaces/types.d.ts +18 -0
  75. data/node_modules/choices.js/public/types/src/scripts/lib/choice-input.d.ts +9 -0
  76. data/node_modules/choices.js/public/types/src/scripts/lib/html-guard-statements.d.ts +4 -0
  77. data/node_modules/choices.js/public/types/src/scripts/lib/utils.d.ts +31 -0
  78. data/node_modules/choices.js/public/types/src/scripts/reducers/choices.d.ts +8 -0
  79. data/node_modules/choices.js/public/types/src/scripts/reducers/groups.d.ts +8 -0
  80. data/node_modules/choices.js/public/types/src/scripts/reducers/items.d.ts +9 -0
  81. data/node_modules/choices.js/public/types/src/scripts/search/fuse.d.ts +14 -0
  82. data/node_modules/choices.js/public/types/src/scripts/search/index.d.ts +3 -0
  83. data/node_modules/choices.js/public/types/src/scripts/search/kmp.d.ts +11 -0
  84. data/node_modules/choices.js/public/types/src/scripts/search/prefix-filter.d.ts +11 -0
  85. data/node_modules/choices.js/public/types/src/scripts/store/store.d.ts +59 -0
  86. data/node_modules/choices.js/public/types/src/scripts/templates.d.ts +8 -0
  87. data/node_modules/choices.js/src/entry.js +3 -0
  88. data/node_modules/choices.js/src/icons/cross-inverse.svg +1 -0
  89. data/node_modules/choices.js/src/icons/cross.svg +1 -0
  90. data/node_modules/choices.js/src/index.ts +8 -0
  91. data/node_modules/choices.js/src/scripts/actions/choices.ts +59 -0
  92. data/node_modules/choices.js/src/scripts/actions/groups.ts +14 -0
  93. data/node_modules/choices.js/src/scripts/actions/items.ts +34 -0
  94. data/node_modules/choices.js/src/scripts/choices.ts +2364 -0
  95. data/node_modules/choices.js/src/scripts/components/container.ts +157 -0
  96. data/node_modules/choices.js/src/scripts/components/dropdown.ts +50 -0
  97. data/node_modules/choices.js/src/scripts/components/index.ts +8 -0
  98. data/node_modules/choices.js/src/scripts/components/input.ts +146 -0
  99. data/node_modules/choices.js/src/scripts/components/list.ts +89 -0
  100. data/node_modules/choices.js/src/scripts/components/wrapped-element.ts +89 -0
  101. data/node_modules/choices.js/src/scripts/components/wrapped-input.ts +3 -0
  102. data/node_modules/choices.js/src/scripts/components/wrapped-select.ts +115 -0
  103. data/node_modules/choices.js/src/scripts/constants.ts +1 -0
  104. data/node_modules/choices.js/src/scripts/defaults.ts +93 -0
  105. data/node_modules/choices.js/src/scripts/interfaces/action-type.ts +15 -0
  106. data/node_modules/choices.js/src/scripts/interfaces/build-flags.ts +17 -0
  107. data/node_modules/choices.js/src/scripts/interfaces/choice-full.ts +30 -0
  108. data/node_modules/choices.js/src/scripts/interfaces/class-names.ts +61 -0
  109. data/node_modules/choices.js/src/scripts/interfaces/event-choice.ts +9 -0
  110. data/node_modules/choices.js/src/scripts/interfaces/event-type.ts +16 -0
  111. data/node_modules/choices.js/src/scripts/interfaces/group-full.ts +12 -0
  112. data/node_modules/choices.js/src/scripts/interfaces/index.ts +14 -0
  113. data/node_modules/choices.js/src/scripts/interfaces/input-choice.ts +17 -0
  114. data/node_modules/choices.js/src/scripts/interfaces/input-group.ts +11 -0
  115. data/node_modules/choices.js/src/scripts/interfaces/item.ts +17 -0
  116. data/node_modules/choices.js/src/scripts/interfaces/keycode-map.ts +13 -0
  117. data/node_modules/choices.js/src/scripts/interfaces/options.ts +619 -0
  118. data/node_modules/choices.js/src/scripts/interfaces/passed-element-type.ts +9 -0
  119. data/node_modules/choices.js/src/scripts/interfaces/passed-element.ts +96 -0
  120. data/node_modules/choices.js/src/scripts/interfaces/position-options-type.ts +1 -0
  121. data/node_modules/choices.js/src/scripts/interfaces/search.ts +12 -0
  122. data/node_modules/choices.js/src/scripts/interfaces/state.ts +12 -0
  123. data/node_modules/choices.js/src/scripts/interfaces/store.ts +84 -0
  124. data/node_modules/choices.js/src/scripts/interfaces/string-pre-escaped.ts +3 -0
  125. data/node_modules/choices.js/src/scripts/interfaces/string-untrusted.ts +5 -0
  126. data/node_modules/choices.js/src/scripts/interfaces/templates.ts +66 -0
  127. data/node_modules/choices.js/src/scripts/interfaces/types.ts +21 -0
  128. data/node_modules/choices.js/src/scripts/lib/choice-input.ts +88 -0
  129. data/node_modules/choices.js/src/scripts/lib/html-guard-statements.ts +7 -0
  130. data/node_modules/choices.js/src/scripts/lib/utils.ts +230 -0
  131. data/node_modules/choices.js/src/scripts/reducers/choices.ts +86 -0
  132. data/node_modules/choices.js/src/scripts/reducers/groups.ts +32 -0
  133. data/node_modules/choices.js/src/scripts/reducers/items.ts +86 -0
  134. data/node_modules/choices.js/src/scripts/search/fuse.ts +59 -0
  135. data/node_modules/choices.js/src/scripts/search/index.ts +17 -0
  136. data/node_modules/choices.js/src/scripts/search/kmp.ts +87 -0
  137. data/node_modules/choices.js/src/scripts/search/prefix-filter.ts +42 -0
  138. data/node_modules/choices.js/src/scripts/store/store.ts +184 -0
  139. data/node_modules/choices.js/src/scripts/templates.ts +409 -0
  140. data/node_modules/choices.js/src/styles/base.scss +189 -0
  141. data/node_modules/choices.js/src/styles/choices.scss +414 -0
  142. data/node_modules/choices.js/src/tsconfig.json +22 -0
  143. metadata +134 -1
@@ -0,0 +1,1360 @@
1
+ # Choices.js [![Actions Status](https://github.com/jshjohnson/Choices/workflows/Build%20and%20test/badge.svg)](https://github.com/jshjohnson/Choices/actions) [![Actions Status](https://github.com/jshjohnson/Choices/workflows/Bundle%20size%20checks/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)
2
+
3
+ A vanilla, lightweight (~20kb gzipped 🎉), configurable select box/text input plugin. Similar to Select2 and Selectize but without the jQuery dependency.
4
+
5
+ [Demo](https://choices-js.github.io/Choices/)
6
+
7
+ ## TL;DR
8
+
9
+ - Lightweight
10
+ - No jQuery dependency
11
+ - Configurable sorting
12
+ - Flexible styling
13
+ - Fast search/filtering
14
+ - Clean API
15
+ - Right-to-left support
16
+ - Custom templates
17
+
18
+ ---
19
+
20
+ ### Interested in writing your own ES6 JavaScript plugins? Check out [ES6.io](https://ES6.io/friend/JOHNSON) for great tutorials! 💪🏼
21
+
22
+ ### Sponsored by:
23
+
24
+ <p align="center">
25
+ <a href="https://forums.sufficientvelocity.com/" target="_blank" rel="noopener noreferrer">
26
+ <img src="https://forums.sufficientvelocity.com/data/assets/static_light_logo.svg" alt="Sufficient Velocity" width="310" height="108">
27
+ </a>
28
+ </p>
29
+
30
+ <p align="center">
31
+ <a href="https://wanderermaps.com/" target="_blank" rel="noopener noreferrer">
32
+ <img src="https://cdn.shopify.com/s/files/1/0614/3357/7715/files/Logo_BlackWithBackground_200x.png?v=1644802773" alt="Wanderer Maps logo">
33
+ </a>
34
+ </p>
35
+
36
+ ---
37
+
38
+ ## Table of Contents
39
+
40
+ - [Installation](#installation)
41
+ - [Setup](#setup)
42
+ - [Terminology](#terminology)
43
+ - [Input Types](#input-types)
44
+ - [Configuration Options](#configuration-options)
45
+ - [Callbacks](#callbacks)
46
+ - [Events](#events)
47
+ - [Methods](#methods)
48
+ - [Development](#development)
49
+ - [License](#license)
50
+
51
+ ## Installation
52
+
53
+ With [NPM](https://www.npmjs.com/package/choices.js):
54
+
55
+ ```zsh
56
+ npm install choices.js
57
+ ```
58
+
59
+ With [Yarn](https://yarnpkg.com/):
60
+
61
+ ```zsh
62
+ yarn add choices.js
63
+ ```
64
+
65
+ From a [CDN](https://www.jsdelivr.com/package/npm/choices.js):
66
+
67
+ **Note:** There is sometimes a delay before the latest version of Choices is reflected on the CDN.
68
+
69
+ ```html
70
+ <!-- Include base CSS (optional) -->
71
+ <link
72
+ rel="stylesheet"
73
+ href="https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/base.min.css"
74
+ />
75
+ <!-- Or versioned -->
76
+ <link
77
+ rel="stylesheet"
78
+ href="https://cdn.jsdelivr.net/npm/choices.js@9.0.1/public/assets/styles/base.min.css"
79
+ />
80
+
81
+ <!-- Include Choices CSS -->
82
+ <link
83
+ rel="stylesheet"
84
+ href="https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/choices.min.css"
85
+ />
86
+ <!-- Or versioned -->
87
+ <link
88
+ rel="stylesheet"
89
+ href="https://cdn.jsdelivr.net/npm/choices.js@9.0.1/public/assets/styles/choices.min.css"
90
+ />
91
+
92
+ <!-- Include Choices JavaScript (latest) -->
93
+ <script src="https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js"></script>
94
+ <!-- Or versioned -->
95
+ <script src="https://cdn.jsdelivr.net/npm/choices.js@9.0.1/public/assets/scripts/choices.min.js"></script>
96
+ ```
97
+
98
+ Or include Choices directly:
99
+
100
+ ```html
101
+ <!-- Include base CSS (optional) -->
102
+ <link rel="stylesheet" href="public/assets/styles/base.min.css" />
103
+ <!-- Include Choices CSS -->
104
+ <link rel="stylesheet" href="public/assets/styles/choices.min.css" />
105
+ <!-- Include Choices JavaScript -->
106
+ <script src="/public/assets/scripts/choices.min.js"></script>
107
+ ```
108
+
109
+ ### CSS/SCSS
110
+
111
+ The use of `import` of css/scss is supported from webpack.
112
+
113
+ In .scss:
114
+ ```scss
115
+ @import "choices.js/src/styles/choices";
116
+ ```
117
+
118
+ In .js/.ts:
119
+ ```javascript
120
+ import "choices.js/public/assets/styles/choices.css";
121
+ ```
122
+
123
+ ## Setup
124
+
125
+ **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.
126
+
127
+ ```js
128
+ // Pass single element
129
+ const element = document.querySelector('.js-choice');
130
+ const choices = new Choices(element);
131
+
132
+ // Pass reference
133
+ const choices = new Choices('[data-trigger]');
134
+ const choices = new Choices('.js-choice');
135
+
136
+ // Pass jQuery element
137
+ const choices = new Choices($('.js-choice')[0]);
138
+
139
+ // Passing options (with default options)
140
+ const choices = new Choices(element, {
141
+ silent: false,
142
+ items: [],
143
+ choices: [],
144
+ renderChoiceLimit: -1,
145
+ maxItemCount: -1,
146
+ closeDropdownOnSelect: 'auto',
147
+ singleModeForMultiSelect: false,
148
+ addChoices: false,
149
+ addItems: true,
150
+ addItemFilter: (value) => !!value && value !== '',
151
+ removeItems: true,
152
+ removeItemButton: false,
153
+ removeItemButtonAlignLeft: false,
154
+ editItems: false,
155
+ allowHTML: false,
156
+ allowHtmlUserInput: false,
157
+ duplicateItemsAllowed: true,
158
+ delimiter: ',',
159
+ paste: true,
160
+ searchEnabled: true,
161
+ searchChoices: true,
162
+ searchFloor: 1,
163
+ searchResultLimit: 4,
164
+ searchFields: ['label', 'value'],
165
+ position: 'auto',
166
+ resetScrollPosition: true,
167
+ shouldSort: true,
168
+ shouldSortItems: false,
169
+ sorter: () => {...},
170
+ shadowRoot: null,
171
+ placeholder: true,
172
+ placeholderValue: null,
173
+ searchPlaceholderValue: null,
174
+ prependValue: null,
175
+ appendValue: null,
176
+ renderSelectedChoices: 'auto',
177
+ loadingText: 'Loading...',
178
+ noResultsText: 'No results found',
179
+ noChoicesText: 'No choices to choose from',
180
+ itemSelectText: 'Press to select',
181
+ uniqueItemText: 'Only unique values can be added',
182
+ customAddItemText: 'Only values matching specific conditions can be added',
183
+ addItemText: (value) => {
184
+ return `Press Enter to add <b>"${value}"</b>`;
185
+ },
186
+ removeItemIconText: () => `Remove item`,
187
+ removeItemLabelText: (value) => `Remove item: ${value}`,
188
+ maxItemText: (maxItemCount) => {
189
+ return `Only ${maxItemCount} values can be added`;
190
+ },
191
+ valueComparer: (value1, value2) => {
192
+ return value1 === value2;
193
+ },
194
+ classNames: {
195
+ containerOuter: ['choices'],
196
+ containerInner: ['choices__inner'],
197
+ input: ['choices__input'],
198
+ inputCloned: ['choices__input--cloned'],
199
+ list: ['choices__list'],
200
+ listItems: ['choices__list--multiple'],
201
+ listSingle: ['choices__list--single'],
202
+ listDropdown: ['choices__list--dropdown'],
203
+ item: ['choices__item'],
204
+ itemSelectable: ['choices__item--selectable'],
205
+ itemDisabled: ['choices__item--disabled'],
206
+ itemChoice: ['choices__item--choice'],
207
+ description: ['choices__description'],
208
+ placeholder: ['choices__placeholder'],
209
+ group: ['choices__group'],
210
+ groupHeading: ['choices__heading'],
211
+ button: ['choices__button'],
212
+ activeState: ['is-active'],
213
+ focusState: ['is-focused'],
214
+ openState: ['is-open'],
215
+ disabledState: ['is-disabled'],
216
+ highlightedState: ['is-highlighted'],
217
+ selectedState: ['is-selected'],
218
+ flippedState: ['is-flipped'],
219
+ loadingState: ['is-loading'],
220
+ notice: ['choices__notice'],
221
+ addChoice: ['choices__item--selectable', 'add-choice'],
222
+ noResults: ['has-no-results'],
223
+ noChoices: ['has-no-choices'],
224
+ },
225
+ // Choices uses the great Fuse library for searching. You
226
+ // can find more options here: https://fusejs.io/api/options.html
227
+ fuseOptions: {
228
+ includeScore: true
229
+ },
230
+ labelId: '',
231
+ callbackOnInit: null,
232
+ callbackOnCreateTemplates: null,
233
+ appendGroupInSearch: false,
234
+ });
235
+ ```
236
+
237
+ ## Terminology
238
+
239
+ | Word | Definition |
240
+ | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
241
+ | Choice | A choice is a value a user can select. A choice would be equivalent to the `<option></option>` element within a select input. |
242
+ | Group | A group is a collection of choices. A group should be seen as equivalent to a `<optgroup></optgroup>` element within a select input. |
243
+ | 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">` |
244
+
245
+ ## Input Types
246
+
247
+ Choices works with the following input types, referenced in the documentation as noted.
248
+
249
+ | HTML Element | Documentation "Input Type" |
250
+ | -------------------------------------------------------------------------------------------------------| -------------------------- |
251
+ | [`<input type="text">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input) | `text` |
252
+ | [`<select>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select) | `select-one` |
253
+ | [`<select multiple>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#attr-multiple) | `select-multiple` |
254
+
255
+ ## Configuration Options
256
+
257
+ ### silent
258
+
259
+ **Type:** `Boolean` **Default:** `false`
260
+
261
+ **Input types affected:** `text`, `select-one`, `select-multiple`
262
+
263
+ **Usage:** Optionally suppress console errors and warnings.
264
+
265
+ ### items
266
+
267
+ **Type:** `Array` **Default:** `[]`
268
+
269
+ **Input types affected:** `text`
270
+
271
+ **Usage:** Add pre-selected items (see terminology) to text input.
272
+
273
+ Pass an array of strings:
274
+
275
+ `['value 1', 'value 2', 'value 3']`
276
+
277
+ Pass an array of objects:
278
+
279
+ ```
280
+ [{
281
+ value: 'Value 1',
282
+ label: 'Label 1',
283
+ id: 1
284
+ },
285
+ {
286
+ value: 'Value 2',
287
+ label: 'Label 2',
288
+ id: 2,
289
+ customProperties: {
290
+ random: 'I am a custom property'
291
+ }
292
+ }]
293
+ ```
294
+
295
+ ### choices
296
+
297
+ **Type:** `Array` **Default:** `[]`
298
+
299
+ **Input types affected:** `select-one`, `select-multiple`
300
+
301
+ **Usage:** Add choices (see terminology) to select input.
302
+
303
+ Pass an array of objects:
304
+
305
+ ```
306
+ [{
307
+ value: 'Option 1',
308
+ label: 'Option 1',
309
+ selected: true,
310
+ disabled: false,
311
+ },
312
+ {
313
+ value: 'Option 2',
314
+ label: 'Option 2',
315
+ selected: false,
316
+ disabled: true,
317
+ customProperties: {
318
+ description: 'Custom description about Option 2',
319
+ random: 'Another random custom property'
320
+ },
321
+ },
322
+ {
323
+ label: 'Group 1',
324
+ choices: [{
325
+ value: 'Option 3',
326
+ label: 'Option 4',
327
+ selected: true,
328
+ disabled: false,
329
+ },
330
+ {
331
+ value: 'Option 2',
332
+ label: 'Option 2',
333
+ selected: false,
334
+ disabled: true,
335
+ customProperties: {
336
+ description: 'Custom description about Option 2',
337
+ random: 'Another random custom property'
338
+ }
339
+ }]
340
+ }]
341
+ ```
342
+
343
+ ### renderChoiceLimit
344
+
345
+ **Type:** `Number` **Default:** `-1`
346
+
347
+ **Input types affected:** `select-one`, `select-multiple`
348
+
349
+ **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.
350
+
351
+ ### maxItemCount
352
+
353
+ **Type:** `Number` **Default:** `-1`
354
+
355
+ **Input types affected:** `text`, `select-multiple`
356
+
357
+ **Usage:** The amount of items a user can input/select ("-1" indicates no limit).
358
+
359
+ ### closeDropdownOnSelect
360
+
361
+ **Type:** `Boolean` | 'auto' **Default:** `auto`
362
+
363
+ **Input types affected:** select-one, select-multiple
364
+
365
+ **Usage:** Control how the dropdown closes after making a selection for select-one or select-multiple.
366
+ - 'auto' defaults based on backing-element type:
367
+ - select-one: true
368
+ - select-multiple: false
369
+
370
+ ### singleModeForMultiSelect
371
+
372
+ **Type:** `Boolean` **Default:** `false`
373
+
374
+ **Input types affected:** select-one, select-multiple
375
+
376
+ **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.
377
+
378
+ ### addChoices
379
+ **Type**: `Boolean` **Default:** `false`
380
+
381
+ **Input types affected:** `select-multiple`, `select-one`
382
+
383
+ **Usage:** Whether a user can add choices dynamically.
384
+
385
+ **Note:** `addItems` must also be `true`
386
+
387
+ ### addItems
388
+
389
+ **Type:** `Boolean` **Default:** `true`
390
+
391
+ **Input types affected:** `text`
392
+
393
+ **Usage:** Whether a user can add items.
394
+
395
+ ### removeItems
396
+
397
+ **Type:** `Boolean` **Default:** `true`
398
+
399
+ **Input types affected:** `text`, `select-multiple`
400
+
401
+ **Usage:** Whether a user can remove items.
402
+
403
+ ### removeItemButton
404
+
405
+ **Type:** `Boolean` **Default:** `false`
406
+
407
+ **Input types affected:** `text`, `select-one`, `select-multiple`
408
+
409
+ **Usage:** Whether each item should have a remove button.
410
+
411
+ ### removeItemButtonAlignLeft
412
+
413
+ **Type:** `Boolean` **Default:** `false`
414
+
415
+ **Input types affected:** `text`, `select-one`, `select-multiple`
416
+
417
+ **Usage:** Align item remove button left vs right
418
+
419
+ ### editItems
420
+
421
+ **Type:** `Boolean` **Default:** `false`
422
+
423
+ **Input types affected:** `text`
424
+
425
+ **Usage:** Whether a user can edit items. An item's value can be edited by pressing the backspace.
426
+
427
+ ### allowHTML
428
+
429
+ **Type:** `Boolean` **Default:** `false`
430
+
431
+ **Input types affected:** `text`, `select-one`, `select-multiple`
432
+
433
+ **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.
434
+
435
+ ### allowHtmlUserInput
436
+
437
+ **Type:** `Boolean` **Default:** `false`
438
+
439
+ **Input types affected:** `text`, `select-one`, `select-multiple`
440
+
441
+ **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.
442
+
443
+ ### duplicateItemsAllowed
444
+
445
+ **Type:** `Boolean` **Default:** `true`
446
+
447
+ **Input types affected:** `text`, `select-multiple`, `select-one`
448
+
449
+ **Usage:** Whether duplicate inputted/chosen items are allowed
450
+
451
+ ### delimiter
452
+
453
+ **Type:** `String` **Default:** `,`
454
+
455
+ **Input types affected:** `text`
456
+
457
+ **Usage:** What divides each value. The default delimiter separates each value with a comma: `"Value 1, Value 2, Value 3"`.
458
+
459
+ ### paste
460
+
461
+ **Type:** `Boolean` **Default:** `true`
462
+
463
+ **Input types affected:** `text`, `select-multiple`
464
+
465
+ **Usage:** Whether a user can paste into the input.
466
+
467
+ ### searchEnabled
468
+
469
+ **Type:** `Boolean` **Default:** `true`
470
+
471
+ **Input types affected:** `select-one`, `select-multiple`
472
+
473
+ **Usage:** Whether a search area should be shown.
474
+
475
+ ### searchChoices
476
+
477
+ **Type:** `Boolean` **Default:** `true`
478
+
479
+ **Input types affected:** `select-one`
480
+
481
+ **Usage:** Whether choices should be filtered by input or not. If `false`, the search event will still emit, but choices will not be filtered.
482
+
483
+ ### searchFields
484
+
485
+ **Type:** `Array/String` **Default:** `['label', 'value']`
486
+
487
+ **Input types affected:**`select-one`, `select-multiple`
488
+
489
+ **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']`.
490
+
491
+ ### searchFloor
492
+
493
+ **Type:** `Number` **Default:** `1`
494
+
495
+ **Input types affected:** `select-one`, `select-multiple`
496
+
497
+ **Usage:** The minimum length a search value should be before choices are searched.
498
+
499
+ ### searchResultLimit: 4,
500
+
501
+ **Type:** `Number` **Default:** `4`
502
+
503
+ **Input types affected:** `select-one`, `select-multiple`
504
+
505
+ **Usage:** The maximum amount of search results to show ("-1" indicates no limit).
506
+
507
+ ### shadowRoot
508
+
509
+ **Type:** Document Element **Default:** null
510
+
511
+ **Input types affected:** `select-one`, `select-multiple`
512
+
513
+ **Usage:** You can pass along the shadowRoot from your application like so.
514
+
515
+ ```js
516
+ var shadowRoot = document
517
+ .getElementById('wrapper')
518
+ .attachShadow({ mode: 'open' });
519
+ ...
520
+ var el = shadowRoot.querySelector(...);
521
+ var choices = new Choices(el, {
522
+ shadowRoot: shadowRoot,
523
+ });
524
+ ```
525
+
526
+ ### position
527
+
528
+ **Type:** `String` **Default:** `auto`
529
+
530
+ **Input types affected:** `select-one`, `select-multiple`
531
+
532
+ **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.
533
+
534
+ ### resetScrollPosition
535
+
536
+ **Type:** `Boolean` **Default:** `true`
537
+
538
+ **Input types affected:** `select-multiple`
539
+
540
+ **Usage:** Whether the scroll position should reset after adding an item.
541
+
542
+ ### addItemFilter
543
+
544
+ **Type:** `string | RegExp | Function` **Default:** `null`
545
+
546
+ **Input types affected:** `text`
547
+
548
+ **Usage:** A RegExp or string (will be passed to RegExp constructor internally) or filter function that will need to return `true` for a user to successfully add an item.
549
+
550
+ **Example:**
551
+
552
+ ```js
553
+ // Only adds items matching the text test
554
+ new Choices(element, {
555
+ addItemFilter: (value) => {
556
+ return ['orange', 'apple', 'banana'].includes(value);
557
+ };
558
+ });
559
+
560
+ // only items ending to `-red`
561
+ new Choices(element, {
562
+ addItemFilter: '-red$';
563
+ });
564
+
565
+ ```
566
+
567
+ ### shouldSort
568
+
569
+ **Type:** `Boolean` **Default:** `true`
570
+
571
+ **Input types affected:** `select-one`, `select-multiple`
572
+
573
+ **Usage:** Whether choices and groups should be sorted. If false, choices/groups will appear in the order they were given.
574
+
575
+ ### shouldSortItems
576
+
577
+ **Type:** `Boolean` **Default:** `false`
578
+
579
+ **Input types affected:** `text`, `select-multiple`
580
+
581
+ **Usage:** Whether items should be sorted. If false, items will appear in the order they were selected.
582
+
583
+ ### sorter
584
+
585
+ **Type:** `Function` **Default:** sortByAlpha
586
+
587
+ **Input types affected:** `select-one`, `select-multiple`
588
+
589
+ **Usage:** The function that will sort choices and items before they are displayed (unless a user is searching). By default choices and items are sorted by alphabetical order.
590
+
591
+ **Example:**
592
+
593
+ ```js
594
+ // Sorting via length of label from largest to smallest
595
+ const example = new Choices(element, {
596
+ sorter: function(a, b) {
597
+ return b.label.length - a.label.length;
598
+ },
599
+ };
600
+ ```
601
+
602
+ ### placeholder
603
+
604
+ **Type:** `Boolean` **Default:** `true`
605
+
606
+ **Input types affected:** `text`
607
+
608
+ **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.
609
+
610
+ **Note:** For select boxes, the recommended way of adding a placeholder is as follows:
611
+
612
+ ```html
613
+ <select data-placeholder="This is a placeholder">
614
+ <option>...</option>
615
+ <option>...</option>
616
+ <option>...</option>
617
+ </select>
618
+ ```
619
+
620
+ For backward compatibility, `<option value="">This is a placeholder</option>` and `<option placeholder>This is a placeholder</option>` are also supported.
621
+
622
+ ### placeholderValue
623
+
624
+ **Type:** `String` **Default:** `null`
625
+
626
+ **Input types affected:** `text`
627
+
628
+ **Usage:** The value of the inputs placeholder.
629
+
630
+ ### searchPlaceholderValue
631
+
632
+ **Type:** `String` **Default:** `null`
633
+
634
+ **Input types affected:** `select-one`
635
+
636
+ **Usage:** The value of the search inputs placeholder.
637
+
638
+ ### prependValue
639
+
640
+ **Type:** `String` **Default:** `null`
641
+
642
+ **Input types affected:** `text`, `select-one`, `select-multiple`
643
+
644
+ **Usage:** Prepend a value to each item added/selected.
645
+
646
+ ### appendValue
647
+
648
+ **Type:** `String` **Default:** `null`
649
+
650
+ **Input types affected:** `text`, `select-one`, `select-multiple`
651
+
652
+ **Usage:** Append a value to each item added/selected.
653
+
654
+ ### renderSelectedChoices
655
+
656
+ **Type:** `String` **Default:** `auto`
657
+
658
+ **Input types affected:** `select-multiple`
659
+
660
+ **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`.
661
+
662
+ ### loadingText
663
+
664
+ **Type:** `String` **Default:** `Loading...`
665
+
666
+ **Input types affected:** `select-one`, `select-multiple`
667
+
668
+ **Usage:** The text that is shown whilst choices are being populated via AJAX.
669
+
670
+ ### noResultsText
671
+
672
+ **Type:** `String/Function` **Default:** `No results found`
673
+
674
+ **Input types affected:** `select-one`, `select-multiple`
675
+
676
+ **Usage:** The text that is shown when a user's search has returned no results. Optionally pass a function returning a string.
677
+
678
+ ### noChoicesText
679
+
680
+ **Type:** `String/Function` **Default:** `No choices to choose from`
681
+
682
+ **Input types affected:** `select-multiple`, `select-one`
683
+
684
+ **Usage:** The text that is shown when a user has selected all possible choices, or no choices exist. Optionally pass a function returning a string.
685
+
686
+ ### itemSelectText
687
+
688
+ **Type:** `String` **Default:** `Press to select`
689
+
690
+ **Input types affected:** `select-multiple`, `select-one`
691
+
692
+ **Usage:** The text that is shown when a user hovers over a selectable choice. Set to empty to not reserve space for this text.
693
+
694
+ ### addItemText
695
+
696
+ **Type:** `String/Function` **Default:** `Press Enter to add "${value}"`
697
+
698
+ **Input types affected:** `text`, `select-one`, `select-multiple`
699
+
700
+ **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.
701
+
702
+ Return type must be safe to insert into HTML (ie use the 1st argument which is sanitised)
703
+
704
+ ### removeItemIconText
705
+
706
+ **Type:** `String/Function` **Default:** `Remove item"`
707
+
708
+ **Input types affected:** `text`, `select-one`, `select-multiple`
709
+
710
+ **Usage:** The text/icon for the remove button. To access the item's 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.
711
+
712
+ Return type must be safe to insert into HTML (ie use the 1st argument which is sanitised)
713
+
714
+ ### removeItemLabelText
715
+
716
+ **Type:** `String/Function` **Default:** `Remove item: ${value}"`
717
+
718
+ **Input types affected:** `text`, `select-one`, `select-multiple`
719
+
720
+ **Usage:** The text for the remove button's aria label. To access the item's 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.
721
+
722
+ Return type must be safe to insert into HTML (ie use the 1st argument which is sanitised)
723
+
724
+ ### maxItemText
725
+
726
+ **Type:** `String/Function` **Default:** `Only ${maxItemCount} values can be added`
727
+
728
+ **Input types affected:** `text`
729
+
730
+ **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.
731
+
732
+ ### valueComparer
733
+
734
+ **Type:** `Function` **Default:** `strict equality`
735
+
736
+ **Input types affected:** `select-one`, `select-multiple`
737
+
738
+ **Usage:** A custom compare function used when finding choices by value (using `setChoiceByValue`).
739
+
740
+ **Example:**
741
+
742
+ ```js
743
+ const example = new Choices(element, {
744
+ valueComparer: (a, b) => value.trim() === b.trim(),
745
+ };
746
+ ```
747
+
748
+ ### labelId
749
+
750
+ **Type:** `String` **Default:** ``
751
+
752
+ **Input types affected:** `select-one`, `select-multiple`
753
+
754
+ **Usage:** The labelId improves accessibility. If set, it will add aria-labelledby to the choices element.
755
+
756
+ ### classNames
757
+
758
+ **Type:** `Object` **Default:**
759
+
760
+ ```
761
+ classNames: {
762
+ containerOuter: ['choices'],
763
+ containerInner: ['choices__inner'],
764
+ input: ['choices__input'],
765
+ inputCloned: ['choices__input--cloned'],
766
+ list: ['choices__list'],
767
+ listItems: ['choices__list--multiple'],
768
+ listSingle: ['choices__list--single'],
769
+ listDropdown: ['choices__list--dropdown'],
770
+ item: ['choices__item'],
771
+ itemSelectable: ['choices__item--selectable'],
772
+ itemDisabled: ['choices__item--disabled'],
773
+ itemChoice: ['choices__item--choice'],
774
+ description: ['choices__description'],
775
+ placeholder: ['choices__placeholder'],
776
+ group: ['choices__group'],
777
+ groupHeading: ['choices__heading'],
778
+ button: ['choices__button'],
779
+ activeState: ['is-active'],
780
+ focusState: ['is-focused'],
781
+ openState: ['is-open'],
782
+ disabledState: ['is-disabled'],
783
+ highlightedState: ['is-highlighted'],
784
+ selectedState: ['is-selected'],
785
+ flippedState: ['is-flipped'],
786
+ loadingState: ['is-loading'],
787
+ notice: ['choices__notice'],
788
+ addChoice: ['choices__item--selectable', 'add-choice'],
789
+ noResults: ['has-no-results'],
790
+ noChoices: ['has-no-choices'],
791
+ }
792
+ ```
793
+
794
+ **Input types affected:** `text`, `select-one`, `select-multiple`
795
+
796
+ **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.
797
+
798
+ ## Callbacks
799
+
800
+ **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`).
801
+
802
+ ### callbackOnInit
803
+
804
+ **Type:** `Function` **Default:** `null`
805
+
806
+ **Input types affected:** `text`, `select-one`, `select-multiple`
807
+
808
+ **Usage:** Function to run once Choices initialises.
809
+
810
+ ### callbackOnCreateTemplates(strToEl: (str: string) => HTMLElement, escapeForTemplate: (allowHTML: boolean, s: StringUntrusted | StringPreEscaped | string) => string, getClassNames: (s: Array<string> | string) => string)
811
+
812
+ **Type:** `Function` **Default:** `null` **Arguments:** `strToEl`, `escapeForTemplate`
813
+
814
+ **Input types affected:** `text`, `select-one`, `select-multiple`
815
+
816
+ **Usage:** Function to run on template creation. Through this callback it is possible to provide custom templates for the various components of Choices (see terminology). For Choices to work with custom templates, it is important you maintain the various data attributes defined [here](https://github.com/Choices-js/Choices/blob/master/src/scripts/templates.ts).
817
+ If you want just extend a little original template then you may use `Choices.defaults.templates` to get access to
818
+ original template function.
819
+
820
+ Templates receive the full Choices config as the first argument to any template, which allows you to conditionally display things based on the options specified.
821
+
822
+ @note For each callback, `this` refers to the current instance of Choices. This can be useful if you need access to methods `(this.disable())`.
823
+
824
+ **Example:**
825
+
826
+ ```js
827
+ const example = new Choices(element, {
828
+ callbackOnCreateTemplates: (strToEl, escapeForTemplate, getClassNames) => ({
829
+ input: (...args) =>
830
+ Object.assign(Choices.defaults.templates.input.call(this, ...args), {
831
+ type: 'email',
832
+ }),
833
+ }),
834
+ });
835
+ ```
836
+
837
+ or more complex:
838
+
839
+ ```js
840
+ const example = new Choices(element, {
841
+ callbackOnCreateTemplates: function(strToEl, escapeForTemplate, getClassNames) {
842
+ return {
843
+ item: ({ classNames }, data) => {
844
+ return template(`
845
+ <div class="${getClassNames(classNames.item).join(' ')} ${
846
+ getClassNames(data.highlighted
847
+ ? classNames.highlightedState
848
+ : classNames.itemSelectable).join(' ')
849
+ } ${
850
+ data.placeholder ? classNames.placeholder : ''
851
+ }" data-item data-id="${data.id}" data-value="${escapeForTemplate(data.value)}" ${
852
+ data.active ? 'aria-selected="true"' : ''
853
+ } ${data.disabled ? 'aria-disabled="true"' : ''}>
854
+ <span>&bigstar;</span> ${escapeForTemplate(data.label)}
855
+ </div>
856
+ `);
857
+ },
858
+ choice: ({ classNames }, data) => {
859
+ return template(`
860
+ <div class="${getClassNames(classNames.item).join(' ')} ${getClassNames(classNames.itemChoice).join(' ')} ${
861
+ getClassNames(data.disabled ? classNames.itemDisabled : classNames.itemSelectable).join(' ')
862
+ }" data-select-text="${this.config.itemSelectText}" data-choice ${
863
+ data.disabled
864
+ ? 'data-choice-disabled aria-disabled="true"'
865
+ : 'data-choice-selectable'
866
+ } data-id="${data.id}" data-value="${escapeForTemplate(data.value)}" ${
867
+ data.groupId > 0 ? 'role="treeitem"' : 'role="option"'
868
+ }>
869
+ <span>&bigstar;</span> ${escapeForTemplate(data.label)}
870
+ </div>
871
+ `);
872
+ },
873
+ };
874
+ },
875
+ });
876
+ ```
877
+
878
+ ## Events
879
+
880
+ **Note:** Events fired by Choices behave the same as standard events. Each event is triggered on the element passed to Choices (accessible via `this.passedElement`. Arguments are accessible within the `event.detail` object.
881
+
882
+ **Example:**
883
+
884
+ ```js
885
+ const element = document.getElementById('example');
886
+ const example = new Choices(element);
887
+
888
+ element.addEventListener(
889
+ 'addItem',
890
+ function(event) {
891
+ // do something creative here...
892
+ console.log(event.detail.id);
893
+ console.log(event.detail.value);
894
+ console.log(event.detail.label);
895
+ console.log(event.detail.customProperties);
896
+ console.log(event.detail.groupValue);
897
+ },
898
+ false,
899
+ );
900
+
901
+ // or
902
+ const example = new Choices(document.getElementById('example'));
903
+
904
+ example.passedElement.element.addEventListener(
905
+ 'addItem',
906
+ function(event) {
907
+ // do something creative here...
908
+ console.log(event.detail.id);
909
+ console.log(event.detail.value);
910
+ console.log(event.detail.label);
911
+ console.log(event.detail.customProperties);
912
+ console.log(event.detail.groupValue);
913
+ },
914
+ false,
915
+ );
916
+ ```
917
+
918
+ ### addItem
919
+
920
+ **Payload:** `id, highlighted, labelClass, labelDescription, customProperties, disabled, active, label, placeholder, value, groupValue, element, keyCode`
921
+
922
+ **Input types affected:** `text`, `select-one`, `select-multiple`
923
+
924
+ **Usage:** Triggered each time an item is added (programmatically or by the user).
925
+
926
+ ### removeItem
927
+
928
+ **Payload:** `id, highlighted, labelClass, labelDescription, customProperties, disabled, active, label, placeholder, value, groupValue, element, keyCode`
929
+
930
+ **Input types affected:** `text`, `select-one`, `select-multiple`
931
+
932
+ **Usage:** Triggered each time an item is removed (programmatically or by the user).
933
+
934
+ ### highlightItem
935
+
936
+ **Payload:** `id, highlighted, labelClass, labelDescription, customProperties, disabled, active, label, placeholder, value, groupValue, element, keyCode`
937
+
938
+ **Input types affected:** `text`, `select-multiple`
939
+
940
+ **Usage:** Triggered each time an item is highlighted.
941
+
942
+ ### unhighlightItem
943
+
944
+ **Payload:** `id, highlighted, labelClass, labelDescription, customProperties, disabled, active, label, placeholder, value, groupValue, element, keyCode`
945
+
946
+ **Input types affected:** `text`, `select-multiple`
947
+
948
+ **Usage:** Triggered each time an item is unhighlighted.
949
+
950
+ ### choice
951
+
952
+ **Payload:** `id, highlighted, labelClass, labelDescription, customProperties, disabled, active, label, placeholder, value, groupValue, element, keyCode`
953
+
954
+ **Input types affected:** `select-one`, `select-multiple`
955
+
956
+ **Usage:** Triggered each time a choice is selected **by a user**, regardless if it changes the value of the input.
957
+ `choice` is a Choice object here (see terminology or typings file)
958
+
959
+ ### change
960
+
961
+ **Payload:** `value: string`
962
+
963
+ **Input types affected:** `text`, `select-one`, `select-multiple`
964
+
965
+ **Usage:** Triggered each time an item is added/removed **by a user**.
966
+
967
+ ### search
968
+
969
+ **Payload:** `value: string`, `resultCount: number`
970
+
971
+ **Input types affected:** `select-one`, `select-multiple`
972
+
973
+ **Usage:** Triggered when a user types into an input to search choices. When a search is ended, a search event with an empty value with no resultCount is triggered.
974
+
975
+ ### showDropdown
976
+
977
+ **Payload:** -
978
+
979
+ **Input types affected:** `select-one`, `select-multiple`
980
+
981
+ **Usage:** Triggered when the dropdown is shown.
982
+
983
+ ### hideDropdown
984
+
985
+ **Payload:** -
986
+
987
+ **Input types affected:** `select-one`, `select-multiple`
988
+
989
+ **Usage:** Triggered when the dropdown is hidden.
990
+
991
+ ### highlightChoice
992
+
993
+ **Payload:** `el`
994
+
995
+ **Input types affected:** `select-one`, `select-multiple`
996
+
997
+ **Usage:** Triggered when a choice from the dropdown is highlighted.
998
+ The `el` argument is choices.passedElement object that was affected.
999
+
1000
+ ## Methods
1001
+
1002
+ Methods can be called either directly or by chaining:
1003
+
1004
+ ```js
1005
+ // Calling a method by chaining
1006
+ const choices = new Choices(element, {
1007
+ addItems: false,
1008
+ removeItems: false,
1009
+ })
1010
+ .setValue(['Set value 1', 'Set value 2'])
1011
+ .disable();
1012
+
1013
+ // Calling a method directly
1014
+ const choices = new Choices(element, {
1015
+ addItems: false,
1016
+ removeItems: false,
1017
+ });
1018
+
1019
+ choices.setValue(['Set value 1', 'Set value 2']);
1020
+ choices.disable();
1021
+ ```
1022
+
1023
+ ### destroy();
1024
+
1025
+ **Input types affected:** `text`, `select-multiple`, `select-one`
1026
+
1027
+ **Usage:** Kills the instance of Choices, removes all event listeners and returns passed input to its initial state.
1028
+
1029
+ ### init();
1030
+
1031
+ **Input types affected:** `text`, `select-multiple`, `select-one`
1032
+
1033
+ **Usage:** Creates a new instance of Choices, adds event listeners, creates templates and renders a Choices element to the DOM.
1034
+
1035
+ **Note:** This is called implicitly when a new instance of Choices is created. This would be used after a Choices instance had already been destroyed (using `destroy()`).
1036
+
1037
+ ### refresh(withEvents: boolean = false, selectFirstOption: boolean = false);
1038
+
1039
+ **Input types affected:** `select-multiple`, `select-one`
1040
+
1041
+ **Usage:** Reads options from backing `<select>` element, and recreates choices. Existing items are preserved. When `withEvents` is truthy, only `addItem` events are generated.
1042
+
1043
+ ### highlightAll();
1044
+
1045
+ **Input types affected:** `text`, `select-multiple`
1046
+
1047
+ **Usage:** Highlight each chosen item (selected items can be removed).
1048
+
1049
+ ### unhighlightAll();
1050
+
1051
+ **Input types affected:** `text`, `select-multiple`
1052
+
1053
+ **Usage:** Un-highlight each chosen item.
1054
+
1055
+ ### removeActiveItemsByValue(value: string);
1056
+
1057
+ **Input types affected:** `text`, `select-multiple`
1058
+
1059
+ **Usage:** Remove each item by a given value.
1060
+
1061
+ ### removeActiveItems(excludedId?: number);
1062
+
1063
+ **Input types affected:** `text`, `select-multiple`
1064
+
1065
+ **Usage:** Remove each selectable item.
1066
+
1067
+ ## removeChoice(value: string);
1068
+
1069
+ **Input types affected:** `text`, `select-multiple`, `select-one`
1070
+
1071
+ **Usage:** Remove an option/item by value
1072
+
1073
+ ### removeHighlightedItems(runEvent?: boolean);
1074
+
1075
+ **Input types affected:** `text`, `select-multiple`
1076
+
1077
+ **Usage:** Remove each item the user has selected.
1078
+
1079
+ ### showDropdown(preventInputFocus?: boolean);
1080
+
1081
+ **Input types affected:** `select-one`, `select-multiple`
1082
+
1083
+ **Usage:** Show choices list dropdown.
1084
+
1085
+ ### hideDropdown(preventInputFocus?: boolean);
1086
+
1087
+ **Input types affected:** ``select-one`, `select-multiple`
1088
+
1089
+ **Usage:** Hide choices list dropdown.
1090
+
1091
+ ### setChoices(choicesArrayOrFetcher?: (InputChoice | InputGroup)[] | ((instance: Choices) => (InputChoice | InputGroup)[] | Promise<(InputChoice | InputGroup)[]>), value?: string | null, label?: string, replaceChoices?: boolean = false, clearSearchFlag?: boolean = false, replaceItems?: boolean = false): this | Promise<this>;
1092
+
1093
+ **Input types affected:** `select-one`, `select-multiple`
1094
+
1095
+ **Usage:** Set choices of select input via an array of objects (or function that returns array of object or promise of it), a value field name and a label field name.
1096
+
1097
+ This behaves the similar as passing items via the `choices` option but can be called after initialising Choices. This can also be used to add groups of choices (see example 3); Optionally pass a true `replaceChoices` value to remove any existing choices. Optionally pass a true `replaceItems` value to remove any items, if false choices for selected items are preserved. Optionally pass a `customProperties` object to add additional data to your choices (useful when searching/filtering etc). Passing an empty array as the first parameter, and a true `replaceChoices` is the same as calling `clearChoices` (see below).
1098
+
1099
+ **Example 1:**
1100
+
1101
+ ```js
1102
+ const example = new Choices(element);
1103
+
1104
+ example.setChoices(
1105
+ [
1106
+ { value: 'One', label: 'Label One', disabled: true },
1107
+ { value: 'Two', label: 'Label Two', selected: true },
1108
+ { value: 'Three', label: 'Label Three' },
1109
+ ],
1110
+ 'value',
1111
+ 'label',
1112
+ false,
1113
+ );
1114
+ ```
1115
+
1116
+ **Example 2:**
1117
+
1118
+ ```js
1119
+ const example = new Choices(element);
1120
+
1121
+ // Passing a function that returns Promise of choices
1122
+ example.setChoices(async () => {
1123
+ try {
1124
+ const items = await fetch('/items');
1125
+ return items.json();
1126
+ } catch (err) {
1127
+ console.error(err);
1128
+ }
1129
+ });
1130
+ ```
1131
+
1132
+ **Example 3:**
1133
+
1134
+ ```js
1135
+ const example = new Choices(element);
1136
+
1137
+ example.setChoices(
1138
+ [
1139
+ {
1140
+ label: 'Group one',
1141
+ disabled: false,
1142
+ choices: [
1143
+ { value: 'Child One', label: 'Child One', selected: true },
1144
+ { value: 'Child Two', label: 'Child Two', disabled: true },
1145
+ { value: 'Child Three', label: 'Child Three' },
1146
+ ],
1147
+ },
1148
+ {
1149
+ label: 'Group two',
1150
+ disabled: false,
1151
+ choices: [
1152
+ { value: 'Child Four', label: 'Child Four', disabled: true },
1153
+ { value: 'Child Five', label: 'Child Five' },
1154
+ {
1155
+ value: 'Child Six',
1156
+ label: 'Child Six',
1157
+ customProperties: {
1158
+ description: 'Custom description about child six',
1159
+ random: 'Another random custom property',
1160
+ },
1161
+ },
1162
+ ],
1163
+ },
1164
+ ],
1165
+ 'value',
1166
+ 'label',
1167
+ false,
1168
+ );
1169
+ ```
1170
+
1171
+ ### clearChoices(clearOptions: boolean = true, clearItems: boolean = false);
1172
+
1173
+ **Input types affected:** `select-one`, `select-multiple`
1174
+
1175
+ **Usage:** Clear all choices from select including any selected items. Does **not** reset the search state.
1176
+ - `clearOptions` If true, clears the backing options from the `<select>` element
1177
+ - `clearItems` If false, preserves selected items instead of clearing them
1178
+
1179
+ ### getValue(valueOnly?: boolean): string[] | EventChoice[] | EventChoice | string;
1180
+
1181
+ **Input types affected:** `text`, `select-one`, `select-multiple`
1182
+
1183
+ **Usage:** Get value(s) of input (i.e. inputted items (text) or selected choices (select)). Optionally pass an argument of `true` to only return values rather than value objects.
1184
+
1185
+ **Example:**
1186
+
1187
+ ```js
1188
+ const example = new Choices(element);
1189
+ const values = example.getValue(true); // returns ['value 1', 'value 2'];
1190
+ const valueArray = example.getValue(); // returns [{ active: true, choiceId: 1, highlighted: false, id: 1, label: 'Label 1', value: 'Value 1'}, { active: true, choiceId: 2, highlighted: false, id: 2, label: 'Label 2', value: 'Value 2'}];
1191
+ ```
1192
+
1193
+ ### setValue(items: string[] | InputChoice[]): this;
1194
+
1195
+ **Input types affected:** `text`, `select-one`, `select-multiple`
1196
+
1197
+ **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.
1198
+
1199
+ **Example:**
1200
+
1201
+ ```js
1202
+ const example = new Choices(element);
1203
+
1204
+ // via an array of objects
1205
+ example.setValue([
1206
+ { value: 'One', label: 'Label One' },
1207
+ { value: 'Two', label: 'Label Two' },
1208
+ { value: 'Three', label: 'Label Three' },
1209
+ ]);
1210
+
1211
+ // or via an array of strings
1212
+ example.setValue(['Four', 'Five', 'Six']);
1213
+ ```
1214
+
1215
+ ### setChoiceByValue(value: string | string[]);
1216
+
1217
+ **Input types affected:** `select-one`, `select-multiple`
1218
+
1219
+ **Usage:** Set value of input based on existing Choice. `value` can be either a single string or an array of strings
1220
+
1221
+ **Example:**
1222
+
1223
+ ```js
1224
+ const example = new Choices(element, {
1225
+ choices: [
1226
+ { value: 'One', label: 'Label One' },
1227
+ { value: 'Two', label: 'Label Two', disabled: true },
1228
+ { value: 'Three', label: 'Label Three' },
1229
+ ],
1230
+ });
1231
+
1232
+ example.setChoiceByValue('Two'); // Choice with value of 'Two' has now been selected.
1233
+ ```
1234
+
1235
+ ### clearStore();
1236
+
1237
+ **Input types affected:** `text`, `select-one`, `select-multiple`
1238
+
1239
+ **Usage:** Removes all items, choices and groups. Resets the search state. Use with caution.
1240
+
1241
+ ### clearInput();
1242
+
1243
+ **Input types affected:** `text`
1244
+
1245
+ **Usage:** Clear input of any user inputted text.
1246
+
1247
+ ### disable();
1248
+
1249
+ **Input types affected:** `text`, `select-one`, `select-multiple`
1250
+
1251
+ **Usage:** Disables input from accepting new value/selecting further choices.
1252
+
1253
+ ### enable();
1254
+
1255
+ **Input types affected:** `text`, `select-one`, `select-multiple`
1256
+
1257
+ **Usage:** Enables input to accept new values/select further choices.
1258
+
1259
+ ## Browser compatibility
1260
+
1261
+ Choices is compiled using [Babel](https://babeljs.io/) targeting browsers [with more than 1% of global usage](https://github.com/jshjohnson/Choices/blob/master/.browserslistrc) and expecting that features [listed below](https://github.com/jshjohnson/Choices/blob/master/.eslintrc.json#L62) are available or polyfilled in browser.
1262
+ You may see exact list of target browsers by running `npm exec browserslist` within this repository folder.
1263
+ If you need to support a browser that does not have one of the features listed below,
1264
+ I suggest including a polyfill from [cdnjs.cloudflare.com/polyfill](https://cdnjs.cloudflare.com/polyfill):
1265
+
1266
+ **Polyfill example used for the demo:**
1267
+
1268
+ ```html
1269
+ <script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?version=4.8.0&features=Array.from%2CArray.prototype.find%2CArray.prototype.includes%2CSymbol%2CSymbol.iterator%2CDOMTokenList%2CObject.assign%2CCustomEvent%2CElement.prototype.classList%2CElement.prototype.closest%2CElement.prototype.dataset%2CElement.prototype.replaceChildren"></script>
1270
+ ```
1271
+
1272
+ **Features used in Choices:**
1273
+
1274
+ ```polyfills
1275
+ Array.from
1276
+ Array.prototype.find
1277
+ Array.prototype.includes
1278
+ Symbol
1279
+ Symbol.iterator
1280
+ DOMTokenList
1281
+ Object.assign
1282
+ CustomEvent
1283
+ Element.prototype.classList
1284
+ Element.prototype.closest
1285
+ Element.prototype.dataset
1286
+ Element.prototype.replaceChildren
1287
+ ```
1288
+
1289
+ ## Development
1290
+
1291
+ To setup a local environment: clone this repo, navigate into its directory in a terminal window and run the following command:
1292
+
1293
+ `npm install`
1294
+
1295
+ ### playwright
1296
+
1297
+ e2e (End-to-end) tests are implemented using playwright, which requires installing likely with OS support.
1298
+
1299
+ `npx playwright install`
1300
+ `npx playwright install-deps `
1301
+
1302
+ For JetBrain IDE's the `Test automation` plugin is recommended:
1303
+ https://plugins.jetbrains.com/plugin/20175-test-automation
1304
+ For usage see:
1305
+ https://www.jetbrains.com/help/phpstorm/playwright.html
1306
+
1307
+ ### NPM tasks
1308
+
1309
+ | Task | Usage |
1310
+ |---------------------------|--------------------------------------------------------------|
1311
+ | `npm run start` | Fire up local server for development |
1312
+ | `npm run test:unit` | Run sequence of tests once |
1313
+ | `npm run test:unit:watch` | Fire up test server and re-test on file change |
1314
+ | `npm run test:e2e` | Run sequence of e2e tests (with local server) |
1315
+ | `npm run test` | Run both unit and e2e tests |
1316
+ | `npm run playwright:gui` | Run Playwright e2e tests (GUI) |
1317
+ | `npm run playwright:cli` | Run Playwright e2e tests (CLI) |
1318
+ | `npm run js:build` | Compile Choices to an uglified JavaScript file |
1319
+ | `npm run css:watch` | Watch SCSS files for changes. On a change, run build process |
1320
+ | `npm run css:build` | Compile, minify and prefix SCSS files to CSS |
1321
+
1322
+
1323
+ ### Build flags
1324
+
1325
+ Choices supports various environment variables as build-flags to enable/disable features.
1326
+ The pre-built bundles these features set, and tree shaking uses the non-used parts.
1327
+
1328
+ #### CHOICES_SEARCH_FUSE
1329
+ **Values:** `full` / `basic` / `null`
1330
+ **Default:** `full`
1331
+
1332
+ Fuse.js support a `full`/`basic` profile. `full` adds additional logic operations, which aren't used by default with Choices. The `null` option drops Fuse.js as a dependency and instead uses a simple prefix only search feature.
1333
+
1334
+ #### CHOICES_SEARCH_KMP
1335
+ **Values:** `1` / `0`
1336
+ **Default:** `0`
1337
+
1338
+ If `CHOICES_SEARCH_FUSE` is `null`, this enables an `indexOf`-like [Knuth–Morris–Pratt algorithm](https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm). Useful for very large data sets, without fuzzy searching.
1339
+
1340
+ #### CHOICES_CAN_USE_DOM
1341
+ **Values:** `1` / `0`
1342
+ **Default:** `1`
1343
+
1344
+ Allows loading Choices into a non-browser environment.
1345
+
1346
+ ### Interested in contributing?
1347
+
1348
+ We're always interested in having more active maintainers. Please get in touch if you're interested 👍
1349
+
1350
+ ## License
1351
+
1352
+ MIT License
1353
+
1354
+ ## Web component
1355
+
1356
+ Want to use Choices as a web component? You're in luck. Adidas have built one for their design system which can be found [here](https://github.com/adidas/choicesjs-stencil).
1357
+
1358
+ ## Misc
1359
+
1360
+ Thanks to [@mikefrancis](https://github.com/mikefrancis/) for [sending me on a hunt](https://twitter.com/_mikefrancis/status/701797835826667520) for a non-jQuery solution for select boxes that eventually led to this being built!