Skip to main content
U.S. Flag

An official website of the United States government

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.

Examples

The Autocomplete component is a parent component that adds autocomplete functionality to a TextField component.

Loading

Code

React

React Properties Documentation
NameTypeDefaultDescription
ariaClearLabelstringScreen reader-specific label for the Clear search <button>. Intended to provide a longer, more descriptive explanation of the button's behavior.
autoCompleteLabelstringoffControl the TextField autocomplete attribute. Defaults to "off" to support accessibility. Read more.
childrenReactNodeMust contain a TextField component
classNamestringAdditional classes to be added to the root element. Useful for adding utility classes.
clearInputOnBlurbooleantrueWhen set to false, do not clear the input when the input element loses focus.
clearInputTextReactNodeText rendered on the page if clearInput prop is passed. Default is "Clear search".
clearSearchButtonbooleantrueRemoves the Clear search button when set to false
focusTriggerbooleanUsed to focus child TextField on componentDidMount()
idstringA 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.
getA11yStatusMessage(options: A11yStatusMessageOptions<any>) => stringCustomize the default status messages announced to screen reader users via aria-live when autocomplete results are populated. Read more on downshift docs.
inputRef(...args: any[]) => anyAccess a reference to the child TextField's input element
itemToString(item: any) => string(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.
labelReactNodeAdds 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.
labelIdstringA unique id to be used on the child TextField label tag
loadingbooleanCan be called when the items array is being fetched remotely, or will be delayed for more than 1-2 seconds.
loadingMessageReactNodeMessage users will see when the loading prop is passed to Autocomplete.
noResultsMessageReactNodeMessage users will see when the items array returns empty and the loading prop is passed to <Autocomplete />.
onChange(...args: any[]) => anyCalled 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(inputValue: string, stateAndHelpers: ControllerStateAndHelpers<any>) => voidCalled when the child TextField value changes. Returns a String inputValue. Read more on downshift docs.

Autocomplete items

An <Autocomplete> component accepts a list of items (JavaScript objects) that conform to the shape described by the table below.

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

Styles

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

CSS variables for autocomplete
VariableDefault Core Theme Value
--autocomplete__background-colorhex value: #ffffff--color-white
--autocomplete__border-colorhex value: #d9d9d9--color-border
--autocomplete-item__font-colorhex value: #0071bc--color-primary
--autocomplete-item__background-color--activehex value: #016074--color-primary-alt-darkest
--autocomplete-item__font-color--activehex value: #ffffff--color-white
--autocomplete-item-message__font-colorhex value: #5a5a5a--color-gray

Text Input Options

The <Autocomplete> component also makes use of a text field, which can be customized by the following variables:

CSS variables for text-input
VariableDefault Core Theme Value
--text-input__line-height1.3
--text-input__background-color--disabledhex value: #d9d9d9--color-border
--text-input__border-width2px
--text-input__border-width--error3px
--text-input__border-width--success3px
--text-input__border-colorhex value: #262626--color-base
--text-input__border-color--disabledhex value: #a6a6a6--color-gray-light
--text-input__border-color--errorhex value: #e31c3d--color-error
--text-input__border-color--error--inversehex value: #f18e9e--color-error-light
--text-input__border-color--inversehex value: #000000--color-black
--text-input__border-color--successhex value: #2a9526--color-green-light
--text-input__colorhex value: #262626--color-base
--text-input__padding8px
--text-input__border-radius3px

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

Component maturity

For more information about how we tested and validated our work for each checklist item, read our component maturity documentation.

Accessibility

  • 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.
    Incomplete
  • 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.
    Complete

Code

  • 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.
    Complete

Tokens

  • Code

    Tokens implemented in code.
    Complete
  • Design

    Tokens implemented in the Sketch.
    Complete