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
  • Loading branch information
vhoyer committed Jan 25, 2022
1 parent e6c1f23 commit 9155f65
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>",
{
code: "<div><b-form-input /></div>",
options: [{ controlComponents: ["b-form-input"] }],
errors: [{ messageId: "default" }],
},
],
invalid: ["<input type='text' />", "<textarea type='text'></textarea>"]
});
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 @@ -37,13 +37,31 @@ const rule: Rule.RuleModule = {
messages: {
default:
"Each form element must have a programmatically associated label element."
}
},
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 9155f65

Please sign in to comment.