Skip to content

fix(material/select): don't announce selected value multiple times #24776

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

Closed
Closed
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
19 changes: 7 additions & 12 deletions src/material-experimental/mdc-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,28 +192,24 @@ describe('MDC-based MatSelect', () => {
fixture.detectChanges();

const labelId = fixture.nativeElement.querySelector('label').id;
const valueId = fixture.nativeElement.querySelector('.mat-mdc-select-value').id;

expect(select.getAttribute('aria-labelledby')).toBe(`${labelId} ${valueId} myLabelId`);
expect(select.getAttribute('aria-labelledby')).toBe(`${labelId} myLabelId`);
}));

it('should set aria-labelledby to the value and label IDs', fakeAsync(() => {
it('should set aria-labelledby to the label IDs', fakeAsync(() => {
fixture.detectChanges();

const labelId = fixture.nativeElement.querySelector('label').id;
const valueId = fixture.nativeElement.querySelector('.mat-mdc-select-value').id;
expect(select.getAttribute('aria-labelledby')).toBe(`${labelId} ${valueId}`);
expect(select.getAttribute('aria-labelledby')).toBe(`${labelId}`);
}));

it('should trim the trigger aria-labelledby when there is no label', fakeAsync(() => {
it('should remove aria-labelledby when there is no label', fakeAsync(() => {
fixture.componentInstance.hasLabel = false;
fixture.detectChanges();
flush();
fixture.detectChanges();

// Note that we assert that there are no spaces around the value.
const valueId = fixture.nativeElement.querySelector('.mat-mdc-select-value').id;
expect(select.getAttribute('aria-labelledby')).toBe(`${valueId}`);
expect(select.hasAttribute('aria-labelledby')).toBeFalse();
}));

it('should set the tabindex of the select to 0 by default', fakeAsync(() => {
Expand Down Expand Up @@ -287,15 +283,14 @@ describe('MDC-based MatSelect', () => {
expect(select.getAttribute('tabindex')).toEqual('0');
}));

it('should set `aria-labelledby` to the value ID if there is no form field', () => {
it('should remove `aria-labelledby` if there is no form field', () => {
fixture.destroy();

const labelFixture = TestBed.createComponent(SelectWithChangeEvent);
labelFixture.detectChanges();
select = labelFixture.debugElement.query(By.css('mat-select'))!.nativeElement;
const valueId = labelFixture.nativeElement.querySelector('.mat-mdc-select-value').id;

expect(select.getAttribute('aria-labelledby')?.trim()).toBe(valueId);
expect(select.hasAttribute('aria-labelledby')).toBeFalse();
});

it('should select options via the UP/DOWN arrow keys on a closed select', fakeAsync(() => {
Expand Down
19 changes: 7 additions & 12 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,27 +191,23 @@ describe('MatSelect', () => {
fixture.detectChanges();

const labelId = fixture.nativeElement.querySelector('.mat-form-field-label').id;
const valueId = fixture.nativeElement.querySelector('.mat-select-value').id;

expect(select.getAttribute('aria-labelledby')).toBe(`${labelId} ${valueId} myLabelId`);
expect(select.getAttribute('aria-labelledby')).toBe(`${labelId} myLabelId`);
}));

it('should set aria-labelledby to the value and label IDs', fakeAsync(() => {
it('should set aria-labelledby to the label IDs', fakeAsync(() => {
fixture.detectChanges();

const labelId = fixture.nativeElement.querySelector('.mat-form-field-label').id;
const valueId = fixture.nativeElement.querySelector('.mat-select-value').id;
expect(select.getAttribute('aria-labelledby')).toBe(`${labelId} ${valueId}`);
expect(select.getAttribute('aria-labelledby')).toBe(`${labelId}`);
}));

it('should trim the trigger aria-labelledby when there is no label', fakeAsync(() => {
it('should remove aria-labelledby when there is no label', fakeAsync(() => {
// Reset the `placeholder` which also controls the label of the form field.
fixture.componentInstance.select.placeholder = '';
fixture.detectChanges();

// Note that we assert that there are no spaces around the value.
const valueId = fixture.nativeElement.querySelector('.mat-select-value').id;
expect(select.getAttribute('aria-labelledby')).toBe(`${valueId}`);
expect(select.hasAttribute('aria-labelledby')).toBeFalse();
}));

it('should set the tabindex of the select to 0 by default', fakeAsync(() => {
Expand Down Expand Up @@ -285,15 +281,14 @@ describe('MatSelect', () => {
expect(select.getAttribute('tabindex')).toEqual('0');
}));

it('should set `aria-labelledby` to the value ID if there is no form field', () => {
it('should remove `aria-labelledby` if there is no form field', () => {
fixture.destroy();

const labelFixture = TestBed.createComponent(SelectWithChangeEvent);
labelFixture.detectChanges();
select = labelFixture.debugElement.query(By.css('mat-select'))!.nativeElement;
const valueId = labelFixture.nativeElement.querySelector('.mat-select-value').id;

expect(select.getAttribute('aria-labelledby')?.trim()).toBe(valueId);
expect(select.hasAttribute('aria-labelledby')).toBeFalse();
});

it('should select options via the UP/DOWN arrow keys on a closed select', fakeAsync(() => {
Expand Down
8 changes: 1 addition & 7 deletions src/material/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,13 +1127,7 @@ export abstract class _MatSelectBase<C>
}

const labelId = this._parentFormField?.getLabelId();
let value = (labelId ? labelId + ' ' : '') + this._valueId;

if (this.ariaLabelledby) {
value += ' ' + this.ariaLabelledby;
}

return value;
return this.ariaLabelledby ? labelId + ' ' + this.ariaLabelledby : labelId;
}

/** Called when the overlay panel is done animating. */
Expand Down