|
| 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 { |
| 10 | + Combobox, |
| 11 | + ComboboxInput, |
| 12 | + ComboboxPopup, |
| 13 | + ComboboxPopupContainer, |
| 14 | +} from '@angular/aria/combobox'; |
| 15 | +import {Listbox, Option} from '@angular/aria/listbox'; |
| 16 | +import { |
| 17 | + afterRenderEffect, |
| 18 | + ChangeDetectionStrategy, |
| 19 | + Component, |
| 20 | + ElementRef, |
| 21 | + signal, |
| 22 | + viewChild, |
| 23 | +} from '@angular/core'; |
| 24 | +import {FormsModule} from '@angular/forms'; |
| 25 | + |
| 26 | +/** @title Readonly multiselectable combobox. */ |
| 27 | +@Component({ |
| 28 | + selector: 'combobox-readonly-multiselect-example', |
| 29 | + templateUrl: 'combobox-readonly-multiselect-example.html', |
| 30 | + styleUrl: '../combobox-examples.css', |
| 31 | + imports: [ |
| 32 | + Combobox, |
| 33 | + ComboboxInput, |
| 34 | + ComboboxPopup, |
| 35 | + ComboboxPopupContainer, |
| 36 | + Listbox, |
| 37 | + Option, |
| 38 | + FormsModule, |
| 39 | + ], |
| 40 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 41 | +}) |
| 42 | +export class ComboboxReadonlyMultiselectExample { |
| 43 | + popover = viewChild<ElementRef>('popover'); |
| 44 | + listbox = viewChild<Listbox<any>>(Listbox); |
| 45 | + combobox = viewChild<Combobox<any>>(Combobox); |
| 46 | + |
| 47 | + options = () => states; |
| 48 | + searchString = signal(''); |
| 49 | + |
| 50 | + constructor() { |
| 51 | + afterRenderEffect(() => { |
| 52 | + const popover = this.popover()!; |
| 53 | + const combobox = this.combobox()!; |
| 54 | + combobox._pattern.expanded() ? this.showPopover() : popover.nativeElement.hidePopover(); |
| 55 | + |
| 56 | + // TODO(wagnermaciel): Make this easier for developers to do. |
| 57 | + this.listbox()?._pattern.inputs.activeItem()?.element()?.scrollIntoView({block: 'nearest'}); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + showPopover() { |
| 62 | + const popover = this.popover()!; |
| 63 | + const combobox = this.combobox()!; |
| 64 | + |
| 65 | + const comboboxRect = combobox._pattern.inputs.inputEl()?.getBoundingClientRect(); |
| 66 | + const popoverEl = popover.nativeElement; |
| 67 | + |
| 68 | + if (comboboxRect) { |
| 69 | + popoverEl.style.width = `${comboboxRect.width}px`; |
| 70 | + popoverEl.style.top = `${comboboxRect.bottom + 4}px`; |
| 71 | + popoverEl.style.left = `${comboboxRect.left - 1}px`; |
| 72 | + } |
| 73 | + |
| 74 | + popover.nativeElement.showPopover(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +const states = ['Option 1', 'Option 2', 'Option 3']; |
0 commit comments