File size: 2,554 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Suggestions

Suggestions is a component which displays suggested search terms.

## Usage

`Suggestions` is passed an array of `suggestions` which will display in a list.
A suggestion whose `label` property matches the `query` prop will be highlighted.

```jsx
import { Suggestions } from '@automattic/components';
import React, { useCallback, useMemo, useState } from 'react';
import FormTextInput from 'calypso/components/forms/form-text-input';

export default function SuggestionsExample() {
	const [ query, setQuery ] = useState( '' );
	const updateInput = useCallback( ( e ) => setQuery( e.target.value ), [ setQuery ] );

	const suggestions = useMemo( () => {
		if ( ! query ) {
			return [];
		}
		const allSuggestions = [ 'Foo', 'Bar', 'Baz' ].map( ( s ) => ( { label: s, value: s } ) );
		const r = new RegExp( query, 'i' );
		return allSuggestions.filter( ( { label } ) => r.test( label ) );
	}, [ query ] );

	return (
		<div className="docs__suggestions-container">
			<FormTextInput
				value={ query }
				onChange={ updateInput }
				autoComplete="off"
				autoCorrect="off"
				autoCapitalize="off"
				spellCheck={ false }
				placeholder="Type Foo, Bar or Baz…"
			/>
			<Suggestions
				query={ query }
				suggestions={ suggestions }
				suggest={ ( ...args ) => {
					// eslint-disable-next-line no-console
					console.log( 'Suggest callback invoked with args: %o', args );
				} }
			/>
		</div>
	);
}
```

The suggestion list also supports headings by adding a category field to the suggestions. Suggestions with the same category value are grouped together under the heading. Suggestions with no category will always appear at the top of the list.

For example:

```jsx
const FoodSuggestions = React.forwardRef( ( props, ref ) => (
	<Suggestions
		ref={ ref }
		query=""
		suggest={ props.suggest }
		suggestions={ [
			{ label: 'Oats' },
			{ label: 'Apple', category: 'Fruit' },
			{ label: 'Orange', category: 'Fruit' },
			{ label: 'Carrot', category: 'Vegetable' },
		] }
	/>
) );
```

## Props

The following props are available:

- `query`: (string) The search query that the suggestions are based on. Will be highlighted in the suggestions.
- `suggestions`: ({label: string, category?: string, ...otherProps}[]) An array of possible suggestions that match the query, made of objects of the shape `{ label: 'Label', category: 'This is optional' }
- `suggest`: A function that is called when the suggestion is selected.
- `title`: A string that gets inserted between the input and the list of suggestions, as a title.