You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rules/label-has-for.md
+20-2Lines changed: 20 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,13 @@
1
1
# label-has-for
2
2
3
-
Enforce label tags have htmlFor attribute. Form controls using a label to identify them must have only one label that is programmatically associated with the control using: label htmlFor=[ID of control].
3
+
Enforce label tags have associated control.
4
+
5
+
There are two supported ways to associate a label with a control:
6
+
7
+
- nesting: by wrapping a control in a label tag
8
+
- id: by using the prop `htmlFor` as in `htmlFor=[ID of control]`
9
+
10
+
To fully cover 100% of assistive devices, you're encouraged to validate for both nesting and id.
4
11
5
12
## Rule details
6
13
@@ -11,7 +18,10 @@ This rule takes one optional object argument of type object:
11
18
"rules": {
12
19
"jsx-a11y/label-has-for": [ 2, {
13
20
"components": [ "Label" ],
14
-
}],
21
+
"required": {
22
+
"every": [ "nesting", "id" ]
23
+
}
24
+
}],
15
25
}
16
26
}
17
27
```
@@ -43,6 +53,14 @@ return (
43
53
);
44
54
```
45
55
56
+
The `required` option (defaults to `"required": "id"`) determines which checks are activated. You're allowed to pass in one of the following types:
57
+
58
+
- string: must be one of the acceptable strings (`"nesting"` or `"id"`)
59
+
- object, must have one of the following properties:
60
+
61
+
- some: an array of acceptable strings, will pass if ANY of the requested checks passed
62
+
- every: an array of acceptable strings, will pass if ALL of the requested checks passed
63
+
46
64
Note that passing props as spread attribute without `htmlFor` explicitly defined will cause this rule to fail. Explicitly pass down `htmlFor` prop for rule to pass. The prop must have an actual value to pass. Use `Label` component above as a reference. **It is a good thing to explicitly pass props that you expect to be passed for self-documentation.** For example:
0 commit comments