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.

109 lines
3.6 KiB

  1. # control-has-associated-label
  2. Enforce that a control (an interactive element) has a text label.
  3. There are two supported ways to supply a control with a text label:
  4. - Provide text content inside the element.
  5. - Use the `aria-label` attribute on the element, with a text value.
  6. - Use the `aria-labelledby` attribute on the element, and point the IDREF value to an element with an accessible label.
  7. - Alternatively, with an `img` tag, you may use the `alt` attribute to supply a text description of the image.
  8. The rule is permissive in the sense that it will assume that expressions will eventually provide a label. So an element like this will pass.
  9. ```jsx
  10. <button type="button">{maybeSomethingThatContainsALabel}</button>
  11. ```
  12. ## How do I resolve this error?
  13. ### Case: I have a simple button that requires a label.
  14. Provide text content in the `button` element.
  15. ```jsx
  16. <button type="button">Save</button>
  17. ```
  18. ### Case: I have an icon button and I don't want visible text.
  19. Use the `aria-label` attribute and provide the text label as the value.
  20. ```jsx
  21. <button type="button" aria-label="Save" class="icon-save" />
  22. ```
  23. ### Case: The label for my element is already located on the page and I don't want to repeat the text in my source code.
  24. Use the `aria-labelledby` attribute and point the IDREF value to an element with an accessible label.
  25. ```jsx
  26. <div id="js_1">Comment</div>
  27. <textarea aria-labelledby="js_1"></textarea>
  28. ```
  29. ### Case: My label and input components are custom components, but I still want to require that they have an accessible text label.
  30. You can configure the rule to be aware of your custom components. Refer to the Rule Details below.
  31. ```jsx
  32. <CustomInput label="Surname" type="text" value={value} />
  33. ```
  34. ## Rule details
  35. This rule takes one optional object argument of type object:
  36. ```json
  37. {
  38. "rules": {
  39. "jsx-a11y/control-has-associated-label": [ 2, {
  40. "labelAttributes": ["label"],
  41. "controlComponents": ["CustomComponent"],
  42. "ignoreElements": [
  43. "audio",
  44. "canvas",
  45. "embed",
  46. "input",
  47. "textarea",
  48. "tr",
  49. "video",
  50. ],
  51. "ignoreRoles": [
  52. "grid",
  53. "listbox",
  54. "menu",
  55. "menubar",
  56. "radiogroup",
  57. "row",
  58. "tablist",
  59. "toolbar",
  60. "tree",
  61. "treegrid",
  62. ],
  63. "depth": 3,
  64. }],
  65. }
  66. }
  67. ```
  68. - `labelAttributes` is a list of attributes to check on the control 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.
  69. - `controlComponents` is a list of custom React Components names that will render down to an interactive element.
  70. - `ignoreElements` is an array of elements that should not be considered control (interactive) elements and therefore they do not require a text label.
  71. - `ignoreRoles` is an array of ARIA roles that should not be considered control (interactive) roles and therefore they do not require a text label.
  72. - `depth` (default 2, max 25) is an integer that determines how deep within a `JSXElement` the rule should look for text content or an element with a label to determine if the interactive element will have an accessible label.
  73. ### Succeed
  74. ```jsx
  75. <button type="button" aria-label="Save" class="icon-save" />
  76. ```
  77. ### Fail
  78. ```jsx
  79. <button type="button" class="icon-save" />
  80. ```
  81. ## Accessibility guidelines
  82. - [WCAG 1.3.1](https://www.w3.org/WAI/WCAG21/Understanding/info-and-relationships)
  83. - [WCAG 3.3.2](https://www.w3.org/WAI/WCAG21/Understanding/labels-or-instructions)
  84. - [WCAG 4.1.2](https://www.w3.org/WAI/WCAG21/Understanding/name-role-value)