Skip to content

Commit 1371e31

Browse files
committed
fix(material/autocomplete): pass in form field id in ng-template
1 parent ffed73f commit 1371e31

File tree

5 files changed

+11
-14
lines changed

5 files changed

+11
-14
lines changed

src/material-experimental/mdc-autocomplete/autocomplete.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
<ng-template>
1+
<ng-template let-formFieldId="id">
22
<div
33
class="mat-mdc-autocomplete-panel mdc-menu-surface mdc-menu-surface--open"
44
role="listbox"
55
[id]="id"
66
[ngClass]="_classList"
77
[attr.aria-label]="ariaLabel || null"
8-
[attr.aria-labelledby]="_getPanelAriaLabelledby()"
8+
[attr.aria-labelledby]="_getPanelAriaLabelledby(formFieldId)"
99
#panel>
1010
<ng-content></ng-content>
1111
</div>

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,9 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
591591
let overlayRef = this._overlayRef;
592592

593593
if (!overlayRef) {
594-
this._portal = new TemplatePortal(this.autocomplete.template, this._viewContainerRef);
594+
this._portal = new TemplatePortal(this.autocomplete.template,
595+
this._viewContainerRef,
596+
{id: this._formField?._labelId});
595597
overlayRef = this._overlay.create(this._getOverlayConfig());
596598
this._overlayRef = overlayRef;
597599

@@ -632,7 +634,6 @@ export abstract class _MatAutocompleteTriggerBase implements ControlValueAccesso
632634

633635
this.autocomplete._setVisibility();
634636
this.autocomplete._isOpen = this._overlayAttached = true;
635-
this.autocomplete._formFieldLabelId = this._formField?._labelId;
636637

637638
// We need to do an extra `panelOpen` check in here, because the
638639
// autocomplete won't be shown if there are no options.

src/material/autocomplete/autocomplete.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<ng-template>
1+
<ng-template let-formFieldId="id">
22
<div class="mat-autocomplete-panel"
3-
role="listbox" [id]="id"
3+
role="listbox"
4+
[id]="id"
45
[attr.aria-label]="ariaLabel || null"
5-
[attr.aria-labelledby]="_getPanelAriaLabelledby()"
6+
[attr.aria-labelledby]="_getPanelAriaLabelledby(formFieldId)"
67
[ngClass]="_classList"
78
#panel>
89
<ng-content></ng-content>

src/material/autocomplete/autocomplete.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ export abstract class _MatAutocompleteBase extends _MatAutocompleteMixinBase imp
115115
get isOpen(): boolean { return this._isOpen && this.showPanel; }
116116
_isOpen: boolean = false;
117117

118-
/** Label id of form field the autocomplete is associated with. */
119-
_formFieldLabelId: string;
120-
121118
// The @ViewChild query for TemplateRef here needs to be static because some code paths
122119
// lead to the overlay being created before change detection has finished for this component.
123120
// Notably, another component may trigger `focus` on the autocomplete-trigger.
@@ -261,12 +258,11 @@ export abstract class _MatAutocompleteBase extends _MatAutocompleteMixinBase imp
261258
}
262259

263260
/** Gets the aria-labelledby for the autocomplete panel. */
264-
_getPanelAriaLabelledby(): string | null {
261+
_getPanelAriaLabelledby(labelId: string): string | null {
265262
if (this.ariaLabel) {
266263
return null;
267264
}
268265

269-
const labelId = this._formFieldLabelId ?? '';
270266
return this.ariaLabelledby ? labelId + ' ' + this.ariaLabelledby : labelId;
271267
}
272268

tools/public_api_guard/material/autocomplete.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export declare abstract class _MatAutocompleteBase extends _MatAutocompleteMixin
22
_classList: {
33
[key: string]: boolean;
44
};
5-
_formFieldLabelId: string;
65
protected abstract _hiddenClass: string;
76
_isOpen: boolean;
87
_keyManager: ActiveDescendantKeyManager<_MatOptionBase>;
@@ -28,7 +27,7 @@ export declare abstract class _MatAutocompleteBase extends _MatAutocompleteMixin
2827
template: TemplateRef<any>;
2928
constructor(_changeDetectorRef: ChangeDetectorRef, _elementRef: ElementRef<HTMLElement>, defaults: MatAutocompleteDefaultOptions, platform?: Platform);
3029
_emitSelectEvent(option: _MatOptionBase): void;
31-
_getPanelAriaLabelledby(): string | null;
30+
_getPanelAriaLabelledby(labelId: string): string | null;
3231
_getScrollTop(): number;
3332
_setScrollTop(scrollTop: number): void;
3433
_setVisibility(): void;

0 commit comments

Comments
 (0)