4.65.5 • Published 9 days ago

@contentful/f36-autocomplete v4.65.5

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

title: 'Autocomplete' slug: /components/autocomplete/ github: 'https://github.com/contentful/forma-36/tree/main/packages/components/autocomplete' storybook: 'https://f36-storybook.contentful.com/?path=/story/components-autocomplete--basic'

typescript: ./src/Autocomplete.tsx

Search through a long list 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

It is also possible to use the Autocomplete as multiselect. To improve the user experience, you can keep the dropdown open after selection by setting the "closeAfterSelect" property to false.

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

Fully controlled selection from outside

In order to use proper form validation you need to be able to control the actual input field from the autocomplete, because the search query value is not the actual selection. This is done via the selectedItem property.

Error validation with FormControl

Fetching async data

Custom icon

Pass a custom icon to the text input, example: to indicate a search input

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
4.65.5

9 days ago

4.65.4

11 days ago

4.65.3

12 days ago

4.65.2

12 days ago

4.65.1

15 days ago

4.65.0

18 days ago

4.63.0

30 days ago

4.64.0

30 days ago

4.62.0

1 month ago

4.61.0

2 months ago

4.60.3

2 months ago

4.60.2

2 months ago

4.60.1

3 months ago

4.60.0

3 months ago

4.59.3

3 months ago

4.59.2

3 months ago

4.59.1

3 months ago

4.59.0

3 months ago

4.58.4

4 months ago

4.58.3

4 months ago

4.58.2

4 months ago

4.58.1

4 months ago

4.58.0

4 months ago

4.57.0

4 months ago

4.56.1

5 months ago

4.56.2

5 months ago

4.56.0

5 months ago

4.55.1

5 months ago

4.55.0

5 months ago

4.54.5

5 months ago

4.54.4

5 months ago

4.49.1

9 months ago

4.49.0

9 months ago

4.52.1

7 months ago

4.52.2

7 months ago

4.52.0

8 months ago

4.52.3

7 months ago

4.47.0

10 months ago

4.48.2

9 months ago

4.48.1

9 months ago

4.48.0

10 months ago

4.51.0

8 months ago

4.54.0

6 months ago

4.54.3

6 months ago

4.54.1

6 months ago

4.54.2

6 months ago

4.53.0

7 months ago

4.46.0

10 months ago

4.47.3

10 months ago

4.47.2

10 months ago

4.47.1

10 months ago

4.50.0

8 months ago

4.50.1

8 months ago

4.50.2

8 months ago

4.45.0

11 months ago

4.44.0

11 months ago

4.37.1

1 year ago

4.37.0

1 year ago

4.40.3

12 months ago

4.40.2

12 months ago

4.40.1

12 months ago

4.40.0

12 months ago

4.40.7

12 months ago

4.40.6

12 months ago

4.40.5

12 months ago

4.40.4

12 months ago

4.36.0

1 year ago

4.39.0

1 year ago

4.42.0

11 months ago

4.38.1

1 year ago

4.38.0

1 year ago

4.41.1

12 months ago

4.41.0

12 months ago

4.43.0

11 months ago

4.35.0

1 year ago

4.31.0

1 year ago

4.30.2

1 year ago

4.30.1

1 year ago

4.30.0

1 year ago

4.30.3

1 year ago

4.33.0

1 year ago

4.29.0

1 year ago

4.32.0

1 year ago

4.34.1

1 year ago

4.34.0

1 year ago

4.28.0

1 year ago

4.27.0

1 year ago

4.25.0

1 year ago

4.26.0

1 year ago

4.24.1

1 year ago

4.25.1

1 year ago

4.25.2

1 year ago

4.21.4

2 years ago

4.24.0

1 year ago

4.21.5

2 years ago

4.21.6

1 year ago

4.21.7

1 year ago

4.21.8

1 year ago

4.23.0

1 year ago

4.23.1

1 year ago

4.23.2

1 year ago

4.22.0

1 year ago

4.23.3

1 year ago

4.21.1

2 years ago

4.21.2

2 years ago

4.21.3

2 years ago

4.21.0

2 years ago

5.0.0

2 years ago

4.20.9

2 years ago

4.18.0

2 years ago

4.17.0

2 years ago

4.20.2

2 years ago

4.20.3

2 years ago

4.20.4

2 years ago

4.20.5

2 years ago

4.20.0

2 years ago

4.20.1

2 years ago

4.16.0

2 years ago

4.20.6

2 years ago

4.20.7

2 years ago

4.20.8

2 years ago

4.19.0

2 years ago

4.19.1

2 years ago

4.19.2

2 years ago

4.15.1

2 years ago

4.15.0

2 years ago

4.10.5

2 years ago

4.10.1

2 years ago

4.10.2

2 years ago

4.10.3

2 years ago

4.14.0

2 years ago

4.10.4

2 years ago

4.13.0

2 years ago

4.12.0

2 years ago

4.11.0-beta.0

2 years ago

4.11.0

2 years ago

4.10.0

2 years ago

4.9.0

2 years ago

4.8.1

2 years ago

4.8.2

2 years ago

4.4.1

2 years ago

4.8.0

2 years ago

4.7.0

2 years ago

4.7.1

2 years ago

4.6.1

2 years ago

4.6.0

2 years ago

4.6.2

2 years ago

4.5.0

2 years ago

4.4.0

2 years ago

4.3.11

2 years ago

4.3.10

2 years ago

4.2.11

2 years ago

4.3.9

2 years ago

4.2.10

2 years ago

4.2.5

2 years ago

4.2.7

2 years ago

4.2.6

2 years ago

4.2.9

2 years ago

4.2.8

2 years ago

4.1.8

2 years ago

4.1.7

2 years ago

4.1.9

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.2

2 years ago

4.0.1-beta.2705

2 years ago

4.0.1-beta.2590

2 years ago

4.0.1-beta.2595

2 years ago

4.2.3

2 years ago

4.2.2

2 years ago

4.2.4

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.10

2 years ago

4.1.11

2 years ago

4.0.1-beta.2696

2 years ago

4.0.1-beta.2694

2 years ago

4.1.4

2 years ago

4.1.3

2 years ago

4.1.6

2 years ago

4.1.5

2 years ago

4.1.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

4.0.1-beta.2524

2 years ago

4.0.1-beta.2522

2 years ago

4.0.1-beta.2529

2 years ago

4.0.1-beta.2527

2 years ago

4.0.1-beta.2555

2 years ago

4.0.1-beta.2550

2 years ago

4.0.1-beta.2558

2 years ago

4.0.1-beta.2515

2 years ago

4.0.1-beta.2543

2 years ago

4.0.1-beta.2500

3 years ago

4.0.1-beta.2542

2 years ago

4.0.1-beta.2540

2 years ago

4.0.1-beta.2505

3 years ago

4.0.1-beta.2506

3 years ago

4.0.1-beta.2534

2 years ago

4.0.1-beta.2533

2 years ago

4.0.1-beta.2530

2 years ago

4.0.1-beta.2531

2 years ago

4.0.1-beta.2496

3 years ago

4.0.1-beta.2538

2 years ago

4.0.1-beta.2539

2 years ago

4.0.1-beta.2536

2 years ago

4.0.1-beta.2482

3 years ago

4.0.1-beta.2483

3 years ago

4.0.1-beta.2489

3 years ago

4.0.1-beta1.2482

3 years ago