@financial-times/o-autocomplete 1.7.4 → 1.8.1
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 +14 -0
- package/README.md +85 -0
- package/demos/src/dynamic-custom-suggestion/data.js +2098 -0
- package/demos/src/dynamic-custom-suggestion/dynamic-custom-suggestion.js +83 -0
- package/demos/src/dynamic-custom-suggestion/dynamic-custom-suggestion.mustache +14 -0
- package/origami.json +8 -0
- package/package.json +2 -2
- package/src/js/autocomplete.js +16 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.8.1](https://github.com/Financial-Times/origami/compare/o-autocomplete-v1.8.0...o-autocomplete-v1.8.1) (2023-08-25)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* update o-typography dependency ([fb45b47](https://github.com/Financial-Times/origami/commit/fb45b47274241ea828f7dd50233441a76a215a51))
|
|
9
|
+
|
|
10
|
+
## [1.8.0](https://www.github.com/Financial-Times/origami/compare/o-autocomplete-v1.7.4...o-autocomplete-v1.8.0) (2023-05-30)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* autocomplete, add a suggestionTemplate override option ([23e1397](https://www.github.com/Financial-Times/origami/commit/23e1397deb29034faaf009c16e41ab169dcc3a42))
|
|
16
|
+
|
|
3
17
|
### [1.7.4](https://www.github.com/Financial-Times/origami/compare/o-autocomplete-v1.7.3...o-autocomplete-v1.7.4) (2023-04-28)
|
|
4
18
|
|
|
5
19
|
|
package/README.md
CHANGED
|
@@ -157,6 +157,7 @@ If the `source` function is returning an array of strings which are already suit
|
|
|
157
157
|
|
|
158
158
|
The most common scenario which requires having to define a `mapOptionToSuggestedValue` function is when the `source` function is returning an array of objects, where one of the properties in the object should be used as the suggestion.
|
|
159
159
|
|
|
160
|
+
|
|
160
161
|
#### Example
|
|
161
162
|
|
|
162
163
|
```js
|
|
@@ -228,6 +229,90 @@ new oAutocomplete(oAutocompleteElement, {
|
|
|
228
229
|
});
|
|
229
230
|
```
|
|
230
231
|
|
|
232
|
+
|
|
233
|
+
### suggestionTemplate
|
|
234
|
+
|
|
235
|
+
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.
|
|
236
|
+
|
|
237
|
+
It is typically used when wanting to provide additional context or styling for each suggestion item.
|
|
238
|
+
|
|
239
|
+
: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
|
+
|
|
241
|
+
#### Example
|
|
242
|
+
|
|
243
|
+
```js
|
|
244
|
+
import oAutocomplete from 'o-autocomplete';
|
|
245
|
+
|
|
246
|
+
async function customSuggestions(query, populateOptions) {
|
|
247
|
+
const suggestions = await getSuggestions(query);
|
|
248
|
+
populateOptions(suggestions);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @param {{"name": string, "role": string}} option - The option to transform into a suggestion
|
|
253
|
+
* @returns {string} The HTML to render in the suggestion list*/
|
|
254
|
+
function suggestionTemplate(option) {
|
|
255
|
+
return `
|
|
256
|
+
<div>
|
|
257
|
+
<strong>${option.name}</strong>
|
|
258
|
+
<em>${option.role}</em>
|
|
259
|
+
</div>
|
|
260
|
+
`;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const oAutocompleteElement = document.getElementById('#my-o-autocomplete-element');
|
|
264
|
+
new oAutocomplete(oAutocompleteElement, {
|
|
265
|
+
suggestionTemplate,
|
|
266
|
+
source: customSuggestions,
|
|
267
|
+
});
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
<a name="SuggestionTemplate"></a>
|
|
271
|
+
|
|
272
|
+
#### SuggestionTemplate ⇒ <code>string</code>
|
|
273
|
+
**Returns**: <code>string</code> - The HTML string to render as the suggestion for this option
|
|
274
|
+
|
|
275
|
+
| Param | Type | Description |
|
|
276
|
+
| --- | --- | --- |
|
|
277
|
+
| option | <code>\*</code> | The option to transform into a suggestio |
|
|
278
|
+
|
|
279
|
+
### onConfirm
|
|
280
|
+
|
|
281
|
+
This function is called when the user selects an option and is called with the option the user selected.
|
|
282
|
+
|
|
283
|
+
#### Example
|
|
284
|
+
|
|
285
|
+
```js
|
|
286
|
+
import oAutocomplete from 'o-autocomplete';
|
|
287
|
+
|
|
288
|
+
async function customSuggestions(query, populateOptions) {
|
|
289
|
+
const suggestions = await getSuggestions(query);
|
|
290
|
+
populateOptions(suggestions);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* @param {{"suggestionText": string}} option - The option to transform into a suggestion string
|
|
295
|
+
* @returns {string} The string to display as the suggestions for this option
|
|
296
|
+
*/
|
|
297
|
+
function mapOptionToSuggestedValue(option) {
|
|
298
|
+
return option.suggestionText;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @param {{"suggestionText": string}} option - The option the user selected
|
|
303
|
+
*/
|
|
304
|
+
function onConfirm(option) {
|
|
305
|
+
console.log('You selected option: ', option);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const oAutocompleteElement = document.getElementById('#my-o-autocomplete-element');
|
|
309
|
+
new oAutocomplete(oAutocompleteElement, {
|
|
310
|
+
onConfirm
|
|
311
|
+
mapOptionToSuggestedValue,
|
|
312
|
+
source: customSuggestions,
|
|
313
|
+
});
|
|
314
|
+
```
|
|
315
|
+
|
|
231
316
|
<a name="onConfirm"></a>
|
|
232
317
|
|
|
233
318
|
#### onConfirm ⇒ <code>void</code>
|