Skip to content

Commit 52dc0fd

Browse files
Ivan KitanovIvan Kitanov
authored andcommitted
feat(dropdown): Adding active descendant fo screen readers
1 parent 7ad9adf commit 52dc0fd

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

projects/igniteui-angular/src/lib/drop-down/drop-down-navigation.directive.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, Optional, Self, Input, HostListener, Inject } from '@angular/core';
1+
import { Directive, Optional, Self, Input, HostListener, Inject, HostBinding } from '@angular/core';
22
import { IGX_DROPDOWN_BASE } from './drop-down.common';
33
import { IDropDownNavigationDirective } from './drop-down.common';
44
import { IgxDropDownBaseDirective } from './drop-down.base';
@@ -53,6 +53,11 @@ export class IgxDropDownItemNavigationDirective implements IDropDownNavigationDi
5353
this._target = target ? target : this.dropdown;
5454
}
5555

56+
@HostBinding('attr.aria-activedescendant')
57+
public get activeDescendant(): string {
58+
return this._target?.activeDescendant;
59+
}
60+
5661
/**
5762
* Captures keydown events and calls the appropriate handlers on the target component
5863
*/

projects/igniteui-angular/src/lib/drop-down/drop-down.base.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ export abstract class IgxDropDownBaseDirective implements IDropDownList, OnInit
162162
return this.element;
163163
}
164164

165+
/**
166+
* @hidden @internal
167+
* Gets the id of the focused item during dropdown navigation.
168+
* This is used to update the `aria-activedescendant` attribute of
169+
* the IgxDropDownNavigationDirective host element.
170+
*/
171+
public get activeDescendant (): string {
172+
return this.focusedItem ? this.focusedItem.id : null;
173+
}
174+
165175
/**
166176
* @hidden
167177
* @internal

projects/igniteui-angular/src/lib/drop-down/drop-down.component.spec.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,13 +1046,9 @@ describe('IgxDropDown ', () => {
10461046
beforeEach(() => {
10471047
fixture = TestBed.createComponent(IgxDropDownTestComponent);
10481048
fixture.detectChanges();
1049-
dropdown = fixture.componentInstance.dropDown;
1049+
dropdown = fixture.componentInstance.dropdown;
10501050
});
10511051
it('should set the aria-label property correctly', () => {
1052-
fixture = TestBed.createComponent(IgxDropDownTestComponent);
1053-
fixture.detectChanges();
1054-
dropdown = fixture.componentInstance.dropdown;
1055-
10561052
// Initially aria-label should be null
10571053
dropdown.toggle();
10581054
fixture.detectChanges();
@@ -1083,6 +1079,35 @@ describe('IgxDropDown ', () => {
10831079
expect(item.getAttribute('aria-label')).toBe(`label ${index}`);
10841080
});
10851081
});
1082+
it('should update aria-activedescendant to the id of the focused item', fakeAsync(() => {
1083+
dropdown.toggle();
1084+
tick();
1085+
fixture.detectChanges();
1086+
1087+
const dropdownElement = fixture.debugElement.query(By.css(`.${CSS_CLASS_DROP_DOWN_BASE}`)).nativeElement;
1088+
let focusedItem = fixture.debugElement.query(By.css(`.${CSS_CLASS_FOCUSED}`)).nativeElement;
1089+
1090+
expect(focusedItem).toBeTruthy();
1091+
let focusedItemId = focusedItem.getAttribute('id');
1092+
expect(focusedItemId).toBeTruthy();
1093+
expect(dropdownElement.getAttribute('aria-activedescendant')).toBe(focusedItemId);
1094+
1095+
dropdown.toggle();
1096+
tick();
1097+
fixture.detectChanges();
1098+
dropdown.toggle();
1099+
tick();
1100+
fixture.detectChanges();
1101+
1102+
UIInteractions.triggerEventHandlerKeyDown('ArrowDown', fixture.debugElement.query(By.css(`.${CSS_CLASS_DROP_DOWN_BASE}`)));
1103+
tick();
1104+
fixture.detectChanges();
1105+
1106+
focusedItem = fixture.debugElement.query(By.css(`.${CSS_CLASS_FOCUSED}`)).nativeElement;
1107+
focusedItemId = focusedItem.getAttribute('id');
1108+
1109+
expect(dropdownElement.getAttribute('aria-activedescendant')).toBe(focusedItemId);
1110+
}));
10861111
});
10871112
describe('Grouped items', () => {
10881113
beforeEach(waitForAsync(() => {

src/app/drop-down/drop-down.sample.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
>
1717
@for(item of items; track item) {
1818
<igx-drop-down-item
19+
[ariaLabel]="item.field"
1920
[disabled]="item.disabled"
2021
[isHeader]="item.header"
2122
>
@@ -161,6 +162,7 @@ <h5>Angular Dropdown With Action directive</h5>
161162
@for(item of items; track item) {
162163
<igx-drop-down-item
163164
[value]="item"
165+
[ariaLabel]="item.field"
164166
[disabled]="item.disabled"
165167
[isHeader]="item.header"
166168
>

0 commit comments

Comments
 (0)