feat: Add query parameter to custom suggestionTemplate function #1516
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe your changes
This PR makes changes which extend on those introduced by #1127 and enables highlighting of custom rendering of suggestions.
If the user does not provide a
suggestionTemplate
in the options when instantiating an instance ofoAutocomplete
then theAutocomplete.suggestionTemplate
will be used:origami/components/o-autocomplete/src/js/autocomplete.js
Lines 387 to 413 in f11860c
This has a direct reference to
autocompleteEl
(viathis.autocompleteEl
), which can be used to acquire the current text input value, which is then used to enable highlighting of the suggestion.However, when providing a custom
suggestionTemplate
function, this direct reference toautocompleteEl
is not an option. It is possible to obtain the text input viaconst input = document.getElementById('autocomplete-input');
, though to do this prior to rendering every suggestion is inefficient.The changes made in PR Financial-Times/accessible-autocomplete#29 means that
query
(the text which was typed into the autocomplete text input field by the user) can be included in a customsuggestionTemplate
callback function's parameters and so used to highlight theoption
value (or, in the scenario ofoption
being an object, a value thereof).This screengrab demonstrates the benefit of these changes via a scenario in which a custom
suggestionTemplate
function was used (in order to include the suffix that describes the type of thing the suggestion is: person, venue, company, etc.) and where the suggestion's name is also highlighted in accordance with the text the user has input:Open question:
Autocomplete.suggestionTemplate
can continue to acquire thequery
value viaconst input = this.autocompleteEl.querySelector('input');
(and then acquire the value frominput.value
); to remove that line and provide thequery
in the same way as a customsuggestionTemplate
function (as a second function parameter) results in linting error (see here):Error: 396:21 error Expected 'this' to be used by class method 'suggestionTemplate' class-methods-use-this
. This implies the class method would need to be changed into a standalone function which in turn entails a fair amount of overhauling perhaps excessive for the intention of this work, though I'd be interested to hear opinions.I've added @adgad as a reviewer on the basis that they may be familiar with this code from their work on #1127.
Issue ticket number and link
N/A
Link to Figma designs
N/A
TODO (pre-merge)
dynamic-custom-highlighted-suggestion
(anddynamic-custom-suggestion
) should have theirhidden
value set totrue
hidden
value is set totrue
should remain as suchDependency PRs
Release
This adds functionality in a backwards-compatible manner so I propose to release these changes as a minor version.
Checklist before requesting a review
percy
label on my PR before merging and after review.