Skip to content

Commit 9155f65

Browse files
committed
✨ Allow custom form controls in form-control-has-label
Closes vue-a11y#373
1 parent e6c1f23 commit 9155f65

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ makeRuleTester("form-control-has-label", rule, {
2121
<div aria-hidden="true">
2222
<input value="1" type="text" />
2323
</div>
24-
`
24+
`,
25+
"<b-form-input />",
26+
],
27+
invalid: [
28+
"<input type='text' />",
29+
"<textarea type='text'></textarea>",
30+
{
31+
code: "<div><b-form-input /></div>",
32+
options: [{ controlComponents: ["b-form-input"] }],
33+
errors: [{ messageId: "default" }],
34+
},
2535
],
26-
invalid: ["<input type='text' />", "<textarea type='text'></textarea>"]
2736
});

src/rules/form-control-has-label.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,31 @@ const rule: Rule.RuleModule = {
3737
messages: {
3838
default:
3939
"Each form element must have a programmatically associated label element."
40-
}
40+
},
41+
schema: [{
42+
type: "object",
43+
properties: {
44+
controlComponents: {
45+
type: "array",
46+
items: {
47+
type: "string",
48+
},
49+
uniqueItems: true,
50+
},
51+
},
52+
}],
4153
},
4254
create(context) {
55+
const {
56+
controlComponents: customControlComponents = [],
57+
} = context.options[0] || {};
58+
59+
const controlComponents = ["input", "textarea", ...customControlComponents];
60+
4361
return defineTemplateBodyVisitor(context, {
4462
VElement(node) {
4563
const elementType = getElementType(node);
46-
if (!["input", "textarea"].includes(elementType)) {
64+
if (!controlComponents.includes(elementType)) {
4765
return;
4866
}
4967

0 commit comments

Comments
 (0)