AutoComplete is an input component that provides real-time suggestions when being typed.
import { AutoComplete } from 'primereact/autocomplete';
<script src="https://unpkg.com/primereact/core/core.min.js"></script>
<script src="https://unpkg.com/primereact/autocomplete/autocomplete.min.js"></script>
AutoComplete is used as a controlled component with value and onChange properties. In addition, the component requires a list of suggestions and a completeMethod to query the results.
<AutoComplete value={selectedCountry} suggestions={filteredCountries} completeMethod={searchCountry} field="name" onChange={(e) => setSelectedCountry(e.value)} />
const countries = // datasource
const searchCountry = (event) => {
let filteredCountries = //suggestions
setFilteredCountries(filteredCountries);
}
render() {
return (
<AutoComplete value={selectedCountry} suggestions={filteredCountries} completeMethod={searchCountry} onChange={(e) => setSelectedCountry(e.value)} />
);
}
Enabling dropdown property displays a button next to the input field where click behavior of the button is defined using dropdownMode property that takes "blank" or "current" as possible values. "blank" is the default mode to send a query with an empty string whereas "current" setting sends a query with the current value of the input.
<AutoComplete dropdown value={selectedCountry} suggestions={filteredCountries} completeMethod={searchCountry} onChange={(e) => setSelectedCountry(e.value)} />
Multiple mode is enabled using multiple property used to select more than one value from the autocomplete. In this case, value reference should be an array.
<AutoComplete multiple value={selectedCountry} suggestions={filteredCountries} completeMethod={searchCountry} onChange={(e) => setSelectedCountry(e.value)} />
AutoComplete can also work with objects using the field property that defines the label to display as a suggestion. The value passed to the model would still be the object instance of a suggestion. Here is an example with a Country object that has name and code fields such as {name:"United States",code:"USA"}.
<AutoComplete field="name" value={selectedCountry} suggestions={filteredCountries} completeMethod={searchCountry} onChange={(e) => setSelectedCountry(e.value)} />
Options groups are specified with the optionGroupLabel and optionGroupChildren properties.
const groupedCities = [
{
label: 'Germany', code: 'DE',
items: [
{ label: 'Berlin', value: 'Berlin' },
{ label: 'Frankfurt', value: 'Frankfurt' },
{ label: 'Hamburg', value: 'Hamburg' },
{ label: 'Munich', value: 'Munich' }
]
},
{
label: 'USA', code: 'US',
items: [
{ label: 'Chicago', value: 'Chicago' },
{ label: 'Los Angeles', value: 'Los Angeles' },
{ label: 'New York', value: 'New York' },
{ label: 'San Francisco', value: 'San Francisco' }
]
},
{
label: 'Japan', code: 'JP',
items: [
{ label: 'Kyoto', value: 'Kyoto' },
{ label: 'Osaka', value: 'Osaka' },
{ label: 'Tokyo', value: 'Tokyo' },
{ label: 'Yokohama', value: 'Yokohama' }
]
}
];
<AutoComplete value={selectedCity} suggestions={filteredCities} completeMethod={searchCity} field="label" optionGroupLabel="label" optionGroupChildren="items" optionGroupTemplate={groupedItemTemplate} onChange={(e) => setSelectedCity(e.value)}/>
ForceSelection mode validates the manual input to check whether it also exists in the suggestions list, if not the input value is cleared to make sure the value passed to the model is always one of the suggestions. Simply enable forceSelection to enforce that input is always from the suggestion list.
<AutoComplete forceSelection value={selectedCountry} suggestions={filteredCountries} completeMethod={searchCountry} onChange={(e) => setSelectedCountry(e.value)} />
Custom content can be displayed using itemTemplate property that references a function or JSXElement or string which gets the suggestion option and returns an element. Similarly selectedItemTemplate property is available to customize the chips in multiple mode using the same approach.
<AutoComplete value={selectedCountry} suggestions={filteredCountries} completeMethod={searchCountry} onChange={(e) => setSelectedCountry(e.value)} itemTemplate={itemTemplate} />
itemTemplate(item) {
//return custom element
}
Standard HTMLSpanElement properties are passed to the wrapping div element.
In addition the component uses these properties:
Name | Type | Default | Description |
---|---|---|---|
id | string | null | Unique identifier of the element. |
value | any | null | Value of the component. |
name | string | null | Name of the input element. |
type | string | text | Type of the input element. |
suggestions | array | null | An array of suggestions to display. |
field | any | null | Field of a suggested object to resolve and display. |
optionGroupLabel | string | null | Property name or getter function to use as the label of an option group. |
optionGroupChildren | string | null | Property name or getter function that refers to the children options of option group. |
forceSelection | boolean | false | When present, autocomplete clears the manual input if it does not match of the suggestions to force only accepting values from the suggestions. |
autoHighlight | boolean | false | When enabled, highlights the first item in the list by default. |
scrollHeight | string | 200px | Maximum height of the suggestions panel. |
dropdown | boolean | false | Displays a button next to the input field when enabled. |
dropdownMode | string | blank | Specifies the behavior dropdown button. Default "blank" mode sends an empty string and "current" mode sends the input value. |
dropdownAutoFocus | boolean | true | Focus the input field when the dropdown button is clicked if enabled. |
multiple | boolean | false | Specifies if multiple values can be selected. |
showEmptyMessage | boolean | false | Whether to show the empty message or not. |
emptyMessage | string | No results found. | Text to display when there is no data. Defaults to global value in i18n translation configuration. |
minLength | number | 1 | Minimum number of characters to initiate a search. |
delay | number | 300 | Delay between keystrokes to wait before sending a query. |
style | string | null | Inline style of the component. |
className | string | null | Style class of the component. |
inputId | string | null | Identifier of the input element. |
inputStyle | string | null | Inline style of the input field. |
inputClassName | string | null | Inline style of the input field. |
panelClassName | string | null | Style class of the overlay panel element. |
panelStyle | string | null | Inline style of the overlay panel element. |
placeholder | string | null | Hint text for the input field. |
readOnly | boolean | false | When present, it specifies that the input cannot be typed. |
disabled | boolean | false | When present, it specifies that the component should be disabled. |
maxlength | number | null | Maximum number of character allows in the input field. |
size | number | null | Size of the input field. |
appendTo | DOM element | string | document.body | DOM element instance where the overlay panel should be mounted. Valid values are any DOM Element and 'self'. The self value is used to render a component where it is located. |
tabIndex | number | null | Index of the element in tabbing order. |
autoFocus | boolean | false | When present, it specifies that the component should automatically get focus on load. |
tooltip | any | null | Content of the tooltip. |
tooltipOptions | object | null | Configuration of the tooltip, refer to the tooltip documentation for more information. |
itemTemplate | any | null | Template of a list item. |
selectedItemTemplate | any | null | Template of a selected item. |
optionGroupTemplate | any | null | Template of an option group item. |
transitionOptions | object | null | The properties of CSSTransition can be customized, except for "nodeRef" and "in" properties. |
dropdownAriaLabel | string | Choose | ARIA label for the dropdown button. Defaults to placeholder then Locale "choose" label. |
dropdownIcon | any | pi pi-chevron-down | Icon class of the dropdown icon. |
removeIcon | any | pi pi-times-circle | Icon of the remove chip element in multiple mode. |
virtualScrollerOptions | object | null | Whether to use the virtualScroller feature. The properties of VirtualScroller component can be used like an object in it. |
Name | Parameters | Description |
---|---|---|
completeMethod | event.originalEvent: browser event event.query: Value to search with | Callback to invoke to search for suggestions. |
onChange | event.originalEvent: Browser event event.value: Value of the component | Callback to invoke when autocomplete value changes. |
onFocus | event: Browser event | Callback to invoke when autocomplete gets focus. |
onBlur | event: Browser event | Callback to invoke when autocomplete loses focus. |
onSelect | event.originalEvent: Browser event event.value: Value of the component | Callback to invoke when a suggestion is selected. |
onUnselect | event.originalEvent: Browser event event.value: Value of the component | Callback to invoke when a selected value is removed. |
onDropdownClick | event.originalEvent: browser event event.query: Current value of the input field | Callback to invoke to when dropdown button is clicked. |
onClick | event: Browser event | Callback to invoke on click. |
onDblClick | event: Browser event | Callback to invoke on double click. |
onMouseDown | event: Browser event | Callback to invoke to when a mouse button is pressed. |
onKeyUp | event: Browser event | Callback to invoke to when a key is released. |
onKeyPress | event: Browser event | Callback to invoke to when a key is pressed. |
onContextMenu | event: Browser event | Callback to invoke on right-click. |
onClear | event: Browser event | Callback to invoke when input is cleared by the user. |
onShow | - | Callback to invoke when overlay panel becomes visible. |
onHide | - | Callback to invoke when overlay panel becomes hidden. |
Following is the list of structural style classes
Name | Element |
---|---|
p-autocomplete | Container element |
p-autocomplete-panel | Overlay panel of suggestions. |
p-autocomplete-items | List container of suggestions. |
p-autocomplete-item | List item of a suggestion. |
p-autocomplete-token | Element of a selected item in multiple mode. |
p-autocomplete-token-icon | Close icon element of a selected item in multiple mode. |
p-autocomplete-token-label | Label of a selected item in multiple mode. |
This section is under development. After the necessary tests and improvements are made, it will be shared with the users as soon as possible.
None.
Built-in component themes created by the PrimeReact Theme Designer.
Premium themes are only available exclusively for PrimeReact Theme Designer subscribers and therefore not included in PrimeReact core.
Beautifully crafted premium create-react-app application templates by the PrimeTek design team.