TypeaheadSearch is a search input that provides a menu of options based on the current search query. It is meant to help users search for and navigate to content.
TypeaheadSearch consists of a search input and a submit button. As users type, predictive search results are displayed in a menu. Additionally, there's a final option at the end of the search results list, enabling users to navigate to the search page.
Use the TypeaheadSearch component when you need a predictive list of options in a menu that updates as users type within the input field. Avoid using TypeaheadSearch if your goal is to enable users to perform text-based searches to find specific content, such as locating results on a page. In this case use the SearchInput component instead.
TypeaheadSearch may include the following elements:
A search input where users can type their searches. The input features the 'search' icon for clarity and should also include a placeholder to clarify its purpose.
- Button (optional)
The input can be accompanied with a button, in order to trigger the search action. It's crucial not to customize the label of the search button: consistently employ the term "Search" or its appropriate translation. Additionally, avoid using long text within this button.
When the user types within the input, a menu component with results is displayed. By default, menu items will feature a label and an optional description. Thumbnails can also be included.
The final menu item at the end of the search results menu list allows users to navigate to the search page.
As with SearchInput, TypeaheadSearch can be categorized based on the visibility and type of button it contains:
- Without button
The TypeaheadSearch can consist of the input field alone or include the decorative icon. In this scenario, using the icon is suggested to emphasize that the input serves as a search input, distinguishing it from a simple text input.
- With text button
The TypeaheadSearch also has the option to feature a text button to initiate the search process.
TypeaheadSearch has the following visually separate states:
- Active with results
- Active with no results
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
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 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
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
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.
|ID attribute for the form.|
|Action attribute for form.|
|Label attribute for the list of search results.|
|List of search results. See the SearchResult type.|
|Label for the submit button.|
If no label is provided, the submit button will not be displayed.
|Initial value for the text input.|
Triggers an initial
|Link for the final menu item.|
This will typically be a link to the search page for the current search query.
|Time interval for debouncing input events, in ms.|
|Whether the search query should be highlighted within a search result's title.|
|Whether to show search results' thumbnails (or a placeholder icon).|
|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.
|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.
|Focus the component's input element.||Returns: |
|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.
|value ||When the text input value changes. Debounced by default.|
|event ||When a search result is selected.|
|event ||When the form is submitted.|
|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 |
|default||A slot for passing hidden inputs, i.e.|
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--auto-expand-width classes to the wrapper
<div> to reduce the starting size of the input.