Skip to content

Commit d62cc12

Browse files
eminguyenmmalerba
authored andcommitted
Toggle aria-modal attribute in parent modal when autocomplete panel opens / closes (#27663)
* Toggle parent modal aria-owns attribute when autocomplete is closed * Add unit tests and update comments (cherry picked from commit f6c5ea4)
1 parent adbc4b4 commit d62cc12

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ export abstract class _MatAutocompleteTriggerBase
263263
openPanel(): void {
264264
this._attachOverlay();
265265
this._floatLabel();
266+
// Add aria-owns attribute when the autocomplete becomes visible.
267+
if (this._trackedModal) {
268+
const panelId = this.autocomplete.id;
269+
addAriaReferencedId(this._trackedModal, 'aria-owns', panelId);
270+
}
266271
}
267272

268273
/** Closes the autocomplete suggestion panel. */
@@ -302,6 +307,12 @@ export abstract class _MatAutocompleteTriggerBase
302307
// user clicks outside.
303308
this._changeDetectorRef.detectChanges();
304309
}
310+
311+
// Remove aria-owns attribute when the autocomplete is no longer visible.
312+
if (this._trackedModal) {
313+
const panelId = this.autocomplete.id;
314+
removeAriaReferencedId(this._trackedModal, 'aria-owns', panelId);
315+
}
305316
}
306317

307318
/**

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3729,6 +3729,31 @@ describe('MDC-based MatAutocomplete', () => {
37293729
.withContext('expecting modal to own the autocommplete panel')
37303730
.toContain(panelId);
37313731
}));
3732+
3733+
it('should remove the aria-owns attribute of the modal when the autocomplete panel closes', fakeAsync(() => {
3734+
fixture.componentInstance.trigger.openPanel();
3735+
fixture.componentInstance.trigger.closePanel();
3736+
fixture.detectChanges();
3737+
3738+
const panelId = fixture.componentInstance.autocomplete.id;
3739+
const modalElement = fixture.componentInstance.modal.nativeElement;
3740+
3741+
expect(modalElement.getAttribute('aria-owns')).toBeFalsy();
3742+
}));
3743+
3744+
it('should readd the aria-owns attribute of the modal when the autocomplete panel opens again', fakeAsync(() => {
3745+
fixture.componentInstance.trigger.openPanel();
3746+
fixture.componentInstance.trigger.closePanel();
3747+
fixture.componentInstance.trigger.openPanel();
3748+
fixture.detectChanges();
3749+
3750+
const panelId = fixture.componentInstance.autocomplete.id;
3751+
const modalElement = fixture.componentInstance.modal.nativeElement;
3752+
3753+
expect(modalElement.getAttribute('aria-owns')?.split(' '))
3754+
.withContext('expecting modal to own the autocommplete panel')
3755+
.toContain(panelId);
3756+
}));
37323757
});
37333758
});
37343759

0 commit comments

Comments
 (0)