Skip to content

fix(option): Remove aria-selected='false' in single-selection mode #15617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/lib/core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const MAT_OPTION_PARENT_COMPONENT =
'[class.mat-option-multiple]': 'multiple',
'[class.mat-active]': 'active',
'[id]': 'id',
'[attr.aria-selected]': 'selected.toString()',
'[attr.aria-selected]': '_getAriaSelected()',
'[attr.aria-disabled]': 'disabled.toString()',
'[class.mat-option-disabled]': 'disabled',
'(click)': '_selectViaInteraction()',
Expand Down Expand Up @@ -220,6 +220,16 @@ export class MatOption implements AfterViewChecked, OnDestroy {
}
}

/**
* Gets the `aria-selected` value for the option. We explicitly omit the `aria-selected`
* attribute from single-selection, unselected options. Including the `aria-selected="false"`
* attributes adds a significant amount of noise to screen-reader users without providing useful
* information.
*/
_getAriaSelected(): boolean|null {
return this.selected || (this.multiple ? false : null);
}

/** Returns the correct tabindex for the option depending on disabled state. */
_getTabIndex(): string {
return this.disabled ? '-1' : '0';
Expand Down
51 changes: 41 additions & 10 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ describe('MatSelect', () => {
describe('for options', () => {
let fixture: ComponentFixture<BasicSelect>;
let trigger: HTMLElement;
let options: NodeListOf<HTMLElement>;
let options: Array<HTMLElement>;

beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(BasicSelect);
Expand All @@ -872,8 +872,7 @@ describe('MatSelect', () => {
trigger.click();
fixture.detectChanges();

options =
overlayContainerElement.querySelectorAll('mat-option') as NodeListOf<HTMLElement>;
options = Array.from(overlayContainerElement.querySelectorAll('mat-option'));
}));

it('should set the role of mat-option to option', fakeAsync(() => {
Expand All @@ -882,10 +881,9 @@ describe('MatSelect', () => {
expect(options[2].getAttribute('role')).toEqual('option');
}));

it('should set aria-selected on each option', fakeAsync(() => {
expect(options[0].getAttribute('aria-selected')).toEqual('false');
expect(options[1].getAttribute('aria-selected')).toEqual('false');
expect(options[2].getAttribute('aria-selected')).toEqual('false');
it('should set aria-selected on each option for single select', fakeAsync(() => {
expect(options.every(option => !option.hasAttribute('aria-selected'))).toBe(true,
'Expected all unselected single-select options not to have aria-selected set.');

options[1].click();
fixture.detectChanges();
Expand All @@ -894,11 +892,44 @@ describe('MatSelect', () => {
fixture.detectChanges();
flush();

expect(options[0].getAttribute('aria-selected')).toEqual('false');
expect(options[1].getAttribute('aria-selected')).toEqual('true');
expect(options[2].getAttribute('aria-selected')).toEqual('false');
expect(options[1].getAttribute('aria-selected')).toEqual('true',
'Expected selected single-select option to have aria-selected="true".');
options.splice(1, 1);
expect(options.every(option => !option.hasAttribute('aria-selected'))).toBe(true,
'Expected all unselected single-select options not to have aria-selected set.');
}));

it('should set aria-selected on each option for multi-select', fakeAsync(() => {
fixture.destroy();

const multiFixture = TestBed.createComponent(MultiSelect);
multiFixture.detectChanges();

trigger = multiFixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
trigger.click();
multiFixture.detectChanges();

options = Array.from(overlayContainerElement.querySelectorAll('mat-option'));

expect(options.every(option => option.hasAttribute('aria-selected') &&
option.getAttribute('aria-selected') === 'false')).toBe(true,
'Expected all unselected multi-select options to have aria-selected="false".');

options[1].click();
multiFixture.detectChanges();

trigger.click();
multiFixture.detectChanges();
flush();

expect(options[1].getAttribute('aria-selected')).toEqual('true',
'Expected selected multi-select option to have aria-selected="true".');
options.splice(1, 1);
expect(options.every(option => option.hasAttribute('aria-selected') &&
option.getAttribute('aria-selected') === 'false')).toBe(true,
'Expected all unselected multi-select options to have aria-selected="false".');
}));

it('should set the tabindex of each option according to disabled state', fakeAsync(() => {
expect(options[0].getAttribute('tabindex')).toEqual('0');
expect(options[1].getAttribute('tabindex')).toEqual('0');
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/lib/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export declare class MatOption implements AfterViewChecked, OnDestroy {
value: any;
readonly viewValue: string;
constructor(_element: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _parent: MatOptionParentComponent, group: MatOptgroup);
_getAriaSelected(): boolean | null;
_getHostElement(): HTMLElement;
_getTabIndex(): string;
_handleKeydown(event: KeyboardEvent): void;
Expand Down