Skip to content

Commit 5f38b88

Browse files
committed
fix(material/select): remove value from aria-labelledby
1 parent 2bade9d commit 5f38b88

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
@@ -107,11 +107,9 @@ describe('MatPaginator', () => {
107107
expect(getNextButton(fixture).getAttribute('aria-label')).toBe('Next page');
108108

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

117115
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
@@ -179,31 +179,17 @@ describe('MatSelect', () => {
179179
fixture.detectChanges();
180180

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

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

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

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

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

301-
it('should set `aria-labelledby` to the value ID if there is no form field', () => {
302-
fixture.destroy();
303-
304-
const labelFixture = TestBed.createComponent(SelectWithChangeEvent);
305-
labelFixture.detectChanges();
306-
select = labelFixture.debugElement.query(By.css('mat-select'))!.nativeElement;
307-
const valueId = labelFixture.nativeElement.querySelector('.mat-mdc-select-value').id;
308-
309-
expect(select.getAttribute('aria-labelledby')?.trim()).toBe(valueId);
310-
});
311-
312287
it('should select options via the UP/DOWN arrow keys on a closed select', fakeAsync(() => {
313288
const formControl = fixture.componentInstance.control;
314289
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)