Autocomplete
React Bootstrap 5 Autocomplete component
Autocomplete component predicts the words being typed based on the first few letters given by the user. You can search the list using basic scroll and the keyboard arrows
Note: Read the API tab to find all available options and advanced customization
Basic example
The dataFilter
property is required in order for component to work properly. The option
accepts a function that is expected to return an array of results or a
Promise
that resolves to an array of results.
Controlled value
The value
property allows to control the value of the input.
Add a handler to the onChange
event to update the outer state.
Controlled open state
The open
property allows to control the visibility of the result list.
Display value
The displayValue
property allow to separate oryginal result value from the value
that will be displayed in the result list or input (after selection). Its useful when the data
returned by the dataFilter
function is an array of objects. You can specify which
parameter of the object should be displayed.
Asynchronous search
The function passed to the dataFilter
property can return a Promise
that
resolves to an array of results. By default the component expects to receive data as an array
of strings. If you want to return an array of objects instead, you can use
displayValue
property to specify which parameter should be used as a display value
of the specific result.
Threshold
Use threshold
property to specify a minimum number of the characters in the input
field needed to perform a search operation.
Custom item template
The itemContent
property allow to display custom HTML in the result list.
Custom content
A custom content container with a class .autocomplete-custom-content will be displayed at the end of the autocomplete-custom-item-subtitle dropdown. You can use it to display a number of search results.
Validation
Autocomplete - API
Import
Properties
MDBAutocomplete
Name | Type | Default | Description | Example |
---|---|---|---|---|
autoSelect
|
boolean | false |
Enables auto selecting on Tab press |
<MDBAutocomplete autoSelect />
|
customContent
|
ReactNode | - |
Custom HTML template that will be displayed at the end of the component dropdown |
<MDBAutocomplete customContent={element} />
|
data
|
string[] | Record |
[] |
Autocomplete data state |
<MDBAutocomplete data={defaultData} />
|
listHeight
|
string | '190px' |
The autcomplete list height |
<MDBAutocomplete listHeight="200px" />
|
isLoading
|
boolean | false |
Enables input spinner |
<MDBAutocomplete isLoading={isLoading} />
|
open
|
boolean | - |
Enables controlling the open state |
<MDBAutocomplete open={true} />
|
noResults
|
string | 'No results found' |
Text displayed with no matching results |
<MDBAutocomplete noResults='-' />
|
ref
|
React.Ref |
undefined |
Reference to the input element |
<MDBInput ref={inputReference} />
|
Events
Name | Type | Description |
---|---|---|
displayValue
|
(row: Record |
Function executed for complex search results, to get value to display in the results list |
itemContent
|
(value: Record |
Function that returns custom template for result item |
onClose
|
() => void | Callback fired when the component requests to be closed |
onClosed
|
() => void | Executed when closing transition is over |
onOpen
|
() => void | Callback fired when the component requests to be opened |
onOpened
|
() => void | Executed when opening transition is over |
onSelect
|
(itemData: string | Record |
Executed when option selected |
onSearch
|
(data: string) => void | Executed when the autocomplete value changes |