Skip to content

Commit 9924d03

Browse files
stevemaobeefancohen
authored andcommitted
document jsx-a11y/label-has-associated-control assert option
Fixes https://github.com/evcohen/eslint-plugin-jsx-a11y/issues/527
1 parent fff6a4b commit 9924d03

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/rules/label-has-associated-control.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ And the configuration:
5858
}
5959
```
6060

61+
### Case: I have two labels for the same input
62+
63+
If 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.
64+
65+
```jsx
66+
{/* eslint jsx-a11y/label-has-associated-control: ["error", { assert: "either" } ] */}
67+
<label htmlFor="a">
68+
Username:
69+
</label>
70+
...
71+
<label htmlFor="a">
72+
<input id="a" />
73+
</label>
74+
```
75+
6176
## Rule details
6277

6378
This rule takes one optional object argument of type object:
@@ -69,6 +84,7 @@ This rule takes one optional object argument of type object:
6984
"labelComponents": ["CustomLabel"],
7085
"labelAttributes": ["inputLabel"],
7186
"controlComponents": ["CustomInput"],
87+
"assert": "both",
7288
"depth": 3,
7389
}],
7490
}
@@ -78,6 +94,7 @@ This rule takes one optional object argument of type object:
7894
`labelComponents` is a list of custom React Component names that should be checked for an associated control.
7995
`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.
8096
`controlComponents` is a list of custom React Components names that will output an input element.
97+
`assert` asserts that the label has htmlFor, a nested label, both or either. Available options: `'htmlFor', 'nesting', 'both', 'either'`.
8198
`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.
8299

83100
### Fail

0 commit comments

Comments
 (0)