TypeaheadSearch
A search form that provides a menu with 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.
Name | Value |
---|---|
View | |
Reading direction |
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 name | Description | Type | Default |
---|---|---|---|
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[] | |
buttonLabel | Label for the submit button. If no label is provided, the submit button will not be displayed. | string | '' |
initialInputValue | Initial value for the text input. Triggers an initial input event on mount. | string | '' |
searchFooterUrl | Link for the final menu item. This will typically be a link to the search page for the current search query. | string | '' |
debounceInterval | Time interval for debouncing input events, in ms. | number | DebounceInterval |
highlightQuery | Whether the search query should be highlighted within a search result's title. | boolean | false |
showThumbnail | Whether to show search results' thumbnails (or a placeholder icon). | boolean | false |
autoExpandWidth | Contract 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. | boolean | false |
visibleItemLimit | Limit the number of menu items to display before scrolling. Setting this prop to anything falsy will show all menu items. By default, all menu items are shown. | number|null | null |
Methods
Method name | Description | Signature |
---|---|---|
focus | Focus the component's input element. | Returns: void |
Events
Event name | Properties | Description |
---|---|---|
load-more | When the user scrolls towards the bottom of the menu. If it is possible to add or load more menu items, then now would be a good moment so that the user can experience infinite scrolling. | |
input | value string - The new input value | When the text input value changes. Debounced by default. |
search-result-click | event SearchResultClickEvent - Data for the selected result | When a search result is selected. |
submit | event SearchResultClickEvent - Data for the selected result | When the form is submitted. |
Slots
Name | Description | Bindings |
---|---|---|
search-results-pending | A slot for a translated "Loading search results" message. | |
search-no-results-text | A slot for passing in a translated "no results" message. | |
search-footer-text | A slot for passing in translated search footer text. | search-query string - Input text entered by the user |
default | A slot for passing hidden inputs, i.e.<input type="hidden" name="language" value="en"> |
CSS-only version
Markup structure
The CSS-only version of TypeaheadSearch is simply a SearchInput component styled to look like the Vue version of TypeaheadSearch. It will have no menu of results and is meant to be replaced by the Vue component once JavaScript has loaded. If you just need a SearchInput, check out the CSS-only version of the SearchInput component. Note that the search icon is automatically added for you.
With thumbnails
When your CSS-only TypeaheadSearch component will be swapped out for a Vue version that shows thumbnails (see the "With initial input value" demo above), apply the .cdx-typeahead-search--show-thumbnail
class to the wrapper <div>
to expand the start icon width.
With thumbnails and auto-expand width
When your CSS-only TypeaheadSearch component will be swapped out for a Vue version that shows thumbnails and expands when results are shown (see the "Search Wikipedia articles" demo above), apply the .cdx-typeahead-search--show-thumbnail
and .cdx-typeahead-search--auto-expand-width
classes to the wrapper <div>
to reduce the starting size of the input.