|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {afterRenderEffect, Directive, ElementRef, inject} from '@angular/core'; |
| 9 | +import {afterRenderEffect, Directive, ElementRef, inject, input} from '@angular/core'; |
| 10 | +import {_IdGenerator} from '@angular/cdk/a11y'; |
10 | 11 | import {ComboboxDialogPattern} from '../private'; |
11 | 12 | import {Combobox} from './combobox'; |
12 | 13 | import {ComboboxPopup} from './combobox-popup'; |
@@ -46,35 +47,34 @@ export class ComboboxDialog { |
46 | 47 | private readonly _elementRef = inject(ElementRef<HTMLDialogElement>); |
47 | 48 |
|
48 | 49 | /** A reference to the dialog element. */ |
49 | | - readonly element = this._elementRef.nativeElement as HTMLElement; |
| 50 | + readonly element = this._elementRef.nativeElement as HTMLDialogElement; |
50 | 51 |
|
51 | 52 | /** The combobox that the dialog belongs to. */ |
52 | 53 | readonly combobox = inject(Combobox); |
53 | 54 |
|
| 55 | + /** The unique identifier for the trigger. */ |
| 56 | + readonly id = input(inject(_IdGenerator).getId('ng-combobox-dialog-', true)); |
| 57 | + |
54 | 58 | /** A reference to the parent combobox popup, if one exists. */ |
55 | 59 | private readonly _popup = inject<ComboboxPopup<unknown>>(ComboboxPopup, { |
56 | 60 | optional: true, |
57 | 61 | }); |
58 | 62 |
|
59 | | - readonly _pattern: ComboboxDialogPattern; |
| 63 | + readonly _pattern: ComboboxDialogPattern = new ComboboxDialogPattern({ |
| 64 | + id: this.id, |
| 65 | + element: () => this.element, |
| 66 | + combobox: this.combobox._pattern, |
| 67 | + }); |
60 | 68 |
|
61 | 69 | constructor() { |
62 | | - this._pattern = new ComboboxDialogPattern({ |
63 | | - id: () => '', |
64 | | - element: () => this._elementRef.nativeElement, |
65 | | - combobox: this.combobox._pattern, |
66 | | - }); |
67 | | - |
68 | 70 | if (this._popup) { |
69 | 71 | this._popup._controls.set(this._pattern); |
70 | 72 | } |
71 | 73 |
|
72 | | - afterRenderEffect(() => { |
73 | | - if (this._elementRef) { |
74 | | - this.combobox._pattern.expanded() |
75 | | - ? this._elementRef.nativeElement.showModal() |
76 | | - : this._elementRef.nativeElement.close(); |
77 | | - } |
| 74 | + afterRenderEffect({ |
| 75 | + write: () => { |
| 76 | + this.combobox._pattern.expanded() ? this.element.showModal() : this.element.close(); |
| 77 | + }, |
78 | 78 | }); |
79 | 79 | } |
80 | 80 |
|
|
0 commit comments