Skip to content

Commit

Permalink
fix(multiple): remove label for attribute on non-native elements (#28948
Browse files Browse the repository at this point in the history
)

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.

(cherry picked from commit 94a0834)
  • Loading branch information
crisbeto committed Apr 24, 2024
1 parent d78c770 commit 73d1e2e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/material/datepicker/date-range-input.ts
Expand Up @@ -256,6 +256,12 @@ export class MatDateRangeInput<D>
/** Emits when the input's state has changed. */
readonly stateChanges = new Subject<void>();

/**
* Disable the automatic labeling to avoid issues like #27241.
* @docs-private
*/
readonly disableAutomaticLabeling = true;

constructor(
private _changeDetectorRef: ChangeDetectorRef,
private _elementRef: ElementRef<HTMLElement>,
Expand Down
7 changes: 7 additions & 0 deletions src/material/form-field/form-field-control.ts
Expand Up @@ -68,6 +68,13 @@ export abstract class MatFormFieldControl<T> {
*/
readonly userAriaDescribedBy?: string;

/**
* Whether to automatically assign the ID of the form field as the `for` attribute
* on the `<label>` inside the form field. Set this to true to prevent the form
* field from associating the label with non-native elements.
*/
readonly disableAutomaticLabeling?: boolean;

/** Sets the list of element IDs that currently describe this control. */
abstract setDescribedByIds(ids: string[]): void;

Expand Down
2 changes: 1 addition & 1 deletion src/material/form-field/form-field.html
Expand Up @@ -16,7 +16,7 @@
[floating]="_shouldLabelFloat()"
[monitorResize]="_hasOutline()"
[id]="_labelId"
[attr.for]="_control.id">
[attr.for]="_control.disableAutomaticLabeling ? null : _control.id">
<ng-content select="mat-label"></ng-content>
<!--
We set the required marker as a separate element, in order to make it easier to target if
Expand Down
6 changes: 6 additions & 0 deletions src/material/select/select.ts
Expand Up @@ -332,6 +332,12 @@ export class MatSelect
*/
readonly stateChanges = new Subject<void>();

/**
* Disable the automatic labeling to avoid issues like #27241.
* @docs-private
*/
readonly disableAutomaticLabeling = true;

/**
* Implemented as part of MatFormFieldControl.
* @docs-private
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/datepicker.md
Expand Up @@ -573,6 +573,7 @@ export class MatDateRangeInput<D> implements MatFormFieldControl<DateRange<D>>,
controlType: string;
get dateFilter(): DateFilterFn<D>;
set dateFilter(value: DateFilterFn<D>);
readonly disableAutomaticLabeling = true;
get disabled(): boolean;
set disabled(value: boolean);
get empty(): boolean;
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/form-field.md
Expand Up @@ -165,6 +165,7 @@ export type MatFormFieldAppearance = 'fill' | 'outline';
export abstract class MatFormFieldControl<T> {
readonly autofilled?: boolean;
readonly controlType?: string;
readonly disableAutomaticLabeling?: boolean;
readonly disabled: boolean;
readonly empty: boolean;
readonly errorState: boolean;
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/material/select.md
Expand Up @@ -102,6 +102,7 @@ export class MatSelect implements AfterContentInit, OnChanges, OnDestroy, OnInit
// (undocumented)
protected _defaultOptions?: MatSelectConfig | undefined;
protected readonly _destroy: Subject<void>;
readonly disableAutomaticLabeling = true;
disabled: boolean;
disableOptionCentering: boolean;
disableRipple: boolean;
Expand Down

0 comments on commit 73d1e2e

Please sign in to comment.