Virtual Select Field for Angular Material
npm install ngx-virtual-select-field-filterable!NPM Version
!GitHub Actions Workflow Status
- Description
- Getting started
- Examples
- Keyboard Shortcuts
- Customization
- API
- NgxVirtualSelectFieldComponent
- NgxVirtualSelectFieldOptionComponent
- NgxVirtualSelectFieldOptionSelectionChangeEvent
- NgxVirtualSelectFieldTriggerComponent
- NgxVirtualSelectFieldOptionForDirective
- NgxVirtualSelectFieldOptionModel
- NGX_VIRTUAL_SELECT_FIELD_CONFIG
- NgxVirtualSelectFieldConfig
- CSS variables
This package replicates the Angular Material Select component with virtual scroll capabilities using cdk-virtual-scroll. It provides most major features of the original Angular Material Select component. The goal of this package is to provide a similar API and features as the original Angular Material Select component but with virtual scroll capabilities for handling large datasets efficiently.
Features:
- Virtual scroll for large datasets (100,000+ items)
- Single select
- Multi select with checkboxes
- Select all checkbox (for multi-select with filter)
- Filterable options with search input
- Clearable selection
- Loading spinner for async data
- Integrates with Angular Material Form Field
- Reactive Forms and Template-driven forms support
- Custom trigger template
- Custom option template
- Full keyboard navigation and shortcuts
- Type-ahead search
- Theming through CSS variables
Not Supported Features for now:
- Animations
- Custom Error state matcher
- Custom scroll strategy
- Full accessibility (in progress)
- Option groups
1. Install package
``bash`
npm install ngx-virtual-select-field-filterable
1. Import bundle into your component
`typescript`
import { NgxVirtualSelectFieldBundle } from 'ngx-virtual-select-field-filterable';
...
@Component({
imports: [
NgxVirtualSelectFieldBundle,
...
],
...
})
1. Create options collection in component. Options collection should be an array of objects with value and label properties. Optionally, you can add disabled property to disable specific options and getLabel() for type-ahead search.
`typescript
...
protected options: NgxVirtualSelectFieldOptionModel
constructor() {
this.options = new Array(100000)
.fill(null)
.map((_, index) => ({
value: index,
label: ${index} Option,`
disabled: index % 5 === 0,
}));
}
1. Setup template markup. ngxVirtualSelectFieldOptionFor directive should be used to pass options collection to the component and provide custom option template.
`html`
[value]="option.value"
[disabled]="option.disabled">
{{ option.label }}
1. Include theme styles. You can define your own theme with help of CSS variables or inherit from material theme.
`scss
@use 'ngx-virtual-select-field-filterable/theme' as theme;
@include theme.inherit-material-theme(); // this will inherit css variables from material theme
`
Basic setup with value input and output binding
`html`
{{ option.label }}
Form control integration
`html`
{{ option.label }}
Multi select
`html`
[value]="option.value">
{{ option.label }}
Custom trigger template
`html`
{{ value.length }} selected
[value]="option.value">
{{ option.label }}
Filterable select with search input
`html`
[filterable]="true"
filterPlaceholder="Type to filter..."
(valueChange)="onValueChange($event)">
[value]="option.value">
{{ option.label }}
Clearable select with clear button
`html`
[clearable]="true"
multiple
(valueChange)="onValueChange($event)">
[value]="option.value">
{{ option.label }}
Loading state for async data
`html`
[loading]="isLoading"
(valueChange)="onValueChange($event)">
[value]="option.value">
{{ option.label }}
The component supports full keyboard navigation:
| Shortcut | Single Select | Multi Select |
|----------|---------------|--------------|
| Space / Enter | Open panel | Open panel |Alt + ArrowDown
| | Open panel | Open panel |ArrowDown
| / ArrowUp | Navigate & select | Open panel |
| Type any character | Type-ahead search | Type-ahead search |
| Shortcut | Description |
|----------|-------------|
| ArrowDown / ArrowUp | Navigate through options |Alt + ArrowDown
| / Alt + ArrowUp | Close panel |Enter
| / Space | Toggle selection of active option |Ctrl + A
| | Select/deselect all options (multi-select only) |Shift + ArrowDown
| / Shift + ArrowUp | Extend selection (multi-select only) |Home
| / End | Jump to first/last option |Page Up
| / Page Down | Navigate by page |Escape
| | Close panel |Tab
| | Select active item and close panel |
| Type any character | Type-ahead search (300ms debounce) |
| Shortcut | Description |
|----------|-------------|
| ArrowDown / ArrowUp | Move focus to options list |ArrowLeft
| / ArrowRight | Move cursor in filter input |Escape
| | Close panel |Tab
| | Close panel |
Components supports custom templates for trigger and option elements. You can use ngx-virtual-select-field-trigger and ngx-virtual-select-field-option components to define custom templates.
There are number of input parameters available to specify specific behavior of the component.
Injection tokens might be used to customize all component instances
All styles are defined with css variables, so you can easily override them in your own styles.
See more in API section below.
selector: ngx-virtual-select-field
Component to define select field
| Input | Type | Default | Description |
| ------------------------- | ---------------------------- | ------------ | ------------------------------------------------------------------------------ |
| panelWidth | string\|number \|null | auto | Width for overlay panel |number
| optionHeight | | 48 | Height for an option element |number
| panelViewportPageSize | | 8 | Amount of visible items in list |boolean
| multiple | | false | Enable multiple selection |number
| tabIndex | | 0 | Tab index for keyboard navigation |number
| typeaheadDebounceInterval | | 300 | Milliseconds to wait before navigating to active element after keyboard search |string \| string[] \| null
| panelClass | | null | CSS class to be added to the panel element |boolean
| filterable | | false | Enable filtering of options with search input |string
| filterPlaceholder | | 'Search...'| Placeholder text for the filter input |boolean
| filterClearable | | true | Show clear button in filter input |boolean
| clearable | | false | Show clear button in select trigger to clear all selections |boolean
| loading | | false | Show loading spinner while data is being loaded |boolean
| showSelectAll | | true | Show select all checkbox when multiple and filterable are enabled |TValue[] \| TValue \| null
| value | | null | Value of the select field |string
| placeholder | | none | Placeholder for the select field |boolean
| required | | false | Define if field is required |boolean
| disabled | | false | Define if field is disabled |
| Output | Type | Description |
| --------------- | ----------------------------- | -------------------------- |
| valueChange | TValue \| TValue[] | Value change output |NgxVirtualSelectFieldChange
| selectionChange | | Selection change output |
selector: ngx-virtual-select-field-option
Component to define option element
| Input | Type | Default | Description |
| ---------------- | --------- | ------- | ------------------------------ |
| value (required) | TValue | | Value of the option |boolean
| disabled | | false | Whether the option is disabled |
| Output | Type | Description |
| -------------- | --------------------------------------------------------- | ---------------------------- |
| selectedChange | NgxVirtualSelectFieldOptionSelectionChangeEvent | Option selected value change |
Interface to define option selection change event contract
Properties:
| Name | Type | Description |
|----------|-----------------------------------------------------------|----------------------------|
| source | NgxVirtualSelectFieldOptionComponent | Option component instance |TValue
| value | | Value of the option |boolean
| selected | | Whether the option is selected |
selector: ngx-virtual-select-field-trigger
Directive to define custom trigger template
selector: *ngxVirtualSelectFieldOptionForNgxVirtualSelectFieldOptionModel
Directive to define custom option template and iterate over options
| Input | Type | Description |
|----------------------------------|----------------------------------------------|---------------------|
| of (required) | | Options collection |
Interface to define option model contract
Properties:
| Name | Type | Description |
|-----------------------|----------------------------------------------------------------|----------------------------|
| value | TValue | Value of the option |string
| label | | Label of the option |boolean
| disabled? | | Whether the option is disabled |(option: NgxVirtualSelectFieldOptionModel
| getLabel() optional | | Function to get label for type-ahead search |
Injection token to define global configuration for all instances of the component
Config see in NgxVirtualSelectFieldConfig interface
Interface to define global configuration contract
Properties:
| Name | Type | Description |
|-----------------------|--------------------------------------------|----------------------------|
| panelWidth | string\|number | Width for overlay panel |string \| string[]
| overlayPanelClass | | CSS class to be added to the panel element|number
| optionHeight | | Height for an option element |number
| panelViewportPageSize | | Amount of visible items in list |boolean
| showSelectAll | | Show select all checkbox when multiple and filterable |
Class to define event for selectionChange outputNgxVirtualSelectFieldComponent
Properties:
| Name | Type | Description |
|-----------------------|--------------------------------------------|----------------------------|
| source | | Instance of the selector component |TValue
| value | or TValue[] | New selection value|
All styles are defined with css variables, so you can easily override them in your own styles.
CSS variables for main component:
`scss
:root {
--ngx-virtual-select-field-trigger-text-color: ...;
--ngx-virtual-select-field-trigger-text-color--disabled: ...;
--ngx-virtual-select-field-trigger-font-family: ...;
--ngx-virtual-select-field-trigger-line-height: ...;
--ngx-virtual-select-field-trigger-font-size: ...;
--ngx-virtual-select-field-trigger-font-weight: ...;
--ngx-virtual-select-field-trigger-letter-spacing: ...;
--ngx-virtual-select-field-placeholder-text-color: ...;
--ngx-virtual-select-field-placeholder-transition: ...;
--ngx-virtual-select-field-arrow-size: ...;
--ngx-virtual-select-field-arrow-color--enabled: ...;
--ngx-virtual-select-field-arrow-color--focused: ...;
--ngx-virtual-select-field-arrow-color--invalid: ...;
--ngx-virtual-select-field-arrow-color--disabled: ...;
--ngx-virtual-select-field-panel-background: ...;
--ngx-virtual-select-field-panel-box-shadow: ...;
--ngx-virtual-select-field-panel-list-wrapper-padding: ...;
--ngx-virtual-select-field-divider-color: ...;
--ngx-virtual-select-field-filter-input-border-color: ...;
--ngx-virtual-select-field-filter-input-border-color--focused: ...;
}
`
CSS variables for option component:
`scss
:root {
--ngx-virtual-select-field-option-color: ...;
--ngx-virtual-select-field-option-font-family: ...;
--ngx-virtual-select-field-option-line-height: ...;
--ngx-virtual-select-field-option-font-size: ...;
--ngx-virtual-select-field-option-letter-spacing: ...;
--ngx-virtual-select-field-option-font-weight: ...;
--ngx-virtual-select-field-option-selected-state-label-text-color: ...;
--ngx-virtual-select-field-option-background-color--active: ...;
--ngx-virtual-select-field-option-background-color--hover: ...;
--ngx-virtual-select-field-option-background-color--selected: ...;
--ngx-virtual-select-field-option-disabled-state-opacity: ...;
--ngx-virtual-select-field-option-gap: ...;
--ngx-virtual-select-field-option-padding: ...;
}
``