TypeaheadSearch
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.
Name | Value |
---|---|
Props | |
useButton | |
buttonLabel | |
highlightQuery | |
showThumbnail | |
autoExpandWidth | |
View | |
Reading direction |
Overview
When to use TypeaheadSearch
Use the TypeaheadSearch component to enable users to navigate to a new page and when you need a predictive list of options in a menu that updates as users type within the input field. To enable users to perform text-based searches to find specific content, such as locating results on a page, use SearchInput instead.
TypeaheadSearch is not a form input. Avoid using TypeaheadSearch to enable users to search a dataset of options for a form field. Instead, use Lookup.
About TypeaheadSearch
TypeaheadSearch includes the following elements.
Input
A SearchInput where users can type their search queries. The input features the 'search' icon for clarity and can also include a placeholder to clarify its purpose.
Placeholder text (optional)
Placeholder text provides an example of what the user might type in the input.
- Placeholder text should further explain what is being searched or sample search queries.
- Placeholder text should never replace the label nor be the input's only description.
- Placeholder text should not duplicate the button label.
Button (optional)
The input can be accompanied by a button, in order to trigger the search action.
- Use the term "Search" or its appropriate translation.
- Don't use long button text or duplicate the placeholder text.
Menu
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 to show a preview of each result.
- Limit visible menu items to a maximum of 10.
- Enable scrolling if users need access to more than 10 results.
Menu footer
The final menu item at the end of the search results menu allows users to navigate to the search page.
Examples
Search Wikipedia articles
This implementation of TypeaheadSearch fetches articles from English Wikipedia. Like the search bar on English Wikipedia, thumbnails are included in search results, the input expands on focus to accommodate the width of the thumbnails, and the "Search" button is added. Results are limited to 10 items.
Developer notes
The showThumbnails
prop is set to true, along with the autoExpandWidth
prop to expand the input on focus. The useButton
prop enables the search button.
Open up the console to review emitted events.
Search Wikidata items
In this example, results are fetched from Wikidata. To support Wikidata items as results, Thumbnails are not shown and the input does not expand on focus. This example also includes a visibleItemLimit
of 5 items—further items can be access by scrolling.
Developer notes
Open up the console to review emitted events.
With initial input value
An initialInputValue
can be included. This is useful when replacing a server-rendered interface 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.
Developer notes
On mount, TypeaheadSearch will fetch search results for the initialInputValue
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 ProgressBar and a message stating that results are pending, can be displayed to users with slower connections while search results are being fetched.
The pending state indicators will display when a search takes longer than half a second, so you may need to throttle your connection to review them in the demo below.
Developer notes
To enable pending state indicators, provide content in the search-results-pending
slot.
No results message
You can show a message when no search results were found.
Developer notes
To enable the no results message, provide content in the search-no-results-text
slot.
Vue usage
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.
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 only 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 (refer to 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 (refer to 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.
Props
Prop name | Description | Type | Default |
---|---|---|---|
id (required) | ID attribute for the form. | string | |
formAction (required) | Action attribute for form. | string | |
searchResults (required) | List of search results. See the SearchResult type. | SearchResult[] | |
useButton | Whether to display a submit button. | boolean | false |
buttonLabel | Custom label for the submit button. Omit this prop to use the default value, "Search". | 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. |
Keyboard navigation
Key | Function |
---|---|
Down arrow / Up arrow | When the menu is displayed, it navigates through the menu items. |
Enter | If the focus is placed in any of the options within the menu, the focused option is selected. |
Esc | This key closes the menu when it is open. |