Skip to content

Commit

Permalink
✨ Allow custom form controls in form-control-has-label
Browse files Browse the repository at this point in the history
Closes #373
  • Loading branch information
vhoyer committed Jan 25, 2022
1 parent 83d8f05 commit 7c2ac4b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/rules/__tests__/form-control-has-label.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ makeRuleTester("form-control-has-label", rule, {
<div aria-hidden="true">
<input value="1" type="text" />
</div>
`
`,
"<b-form-input />"
],
invalid: ["<input type='text' />", "<textarea type='text'></textarea>"]
invalid: [
"<input type='text' />",
"<textarea type='text'></textarea>",
{
code: "<div><b-form-input /></div>",
options: [{ controlComponents: ["b-form-input"] }],
errors: [{ messageId: "default" }]
}
]
});
22 changes: 20 additions & 2 deletions src/rules/form-control-has-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,31 @@ const rule: Rule.RuleModule = {
default:
"Each form element must have a programmatically associated label element."
},
schema: []
schema: [
{
type: "object",
properties: {
controlComponents: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
}
}
}
]
},
create(context) {
const { controlComponents: customControlComponents = [] } =
context.options[0] || {};

const controlComponents = ["input", "textarea", ...customControlComponents];

return defineTemplateBodyVisitor(context, {
VElement(node) {
const elementType = getElementType(node);
if (!["input", "textarea"].includes(elementType)) {
if (!controlComponents.includes(elementType)) {
return;
}

Expand Down

0 comments on commit 7c2ac4b

Please sign in to comment.