Skip to content

Commit

Permalink
fix(autocomplete): adding aria-activedescendant while closed if autoA…
Browse files Browse the repository at this point in the history
…ctiveFirstOption is enabled (angular#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 angular#14453.
  • Loading branch information
crisbeto authored and josephperrott committed Jan 14, 2019
1 parent d46a7d5 commit ad7528a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/lib/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit ad7528a

Please sign in to comment.