@financial-times/o-autocomplete 2.1.0 → 2.3.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.
- package/CHANGELOG.md +19 -0
- package/README.md +1 -37
- package/package.json +1 -1
- package/src/js/autocomplete.js +32 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.3.0](https://github.com/Financial-Times/origami/compare/o-autocomplete-v2.2.0...o-autocomplete-v2.3.0) (2026-01-15)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* Add o-autocomplete confirmOnBlur option ([#2322](https://github.com/Financial-Times/origami/issues/2322)) ([ba27a93](https://github.com/Financial-Times/origami/commit/ba27a93fa1c91ce2f8f1d3897095b88ed34ef6cc))
|
|
9
|
+
|
|
10
|
+
## [2.2.0](https://github.com/Financial-Times/origami/compare/o-autocomplete-v2.1.0...o-autocomplete-v2.2.0) (2026-01-12)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* Add o-autocomplete showNoOptionsFound option ([a869a04](https://github.com/Financial-Times/origami/commit/a869a04ffc045d526bb1facf7c89215c4933754f))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* Add o-autocomplete clear button post-click logic ([67f4fed](https://github.com/Financial-Times/origami/commit/67f4fed14380e8e7b12d3d36156604e0a51e6446))
|
|
21
|
+
|
|
3
22
|
## [2.1.0](https://github.com/Financial-Times/origami/compare/o-autocomplete-v2.0.0...o-autocomplete-v2.1.0) (2025-11-13)
|
|
4
23
|
|
|
5
24
|
|
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ An Origami component for autocomplete inputs. This is built on top of the excell
|
|
|
11
11
|
- [JavaScript](#javascript)
|
|
12
12
|
- [dynamic suggestions function](#dynamic-suggestions-function)
|
|
13
13
|
- [mapOptionToSuggestedValue](#mapoptiontosuggestedvalue)
|
|
14
|
+
- [suggestionTemplate](#suggestiontemplate)
|
|
14
15
|
- [onConfirm](#onconfirm)
|
|
15
16
|
- [Keyboard Support](#keyboard-support)
|
|
16
17
|
- [When focus is within the text input](#when-focus-is-within-the-text-input)
|
|
@@ -202,43 +203,6 @@ new oAutocomplete(oAutocompleteElement, {
|
|
|
202
203
|
| ------ | --------------- | ------------------------------------------------ |
|
|
203
204
|
| option | <code>\*</code> | The option to transform into a suggestion string |
|
|
204
205
|
|
|
205
|
-
### onConfirm
|
|
206
|
-
|
|
207
|
-
This function is called when the user selects an option and is called with the option the user selected.
|
|
208
|
-
|
|
209
|
-
#### Example
|
|
210
|
-
|
|
211
|
-
```js
|
|
212
|
-
import oAutocomplete from 'o-autocomplete';
|
|
213
|
-
|
|
214
|
-
async function customSuggestions(query, populateOptions) {
|
|
215
|
-
const suggestions = await getSuggestions(query);
|
|
216
|
-
populateOptions(suggestions);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* @param {{"suggestionText": string}} option - The option to transform into a suggestion string
|
|
221
|
-
* @returns {string} The string to display as the suggestions for this option
|
|
222
|
-
*/
|
|
223
|
-
function mapOptionToSuggestedValue(option) {
|
|
224
|
-
return option.suggestionText;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* @param {{"suggestionText": string}} option - The option the user selected
|
|
229
|
-
*/
|
|
230
|
-
function onConfirm(option) {
|
|
231
|
-
console.log('You selected option: ', option);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const oAutocompleteElement = document.getElementById('my-o-autocomplete-element');
|
|
235
|
-
new oAutocomplete(oAutocompleteElement, {
|
|
236
|
-
onConfirm
|
|
237
|
-
mapOptionToSuggestedValue,
|
|
238
|
-
source: customSuggestions,
|
|
239
|
-
});
|
|
240
|
-
```
|
|
241
|
-
|
|
242
206
|
### suggestionTemplate
|
|
243
207
|
|
|
244
208
|
This function is used to override the default rendering of suggestion items, with a function that returns a custom HTML string for the given option.
|
package/package.json
CHANGED
package/src/js/autocomplete.js
CHANGED
|
@@ -209,6 +209,8 @@ class Autocomplete {
|
|
|
209
209
|
if (opts.onConfirm) {
|
|
210
210
|
this.options.onConfirm = opts.onConfirm;
|
|
211
211
|
}
|
|
212
|
+
this.options.showNoOptionsFound = opts.showNoOptionsFound || false;
|
|
213
|
+
this.options.confirmOnBlur = opts.confirmOnBlur ?? true;
|
|
212
214
|
if (opts.suggestionTemplate) {
|
|
213
215
|
this.options.suggestionTemplate = opts.suggestionTemplate;
|
|
214
216
|
}
|
|
@@ -247,7 +249,33 @@ class Autocomplete {
|
|
|
247
249
|
* @returns {void}
|
|
248
250
|
*/
|
|
249
251
|
this.options.source = (query, populateOptions) => {
|
|
250
|
-
|
|
252
|
+
// One way this function can be invoked is following a clearButton click event.
|
|
253
|
+
|
|
254
|
+
// The consumer-provided `customSource()` function can be wrapped in `debounce()`, creating the potential
|
|
255
|
+
// for a user-defined delay between the invocation of the `customSource()` and `callback()` functions.
|
|
256
|
+
// This delay could be longer than the 100ms frequency at which accessible-autocomplete polls the value of the input.
|
|
257
|
+
|
|
258
|
+
// The following code blocks accommodate the coincident of these circumstances.
|
|
259
|
+
|
|
260
|
+
// A clearButton click event will hide the loading pane and set the input value as an empty string.
|
|
261
|
+
if (query) {
|
|
262
|
+
// Therefore only show the loading pane if a `query` (i.e. input) value is present
|
|
263
|
+
// so as to avoid temporarily re-showing the loading pane prior to this function's callback hiding it once again.
|
|
264
|
+
showLoadingPane(this);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// A clearButton click event will blur the input field, resulting in the listbox of options becoming hidden
|
|
268
|
+
// (but without de-populating the options).
|
|
269
|
+
if (!query) {
|
|
270
|
+
// If the accessible-autocomplete poll occurs after a clearButton click event
|
|
271
|
+
// but before this function's callback is invoked then the listbox and its options will be re-displayed.
|
|
272
|
+
// When the callback eventually runs, it will de-populate the options, consequently re-hiding the listbox.
|
|
273
|
+
// To prevent this, in the event of no `query` value being present
|
|
274
|
+
// (where, logically, there will be no corresponding options),
|
|
275
|
+
// immediately de-populate the options rather than waiting for the callback to do so.
|
|
276
|
+
populateOptions([]);
|
|
277
|
+
}
|
|
278
|
+
|
|
251
279
|
/**
|
|
252
280
|
* @param {Array<string>} options - The options which match the rext which was typed into the autocomplete by the user
|
|
253
281
|
* @returns {void}
|
|
@@ -281,11 +309,12 @@ class Autocomplete {
|
|
|
281
309
|
this.options.onConfirm(option);
|
|
282
310
|
}
|
|
283
311
|
},
|
|
312
|
+
confirmOnBlur: this.options.confirmOnBlur,
|
|
284
313
|
source: this.options.source,
|
|
285
314
|
cssNamespace: 'o-autocomplete',
|
|
286
315
|
displayMenu: 'overlay',
|
|
287
316
|
defaultValue: this.options.defaultValue || '',
|
|
288
|
-
showNoOptionsFound:
|
|
317
|
+
showNoOptionsFound: this.options.showNoOptionsFound,
|
|
289
318
|
autoselect: this.options.autoselect || false,
|
|
290
319
|
templates: {
|
|
291
320
|
/**
|
|
@@ -381,7 +410,7 @@ class Autocomplete {
|
|
|
381
410
|
placeholder: '',
|
|
382
411
|
cssNamespace: 'o-autocomplete',
|
|
383
412
|
displayMenu: 'overlay',
|
|
384
|
-
showNoOptionsFound:
|
|
413
|
+
showNoOptionsFound: this.options.showNoOptionsFound,
|
|
385
414
|
templates: {
|
|
386
415
|
suggestion: this.suggestionTemplate.bind(this)
|
|
387
416
|
}
|