source: frontend/node_modules/eslint-plugin-jsx-a11y/docs/rules/label-has-associated-control.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[9af201e]1# jsx-a11y/label-has-associated-control
2
3💼 This rule is enabled in the following configs: ☑️ `recommended`, 🔒 `strict`.
4
5<!-- end auto-generated rule header -->
6
7Enforce that a label tag has a text label and an associated control.
8
9There are two supported ways to associate a label with a control:
10
11- Wrapping a control in a label tag.
12- Adding `htmlFor` to a label and assigning it a DOM ID string that indicates an input on the page.
13
14This rule checks that any `label` tag (or an indicated custom component that will output a `label` tag) either (1) wraps an `input` element (or an indicated custom component that will output an `input` tag) or (2) has an `htmlFor` attribute and that the `label` tag has text content.
15
16## How do I resolve this error?
17
18### Case: I just want a text label associated with an input.
19
20The simplest way to achieve an association between a label and an input is to wrap the input in the label.
21
22```jsx
23<label>
24 Surname
25 <input type="text" />
26</label>
27```
28
29All modern browsers and assistive technology will associate the label with the control.
30
31### Case: The label is a sibling of the control.
32
33In this case, use `htmlFor` and an ID to associate the controls.
34
35```jsx
36<label htmlFor={domId}>Surname</label>
37<input type="text" id={domId} />
38```
39
40### Case: My label and input components are custom components.
41
42You can configure the rule to be aware of your custom components.
43
44```jsx
45<CustomInputLabel label="Surname">
46 <CustomInput type="text" value={value} />
47</CustomInputLabel>
48```
49
50And the configuration:
51
52```json
53{
54 "rules": {
55 "jsx-a11y/label-has-associated-control": [ 2, {
56 "labelComponents": ["CustomInputLabel"],
57 "labelAttributes": ["label"],
58 "controlComponents": ["CustomInput"],
59 "depth": 3,
60 }],
61 }
62}
63```
64
65### Case: I have two labels for the same input
66
67If the second `label` is in a different part of the HTML, then the second one can only contain `htmlFor` but not nesting. You will probably need eslint override comment on the second label.
68
69```jsx
70{/* eslint jsx-a11y/label-has-associated-control: ["error", { assert: "either" } ] */}
71<label htmlFor="a">
72 Username:
73</label>
74...
75<label htmlFor="a">
76 <input id="a" />
77</label>
78```
79
80## How to manage IDs of `input`
81
82A common way to think of `id` with libraries like React is, `id`s should be avoided since it must be unique on the page, and components need to be reusable. Hence it is tempted to generate `id` during render-time if `id` is required. *However:*
83
84IDs shouldn't be generated in the browser, so that server and client rendering are deterministic. Render-time uuids aren't just a hack, they're actually broken and should never be used.
85
86To restate, **every ID needs to be deterministic**, on the server and the client, and guaranteed to be unique on the page. EG: For each input, a required ID prop can be passed down from as far up the tree as possible to guarantee uniqueness.
87
88## Rule options
89
90This rule takes one optional object argument of type object:
91
92```json
93{
94 "rules": {
95 "jsx-a11y/label-has-associated-control": [ 2, {
96 "labelComponents": ["CustomLabel"],
97 "labelAttributes": ["inputLabel"],
98 "controlComponents": ["CustomInput"],
99 "assert": "both",
100 "depth": 3,
101 }],
102 }
103}
104```
105
106`labelComponents` is a list of custom React Component names that should be checked for an associated control.
107
108`labelAttributes` is a list of attributes to check on the label component and its children for a label. Use this if you have a custom component that uses a string passed on a prop to render an HTML `label`, for example.
109
110`controlComponents` is a list of custom React Components names that will output an input element. [Glob format](https://linuxhint.com/bash_globbing_tutorial/) is also supported for specifying names (e.g., `Label*` matches `LabelComponent` but not `CustomLabel`, `????Label` matches `LinkLabel` but not `CustomLabel`).
111
112`assert` asserts that the label has htmlFor, a nested label, both or either. Available options: `'htmlFor', 'nesting', 'both', 'either'`.
113
114`depth` (default 2, max 25) is an integer that determines how deep within a `JSXElement` label the rule should look for text content or an element with a label to determine if the `label` element will have an accessible label.
115
116### Fail
117```jsx
118function Foo(props) {
119 return <label {...props} />
120}
121```
122
123### Succeed
124```jsx
125function Foo(props) {
126 const {
127 htmlFor,
128 ...otherProps
129 } = props;
130
131 return <label htmlFor={htmlFor} {...otherProps} />
132}
133```
134
135### Fail
136```jsx
137<input type="text" />
138<label>Surname</label>
139```
140
141### Succeed
142```jsx
143<label>
144 <input type="text" />
145 Surname
146</label>
147```
148
149## Accessibility guidelines
150- [WCAG 1.3.1](https://www.w3.org/WAI/WCAG21/Understanding/info-and-relationships)
151- [WCAG 3.3.2](https://www.w3.org/WAI/WCAG21/Understanding/labels-or-instructions)
152- [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value)
Note: See TracBrowser for help on using the repository browser.