Skip to content

Commit fee5eb5

Browse files
committed
refactor(aria/combobox): clean up afterRenderEffect usage and fix missing id
1 parent 6ec07bc commit fee5eb5

4 files changed

Lines changed: 25 additions & 21 deletions

File tree

goldens/aria/combobox/index.api.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ export class ComboboxDialog {
3636
// (undocumented)
3737
close(): void;
3838
readonly combobox: Combobox<any>;
39-
readonly element: HTMLElement;
39+
readonly element: HTMLDialogElement;
40+
readonly id: _angular_core.InputSignal<string>;
4041
// (undocumented)
4142
readonly _pattern: ComboboxDialogPattern;
4243
// (undocumented)
43-
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<ComboboxDialog, "dialog[ngComboboxDialog]", ["ngComboboxDialog"], {}, {}, never, never, true, [{ directive: typeof ComboboxPopup; inputs: {}; outputs: {}; }]>;
44+
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<ComboboxDialog, "dialog[ngComboboxDialog]", ["ngComboboxDialog"], { "id": { "alias": "id"; "required": false; "isSignal": true; }; }, {}, never, never, true, [{ directive: typeof ComboboxPopup; inputs: {}; outputs: {}; }]>;
4445
// (undocumented)
4546
static ɵfac: _angular_core.ɵɵFactoryDeclaration<ComboboxDialog, never>;
4647
}

src/aria/combobox/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ng_project(
1111
deps = [
1212
"//:node_modules/@angular/core",
1313
"//src/aria/private",
14+
"//src/cdk/a11y",
1415
"//src/cdk/bidi",
1516
],
1617
)

src/aria/combobox/combobox-dialog.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

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';
1011
import {ComboboxDialogPattern} from '../private';
1112
import {Combobox} from './combobox';
1213
import {ComboboxPopup} from './combobox-popup';
@@ -46,35 +47,34 @@ export class ComboboxDialog {
4647
private readonly _elementRef = inject(ElementRef<HTMLDialogElement>);
4748

4849
/** A reference to the dialog element. */
49-
readonly element = this._elementRef.nativeElement as HTMLElement;
50+
readonly element = this._elementRef.nativeElement as HTMLDialogElement;
5051

5152
/** The combobox that the dialog belongs to. */
5253
readonly combobox = inject(Combobox);
5354

55+
/** The unique identifier for the trigger. */
56+
readonly id = input(inject(_IdGenerator).getId('ng-combobox-dialog-', true));
57+
5458
/** A reference to the parent combobox popup, if one exists. */
5559
private readonly _popup = inject<ComboboxPopup<unknown>>(ComboboxPopup, {
5660
optional: true,
5761
});
5862

59-
readonly _pattern: ComboboxDialogPattern;
63+
readonly _pattern: ComboboxDialogPattern = new ComboboxDialogPattern({
64+
id: this.id,
65+
element: () => this.element,
66+
combobox: this.combobox._pattern,
67+
});
6068

6169
constructor() {
62-
this._pattern = new ComboboxDialogPattern({
63-
id: () => '',
64-
element: () => this._elementRef.nativeElement,
65-
combobox: this.combobox._pattern,
66-
});
67-
6870
if (this._popup) {
6971
this._popup._controls.set(this._pattern);
7072
}
7173

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+
},
7878
});
7979
}
8080

src/aria/combobox/combobox-input.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ export class ComboboxInput {
8181
}
8282

8383
/** Focuses & selects the first item in the combobox if the user changes the input value. */
84-
afterRenderEffect(() => {
85-
this.value();
86-
controls?.items();
87-
untracked(() => this.combobox._pattern.onFilter());
84+
afterRenderEffect({
85+
write: () => {
86+
this.value();
87+
controls?.items();
88+
untracked(() => this.combobox._pattern.onFilter());
89+
},
8890
});
8991
}
9092
}

0 commit comments

Comments
 (0)