Skip to main content

An official website of the United States government

Here's how you know

An official website of the United States government

Here's how you know

Theme:

Design system switcher

Version:

Design system switcher

Theme:

Design system switcher

Version:

Design system switcher

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

See Storybook for React 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.

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

Style customization

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: #00395e--color-background-inverse
--autocomplete-item__font-color--activehex value: #ffffff--color-white
--autocomplete-item-message__font-colorhex value: #5a5a5a--color-gray-dark

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-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: #f7bbc5--color-error-lighter
--text-input__border-color--inversehex value: #000000--color-black
--text-input__colorhex value: #262626--color-base
--text-input__divider-colorhex value: #a6a6a6--color-gray-light
--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

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

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