Combobox

An input combined with a list of predefined items to select.

Installation

ng g @spartan-ng/cli:ui combobox

Usage

import { HlmComboboxImports } from '@spartan-ng/helm/combobox';
<hlm-combobox>
  <hlm-combobox-input placeholder="Select a framework" />
  <hlm-combobox-content *hlmComboboxPortal>
    <hlm-combobox-empty>No items found.</hlm-combobox-empty>
    <div hlmComboboxList>
      @for (framework of frameworks; track $index) {
      	<hlm-combobox-item [value]="framework">{{ framework.label }}</hlm-combobox-item>
      }
    </div>
  </hlm-combobox-content>
</hlm-combobox>

Configuration

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

import { comboboxContainsFilter, provideBrnComboboxConfig } from '@spartan-ng/brain/combobox';

provideBrnComboboxConfig({
	filterOptions: {
		usage: 'search',
		sensitivity: 'base',
		ignorePunctuation: true,
	},
	filter: (itemValue: T, search: string, collator: Intl.Collator, itemToString?: ComboboxItemToString<T>) =>
		comboboxContainsFilter(itemValue, search, collator, itemToString),
	isItemEqualToValue: (itemValue: T, selectedValue: T | null) => Object.is(itemValue, selectedValue),
	itemToString: undefined,
});

Filter

The combobox matches items using Intl.Collator for robust string comparison. The filterOptions accepts all Intl.CollatorOptions , plus a locale .

The default filter implementation uses a comboboxContainsFilter to check if the item value contains the search string. Provide a custom filter function for other filtering behavior.

Built-in combobox filters are:

  • comboboxContainsFilter - Returns true if the item value contains the search string.
  • comboboxStartsWithFilter - Returns true if the item value starts with the search string.
  • comboboxEndsWithFilter - Returns true if the item value ends with the search string.

Objects

The combobox 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 a combobox item value matches the current selected value. Defaults to Object.is comparison.

Examples

Multiple select

Angular

Objects (itemToString)

The itemToString function transforms an item object into a display string. For example, it can resolve an id value to its corresponding name for display.

With Clear Button

Disabled (single)

Disabled (multiple)

AnalogAstro

Auto highlight (single)

Auto highlight (multiple)

Angular

With Groups

With Groups and Separators

With Icon Addon

Invalid

Angular

Async search (single)

Async search (multiple)

Form (single)

Form (popover)

Form (multiple)

Analog

RTL

To enable RTL support in spartan-ng, see the RTL configuration guide.

الفئاتالتكنولوجيا

Brain API

BrnComboboxAnchor

Selector: [brnComboboxAnchor]

ExportAs: brnComboboxAnchor

BrnComboboxChipInput

Selector: input[brnComboboxChipInput]

ExportAs: brnComboboxChipInput

Inputs

PropTypeDefaultDescription
id string `brn-combobox-input-${++BrnComboboxChipInput._id}` The id of the combobox input
aria-invalid boolean | undefined undefined Manual override for aria-invalid. When not set, auto-detects from the parent combobox error state.

BrnComboboxChipRemove

Selector: button[brnComboboxChipRemove]

ExportAs: brnComboboxChipRemove

BrnComboboxChip

Selector: [brnComboboxChip]

ExportAs: brnComboboxChip

Inputs

PropTypeDefaultDescription
value* (required) T --

BrnComboboxClear

Selector: [brnComboboxClear]

ExportAs: brnComboboxClear

BrnComboboxContent

Selector: [brnComboboxContent]

ExportAs: brnComboboxContent

Inputs

PropTypeDefaultDescription
id string `brn-combobox-content-${++BrnComboboxContent._id}` The id of the combobox content

BrnComboboxEmpty

Selector: [brnComboboxEmpty]

ExportAs: brnComboboxEmpty

BrnComboboxGroup

Selector: [brnComboboxGroup]

ExportAs: brnComboboxGroup

BrnComboboxInput

Selector: input[brnComboboxInput]

ExportAs: brnComboboxInput

Inputs

PropTypeDefaultDescription
id string `brn-combobox-input-${++BrnComboboxInput._id}` The id of the combobox input
aria-invalid boolean | undefined undefined Manual override for aria-invalid. When not set, auto-detects from the parent combobox error state.
forceInvalid boolean false Forces the invalid state visually, regardless of form control state.

BrnComboboxItem

Selector: [brnComboboxItem]

ExportAs: brnComboboxItem

Inputs

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

BrnComboboxLabel

Selector: [brnComboboxLabel]

ExportAs: brnComboboxLabel

Inputs

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

BrnComboboxList

Selector: [brnComboboxList]

ExportAs: brnComboboxList

Inputs

PropTypeDefaultDescription
id string `brn-combobox-list-${++BrnComboboxList._id}` The id of the combobox list
aria-label string | undefined undefined Optional accessible name for the listbox. Usually not needed as the combobox names the widget.

BrnComboboxMultiple

Selector: [brnCombobox]

ExportAs: brnComboboxMultiple

Inputs

PropTypeDefaultDescription
disabled boolean false Whether the combobox is disabled
filterOptions ComboboxFilterOptions {} Options for filtering the combobox items
isItemEqualToValue ComboboxItemEqualToValue<T> this._config.isItemEqualToValue A function to compare an item with the selected value.
itemToString ComboboxItemToString<T> | undefined this._config.itemToString A function to convert an item to a string for display.
filter ComboboxFilter<T> this._config.filter A custom filter function to use when searching.
autoHighlight boolean this._config.autoHighlight Whether to auto-highlight the first matching item.
closeOnSelect boolean this._config.closeOnSelect Whether to reset the search and close the popover after selecting an item when the search is active.
isValuePresent (value: T | undefined | null) => boolean this._config.isSingleValuePresent Determines whether a value is present (i.e., has been selected). Considers `undefined`, `null`, and empty string as "not present". Used to validate an item value on Enter key selection.
value T[] | undefined | null null The selected values of the combobox.
search string '' The current search query.

Outputs

PropTypeDefaultDescription
valueChange T[] | undefined | null null The selected values of the combobox.
searchChange string '' The current search query.

BrnComboboxPlaceholder

Selector: [brnComboboxPlaceholder]

ExportAs: brnComboboxPlaceholder

BrnComboboxPopoverTrigger

Selector: [brnComboboxPopoverTrigger]

ExportAs: brnComboboxPopoverTrigger

Inputs

PropTypeDefaultDescription
closeOnTriggerClick boolean true Whether clicking the trigger may close an open popover. Button triggers toggle (click again to close); text-input triggers set this to `false` so clicking or typing in an open combobox only ever opens it and never closes it underneath the user.

BrnComboboxSeparator

Selector: [brnComboboxSeparator]

ExportAs: brnComboboxSeparator

Inputs

PropTypeDefaultDescription
orientation 'horizontal' | 'vertical' 'horizontal' -

BrnComboboxStatus

Selector: [brnComboboxStatus]

ExportAs: brnComboboxStatus

BrnComboboxTrigger

Selector: button[brnComboboxTrigger]

ExportAs: brnComboboxTrigger

Inputs

PropTypeDefaultDescription
forceInvalid boolean false -

BrnComboboxValueTemplate

Selector: [brnComboboxValueTemplate]

ExportAs: brnComboboxValueTemplate

BrnComboboxValue

Selector: [brnComboboxValue]

ExportAs: brnComboboxValue

Inputs

PropTypeDefaultDescription
placeholder string '' -

BrnComboboxValues

Selector: [brnComboboxValues]

ExportAs: brnComboboxValues

BrnCombobox

Selector: [brnCombobox]

ExportAs: brnCombobox

Inputs

PropTypeDefaultDescription
disabled boolean false Whether the combobox is disabled
filterOptions ComboboxFilterOptions {} Options for filtering the combobox items
isItemEqualToValue ComboboxItemEqualToValue<T> this._config.isItemEqualToValue A function to compare an item with the selected value.
itemToString ComboboxItemToString<T> | undefined this._config.itemToString A function to convert an item to a string for display.
filter ComboboxFilter<T> this._config.filter A custom filter function to use when searching.
autoHighlight boolean this._config.autoHighlight Whether to auto-highlight the first matching item.
closeOnSelect boolean this._config.closeOnSelect Whether to close the popover after selecting an item.
isSingleValuePresent (value: T | undefined | null) => boolean this._config.isSingleValuePresent Determines whether a value is present (i.e., has been selected). Considers `undefined`, `null`, and empty string as "not present". Used to check if the selected value is present and to validate an item value on Enter key selection.
value T | undefined | null null The selected value of the combobox.
search string '' The current search query.

Outputs

PropTypeDefaultDescription
valueChange T | undefined | null null The selected value of the combobox.
searchChange string '' The current search query.

Helm API

HlmComboboxChipInput

Selector: input[hlmComboboxChipInput]

ExportAs: hlmComboboxChipInput

Inputs

PropTypeDefaultDescription
id string `brn-combobox-input-${++BrnComboboxChipInput._id}` The id of the combobox input
aria-invalid boolean | undefined undefined Manual override for aria-invalid. When not set, auto-detects from the parent combobox error state.

HlmComboboxChipRemove

Selector: button[hlmComboboxChipRemove]

ExportAs: hlmComboboxChipRemove

HlmComboboxChip

Selector: hlm-combobox-chip

ExportAs: hlmComboboxChip

Inputs

PropTypeDefaultDescription
showRemove boolean true -
value* (required) T --

HlmComboboxChips

Selector: [hlmComboboxChips],hlm-combobox-chips

ExportAs: hlmComboboxChips

Inputs

PropTypeDefaultDescription
forceInvalid boolean false -

HlmComboboxContent

Selector: [hlmComboboxContent],hlm-combobox-content

ExportAs: hlmComboboxContent

Inputs

PropTypeDefaultDescription
id string `brn-combobox-content-${++BrnComboboxContent._id}` The id of the combobox content

HlmComboboxEmpty

Selector: [hlmComboboxEmpty],hlm-combobox-empty

ExportAs: hlmComboboxEmpty

HlmComboboxGroup

Selector: [hlmComboboxGroup]

ExportAs: hlmComboboxGroup

HlmComboboxInput

Selector: hlm-combobox-input

ExportAs: hlmComboboxInput

Inputs

PropTypeDefaultDescription
inputId string `hlm-combobox-input-${HlmComboboxInput._id++}` -
placeholder string '' -
showTrigger boolean true -
showClear boolean false -
forceInvalid boolean false -
triggerAriaLabel string 'Toggle options' Accessible name for the icon-only popover trigger button.
clearAriaLabel string 'Clear selection' Accessible name for the icon-only clear button.
aria-invalid boolean | undefined undefined Manual override for aria-invalid. When not set, auto-detects from the parent combobox error state.

HlmComboboxItem

Selector: hlm-combobox-item

ExportAs: hlmComboboxItem

Inputs

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

HlmComboboxLabel

Selector: [hlmComboboxLabel]

ExportAs: hlmComboboxLabel

Inputs

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

HlmComboboxList

Selector: [hlmComboboxList]

ExportAs: hlmComboboxList

Inputs

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

HlmComboboxMultiple

Selector: [hlmComboboxMultiple],hlm-combobox-multiple

ExportAs: hlmComboboxMultiple

Inputs

PropTypeDefaultDescription
autoHighlight boolean this._config.autoHighlight Whether to auto-highlight the first matching item.
closeOnSelect boolean this._config.closeOnSelect Whether to reset the search and close the popover after selecting an item when the search is active.
disabled boolean false Whether the combobox is disabled
filter ComboboxFilter<T> this._config.filter A custom filter function to use when searching.
search string '' The current search query.
value T[] | undefined | null null The selected values of the combobox.
itemToString ComboboxItemToString<T> | undefined this._config.itemToString A function to convert an item to a string for display.
filterOptions ComboboxFilterOptions {} Options for filtering the combobox items
isItemEqualToValue ComboboxItemEqualToValue<T> this._config.isItemEqualToValue A function to compare an item with the selected value.
align BrnPopoverAlign this._config.align -
closeOnOutsidePointerEvents boolean this._defaultOptions.closeOnOutsidePointerEvents -
sideOffset number this._config.sideOffset -
state BrnOverlayState | null null -
offsetX number this._config.offsetX -
scrollStrategy BrnOverlayOptions['scrollStrategy'] | 'close' | 'reposition' | null this._defaultOptions.scrollStrategy -

Outputs

PropTypeDefaultDescription
searchChange string - The current search query.
valueChange T[] | undefined | null - The selected values of the combobox.
stateChanged BrnOverlayState --
closed TResult --

HlmComboboxPlaceholder

Selector: [hlmComboboxPlaceholder],hlm-combobox-placeholder

ExportAs: hlmComboboxPlaceholder

HlmComboboxPortal

Selector: [hlmComboboxPortal]

ExportAs: hlmComboboxPortal

Inputs

PropTypeDefaultDescription
context T | undefined undefined -
class string | null | undefined undefined -

HlmComboboxSeparator

Selector: [hlmComboboxSeparator]

ExportAs: hlmComboboxSeparator

Inputs

PropTypeDefaultDescription
orientation 'horizontal' | 'vertical' 'horizontal' -

HlmComboboxStatus

Selector: [hlmComboboxStatus],hlm-combobox-status

ExportAs: hlmComboboxStatus

HlmComboboxTrigger

Selector: hlm-combobox-trigger

ExportAs: hlmComboboxTrigger

Inputs

PropTypeDefaultDescription
class ClassValue '' -
buttonId string `hlm-combobox-trigger-${HlmComboboxTrigger._id++}` -
variant ButtonVariants['variant'] 'outline' -
forceInvalid boolean false -

HlmComboboxValueTemplate

Selector: [hlmComboboxValueTemplate]

ExportAs: hlmComboboxValueTemplate

HlmComboboxValue

Selector: [hlmComboboxValue],hlm-combobox-value

ExportAs: hlmComboboxValue

Inputs

PropTypeDefaultDescription
placeholder string '' -

HlmComboboxValues

Selector: [hlmComboboxValues]

ExportAs: hlmComboboxValues

HlmCombobox

Selector: [hlmCombobox],hlm-combobox

ExportAs: hlmCombobox

Inputs

PropTypeDefaultDescription
autoHighlight boolean this._config.autoHighlight Whether to auto-highlight the first matching item.
closeOnSelect boolean this._config.closeOnSelect Whether to close the popover after selecting an item.
disabled boolean false Whether the combobox is disabled
filter ComboboxFilter<T> this._config.filter A custom filter function to use when searching.
search string '' The current search query.
value T | undefined | null null The selected value of the combobox.
itemToString ComboboxItemToString<T> | undefined this._config.itemToString A function to convert an item to a string for display.
filterOptions ComboboxFilterOptions {} Options for filtering the combobox items
isItemEqualToValue ComboboxItemEqualToValue<T> this._config.isItemEqualToValue A function to compare an item with the selected value.
align BrnPopoverAlign this._config.align -
closeOnOutsidePointerEvents boolean this._defaultOptions.closeOnOutsidePointerEvents -
sideOffset number this._config.sideOffset -
state BrnOverlayState | null null -
offsetX number this._config.offsetX -
scrollStrategy BrnOverlayOptions['scrollStrategy'] | 'close' | 'reposition' | null this._defaultOptions.scrollStrategy -

Outputs

PropTypeDefaultDescription
searchChange string - The current search query.
valueChange T | undefined | null - The selected value of the combobox.
stateChanged BrnOverlayState --
closed TResult --
Command Collapsible