Skip to content

fix(multiple): remove label for attribute on non-native elements #28948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/material/datepicker/date-range-input.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have @docs-private like the other disableAutomaticLabeling?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah this one is an actual public API. In the other places it's something users shouldn't touch.


/** 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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
Original file line number Diff line number Diff line change
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