Autocomplete

import Autocomplete from 'frui/fields/Autocomplete';
Copy

Props

The following props are accepted by Autocomplete.

NameTypeRequiredNotes
classNamestringNoStandard HTML class names
defaultValuestringNoAlias to value
errorstring|booleanNoAny error message
namestringNoUsed for react server components.
onChangeFunctionNoEvent handler when value has changed
onDropdownFunctionNoEvent handler when dropdown opens/closes
onQueryFunctionNoEvent handler when something is searched
onUpdateFunctionNoUpdate event handler
optionsstring[]NoList of select options.
styleCSS ObjectNoStandard CSS object
valuestringNoSelected value from the options

Basics

The following is a basic example of an Autocomplete field.

<Autocomplete 
  className="w-full" 
  options={[ 'foo', 'bar' ]} 
  placeholder="Enter foo or bar"
/>
Copy

Events

The following example makes use of all the possible events for Autocomplete.

<Autocomplete 
  className="w-full" 
  options={['foo', 'bar']}
  onQuery={(query, set) => setTimeout(
    () => set(['boo', 'bar', 'baz']), 
    1000
  )}
  onDropdown={open => console.log('dropdown', open)}
  onChange={e => console.log('change', e)}
  onUpdate={value => console.log('update', value)}
  placeholder="Enter 'b'"
/>
Copy

On Change

The onChange event is triggered when the value has changed. The following arguments are passed to the event handler:

NameTypeSample
eventEvent Objectsee: Change Event

On Dropdown

The onDropdown event is triggered when the dropdown opens or closes. The following arguments are passed to the event handler:

NameTypeSample
openbooleantrue

On Query

The onQuery event is triggered when the user searches for something. The following arguments are passed to the event handler:

NameTypeSample
querystring`foobar`
setOptionsFunction
set(['boo', 'bar', 'baz'])
Copy

On Update

The onUpdate event is triggered when the value has been updated. The following arguments are passed to the event handler:

NameTypeSample
valuestring`foobar`

Errors

You can pass the error prop to highlight the Autocomplete field red.

<Autocomplete 
  error
  className="w-full" 
  options={[ 'foo', 'bar' ]} 
  placeholder="Enter foo or bar"
/>
Copy

Custom Styles

You can add your own custom class to Autocomplete components or use any combination of frui-field-autocomplete, frui-field-autocomplete-dropdown, frui-field-autocomplete-options, and frui-field-autocomplete-option CSS classes.