| 1 | # jsx-a11y/label-has-for
|
|---|
| 2 |
|
|---|
| 3 | ❌ This rule is deprecated. It was replaced by [`jsx-a11y/label-has-associated-control`](label-has-associated-control.md).
|
|---|
| 4 |
|
|---|
| 5 | 🚫 This rule is _disabled_ in the following configs: ☑️ `recommended`, 🔒 `strict`.
|
|---|
| 6 |
|
|---|
| 7 | <!-- end auto-generated rule header -->
|
|---|
| 8 |
|
|---|
| 9 | _This rule was deprecated in v6.1.0. It will no longer be maintained._
|
|---|
| 10 |
|
|---|
| 11 | Enforce label tags have associated control.
|
|---|
| 12 |
|
|---|
| 13 | There are two supported ways to associate a label with a control:
|
|---|
| 14 |
|
|---|
| 15 | - nesting: by wrapping a control in a label tag
|
|---|
| 16 | - id: by using the prop `htmlFor` (or any configured attribute) as in `htmlFor=[ID of control]`
|
|---|
| 17 |
|
|---|
| 18 | To fully cover 100% of assistive devices, you're encouraged to validate for both nesting and id.
|
|---|
| 19 |
|
|---|
| 20 | ## Rule options
|
|---|
| 21 |
|
|---|
| 22 | This rule takes one optional object argument of type object:
|
|---|
| 23 |
|
|---|
| 24 | ```json
|
|---|
| 25 | {
|
|---|
| 26 | "rules": {
|
|---|
| 27 | "jsx-a11y/label-has-for": [ 2, {
|
|---|
| 28 | "components": [ "Label" ],
|
|---|
| 29 | "required": {
|
|---|
| 30 | "every": [ "nesting", "id" ]
|
|---|
| 31 | },
|
|---|
| 32 | "allowChildren": false
|
|---|
| 33 | }]
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 | ```
|
|---|
| 37 |
|
|---|
| 38 | For the `components` option, these strings determine which JSX elements (**always including** `<label>`) should be checked for having `htmlFor` prop. This is a good use case when you have a wrapper component that simply renders a `label` element (like in React):
|
|---|
| 39 |
|
|---|
| 40 | ```js
|
|---|
| 41 | // Label.js
|
|---|
| 42 | const Label = props => {
|
|---|
| 43 | const {
|
|---|
| 44 | htmlFor,
|
|---|
| 45 | ...otherProps
|
|---|
| 46 | } = props;
|
|---|
| 47 |
|
|---|
| 48 | return (
|
|---|
| 49 | <label htmlFor={htmlFor} {...otherProps} />
|
|---|
| 50 | );
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | ...
|
|---|
| 54 |
|
|---|
| 55 | // CreateAccount.js (for example)
|
|---|
| 56 | ...
|
|---|
| 57 | return (
|
|---|
| 58 | <form>
|
|---|
| 59 | <input id="firstName" type="text" />
|
|---|
| 60 | <Label htmlFor="firstName">First Name</Label>
|
|---|
| 61 | </form>
|
|---|
| 62 | );
|
|---|
| 63 | ```
|
|---|
| 64 |
|
|---|
| 65 | The `required` option (defaults to `"required": { "every": ["nesting", "id"] }`) determines which checks are activated. You're allowed to pass in one of the following types:
|
|---|
| 66 |
|
|---|
| 67 | - string: must be one of the acceptable strings (`"nesting"` or `"id"`)
|
|---|
| 68 | - object, must have one of the following properties:
|
|---|
| 69 |
|
|---|
| 70 | - some: an array of acceptable strings, will pass if ANY of the requested checks passed
|
|---|
| 71 | - every: an array of acceptable strings, will pass if ALL of the requested checks passed
|
|---|
| 72 |
|
|---|
| 73 | The `allowChildren` option (defaults to `false`) determines whether `{children}` content is allowed to be passed into a `label` element. For example, the following pattern, by default, is not allowed:
|
|---|
| 74 |
|
|---|
| 75 | ```js
|
|---|
| 76 | <label>{children}</label>
|
|---|
| 77 | ```
|
|---|
| 78 |
|
|---|
| 79 | However, if `allowChildren` is set to `true`, no error will be raised. If you want to pass in `{children}` content without raising an error, because you cannot be sure what `{children}` will render, then set `allowChildren` to `true`.
|
|---|
| 80 |
|
|---|
| 81 | 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:
|
|---|
| 82 |
|
|---|
| 83 | #### Bad
|
|---|
| 84 |
|
|---|
| 85 | ```jsx
|
|---|
| 86 | function Foo(props) {
|
|---|
| 87 | return <label {...props} />
|
|---|
| 88 | }
|
|---|
| 89 | ```
|
|---|
| 90 |
|
|---|
| 91 | #### Good
|
|---|
| 92 |
|
|---|
| 93 | ```jsx
|
|---|
| 94 | function Foo({ htmlFor, ...props}) {
|
|---|
| 95 | return <label htmlFor={htmlFor} {...props} />
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | // OR
|
|---|
| 99 |
|
|---|
| 100 | function Foo(props) {
|
|---|
| 101 | const {
|
|---|
| 102 | htmlFor,
|
|---|
| 103 | ...otherProps
|
|---|
| 104 | } = props;
|
|---|
| 105 |
|
|---|
| 106 | return <label htmlFor={htmlFor} {...otherProps} />
|
|---|
| 107 | }
|
|---|
| 108 | ```
|
|---|
| 109 |
|
|---|
| 110 | ### Succeed
|
|---|
| 111 |
|
|---|
| 112 | ```jsx
|
|---|
| 113 | <label htmlFor="firstName">
|
|---|
| 114 | <input type="text" id="firstName" />
|
|---|
| 115 | First Name
|
|---|
| 116 | </label>
|
|---|
| 117 | ```
|
|---|
| 118 |
|
|---|
| 119 | ### Fail
|
|---|
| 120 |
|
|---|
| 121 | ```jsx
|
|---|
| 122 | <input type="text" id="firstName" />
|
|---|
| 123 | <label>First Name</label>
|
|---|
| 124 | ```
|
|---|
| 125 |
|
|---|
| 126 | ## Accessibility guidelines
|
|---|
| 127 |
|
|---|
| 128 | - [WCAG 1.3.1](https://www.w3.org/WAI/WCAG21/Understanding/info-and-relationships)
|
|---|
| 129 | - [WCAG 3.3.2](https://www.w3.org/WAI/WCAG21/Understanding/labels-or-instructions)
|
|---|
| 130 | - [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value)
|
|---|