@contentful/f36-autocomplete

Forma 36: Autocomplete component

Usage no npm install needed!

<script type="module">
  import contentfulF36Autocomplete from 'https://cdn.skypack.dev/@contentful/f36-autocomplete';
</script>

README


title: 'Autocomplete' slug: /components/autocomplete/ github: 'https://github.com/contentful/forma-36/tree/master/packages/components/autocomplete' storybook: 'https://v4-f36-storybook.netlify.app/?path=/story/components-autocomplete--basic' typescript: ./src/Autocomplete.tsx

It allows users to quickly search through and select from large collections of options.

Import

import { Autocomplete } from '@contentful/f36-components';
// or
import { Autocomplete } from '@contentful/f36-autocomplete';

Examples

Basic usage

The Autocomplete requires 3 props to work:

  • items: It’s an array containing the items that will be shown as selectable options when the user types something in the TextInput.
  • onInputValueChange: This function will be called every time the user types something in the input. The component will pass the item, which the filter method is currently iterating over, and the inputValue prop of the TextInput component.
  • onSelectItem: This function is called when the user selects one of the options of the list. The component will pass the selected item as an argument to the function.

An Autocomplete with a list of spaces will look like this:


Using objects as items

We showed how to create an Autocomplete with an array of string but it’s also possible to use other types of data as items. A very common way of using the Autocomplete is with objects and for that, with a few changes to the previous example this can be done:


Both itemToString and renderItem are necessary when passing objects as items and they both will receive an "item" as an argument.

If you are using Typescript, you can tell the Autocomplete what is the type of your items to make these functions strongly typed. You can do that by writing the component like this <Autocomplete<ItemType> {...props}/>

Highlighting an item with getStringMatch

A common use case for Autocomplete components is to highlight in each suggestion what is typed in the input. Using the previous example, if a user types "fi" we want to show a list of suggestions where only "fi" is bold. This is possible by using the renderItem prop and the getStringMatch utility function:


Selecting multiple items


Using grouped objects as items

As an extension of "Use objects as items" section, you are also able to use a nested object to group your entries. The most important part of making this work is the shape of the grouped object. The options themselves work exactly as in the object example and require the itemToString and renderItem functions. Besides the correct shape of the object the Autocomplete component needs to receive the prop isGrouped


Error validation with FormControl


Fetching async data


Props (API reference)

Content guidelines

  • Autocomplete label should be short, contain 1 to 3 words
  • Label should be written in a sentence case (the first word capitalized, the rest lowercase)

Accessibility

  • dismisses the dropdown when selecting with the enter key