Skip to main content

Autocomplete

A simple autocomplete select component.


Usage

import { Autocomplete } from "@newfold/ui-component-library";

<Autocomplete
id="autocomplete-input"
onChange={handleChange}
onQueryChange={handleQueryChange}
placeholder="Type to autocomplete options"
value=""
>
<Autocomplete.Option value="1">
Option 1
</Autocomplete.Option>
<Autocomplete.Option value="2">
Option 2
</Autocomplete.Option>
<Autocomplete.Option value="3">
Option 3
</Autocomplete.Option>
</Autocomplete>

Label

An example with a label using label prop.

<Autocomplete
id="autocomplete-input"
onChange={handleChange}
onQueryChange={handleQueryChange}
placeholder="Type to autocomplete options"
value=""
label="Select an Option"
>
<Autocomplete.Option value="1">
Option 1
</Autocomplete.Option>
<Autocomplete.Option value="2">
Option 2
</Autocomplete.Option>
<Autocomplete.Option value="3">
Option 3
</Autocomplete.Option>
</Autocomplete>

Placeholder

An example with placeholder using placeholder prop.

<Autocomplete
id="autocomplete-input"
onChange={handleChange}
onQueryChange={handleQueryChange}
value=""
label="Select an Option"
placeholder="A custom placeholder"
>
<Autocomplete.Option value="1">
Option 1
</Autocomplete.Option>
<Autocomplete.Option value="2">
Option 2
</Autocomplete.Option>
<Autocomplete.Option value="3">
Option 3
</Autocomplete.Option>
</Autocomplete>

Selected Label

An example with default value using selectedLabel prop.

<Autocomplete
id="autocomplete-input"
onChange={handleChange}
onQueryChange={handleQueryChange}
value="2"
label="Select an Option"
placeholder="Type to autocomplete options"
selectedLabel="Option 2"
>
<Autocomplete.Option value="1">
Option 1
</Autocomplete.Option>
<Autocomplete.Option value="2">
Option 2
</Autocomplete.Option>
<Autocomplete.Option value="3">
Option 3
</Autocomplete.Option>
</Autocomplete>

Nullable

Allow empty values with reset button X or deleting the option and clicking outside the field.

<Autocomplete
id="autocomplete-input"
onChange={handleChange}
onQueryChange={handleQueryChange}
value=""
label="Select an Option"
placeholder="Type to autocomplete options"
selectedLabel=""
>
<Autocomplete.Option value="1">
Option 1
</Autocomplete.Option>
<Autocomplete.Option value="2">
Option 2
</Autocomplete.Option>
<Autocomplete.Option value="3">
Option 3
</Autocomplete.Option>
</Autocomplete>

onChange

The onChange prop is a function that will be called when the value of the Autocomplete component changes. The function will receive the new value of the Autocomplete component as a parameter.

<Autocomplete
id="autocomplete-input"
onChange={new_val => console.log(new_val)}
onQueryChange={handleQueryChange}
>
<Autocomplete.Option value="1">
Option 1
</Autocomplete.Option>
<Autocomplete.Option value="2">
Option 2
</Autocomplete.Option>
<Autocomplete.Option value="3">
Option 3
</Autocomplete.Option>
</Autocomplete>

onQueryChange

The onQueryChange prop is a function that will be called when the query of the Autocomplete component changes. The function will receive the new query event of the Autocomplete component as a parameter.

<Autocomplete
id="autocomplete-input"
onChange={(new_val) => console.log(new_val)}
onQueryChange={(event) => console.log(event.target.value)}
>
<Autocomplete.Option value="1">
Option 1
</Autocomplete.Option>
<Autocomplete.Option value="2">
Option 2
</Autocomplete.Option>
<Autocomplete.Option value="3">
Option 3
</Autocomplete.Option>
</Autocomplete>

Props

AttributeTypeDescriptionDefault
id*string | HTML id attribute.-
valuestring | number | bool | --
placeholderstring | --
childrennode | Pass `Autocomplete.Option` components as children-
selectedLabelstring | The label of the selected option.-
labelstring | The label of the autocomplete input.-
labelPropsobject | --
labelSuffixnode | --
onChange*function | Callback function that is fired when an item has been selected.-
onQueryChange*function | Callback function that is fired when the query changes.-
classNamestring | --
nullablebool | If true, the user can clear the selected value with `X` button.-
buttonPropsobject | Props to be passed to the `X` button element.-
clearButtonScreenReaderTextstring | Screen reader text for the clear button.Clear

Hello From Root