Skip to content

Commit 94a0834

Browse files
authored
fix(multiple): remove label for attribute on non-native elements (#28948)
Adds an option to remove the `for` attribute from the `<label>` inside the form field from elements that can't be labeled. Also applies the new option to `mat-select` and `mat-date-range-picker`. This isn't an accessibility regression, because those elements were already being labeled using `aria-labelledby`. Fixes #27241.
1 parent cf3506a commit 94a0834

File tree

7 files changed

+23
-1
lines changed

7 files changed

+23
-1
lines changed

src/material/datepicker/date-range-input.ts

+6
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ export class MatDateRangeInput<D>
256256
/** Emits when the input's state has changed. */
257257
readonly stateChanges = new Subject<void>();
258258

259+
/**
260+
* Disable the automatic labeling to avoid issues like #27241.
261+
* @docs-private
262+
*/
263+
readonly disableAutomaticLabeling = true;
264+
259265
constructor(
260266
private _changeDetectorRef: ChangeDetectorRef,
261267
private _elementRef: ElementRef<HTMLElement>,

src/material/form-field/form-field-control.ts

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ export abstract class MatFormFieldControl<T> {
6868
*/
6969
readonly userAriaDescribedBy?: string;
7070

71+
/**
72+
* Whether to automatically assign the ID of the form field as the `for` attribute
73+
* on the `<label>` inside the form field. Set this to true to prevent the form
74+
* field from associating the label with non-native elements.
75+
*/
76+
readonly disableAutomaticLabeling?: boolean;
77+
7178
/** Sets the list of element IDs that currently describe this control. */
7279
abstract setDescribedByIds(ids: string[]): void;
7380

src/material/form-field/form-field.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[floating]="_shouldLabelFloat()"
1717
[monitorResize]="_hasOutline()"
1818
[id]="_labelId"
19-
[attr.for]="_control.id">
19+
[attr.for]="_control.disableAutomaticLabeling ? null : _control.id">
2020
<ng-content select="mat-label"></ng-content>
2121
<!--
2222
We set the required marker as a separate element, in order to make it easier to target if

src/material/select/select.ts

+6
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ export class MatSelect
332332
*/
333333
readonly stateChanges = new Subject<void>();
334334

335+
/**
336+
* Disable the automatic labeling to avoid issues like #27241.
337+
* @docs-private
338+
*/
339+
readonly disableAutomaticLabeling = true;
340+
335341
/**
336342
* Implemented as part of MatFormFieldControl.
337343
* @docs-private

tools/public_api_guard/material/datepicker.md

+1
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,
573573
controlType: string;
574574
get dateFilter(): DateFilterFn<D>;
575575
set dateFilter(value: DateFilterFn<D>);
576+
readonly disableAutomaticLabeling = true;
576577
get disabled(): boolean;
577578
set disabled(value: boolean);
578579
get empty(): boolean;

tools/public_api_guard/material/form-field.md

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export type MatFormFieldAppearance = 'fill' | 'outline';
166166
export abstract class MatFormFieldControl<T> {
167167
readonly autofilled?: boolean;
168168
readonly controlType?: string;
169+
readonly disableAutomaticLabeling?: boolean;
169170
readonly disabled: boolean;
170171
readonly empty: boolean;
171172
readonly errorState: boolean;

tools/public_api_guard/material/select.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class MatSelect implements AfterContentInit, OnChanges, OnDestroy, OnInit
102102
// (undocumented)
103103
protected _defaultOptions?: MatSelectConfig | undefined;
104104
protected readonly _destroy: Subject<void>;
105+
readonly disableAutomaticLabeling = true;
105106
disabled: boolean;
106107
disableOptionCentering: boolean;
107108
disableRipple: boolean;

0 commit comments

Comments
 (0)