Skip to main content
U.S. Flag

An official website of the United States government

CMS Design System

Autocomplete

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.

View Source File

Code snippet

Props

React Properties Documentation
NameTypeDefaultDescription
ariaClearLabelstring

Screenreader-specific label for the Clear search <button>. Intended to provide a longer, more descriptive explanation of the button's behavior.

autoCompleteLabelstring'off'

Control the TextField autocomplete attribute. Defaults to "off" to support accessibility. Read more.

childrenrequiredReact.ReactNode

Must contain a TextField component

classNamestring

Additional classes to be added to the root element. Useful for adding utility classes.

clearInputOnBlurbooleantrue

When set to false, do not clear the input when the input element loses focus.

clearInputTextReact.ReactNode

Text rendered on the page if clearInput prop is passed. Default is "Clear search".

clearSearchButtonbooleantrue

Removes the Clear search button when set to false

focusTriggerboolean

Used to focus child TextField on componentDidMount()

idstring

A unique id to be passed to the child TextField. If no id is passed as a prop, the Autocomplete component will auto-generate one. This prop was provided in cases where an id might need to be passed to multiple components, such as the htmlFor attribute on a label and the id of an input.

getA11yStatusMessageDownshiftProps<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 TextField's input element

itemToStringDownshiftProps<any>['itemToString'](item): string => (item ? item.name : '')

Used to determine the string value for the selected item (which is used to compute the inputValue). Read more on downshift docs.

itemsAutocompleteItems[]

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.

labelReact.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.

labelIdstring

A unique id to be used on the child TextField label tag

loadingboolean

Can be called when the items array is being fetched remotely, or will be delayed for more than 1-2 seconds.

loadingMessageReact.ReactNode

Message users will see when the loading prop is passed to Autocomplete.

noResultsMessageReact.ReactNode

Message users will see when the items array returns empty and the loading prop is passed to <Autocomplete />.

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.

onInputValueChangeDownshiftProps<any>['onInputValueChange']

Called when the child TextField value changes. Returns a String inputValue. Read more on downshift docs.

Autocomplete Items

NameTypeDescription
idstringUnique identifier for this item
namestringDisplayed value of the item. May alternatively provide a children value
childrenReact.ReactNodeCustom React node as an alternative to a string-only name
classNamestringAdditional classes to be added to the root element. Useful for adding utility classes.
isResultbooleanWhether 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

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 the inputValue 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 on items matches with the inputValue 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 a String into the hint prop.
  • <Autocomplete> has a button (styled visually as a link) to clear the search, and refocus the <input> element. This resets the local state selectedItem to null, and will re-read the label and screenreader hint text.

Focus Management

  • <Autocomplete> has a new Boolean prop called focusTrigger. Adding this prop will set keyboard focus on the internal <Textfield>. Focus is set immediately when the componentDidMount() 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 the inputValue 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 for blur and click events.

Customization

The following Sass variables can be overridden to customize Autocomplete components:

VariableDefault Core Theme Value
$autocomplete__background-color$color-white
$autocomplete__border-color$color-border
$autocomplete-item__font-color$color-primary
$autocomplete-item__background-color--active$color-primary-alt-darkest
$autocomplete-item__font-color--active$color-white
$autocomplete-item-message__font-color$color-gray

Text Input Options

VariableDefault Core Theme Value
$text-input__line-height1.3
$text-input__background-color--disabled$color-border
$text-input__border-width2px
$text-input__border-color$color-base
$text-input__border-color--disabled$color-gray-light
$text-input__border-color--error$color-error
$text-input__border-color--error--inverse$color-error-light
$text-input__border-color--inverse$color-black
$text-input__border-color--success$color-green-light
$text-input__color$color-base
$text-input__padding8px
$text-input__border-radius3px