Skip to content

Commit fce67b1

Browse files
committed
Custom form controls in form-control-has-label
1 parent 1b191df commit fce67b1

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

Diff for: docs/form-control-has-label.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ This rule takes one optional object argument of type object:
1616
"vuejs-accessibility/form-control-has-label": [
1717
"error",
1818
{
19-
"labelComponents": ["CustomLabel"]
19+
"labelComponents": ["CustomLabel"],
20+
"controlComponents": ["CustomInput"]
2021
}
2122
]
2223
}
@@ -25,6 +26,8 @@ This rule takes one optional object argument of type object:
2526

2627
For the `labelComponents` option, these strings determine which elements (**always including** `<label>`) should be checked for having the `for` prop. This is a good use case when you have a wrapper component that simply renders a `label` element.
2728

29+
For the `controlComponents` option, these strings determine which elements (**always including** `<input>`, `<textarea>` and `<select>`) should be checked for having an associated label. This is a good use case when you have a wrapper component that simply renders an input element.
30+
2831
### Succeed
2932

3033
```

Diff for: src/rules/__tests__/form-control-has-label.test.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ makeRuleTester("form-control-has-label", rule, {
2525
{
2626
code: "<custom-label for='input'>text</custom-label><input type='text' id='input' />",
2727
options: [{ labelComponents: ["CustomLabel"] }]
28-
}
28+
},
29+
"<b-form-input />"
2930
],
3031
invalid: [
3132
"<input type='text' />",
3233
"<textarea type='text'></textarea>",
33-
"<custom-label for='input'>text</custom-label><input type='text' id='input' />"
34+
"<custom-label for='input'>text</custom-label><input type='text' id='input' />",
35+
{
36+
code: "<div><b-form-input /></div>",
37+
options: [{ controlComponents: ["b-form-input"] }],
38+
errors: [{ messageId: "default" }]
39+
}
3440
]
3541
});

Diff for: src/rules/form-control-has-label.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ const rule: Rule.RuleModule = {
6161
type: "string"
6262
},
6363
uniqueItems: true
64+
},
65+
controlComponents: {
66+
type: "array",
67+
items: {
68+
type: "string"
69+
},
70+
uniqueItems: true
6471
}
6572
}
6673
}
@@ -70,21 +77,23 @@ const rule: Rule.RuleModule = {
7077
return defineTemplateBodyVisitor(context, {
7178
VElement(node) {
7279
const options = context.options[0] || {};
73-
const elementType = getElementType(node);
80+
const controlComponents = [
81+
"input",
82+
"textarea",
83+
"select",
84+
...(options.controlComponents || [])
85+
];
7486

75-
if (!["input", "textarea", "select"].includes(elementType)) {
87+
const elementType = getElementType(node);
88+
if (!controlComponents.includes(elementType)) {
7689
return;
7790
}
7891

7992
if (elementType === "input") {
8093
const type = getElementAttributeValue(node, "type");
94+
const types = ["hidden", "button", "image", "submit", "reset"];
8195

82-
if (
83-
!type ||
84-
["hidden", "button", "image", "submit", "reset"].includes(
85-
type as any
86-
)
87-
) {
96+
if (!type || types.includes(type as any)) {
8897
return;
8998
}
9099
}

0 commit comments

Comments
 (0)