Twitter0

Autocomplete

Autocomplete input and dropdown selection with filtering options.

Installation

The Autocomplete component is built with the Popover component.

ng g @spartan-ng/cli:ui autocomplete

Usage

import { HlmAutocompleteImports } from '@spartan-ng/helm/autocomplete';
<hlm-autocomplete [options]="options" />

Examples

Custom Config

Use provideHlmAutocompleteConfig to define custom configurations for the autocomplete component. This is especially useful when the autocomplete's filteredOptions contain objects rather than plain strings.

  • transformOptionToString: (option: T) => string; defines how an option should be transformed into the string displayed in the dropdown list.
  • transformValueToSearch: (option: T) => string; defines how the selected option should be transformed into the string written to the search input.

You can customize a specific instance of hlm-autocomplete by passing transformOptionToString and transformValueToSearch directly as inputs. This allows you to modify the behavior for just that instance, without affecting all autocompletes configured via provideHlmAutocompleteConfig .

Custom Option Template

You can customize the rendering of each option in the dropdown list by using the ng-template with the optionTemplate input. This is especially useful when filteredOptions are objects instead of simple strings, or when you want to display additional content such as icons, descriptions, or custom formatting alongside the option text.

Asynchronous

Form

Transform option value

Use transformOptionToValue to transform an object value into a single value and use displayWith to define how the selected value is displayed in the search input.

In the following example, a list with id: string, label: string objects is used as options. The selected option is transformed to its id using transformOptionToValue and the label is displayed in the search input using displayWith .

Brain API

BrnAutocompleteEmpty

Selector: [brnAutocompleteEmpty]

BrnAutocompleteGroup

Selector: [brnAutocompleteGroup]

Inputs

PropTypeDefaultDescription
id string `brn-autocomplete-group-${++BrnAutocompleteGroup._id}` The id of the autocomplete list

BrnAutocompleteItem

Selector: button[brnAutocompleteItem]

Inputs

PropTypeDefaultDescription
id unknown `brn-autocomplete-item-${++BrnAutocompleteItem._id}` A unique id for the item
value* (required) T - The value this item represents.
disabled boolean false Whether the item is disabled.

Outputs

PropTypeDefaultDescription
selected void - Emits when the item is selected.

BrnAutocompleteList

Selector: [brnAutocompleteList]

Inputs

PropTypeDefaultDescription
id string `brn-autocomplete-list-${++BrnAutocompleteList._id}` The id of the command list

BrnAutocompleteSearchInput

Selector: input[brnAutocompleteSearchInput]

BrnAutocomplete

Selector: [brnAutocomplete]

Inputs

PropTypeDefaultDescription
id string `brn-autocomplete-${++BrnAutocomplete._id}` The id of the autocomplete

Outputs

PropTypeDefaultDescription
valueChange T - when the selection has changed
selectionCleared void - when the selection has been cleared

Helm API

HlmAutocompleteEmpty

Selector: [hlmAutocompleteEmpty]

Inputs

PropTypeDefaultDescription
class ClassValue --

HlmAutocompleteGroup

Selector: hlm-autocomplete-group

Inputs

PropTypeDefaultDescription
class string - The user defined class

HlmAutocompleteItem

Selector: button[hlm-autocomplete-item]

Inputs

PropTypeDefaultDescription
class string - The user defined class

HlmAutocompleteList

Selector: hlm-autocomplete-list

Inputs

PropTypeDefaultDescription
class string - The user defined class

HlmAutocompleteTrigger

Selector: [hlmAutocompleteTrigger]

Inputs

PropTypeDefaultDescription
disabledTrigger boolean false Whether the trigger is disabled.

HlmAutocomplete

Selector: hlm-autocomplete

Inputs

PropTypeDefaultDescription
class ClassValue - The user defined class
autocompleteSearchClass ClassValue - Custom class for the autocomplete search container.
autocompleteInputClass ClassValue - Custom class for the autocomplete input.
autocompleteListClass ClassValue - Custom class for the autocomplete list.
autocompleteItemClass ClassValue - Custom class for each autocomplete item.
autocompleteEmptyClass ClassValue - Custom class for the empty and loading state container.
filteredOptions T[] [] The list of filtered options to display in the autocomplete.
debounceTime number this._config.debounceTime Debounce time in milliseconds for the search input.
transformValueToSearch (option: T) => string this._config.transformValueToSearch Function to transform an option value to a search string. Defaults to identity function for strings.
requireSelection boolean this._config.requireSelection Whether selection of an option is required.
transformOptionToString (option: T) => string this._config.transformOptionToString Function to transform an option value to a display string. Defaults to identity function for strings.
transformOptionToValue ((option: T) => V) | undefined this._config.transformOptionToValue Function to transform the object to the value.
displayWith ((value: V) => string) | undefined undefined Function to display the selected value as a string.
optionTemplate TemplateRef<HlmAutocompleteOption<T>> - Optional template for rendering each option.
loading boolean false Whether the autocomplete is in a loading state.
showClearBtn boolean this._config.showClearBtn Whether to show the clear button when a option is selected.
searchPlaceholderText unknown Select an option Placeholder text for the input field.
loadingText unknown Loading options... Text to display when loading options.
emptyText unknown No options found Text to display when no options are found.
ariaLabelToggleButton unknown Toggle options Aria label for the toggle button.
inputId unknown `hlm-autocomplete-input-${++HlmAutocomplete._id}` The id of the input field.
disabled boolean false Whether the autocomplete is disabled.
value T | V - The selected value.
search string - The search query.

Outputs

PropTypeDefaultDescription
valueChange T | V | null - Emitted when the selected value changes.
searchChange string - Emitted when the search query changes.
valueChanged T | V - The selected value.
searchChanged string - The search query.
Avatar Aspect Ratio