Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

label-has-associated-control.md 2.9 KiB

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # label-has-associated-control
  2. Enforce that a label tag has a text label and an associated control.
  3. There are two supported ways to associate a label with a control:
  4. - Wrapping a control in a label tag.
  5. - Adding `htmlFor` to a label and assigning it a DOM ID string that indicates an input on the page.
  6. This 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.
  7. ## How do I resolve this error?
  8. ### Case: I just want a text label associated with an input.
  9. The simplest way to achieve an association between a label and an input is to wrap the input in the label.
  10. ```jsx
  11. <label>
  12. Surname
  13. <input type="text" />
  14. </label>
  15. ```
  16. All modern browsers and assistive technology will associate the label with the control.
  17. ### Case: The label is a sibling of the control.
  18. In this case, use `htmlFor` and an ID to associate the controls.
  19. ```jsx
  20. <label htmlFor={domId}>Surname</label>
  21. <input type="text" id={domId} />
  22. ```
  23. ### Case: My label and input components are custom components.
  24. You can configure the rule to be aware of your custom components.
  25. ```jsx
  26. <CustomInputLabel label="Surname">
  27. <CustomInput type="text" value={value} />
  28. </CustomInputLabel>
  29. ```
  30. And the configuration:
  31. ```json
  32. {
  33. "rules": {
  34. "jsx-a11y/label-has-associated-control": [ 2, {
  35. "labelComponents": ["CustomInputLabel"],
  36. "labelAttributes": ["label"],
  37. "controlComponents": ["CustomInput"],
  38. "depth": 3,
  39. }],
  40. }
  41. }
  42. ```
  43. ## Rule details
  44. This rule takes one optional object argument of type object:
  45. ```json
  46. {
  47. "rules": {
  48. "jsx-a11y/label-has-associated-control": [ 2, {
  49. "labelComponents": ["CustomLabel"],
  50. "labelAttributes": ["inputLabel"],
  51. "controlComponents": ["CustomInput"],
  52. "depth": 3,
  53. }],
  54. }
  55. }
  56. ```
  57. `labelComponents` is a list of custom React Component names that should be checked for an associated control.
  58. `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.
  59. `controlComponents` is a list of custom React Components names that will output an input element.
  60. `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.
  61. ### Fail
  62. ```jsx
  63. function Foo(props) {
  64. return <label {...props} />
  65. }
  66. ```
  67. ### Succeed
  68. ```jsx
  69. function Foo(props) {
  70. const {
  71. htmlFor,
  72. ...otherProps
  73. } = props;
  74. return <label htmlFor={htmlFor} {...otherProps} />
  75. }
  76. ```
  77. ### Fail
  78. ```jsx
  79. <input type="text" />
  80. <label>Surname</label>
  81. ```
  82. ### Succeed
  83. ```jsx
  84. <label>
  85. <input type="text" />
  86. Surname
  87. </label>
  88. ```