Autocomplete
This component allows the user to select from suggested options
Basic Usage
import { Autocomplete } from '@krakentech/coral';
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
]}
/>;Properties
defaultValue
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
]}
defaultValue="Lobster"
/>disabled
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
]}
disabled
/>error / errorMessage
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
]}
error
errorMessage="Please select an energy provider"
/>Please select an energy provider
filterOptions
You can apply further filtering to the options list with this function. This is useful when requesting options from an API, for example, to perhaps filter out empty values. See here for a complete exampleΒ .
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
{ value: 'Dismiss me', label: 'Dismiss me' },
{ value: 'Dismiss me too', label: 'Dismiss me too' },
]}
// Return all options that do not contain "Dismiss me"
filterOptions={(option?: string) => !option.includes('Dismiss me')}
/>isLoading / loadingText
If you are loading your options from an API, you can use isLoading and loadingText to provide a message to the end user whilst the API call completes. This example simulates a 2s delay on the API call when you type into the box. You can try typing Lobster.
<Autocomplete
label="Energy providers"
options={asyncOptions}
isLoading
loadingText="Loading energy providers..."
/>noDataLabel
<Autocomplete
label="Energy providers"
options={[]}
noDataLabel="No options, sorry!"
/>optional
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
]}
optional
/>theme
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
]}
theme="secondary500"
/>value (for controlled input)
const [selectedValue, setSelectedValue] = useState(options[0].label);
<Button
onClick={() => { setSelectedValue(...) }}
>
{...}
</Button>
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
]}
value={selectedValue}
onChange={(value) => setSelectedValue(value)}
/>Event Handlers
<Autocomplete
onBlur={() => {}}
onChange={(label, value, reason) => {}}
onClose={(reason) => {}}
onHighlightChange={(label, value, reason) => {}}
onInputChange={(label, value, reason) => {}}
onItemClick={(label, value, reason) => {}}
onClear={() => {}}
onOpen={() => {}}
...
/>Responsiveness
<Autocomplete
label="Energy providers"
options={[
{ value: 'lobster', label: 'Lobster' },
{ value: 'prawn', label: 'Prawn' },
{ value: 'crab', label: 'Crab' },
{ value: 'octopus', label: 'Octopus' },
]}
theme={{ base: 'light', lg: 'dark' }}
fullWidth={{ base: false, lg: true }}
/>Asyncronous requests from an external API
Please visit this example in our SandboxΒ . This can be seen live at the bottom of this pageΒ .
Full API
| Name | Type | Default |
|---|---|---|
defaultValue | stringIf set, a default value will be set for the input. | |
disabled | booleanIf | false |
dropdownButtonOpenText | stringThe accessible text to display to screenreaders, labelling the dropdown toggle button when the menu is closed. | Open |
dropdownButtonCloseText | stringThe accessible text to display to screenreaders, labelling the dropdown toggle button when the menu is open. | Close |
error | booleanIf | false |
errorMessage | stringIf set, the | |
fullWidth | ResponsiveVariant<boolean>If | |
id | stringAn accessible ID for the input element. If not provided, one will be generated internally. | |
name | stringThe field name used on the POST request when the form is submitted. | |
noDataLabel | stringThe text to display when the user has entered a search term, but there are no matching options. | 'No options' |
onBlur | () => voidCallback fired whenever the input element loses user focus. | |
onChange | (label: string | null, value: string | null, reason: AutocompleteChangeReason) => voidCallback fired whenever the selected option changes.
| |
onClose | (reason: AutocompleteCloseReason) => voidCallback fired whenever the combobox menu is closed.
| |
onHighlightChange | (label: string | null, value: string | null, reason: AutocompleteHighlightChangeReason) => voidCallback fired whenever the highlighted menu option changes.
| |
onInputChange | (label: string | null, value: string | null, reason: AutocompleteInputChangeReason) => voidCallback fired whenever the input value changes.
| |
onItemClick | (label: string | null | undefined, value: string | null | undefined, reason: string) => voidCallback fired whenever a customer explicitly clicks on a dropdown item.
| |
onClear | () => voidCallback fired whenever the clear icon is clicked on the input. | |
onOpen | () => voidCallback fired whenever the combobox menu is opened. | |
options | AutocompleteOption[]The options to display in the Autocomplete dropdown.
Each option should have a | |
label | stringThe label to display inside the TextField. | |
optional | booleanAdd an optional flag to the label for this field (controlled by https://coral-oe.vercel.app/?path=/docs/guides-configurationβdocs#overrides-optional-indicatorΒ optionalIndicator). This should be set to true if you have set this field to be optional in your validation schema. | false |
theme | ResponsiveVariant<"secondary500" | "base100">The colourway applied to this component. | |
value | string | null
| |
isLoading | booleanIf | |
loadingText | stringThe text to display when the Autocomplete is in a loading state. | |
filterOptions | (option?: string | undefined, inputValue?: string | undefined) => booleanA function that filters the options list based on the supplied option and input value. |
Design
Good to know
Choose an auto-complete if you have a large amount of data you need the user to select from quickly and easily. It should present a list of options, and also save time by providing predictions as they begin to type, that match the data available.
Why donβt we indicate mandatory fields?
It is a better user experience when forms are explicit about optional fields by labelling them so, that way the user can be reassured they can proceed without completing it. Therefore in Coral our mandatory fields are the default and optional fields are marked as such.
What is the difference between an auto-complete and an accordion?
The accordion will push down the content, while the auto-complete lays over the content, this content is also selectable.
Component breakdown
- Corner radius: 12px
- Border: 2px
- Input text: Body 1
- Label container: Caption text
- Horizontal padding: 16px | Vertical padding: 16px