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 in a dropdown menu.

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, thumbnails are enabled via the showThumbnail prop, and the "search" button is added via the buttonLabel prop. Open the console to see emitted events.

Search Wikidata items

In this example, results are fetched from Wikidata. Thumbnails are disabled, and the input doesn't expand on focus. There is no button, because the buttonLabel prop is not set. 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.

Pending state

Pending state indicators, including an inline progress bar and a message stating that results are pending, can be displayed to users with slower connections while search results are being fetched. To enable this, provide content in the search-results-pending slot.

The pending state indicators will display when a search takes longer than half a second, so you may need to throttle your connection to see them in the demo below.

No results message

A message prompt that no search results were found. To enable this, provide content in the search-no-results-text slot.

Usage

Props

Prop nameDescriptionTypeDefault
id(required)ID attribute for the form.string
formAction(required)Action attribute for form.string
searchResultsLabel(required)Label attribute for the list of search results.string
searchResults(required)List of search results. See the SearchResult type.SearchResult[]
buttonLabelLabel for the submit button.

If no label is provided, the submit button will not be displayed.
string''
initialInputValueInitial value for the text input.

Triggers an initial input event on mount.
string''
searchFooterUrlLink for the final menu item.

This will typically be a link to the search page for the current search query.
string''
debounceIntervalTime interval for debouncing input events, in ms.numberDebounceInterval
highlightQueryWhether the search query should be highlighted within a search result's title.booleanfalse
showThumbnailWhether to show search results' thumbnails (or a placeholder icon).booleanfalse
autoExpandWidthContract the width of the input when unfocused and expand the width of the input when focused to accommodate the extra width of the thumbnails.

This prop is ignored if showThumbnail is false.
booleanfalse

Methods

Method nameDescriptionSignature
focusFocus the component's input element.Returns: void

Events

Event namePropertiesDescription
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-results-pendingA slot for a translated "Loading search results" message.
search-no-results-textA slot for passing in a translated "no results" message.
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: