From f3065cccaff168ffb45528168688da5fffc1750d Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 20 Dec 2018 19:27:43 +0100 Subject: [PATCH] fix(autocomplete): adding aria-activedescendant while closed if autoActiveFirstOption is enabled (#14455) Fixes the autocomplete trigger setting its `aria-activedescendant` attribute while the panel is closed and the options aren't in the DOM, if the `autoActiveFirstOption` is enabled. Fixes #14453. --- src/lib/autocomplete/autocomplete-trigger.ts | 2 +- src/lib/autocomplete/autocomplete.spec.ts | 23 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib/autocomplete/autocomplete-trigger.ts b/src/lib/autocomplete/autocomplete-trigger.ts index ea86763a572..2667b9b16fb 100644 --- a/src/lib/autocomplete/autocomplete-trigger.ts +++ b/src/lib/autocomplete/autocomplete-trigger.ts @@ -102,7 +102,7 @@ export function getMatAutocompleteMissingPanelError(): Error { '[attr.autocomplete]': 'autocompleteAttribute', '[attr.role]': 'autocompleteDisabled ? null : "combobox"', '[attr.aria-autocomplete]': 'autocompleteDisabled ? null : "list"', - '[attr.aria-activedescendant]': 'activeOption?.id', + '[attr.aria-activedescendant]': '(panelOpen && activeOption) ? activeOption.id : null', '[attr.aria-expanded]': 'autocompleteDisabled ? null : panelOpen.toString()', '[attr.aria-owns]': '(autocompleteDisabled || !panelOpen) ? null : autocomplete?.id', // Note: we use `focusin`, as opposed to `focus`, in order to open the panel diff --git a/src/lib/autocomplete/autocomplete.spec.ts b/src/lib/autocomplete/autocomplete.spec.ts index 143ea70c8f0..d7f2c71f549 100644 --- a/src/lib/autocomplete/autocomplete.spec.ts +++ b/src/lib/autocomplete/autocomplete.spec.ts @@ -1644,6 +1644,29 @@ describe('MatAutocomplete', () => { .toContain('mat-active', 'Expected first option to be highlighted.'); })); + it('should remove aria-activedescendant when panel is closed with autoActiveFirstOption', + fakeAsync(() => { + const input: HTMLElement = fixture.nativeElement.querySelector('input'); + + expect(input.hasAttribute('aria-activedescendant')) + .toBe(false, 'Expected no active descendant on init.'); + + fixture.componentInstance.trigger.autocomplete.autoActiveFirstOption = true; + fixture.componentInstance.trigger.openPanel(); + fixture.detectChanges(); + zone.simulateZoneExit(); + fixture.detectChanges(); + + expect(input.getAttribute('aria-activedescendant')) + .toBeTruthy('Expected active descendant while open.'); + + fixture.componentInstance.trigger.closePanel(); + fixture.detectChanges(); + + expect(input.hasAttribute('aria-activedescendant')) + .toBe(false, 'Expected no active descendant when closed.'); + })); + it('should be able to configure preselecting the first option globally', fakeAsync(() => { overlayContainer.ngOnDestroy(); fixture.destroy();