From 3b7f0f12d4c3ffb42c1e31dcbed6f52721da1f6c Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 27 Aug 2018 18:58:39 +0200 Subject: [PATCH] fix(select): pointing to non-existent element via aria-labelledby (#12411) 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. --- src/lib/select/select.spec.ts | 11 +++++++++++ src/lib/select/select.ts | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib/select/select.spec.ts b/src/lib/select/select.spec.ts index 80510998722..2f8a152aa0b 100644 --- a/src/lib/select/select.spec.ts +++ b/src/lib/select/select.spec.ts @@ -126,6 +126,7 @@ describe('MatSelect', () => { SelectWithGroups, SelectWithGroupsAndNgContainer, SelectWithFormFieldLabel, + SelectWithChangeEvent, ]); })); @@ -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(); diff --git a/src/lib/select/select.ts b/src/lib/select/select.ts index b51ffcb7a13..775569e4706 100644 --- a/src/lib/select/select.ts +++ b/src/lib/select/select.ts @@ -1065,7 +1065,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; }