Examples
The Autocomplete
component is a parent component that adds autocomplete functionality to a TextField
component.
Code
React
See Storybook for React guidance of this component.
Web Component
See Storybook for Web Component guidance of this component.
Autocomplete items
An <Autocomplete>
component accepts a list of items (JavaScript objects) that conform to the shape described by the table below.
Name | Type | Description |
---|---|---|
id | string | Unique identifier for this item |
name | string | Displayed value of the item. May alternatively provide a children value |
children | React.ReactNode | Custom React node as an alternative to a string-only name |
className | string | Additional classes to be added to the root element. Useful for adding utility classes. |
isResult | boolean | Whether this item should be counted as one of the results for the purpose of announcing the result count to screen readers |
Style customization
The following CSS variables can be overridden to customize Autocomplete components:
Guidance
When to use
- If you are returning results from a known domain like a database of zip codes or a taxonomy of keywords
- If you have a list of options that would cause a dropdown to be unusually long
When to consider alternatives
- When users are choosing from a specific set of options. Consider checkboxes, radio buttons, or a dropdown in these cases.
Usage
- Don't use placeholder text in autocomplete fields. Try to write a descriptive label that identifies what the user is searching for. People who have cognitive or visual disabilities have additional problems with placeholder text.
- The length of the text field provides a hint to users as to how much text to write. Do not ask users to write paragraphs of text in this component; use a
textarea
instead.
View the "Forms" guidelines for additional guidance and best practices.
Accessibility
- The
<Autocomplete>
component has taken special care to ensure accessibility for screenreader devices. It announces the number of results based onitems
matches with theinputValue
string. The component also reads out the name of each list item when users arrow up or down. <Autocomplete>
allows developers to add hint text in the<label>
element. This hint<span>
is added to the markup by passing aString
into thehint
prop.<Autocomplete>
has a button (styled visually as a link) to clear the search, and refocus the<input>
element. This resets the local stateselectedItem
tonull
, and will re-read the label and screenreader hint text.
Component maturity
For more information about how we tested and validated our work for each checklist item, read our component maturity documentation.