diff --git a/README.md b/README.md
index 02f1ed5c0..455b8542a 100644
--- a/README.md
+++ b/README.md
@@ -103,6 +103,9 @@ Add `plugin:jsx-a11y/recommended` or `plugin:jsx-a11y/strict` in `extends`:
"CustomButton": "button",
"MyButton": "button",
"RoundButton": "button"
+ },
+ "attributes": {
+ "for": ["htmlFor", "for"]
}
}
}
@@ -113,6 +116,10 @@ Add `plugin:jsx-a11y/recommended` or `plugin:jsx-a11y/strict` in `extends`:
To enable your custom components to be checked as DOM elements, you can set global settings in your configuration file by mapping each custom component name to a DOM element type.
+#### Attribute Mapping
+
+To configure the JSX property to use for attribute checking, you can set global settings in your configuration file by mapping each DOM attribute to the JSX property you want to check. For example, you may want to allow the `for` attribute in addition to the `htmlFor` attribute for checking label associations.
+
#### Polymorphic Components
You can optionally use the `polymorphicPropName` setting to define the prop your code uses to create polymorphic components.
diff --git a/__tests__/src/rules/label-has-associated-control-test.js b/__tests__/src/rules/label-has-associated-control-test.js
index d0b4c3b31..9e8dc7de5 100644
--- a/__tests__/src/rules/label-has-associated-control-test.js
+++ b/__tests__/src/rules/label-has-associated-control-test.js
@@ -40,6 +40,10 @@ const htmlForValid = [
{ code: '' },
{ code: '' },
{ code: '
' },
+ { code: '', options: [{ depth: 4, htmlForAttributes: ['htmlFor', 'for'] }] },
+ { code: '', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
+ { code: '', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
+ { code: '', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
// Custom label component.
{ code: '', options: [{ labelComponents: ['CustomLabel'] }] },
{ code: '', options: [{ labelAttributes: ['label'], labelComponents: ['CustomLabel'] }] },
diff --git a/__tests__/src/rules/label-has-for-test.js b/__tests__/src/rules/label-has-for-test.js
index 9b4bd1478..85dfbc90d 100644
--- a/__tests__/src/rules/label-has-for-test.js
+++ b/__tests__/src/rules/label-has-for-test.js
@@ -56,10 +56,14 @@ ruleTester.run('label-has-for', rule, {
{ code: '' },
{ code: '' },
{ code: '' },
+ { code: '', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
+ { code: '', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
{ code: '' }, // lower-case convention refers to real HTML elements.
{ code: '' },
+ { code: '', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
{ code: '' },
{ code: 'Test!' },
+ { code: 'Test!', options: [{ htmlForAttributes: ['htmlFor', 'for'] }] },
{ code: 'test' },
// CUSTOM ELEMENT ARRAY OPTION TESTS
diff --git a/docs/rules/label-has-associated-control.md b/docs/rules/label-has-associated-control.md
index 5a3efa48d..2804c3f32 100644
--- a/docs/rules/label-has-associated-control.md
+++ b/docs/rules/label-has-associated-control.md
@@ -53,6 +53,7 @@ And the configuration:
{
"rules": {
"jsx-a11y/label-has-associated-control": [ 2, {
+ "htmlForAttributes": ["htmlFor", "for"],
"labelComponents": ["CustomInputLabel"],
"labelAttributes": ["label"],
"controlComponents": ["CustomInput"],
@@ -103,6 +104,7 @@ This rule takes one optional object argument of type object:
}
```
+`htmlForAttributes`: is an array of strings that specify the attribute to check for an associated control. Default is `["htmlFor"]`.
`labelComponents` is a list of custom React Component names that should be checked for an associated control.
`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.
`controlComponents` is a list of custom React Components names that will output an input element. [Glob format](https://linuxhint.com/bash_globbing_tutorial/) is also supported for specifying names (e.g., `Label*` matches `LabelComponent` but not `CustomLabel`, `????Label` matches `LinkLabel` but not `CustomLabel`).
diff --git a/docs/rules/label-has-for.md b/docs/rules/label-has-for.md
index 71730c998..2028cef84 100644
--- a/docs/rules/label-has-for.md
+++ b/docs/rules/label-has-for.md
@@ -13,7 +13,7 @@ Enforce label tags have associated control.
There are two supported ways to associate a label with a control:
- nesting: by wrapping a control in a label tag
-- id: by using the prop `htmlFor` as in `htmlFor=[ID of control]`
+- id: by using the prop `htmlFor` (or any configured attribute) as in `htmlFor=[ID of control]`
To fully cover 100% of assistive devices, you're encouraged to validate for both nesting and id.
@@ -25,6 +25,7 @@ This rule takes one optional object argument of type object:
{
"rules": {
"jsx-a11y/label-has-for": [ 2, {
+ "htmlForAttributes": ["htmlFor", "for"],
"components": [ "Label" ],
"required": {
"every": [ "nesting", "id" ]
@@ -35,6 +36,8 @@ This rule takes one optional object argument of type object:
}
```
+The `htmlForAttributes` allows you to specify which prop to check for. This is useful when you want to use a different property instead of `htmlFor`, for example `for`.
+
For the `components` option, these strings determine which JSX elements (**always including** `