Examples
The Autocomplete
component is a parent component that adds autocomplete functionality to a TextField
component.
Code
React
Preact/React documentation for Storybook
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:
Text Input Options
The <Autocomplete>
component also makes use of a text field, which can be customized by the following variables:
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
This component meets 100% of our maturity criteria.
What does this mean?
Each component is tested against the following items to gauge the component's maturity. When using incomplete components, consider the unmet criteria as applied to your product.
For more information about how we tested and validated our work for each checklist item, read our component maturity documentation.
- Complete
Color:Â Meets AA color contrast standards for accessibility and color blindness.
- Complete
Forced Colors Mode (FCM):Â While using FCM the components text is legible and improves readability.
- Complete
WCAG 2.1 Level AA Conformance:Â All Axe checks for WCAG AA compliance have passed.
- Complete
Screen readers:Â VoiceOver, NVDA, and JAWS screen readers provide concise communication and interaction.
- Complete
Keyboard navigation:Â Component is fully navigable with a keyboard.
Accessibility
- Complete
Storybook:Â Component has stories to cover all defined props.
- Complete
Responsive:Â Component designed to work in all responsive breakpoints.
- Complete
Spanish translations:Â Includes Spanish translations for default text content.
Code
- Complete
Code:Â Tokens implemented in code.
- Complete
Design:Â Tokens implemented in the Sketch.