Skip to content

Commit

Permalink
fix(select): pointing to non-existent element via aria-labelledby
Browse files Browse the repository at this point in the history
Fixes `mat-select` referring to an element that doesn't exist, through `aria-labelledby`, if the select is inside a form field that doesn't have a label.

Fixes #12405.
  • Loading branch information
crisbeto authored and jelbourn committed Aug 23, 2018
1 parent f1b65b6 commit 3936345
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ describe('MatSelect', () => {
SelectWithGroups,
SelectWithGroupsAndNgContainer,
SelectWithFormFieldLabel,
SelectWithChangeEvent,
]);
}));

Expand Down Expand Up @@ -255,6 +256,16 @@ describe('MatSelect', () => {
expect(select.getAttribute('aria-labelledby')).toBeFalsy();
});

it('should not set `aria-labelledby` if there is no form field label', () => {
fixture.destroy();

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

expect(select.getAttribute('aria-labelledby')).toBeFalsy();
});

it('should select options via the UP/DOWN arrow keys on a closed select', fakeAsync(() => {
const formControl = fixture.componentInstance.control;
const options = fixture.componentInstance.options.toArray();
Expand Down
3 changes: 2 additions & 1 deletion src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,

// Note: we use `_getAriaLabel` here, because we want to check whether there's a
// computed label. `this.ariaLabel` is only the user-specified label.
if (!this._parentFormField || this._getAriaLabel()) {
if (!this._parentFormField || !this._parentFormField._hasFloatingLabel() ||
this._getAriaLabel()) {
return null;
}

Expand Down

0 comments on commit 3936345

Please sign in to comment.