Developer Programs

Learn

Docs

Autocomplete Input

Components > Editing Data > Autocomplete Input
Use this component to...
Prompt the user for a text value, showing them possible matches while they type

Overview

Use the autocomplete component to prompt the user for a text value, showing them possible matches in a dropdown menu while they type.

The user can quickly select an item from the menu rather than having to type out the whole word, which can be a real time saver.


Development

Web component development

Component reference

autocomplete
autocomplete
Module: AutoCompleteModule - Package: primeng
⚠️ This component requires the use of Angular and will not work in non-Angular applications.

Provides predictive autocomplete list for your text list editor

View the autocomplete reference doc

Implementation

Begin by importing the AutoCompleteModule module into your application.

Import the module
import { AutoCompleteModule } from 'primeng/autocomplete';

Then set the formControlName and bind suggestions to a list of all string values possible for the field. Set the inputStyleClass to rui-form-control. Optionally, you can bind dropdown to true if you want to show the dropdown arrow. Otherwise, just typing in the input will display the dropdown menu. Finally, bind completeMethod to your backing method to build out your filtered list.

Autocomplete HTML
<p-autoComplete formControlName="country" inputStyleClass="rui-form-control" [dropdown]="true" [suggestions]="filteredCountries" (completeMethod)="filterCountry($event)" />

In the typescript, create an AutoCompleteCompleteEvent interface and add a property to hold the filtered list. Then create the method called during the completeMethod event and update the filtered list based on the input.

Autocomplete TS
interface AutoCompleteCompleteEvent {
    originalEvent: Event;
    query: string;
}
...
public filteredCountries: any[] | undefined;
...
public filterCountry(event: AutoCompleteCompleteEvent) {
    let filtered: any[] = [];
    let query = event.query;

    for (let i = 0; i < (this.countryList as any[]).length; i++) {
        let country = (this.countryList as any[])[i];
        if (country.toLowerCase().indexOf(query.toLowerCase()) == 0) {
            filtered.push(country);
        }
    }

    this.filteredCountries = filtered;
}

Angular component development

Component reference

autocomplete
autocomplete
Module: AutoCompleteModule - Package: primeng

Provides predictive autocomplete list for your text list editor

View the autocomplete reference doc

Implementation

Begin by importing the AutoCompleteModule module into your application.

Import the module
import { AutoCompleteModule } from 'primeng/autocomplete';

Then set the formControlName and bind suggestions to a list of all string values possible for the field. Set the inputStyleClass to rui-form-control. Optionally, you can bind dropdown to true if you want to show the dropdown arrow. Otherwise, just typing in the input will display the dropdown menu. Finally, bind completeMethod to your backing method to build out your filtered list.

Autocomplete HTML
<p-autoComplete formControlName="country" inputStyleClass="rui-form-control" [dropdown]="true" [suggestions]="filteredCountries" (completeMethod)="filterCountry($event)" />

In the typescript, create an AutoCompleteCompleteEvent interface and add a property to hold the filtered list. Then create the method called during the completeMethod event and update the filtered list based on the input.

Autocomplete TS
interface AutoCompleteCompleteEvent {
    originalEvent: Event;
    query: string;
}
...
public filteredCountries: any[] | undefined;
...
public filterCountry(event: AutoCompleteCompleteEvent) {
    let filtered: any[] = [];
    let query = event.query;

    for (let i = 0; i < (this.countryList as any[]).length; i++) {
        let country = (this.countryList as any[])[i];
        if (country.toLowerCase().indexOf(query.toLowerCase()) == 0) {
            filtered.push(country);
        }
    }

    this.filteredCountries = filtered;
}

Design

Figma design

Figma design info
You can find this component in the Components - Forms page in the Figma UI Kit.
Dev ComponentDesign Component Name
Text inputJHA / Forms / Text Input; set State to “Watermark” if you want to show watermark text instead of user input, otherwise leave State as “Normal”

Adobe XD design

Adobe XD design info
You can find this component in these artboards in the Adobe XD design samples:
  • Sample App - Editing Form Data
Dev ComponentDesign Component Name
Text input

JHA / Forms / Text Input

  • Select the “Watermark Text” state in the component if you want to show watermark text instead of user input.

Support options
Have questions on this topic?
Join the Responsive UI team in Microsoft Teams to connect with the community.
See something in this page that needs to change?
Send us feedback on this page.
Last updated Thu Jun 27 2024