|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Combobox, ComboboxPopup, ComboboxWidget} from '@angular/aria/simple-combobox'; |
| 10 | +import {Listbox, Option} from '@angular/aria/listbox'; |
| 11 | +import { |
| 12 | + afterRenderEffect, |
| 13 | + ChangeDetectionStrategy, |
| 14 | + Component, |
| 15 | + computed, |
| 16 | + signal, |
| 17 | + untracked, |
| 18 | + viewChild, |
| 19 | + ElementRef, |
| 20 | +} from '@angular/core'; |
| 21 | +import {COUNTRIES} from '../countries'; |
| 22 | +import {OverlayModule} from '@angular/cdk/overlay'; |
| 23 | +import {FormsModule} from '@angular/forms'; |
| 24 | + |
| 25 | +/** @title Editable multiselectable combobox with a dialog layout. */ |
| 26 | +@Component({ |
| 27 | + selector: 'simple-combobox-editable-multiselect-example', |
| 28 | + templateUrl: 'simple-combobox-editable-multiselect-example.html', |
| 29 | + styleUrl: '../simple-combobox-example.css', |
| 30 | + imports: [Combobox, ComboboxPopup, ComboboxWidget, Listbox, Option, OverlayModule, FormsModule], |
| 31 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 32 | +}) |
| 33 | +export class SimpleComboboxEditableMultiselectExample { |
| 34 | + readonly listbox = viewChild(Listbox); |
| 35 | + readonly combobox = viewChild(Combobox); |
| 36 | + readonly searchInput = viewChild<ElementRef<HTMLInputElement>>('searchInput'); |
| 37 | + |
| 38 | + popupExpanded = signal(false); |
| 39 | + searchString = signal(''); |
| 40 | + selectedOptions = signal<string[]>([]); |
| 41 | + |
| 42 | + /** The display text for the primary readonly trigger input. */ |
| 43 | + value = computed(() => { |
| 44 | + const current = this.selectedOptions(); |
| 45 | + if (current.length === 0) return ''; |
| 46 | + if (current.length === 1) return current[0]; |
| 47 | + return `${current[0]} + ${current.length - 1} more`; |
| 48 | + }); |
| 49 | + |
| 50 | + /** The list of countries filtered by the inner search input query. */ |
| 51 | + countries = computed(() => { |
| 52 | + const currentQuery = this.searchString().toLowerCase(); |
| 53 | + return COUNTRIES.filter(country => country.toLowerCase().startsWith(currentQuery)); |
| 54 | + }); |
| 55 | + |
| 56 | + constructor() { |
| 57 | + // Automatically auto-focus the inner search text field when the dialog expands |
| 58 | + afterRenderEffect(() => { |
| 59 | + if (this.popupExpanded()) { |
| 60 | + untracked(() => { |
| 61 | + setTimeout(() => { |
| 62 | + this.searchInput()?.nativeElement.focus(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + afterRenderEffect(() => { |
| 69 | + if (this.popupExpanded()) { |
| 70 | + this.listbox()?.scrollActiveItemIntoView(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + /** Clears the search query and all selected options. */ |
| 76 | + clear(): void { |
| 77 | + this.searchString.set(''); |
| 78 | + this.selectedOptions.set([]); |
| 79 | + } |
| 80 | + |
| 81 | + /** Keeps focus inside the dialog when selection changes. */ |
| 82 | + onCommit() { |
| 83 | + this.searchString.set(''); |
| 84 | + } |
| 85 | + |
| 86 | + /** Dismisses the dialog overlay on Escape key. */ |
| 87 | + onSearchEscape(event: Event) { |
| 88 | + this.popupExpanded.set(false); |
| 89 | + this.combobox()?.element.focus(); |
| 90 | + } |
| 91 | + |
| 92 | + /** Handles keydown events on the clear button. */ |
| 93 | + onKeydown(event: KeyboardEvent): void { |
| 94 | + if (event.key === 'Enter') { |
| 95 | + this.clear(); |
| 96 | + this.popupExpanded.set(false); |
| 97 | + event.stopPropagation(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments