Autocompletes allow users to enter any combination of letters, numbers, or symbols of their choosing (unless otherwise restricted), and receive one or more suggested matches in a list below the input.
<Autocomplete>
The Autocomplete
component is a parent component that adds autocomplete functionality to a TextField
component.
Code snippet
Props
Name | Type | Default | Description |
---|---|---|---|
ariaClearLabel | string | Screenreader-specific label for the Clear search | |
autoCompleteLabel | string | 'off' | Control the |
children required | React.ReactNode | Must contain a | |
className | string | Additional classes to be added to the root element. Useful for adding utility classes. | |
clearInputOnBlur | boolean | true | When set to |
clearInputText | React.ReactNode | Text rendered on the page if | |
clearSearchButton | boolean | true | Removes the Clear search button when set to |
focusTrigger | boolean | Used to focus child | |
id | string | A unique id to be passed to the child | |
getA11yStatusMessage | DownshiftProps<any>['getA11yStatusMessage'] | Customize the default status messages announced to screenreader users via aria-live when autocomplete results are populated. Read more on downshift docs. | |
inputRef | (...args: any[]) => any | Access a reference to the child | |
itemToString | DownshiftProps<any>['itemToString'] | (item): string => (item ? item.name : '') | Used to determine the string value for the selected item (which is used to compute the |
items | AutocompleteItems[] | Array of objects used to populate the suggestion list that appears below the input as users type. This array of objects is intended for an async data callback, and should conform to the prescribed shape to avoid errors. | |
label | React.ReactNode | Adds a heading to the top of the autocomplete list. This can be used to convey to the user that they're required to select an option from the autocomplete list. | |
labelId | string | A unique | |
loading | boolean | Can be called when the | |
loadingMessage | React.ReactNode | Message users will see when the | |
noResultsMessage | React.ReactNode | Message users will see when the | |
onChange | (...args: any[]) => any | Called when the user selects an item and the selected item has changed. Called with the item that was selected and the new state. Read more on downshift docs. | |
onInputValueChange | DownshiftProps<any>['onInputValueChange'] | Called when the child |
Autocomplete Items
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 |
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
<Autocomplete>
makes use of the<Downshift>
component, maintained by Paypal: Downshift docs on Github. The above documented props are only those directly exposed by the<Autocomplete>
component, but you can pass props specific to<Downshift>
here as well, e.g. you can set theinputValue
prop if you'd like to provide an initial value to the component or control the input more directly.- We continue to use the ARIA 1.0 Combobox With List Autocomplete pattern. This decision was made ensures good compatibility with assistive devices (JAWS, NVDA, VoiceOver). This was done because the ARIA 1.1 markup pattern triggers a different behavior on containers with a role="combobox" attribute.
- 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.
Focus Management
<Autocomplete>
has a new Boolean prop calledfocusTrigger
. Adding this prop will set keyboard focus on the internal<Textfield>
. Focus is set immediately when thecomponentDidMount()
lifecycle method fires.- In most cases, this isn't needed. It's useful when components are added dynamically, after the application has been rendered. All major screen readers (JAWS, NVDA, VoiceOver) have been tested with this feature, and announce the new input correctly.
- Instances that contain the
focusTrigger
prop may fire Downshift's onInputValueChange method, causing theinputValue
to be set back to an empty string. In these cases, you may want to access Downshift's state reducer and manage your component's local state forblur
andclick
events.
Customization
The following Sass variables can be overridden to customize Autocomplete components: