TypeaheadSearch

A search form with stylized autocomplete suggestions.

TypeaheadSearch contains a form with a text input, a submit button, and a slot for hidden inputs. The parent component must listen for changes in the search query (which are debounced by default), fetch or calculate search results, then provide them as an array of search results for display to the user as a menu of ListTiles.

At the end of the list of search results, a final option to go to the search page for the current search query is provided.

Events are emitted to the parent when a search result is selected and when the form is submitted, with data about the selected item (e.g. for analytics).

TextInput props apply

This component contains a TextInput component. You can bind TextInput props to this component and they will be passed to the TextInput within.

Attributes passed to input

This component will pass any HTML attributes applied to it, except for CSS class, to the <input> element within the component.

Demos

Search Wikipedia articles

This implementation of TypeaheadSearch fetches articles from English Wikipedia. Note that the input expands on focus via the autoExpandWidth prop. Open the console to see emitted events.

Search Wikidata items

In this example, results are fetched from Wikidata. Note that thumbnails are hidden via the hideThumbnail prop. Open the console to see emitted events.

With initial input value

The initialInputValue prop can be used to pass in the initial value of the TextInput. This is useful when replacing a server-rendered UI where the user may have started typing a search query, or for pre-populating the search term when a user navigates back to a page where they had previously entered one.

On mount, TypeaheadSearch will fetch search results for the initial input value if it's provided. After that, the input value is tracked internally and will be emitted up to the parent component when the value changes.

Usage

Props

Prop nameDescriptionTypeValuesDefault
id(required)ID attribute for the form.string-
formAction(required)Action attribute for form.string-
buttonLabel(required)Submit button text.string-
searchResultsLabel(required)Label attribute for the list of search results.string-
searchResultsList of search results. See the SearchResult type.SearchResult[]-() => []
initialInputValueInitial value for the text input.

Triggers an initial new-input event on mount.
string-''
searchFooterUrlLink for the final option.

This will typically be a link to the search page for the current search query.
string-''
debounceIntervalTime interval for debouncing input events, in ms.number-DebounceInterval
highlightQueryWhether the search query should be highlighted within a search result's title.boolean-false
hideThumbnailWhether to hide search results' thumbnails.boolean-false
hideDescriptionWhether to hide search results' descriptions.boolean-false
autoExpandWidthContract the width of the input when unfocused and expand the width of
the input when focused to accomodate the extra width of the thumbnails.
boolean-false

Methods

focus

Focus the component's input element.

Return

TypeDescription
void

Events

Event namePropertiesDescription
new-inputvalue string - The new input valueWhen the text input value changes. Debounced by default.
search-result-clickevent SearchResultClickEvent - Data for the selected resultWhen a search result is selected.
submitevent SearchResultClickEvent - Data for the selected resultWhen the form is submitted.

Slots

NameDescriptionBindings
search-footer-textA slot for passing in translated search footer text.searchQuery string - Input text entered by the user
defaultA slot for passing hidden inputs, i.e.
<input type="hidden" name="language" value="en">

Last updated: