web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.5 KiB

  1. # [Deprecated] label-has-for
  2. *This rule was deprecated in v6.1.0. It will no longer be maintained. Please use [`label-has-associated-control`](label-has-associated-control.md) instead.*
  3. Enforce label tags have associated control.
  4. There are two supported ways to associate a label with a control:
  5. - nesting: by wrapping a control in a label tag
  6. - id: by using the prop `htmlFor` as in `htmlFor=[ID of control]`
  7. To fully cover 100% of assistive devices, you're encouraged to validate for both nesting and id.
  8. ## Rule details
  9. This rule takes one optional object argument of type object:
  10. ```json
  11. {
  12. "rules": {
  13. "jsx-a11y/label-has-for": [ 2, {
  14. "components": [ "Label" ],
  15. "required": {
  16. "every": [ "nesting", "id" ]
  17. },
  18. "allowChildren": false
  19. }]
  20. }
  21. }
  22. ```
  23. 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):
  24. ```js
  25. // Label.js
  26. const Label = props => {
  27. const {
  28. htmlFor,
  29. ...otherProps
  30. } = props;
  31. return (
  32. <label htmlFor={htmlFor} {...otherProps} />
  33. );
  34. }
  35. ...
  36. // CreateAccount.js (for example)
  37. ...
  38. return (
  39. <form>
  40. <input id="firstName" type="text" />
  41. <Label htmlFor="firstName">First Name</Label>
  42. </form>
  43. );
  44. ```
  45. 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:
  46. - string: must be one of the acceptable strings (`"nesting"` or `"id"`)
  47. - object, must have one of the following properties:
  48. - some: an array of acceptable strings, will pass if ANY of the requested checks passed
  49. - every: an array of acceptable strings, will pass if ALL of the requested checks passed
  50. 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:
  51. ```js
  52. <label>{children}</label>
  53. ```
  54. 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`.
  55. 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:
  56. #### Bad
  57. ```jsx
  58. function Foo(props) {
  59. return <label {...props} />
  60. }
  61. ```
  62. #### Good
  63. ```jsx
  64. function Foo({ htmlFor, ...props}) {
  65. return <label htmlFor={htmlFor} {...props} />
  66. }
  67. // OR
  68. function Foo(props) {
  69. const {
  70. htmlFor,
  71. ...otherProps
  72. } = props;
  73. return <label htmlFor={htmlFor} {...otherProps} />
  74. }
  75. ```
  76. ### Succeed
  77. ```jsx
  78. <label htmlFor="firstName">
  79. <input type="text" id="firstName" />
  80. First Name
  81. </label>
  82. ```
  83. ### Fail
  84. ```jsx
  85. <input type="text" id="firstName" />
  86. <label>First Name</label>
  87. ```
  88. ## Accessibility guidelines
  89. - [WCAG 1.3.1](https://www.w3.org/WAI/WCAG21/Understanding/info-and-relationships)
  90. - [WCAG 3.3.2](https://www.w3.org/WAI/WCAG21/Understanding/labels-or-instructions)
  91. - [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value)