Skip to content

Commit 1032655

Browse files
authored
fix(multiple): remove label for attribute on non-native elements (#2258)
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 `sbb-select` and `sbb-textarea`. This isn't an accessibility regression, because those elements were already being labeled using `aria-labelledby`. Fixes #1918
1 parent babc3fa commit 1032655

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ export abstract class SbbFormFieldControl<TValue> {
4141
*/
4242
readonly userAriaDescribedBy?: string;
4343

44+
/**
45+
* Whether to automatically assign the ID of the form field as the `for` attribute
46+
* on the `<label>` inside the form field. Set this to true to prevent the form
47+
* field from associating the label with non-native elements.
48+
*/
49+
readonly disableAutomaticLabeling?: boolean;
50+
4451
/** Sets the list of element IDs that currently describe this control. */
4552
abstract setDescribedByIds(ids: string[]): void;
4653

src/angular/form-field/form-field.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<label
44
class="sbb-label sbb-form-field-label"
55
[id]="_labelId"
6-
[attr.for]="_control.id"
6+
[attr.for]="_control.disableAutomaticLabeling ? null : _control.id"
77
[class.sbb-form-field-empty]="_control.empty"
88
>
99
@switch (_hasLabel()) {

src/angular/select/select.ts

+6
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ export class SbbSelect
276276
*/
277277
readonly stateChanges = new Subject<void>();
278278

279+
/**
280+
* Disable the automatic labeling to avoid issues like #1918.
281+
* @docs-private
282+
*/
283+
readonly disableAutomaticLabeling = true;
284+
279285
/**
280286
* Implemented as part of MatFormFieldControl.
281287
* @docs-private

src/angular/textarea/textarea/textarea.ts

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export class SbbTextarea
7070
return `${this.id || this._uniqueId}-input`;
7171
}
7272

73+
/**
74+
* Disable the automatic labeling to avoid issues like #1918.
75+
* @docs-private
76+
*/
77+
readonly disableAutomaticLabeling = true;
78+
7379
/** Emits when the state of the option changes and any parents have to be notified. */
7480
readonly stateChanges = new Subject<void>();
7581

0 commit comments

Comments
 (0)