@financial-times/o-autocomplete 1.9.2 → 1.10.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 +7 -0
- package/README.md +61 -1
- package/demos/src/dynamic-custom-highlighted-suggestion/data.js +2098 -0
- package/demos/src/dynamic-custom-highlighted-suggestion/dynamic-custom-highlighted-suggestion.js +127 -0
- package/demos/src/dynamic-custom-highlighted-suggestion/dynamic-custom-highlighted-suggestion.mustache +14 -0
- package/demos/src/dynamic-custom-suggestion/dynamic-custom-suggestion.js +2 -1
- package/origami.json +8 -0
- package/package.json +2 -2
- package/src/js/autocomplete.js +4 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.10.0](https://github.com/Financial-Times/origami/compare/o-autocomplete-v1.9.2...o-autocomplete-v1.10.0) (2024-04-19)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* Enable highlighting of custom suggestions ([#1516](https://github.com/Financial-Times/origami/issues/1516)) ([c326099](https://github.com/Financial-Times/origami/commit/c3260998c4a8926f875e7ba95134b230ca517669))
|
|
9
|
+
|
|
3
10
|
## [1.9.2](https://github.com/Financial-Times/origami/compare/o-autocomplete-v1.9.1...o-autocomplete-v1.9.2) (2024-04-12)
|
|
4
11
|
|
|
5
12
|
|
package/README.md
CHANGED
|
@@ -236,10 +236,14 @@ This function is used to override the default rendering of suggestion items, wit
|
|
|
236
236
|
|
|
237
237
|
It is typically used when wanting to provide additional context or styling for each suggestion item.
|
|
238
238
|
|
|
239
|
+
The `query` value (text which was typed into the autocomplete text input field by the user) is provided so that it can be used as a basis for highlighting the `option` value (or one of its values if it is an object).
|
|
240
|
+
|
|
239
241
|
:warning: **Caution:** because this function allows you to output arbitrary HTML, you should [make sure it's trusted](https://en.wikipedia.org/wiki/Cross-site_scripting), and accessible. The HTML will be output within listbox options, so [ensure all descendants are presentational](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/option_role#all_descendants_are_presentational).
|
|
240
242
|
|
|
241
243
|
#### Example
|
|
242
244
|
|
|
245
|
+
Providing additional context by displaying multiple `option` properties:
|
|
246
|
+
|
|
243
247
|
```js
|
|
244
248
|
import oAutocomplete from 'o-autocomplete';
|
|
245
249
|
|
|
@@ -250,6 +254,7 @@ async function customSuggestions(query, populateOptions) {
|
|
|
250
254
|
|
|
251
255
|
/**
|
|
252
256
|
* @param {{"name": string, "role": string}} option - The option to transform into a suggestion
|
|
257
|
+
* @param {string} [query] - Text which was typed into the autocomplete text input field by the user
|
|
253
258
|
* @returns {string} The HTML to render in the suggestion list*/
|
|
254
259
|
function suggestionTemplate(option) {
|
|
255
260
|
return `
|
|
@@ -267,6 +272,60 @@ new oAutocomplete(oAutocompleteElement, {
|
|
|
267
272
|
});
|
|
268
273
|
```
|
|
269
274
|
|
|
275
|
+
Using the `query` value to apply highlighting to one of the `option` properties:
|
|
276
|
+
|
|
277
|
+
```js
|
|
278
|
+
import oAutocomplete from 'o-autocomplete';
|
|
279
|
+
|
|
280
|
+
async function customSuggestions(query, populateOptions) {
|
|
281
|
+
const suggestions = await getSuggestions(query);
|
|
282
|
+
populateOptions(suggestions);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function highlightSuggestion(suggestion, query) {
|
|
286
|
+
const result = suggestion.split('');
|
|
287
|
+
|
|
288
|
+
const matchIndex = suggestion.toLocaleLowerCase().indexOf(query.toLocaleLowerCase());
|
|
289
|
+
return result.map(function(character, index) {
|
|
290
|
+
let shouldHighlight = true;
|
|
291
|
+
const hasMatched = matchIndex > -1;
|
|
292
|
+
const characterIsWithinMatch = index >= matchIndex && index <= matchIndex + query.length - 1;
|
|
293
|
+
if (hasMatched && characterIsWithinMatch) {
|
|
294
|
+
shouldHighlight = false;
|
|
295
|
+
}
|
|
296
|
+
return [character, shouldHighlight];
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @param {{"name": string, "role": string}} option - The option to transform into a suggestion
|
|
302
|
+
* @param {string} [query] - Text which was typed into the autocomplete text input field by the user
|
|
303
|
+
* @returns {string} The HTML to render in the suggestion list*/
|
|
304
|
+
function suggestionTemplate(option, query) {
|
|
305
|
+
const characters = highlightSuggestion(option.name, query || option.name);
|
|
306
|
+
|
|
307
|
+
let output = '';
|
|
308
|
+
for (const [character, shoudHighlight] of characters) {
|
|
309
|
+
if (shoudHighlight) {
|
|
310
|
+
output += `<span class="o-autocomplete__option--highlight">${character}</span>`;
|
|
311
|
+
} else {
|
|
312
|
+
output += `${character}`;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
output += ` (${option.role})`;
|
|
316
|
+
const span = document.createElement('span');
|
|
317
|
+
span.setAttribute('aria-label', option.name);
|
|
318
|
+
span.innerHTML = output;
|
|
319
|
+
return span.outerHTML;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const oAutocompleteElement = document.getElementById('my-o-autocomplete-element');
|
|
323
|
+
new oAutocomplete(oAutocompleteElement, {
|
|
324
|
+
suggestionTemplate,
|
|
325
|
+
source: customSuggestions,
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
270
329
|
<a name="SuggestionTemplate"></a>
|
|
271
330
|
|
|
272
331
|
#### SuggestionTemplate ⇒ <code>string</code>
|
|
@@ -274,7 +333,8 @@ new oAutocomplete(oAutocompleteElement, {
|
|
|
274
333
|
|
|
275
334
|
| Param | Type | Description |
|
|
276
335
|
| --- | --- | --- |
|
|
277
|
-
| option | <code>\*</code> | The option to transform into a
|
|
336
|
+
| option | <code>\*</code> | The option to transform into a suggestion |
|
|
337
|
+
| query | <code>string</code> | Text which was typed into the autocomplete text input field by the user |
|
|
278
338
|
|
|
279
339
|
### onConfirm
|
|
280
340
|
|