Twitter2.3k

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 { BrnPopoverContent } from '@spartan-ng/brain/popover';
import { HlmAutocompleteImports } from '@spartan-ng/helm/autocomplete';
<hlm-autocomplete [(search)]="search">
  <hlm-autocomplete-input placeholder="Search tags" />
  <div *brnPopoverContent hlmAutocompleteContent>
  	<hlm-autocomplete-empty>No tags found.</hlm-autocomplete-empty>
    <div hlmAutocompleteList>
      @for (option of filteredOptions(); track $index) {
      	<hlm-autocomplete-item [value]="option"> {{ option }} </hlm-autocomplete-item>
      }
    </div>
  </div>
</hlm-autocomplete>

Configuration

The autocomplete can be configured via provideBrnAutocompleteConfig or by passing the individual config as input. This is the default autocomplete config:

import { provideBrnAutocompleteConfig } from '@spartan-ng/brain/autocomplete';

provideBrnAutocompleteConfig({
	isItemEqualToValue: (itemValue: T, selectedValue: T | null) => Object.is(itemValue, selectedValue),
	itemToString: undefined,
});

Objects

The autocomplete works out of the box with string values and objects in the shape of { label: string; value: string; } , the label (1) or the value (2) will be used automatically. For other object shapes provide a custom itemToString function to extract the label from the object.

Provide a custom isItemEqualToValue function to determine if an autocomplete item value matches the current selected value. Defaults to Object.is comparison.

Examples

With Clear Button

Disabled

Auto highlight

Tag:

With Groups

With Group and Separators

Object (custom label)

Customize the selected value for object values by providing a custom itemToString function.

Object (id value)

Use itemToString to resolve the id value as the display string name .

Use hlm-autocomplete-search to allow free-form text input along with option selection. The selected option is transformed to string via itemToString .

Tag:

Form

Form (Free-form text)

Create a new tag if it doesn't exist.

Brain API

BrnAutocompleteAnchor

Selector: [brnAutocompleteAnchor]

BrnAutocompleteClear

Selector: [brnAutocompleteClear]

BrnAutocompleteContent

Selector: [brnAutocompleteContent]

BrnAutocompleteEmpty

Selector: [brnAutocompleteEmpty]

BrnAutocompleteGroup

Selector: [brnAutocompleteGroup]

BrnAutocompleteInputWrapper

Selector: [brnAutocompleteInputWrapper]

BrnAutocompleteInput

Selector: input[brnAutocompleteInput]

ExportAs: brnAutocompleteInput

Inputs

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

BrnAutocompleteItem

Selector: [brnAutocompleteItem]

Inputs

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

BrnAutocompleteLabel

Selector: [brnAutocompleteLabel]

Inputs

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

BrnAutocompleteList

Selector: [brnAutocompleteList]

Inputs

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

BrnAutocompleteSearch

Selector: [brnAutocomplete]

Inputs

PropTypeDefaultDescription
disabled boolean false Whether the autocomplete is disabled
itemToString AutocompleteItemToString<T> | undefined this._config.itemToString A function to convert an item to a string for display.
autoHighlight boolean this._config.autoHighlight Whether to auto-highlight the first matching item.
value string | null null The selected value of the autocomplete.
search string - The current search query.

Outputs

PropTypeDefaultDescription
valueChanged string | null null The selected value of the autocomplete.
searchChanged string - The current search query.

BrnAutocompleteSeparator

Selector: [brnAutocompleteSeparator]

Inputs

PropTypeDefaultDescription
orientation 'horizontal' | 'vertical' horizontal -

BrnAutocompleteStatus

Selector: [brnAutocompleteStatus]

BrnAutocomplete

Selector: [brnAutocomplete]

Inputs

PropTypeDefaultDescription
disabled boolean false Whether the autocomplete is disabled
isItemEqualToValue AutocompleteItemEqualToValue<T> this._config.isItemEqualToValue A function to compare an item with the selected value.
itemToString AutocompleteItemToString<T> | undefined this._config.itemToString A function to convert an item to a string for display.
autoHighlight boolean this._config.autoHighlight Whether to auto-highlight the first matching item.
value T | null null The selected value of the autocomplete.
search string - The current search query.

Outputs

PropTypeDefaultDescription
valueChanged T | null null The selected value of the autocomplete.
searchChanged string - The current search query.

Helm API

HlmAutocompleteContent

Selector: [hlmAutocompleteContent]

HlmAutocompleteEmpty

Selector: [hlmAutocompleteEmpty],hlm-autocomplete-empty

HlmAutocompleteGroup

Selector: [hlmAutocompleteGroup]

HlmAutocompleteInput

Selector: hlm-autocomplete-input

Inputs

PropTypeDefaultDescription
placeholder string --
showSearch boolean true -
showClear boolean false -
aria-invalid boolean false -

HlmAutocompleteItem

Selector: hlm-autocomplete-item

HlmAutocompleteLabel

Selector: [hlmAutocompleteLabel]

HlmAutocompleteList

Selector: [hlmAutocompleteList]

HlmAutocompleteSearch

Selector: [hlmAutocompleteSearch],hlm-autocomplete-search

HlmAutocompleteSeparator

Selector: [hlmAutocompleteSeparator]

HlmAutocompleteStatus

Selector: [hlmAutocompleteStatus],hlm-autocomplete-status

HlmAutocomplete

Selector: [hlmAutocomplete],hlm-autocomplete

Avatar Aspect Ratio