Skip to content

Commit f573ce8

Browse files
authored
fix(material/select): remove value from aria-labelledby (#30374)
1 parent 8aaf6f4 commit f573ce8

File tree

3 files changed

+17
-35
lines changed

3 files changed

+17
-35
lines changed

src/material/paginator/paginator.spec.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,9 @@ describe('MatPaginator', () => {
106106
expect(getNextButton(fixture).getAttribute('aria-label')).toBe('Next page');
107107

108108
const select = fixture.nativeElement.querySelector('.mat-mdc-select');
109-
const selectLabelIds = select.getAttribute('aria-labelledby')?.split(/\s/g) as string[];
110-
const selectLabelTexts = selectLabelIds?.map(labelId => {
111-
return fixture.nativeElement.querySelector(`#${labelId}`)?.textContent?.trim();
112-
});
113-
expect(selectLabelTexts).toContain('Items per page:');
109+
const selectLabelId = select.getAttribute('aria-labelledby').trim();
110+
const selectLabelText = fixture.nativeElement.querySelector(`#${selectLabelId}`)?.textContent;
111+
expect(selectLabelText).toContain('Items per page:');
114112
});
115113

116114
it('should re-render when the i18n labels change', () => {

src/material/select/select.spec.ts

+3-28
Original file line numberDiff line numberDiff line change
@@ -178,31 +178,17 @@ describe('MatSelect', () => {
178178
fixture.detectChanges();
179179

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

183-
expect(select.getAttribute('aria-labelledby')).toBe(`${labelId} ${valueId} myLabelId`);
182+
expect(select.getAttribute('aria-labelledby')).toBe(`${labelId} myLabelId`);
184183
});
185184

186-
it('should set aria-labelledby to the value and label IDs', () => {
185+
it('should set aria-labelledby to the label ID', () => {
187186
fixture.detectChanges();
188187

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

194-
it('should trim the trigger aria-labelledby when there is no label', fakeAsync(() => {
195-
fixture.componentInstance.hasLabel = false;
196-
fixture.changeDetectorRef.markForCheck();
197-
fixture.detectChanges();
198-
flush();
199-
fixture.detectChanges();
200-
201-
// Note that we assert that there are no spaces around the value.
202-
const valueId = fixture.nativeElement.querySelector('.mat-mdc-select-value').id;
203-
expect(select.getAttribute('aria-labelledby')).toBe(`${valueId}`);
204-
}));
205-
206192
it('should set the tabindex of the select to 0 by default', () => {
207193
expect(select.getAttribute('tabindex')).toEqual('0');
208194
});
@@ -307,17 +293,6 @@ describe('MatSelect', () => {
307293
expect(select.getAttribute('tabindex')).toEqual('0');
308294
});
309295

310-
it('should set `aria-labelledby` to the value ID if there is no form field', () => {
311-
fixture.destroy();
312-
313-
const labelFixture = TestBed.createComponent(SelectWithChangeEvent);
314-
labelFixture.detectChanges();
315-
select = labelFixture.debugElement.query(By.css('mat-select'))!.nativeElement;
316-
const valueId = labelFixture.nativeElement.querySelector('.mat-mdc-select-value').id;
317-
318-
expect(select.getAttribute('aria-labelledby')?.trim()).toBe(valueId);
319-
});
320-
321296
it('should select options via the UP/DOWN arrow keys on a closed select', fakeAsync(() => {
322297
const formControl = fixture.componentInstance.control;
323298
const options = fixture.componentInstance.options.toArray();

src/material/select/select.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1439,13 +1439,22 @@ export class MatSelect
14391439
return null;
14401440
}
14411441

1442-
const labelId = this._parentFormField?.getLabelId();
1443-
let value = (labelId ? labelId + ' ' : '') + this._valueId;
1442+
let value = this._parentFormField?.getLabelId() || '';
14441443

14451444
if (this.ariaLabelledby) {
14461445
value += ' ' + this.ariaLabelledby;
14471446
}
14481447

1448+
// The value should not be used for the trigger's aria-labelledby,
1449+
// but this currently "breaks" accessibility tests since they complain
1450+
// there is no aria-labelledby. This is because they are not setting an
1451+
// appropriate label on the form field or select.
1452+
// TODO: remove this conditional after fixing clients by ensuring their
1453+
// selects have a label applied.
1454+
if (!value) {
1455+
value = this._valueId;
1456+
}
1457+
14491458
return value;
14501459
}
14511460

0 commit comments

Comments
 (0)