@financial-times/o-autocomplete 2.0.0 → 2.1.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/package.json +1 -1
- package/src/js/autocomplete.js +22 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.1.0](https://github.com/Financial-Times/origami/compare/o-autocomplete-v2.0.0...o-autocomplete-v2.1.0) (2025-11-13)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* Add o-autocomplete configurable highlighting ([b0b7d01](https://github.com/Financial-Times/origami/commit/b0b7d015ec9c2fe07c18ab57bddafb49468bc3c1))
|
|
9
|
+
|
|
3
10
|
## [2.0.0](https://github.com/Financial-Times/origami/compare/o-autocomplete-v1.10.0...o-autocomplete-v2.0.0) (2025-02-20)
|
|
4
11
|
|
|
5
12
|
### ⚠ BREAKING CHANGES
|
package/package.json
CHANGED
package/src/js/autocomplete.js
CHANGED
|
@@ -16,18 +16,19 @@ import accessibleAutocomplete from '@financial-times/accessible-autocomplete';
|
|
|
16
16
|
/**
|
|
17
17
|
* @param {string} suggestion - Text which is going to be suggested to the user
|
|
18
18
|
* @param {string} query - Text which was typed into the autocomplete text input field by the user
|
|
19
|
+
* @param {boolean} isHighlightCorrespondingToMatch - Boolean to determine whether matching or non-matching characters should be highlighted.
|
|
19
20
|
* @returns {CharacterHighlight[]} An array of arrays which contain two items, the first is the character in the suggestion, the second is a boolean which indicates whether the character should be highlighted.
|
|
20
21
|
*/
|
|
21
|
-
function highlightSuggestion(suggestion, query) {
|
|
22
|
+
function highlightSuggestion(suggestion, query, isHighlightCorrespondingToMatch) {
|
|
22
23
|
const result = suggestion.split('');
|
|
23
24
|
|
|
24
25
|
const matchIndex = suggestion.toLocaleLowerCase().indexOf(query.toLocaleLowerCase());
|
|
25
26
|
return result.map(function(character, index) {
|
|
26
|
-
let shouldHighlight =
|
|
27
|
+
let shouldHighlight = !isHighlightCorrespondingToMatch;
|
|
27
28
|
const hasMatched = matchIndex > -1;
|
|
28
29
|
const characterIsWithinMatch = index >= matchIndex && index <= matchIndex + query.length - 1;
|
|
29
30
|
if (hasMatched && characterIsWithinMatch) {
|
|
30
|
-
shouldHighlight =
|
|
31
|
+
shouldHighlight = isHighlightCorrespondingToMatch;
|
|
31
32
|
}
|
|
32
33
|
return [character, shouldHighlight];
|
|
33
34
|
});
|
|
@@ -182,6 +183,7 @@ function initClearButton(instance) {
|
|
|
182
183
|
* @property {MapOptionToSuggestedValue} [mapOptionToSuggestedValue] - Function which transforms a suggestion before rendering.
|
|
183
184
|
* @property {onConfirm} [onConfirm] - Function which is called when the user selects an option
|
|
184
185
|
* @property {SuggestionTemplate} [suggestionTemplate] - Function to override how a suggestion item is rendered.
|
|
186
|
+
* @property {boolean} [isHighlightCorrespondingToMatch] - Boolean to determine whether matching or non-matching characters should be highlighted.
|
|
185
187
|
* @property {boolean} [autoselect] - Boolean to specify whether first option in suggestions list is highlighted.
|
|
186
188
|
*/
|
|
187
189
|
|
|
@@ -210,6 +212,7 @@ class Autocomplete {
|
|
|
210
212
|
if (opts.suggestionTemplate) {
|
|
211
213
|
this.options.suggestionTemplate = opts.suggestionTemplate;
|
|
212
214
|
}
|
|
215
|
+
this.options.isHighlightCorrespondingToMatch = Boolean(opts.isHighlightCorrespondingToMatch);
|
|
213
216
|
if (opts.autoselect) {
|
|
214
217
|
this.options.autoselect = opts.autoselect;
|
|
215
218
|
}
|
|
@@ -293,10 +296,17 @@ class Autocomplete {
|
|
|
293
296
|
* @returns {string|undefined} HTML string to represent a single suggestion.
|
|
294
297
|
*/
|
|
295
298
|
suggestion: (option, query) => {
|
|
299
|
+
const isHighlightCorrespondingToMatch = this.options.isHighlightCorrespondingToMatch;
|
|
300
|
+
|
|
296
301
|
// If the suggestionTemplate override option is provided,
|
|
297
302
|
// use that to render the suggestion.
|
|
298
303
|
if(typeof this.options.suggestionTemplate === 'function') {
|
|
299
|
-
return this.options.suggestionTemplate(
|
|
304
|
+
return this.options.suggestionTemplate(
|
|
305
|
+
option,
|
|
306
|
+
query,
|
|
307
|
+
highlightSuggestion,
|
|
308
|
+
isHighlightCorrespondingToMatch
|
|
309
|
+
);
|
|
300
310
|
}
|
|
301
311
|
if (typeof option === 'object') {
|
|
302
312
|
// If the `mapOptionToSuggestedValue` function is defined
|
|
@@ -315,7 +325,7 @@ class Autocomplete {
|
|
|
315
325
|
throw new Error(`The option trying to be displayed as a suggestion is not a string, it is "${typeof option}". o-autocomplete can only display strings as suggestions. Define a \`mapOptionToSuggestedValue\` function to convert the option into a string to be used as the suggestion.`);
|
|
316
326
|
}
|
|
317
327
|
|
|
318
|
-
return this.suggestionTemplate(option);
|
|
328
|
+
return this.suggestionTemplate(option, isHighlightCorrespondingToMatch);
|
|
319
329
|
},
|
|
320
330
|
/**
|
|
321
331
|
* Used when a suggestion is selected, the return value of this will be used as the value for the input element.
|
|
@@ -387,15 +397,20 @@ class Autocomplete {
|
|
|
387
397
|
* Used when rendering suggestions, the return value of this will be used as the innerHTML for a single suggestion.
|
|
388
398
|
*
|
|
389
399
|
* @param {string} suggestedValue The suggestion to apply the template with.
|
|
400
|
+
* @param {boolean} isHighlightCorrespondingToMatch Boolean to determine whether matching or non-matching characters should be highlighted.
|
|
390
401
|
* @returns {string} HTML string to be represent a single suggestion.
|
|
391
402
|
*/
|
|
392
|
-
suggestionTemplate (suggestedValue) {
|
|
403
|
+
suggestionTemplate (suggestedValue, isHighlightCorrespondingToMatch) {
|
|
393
404
|
// o-autocomplete has a UI design to highlight characters in the suggestions.
|
|
394
405
|
const input = this.autocompleteEl.querySelector('input');
|
|
395
406
|
/**
|
|
396
407
|
* @type {CharacterHighlight[]} An array of arrays which contain two items, the first is the character in the suggestion, the second is a boolean which indicates whether the character should be highlighted.
|
|
397
408
|
*/
|
|
398
|
-
const characters = highlightSuggestion(
|
|
409
|
+
const characters = highlightSuggestion(
|
|
410
|
+
suggestedValue,
|
|
411
|
+
input ? input.value : suggestedValue,
|
|
412
|
+
isHighlightCorrespondingToMatch
|
|
413
|
+
);
|
|
399
414
|
|
|
400
415
|
let output = '';
|
|
401
416
|
for (const [character, shoudHighlight] of characters) {
|