Skip to content

Add labelComponents option to form control has label #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

### Added

- `labelComponents` options to add custom label components in `form-control-has-label`

### Changed

- Deprecate the accessible-emoji rule. See https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/issues/627 for details.
Expand Down
17 changes: 17 additions & 0 deletions docs/form-control-has-label.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ _References:_

## Rule details

This rule takes one optional object argument of type object:

```json
{
"rules": {
"vuejs-accessibility/form-control-has-label": [
"error",
{
"labelComponents": ["CustomLabel"],
}
]
}
}
```

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.

### Succeed

```
Expand Down
12 changes: 10 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,15 @@ makeRuleTester("form-control-has-label", rule, {
<div aria-hidden="true">
<input value="1" type="text" />
</div>
`
`,
{
code: "<custom-label for='input'>text</custom-label><input type='text' id='input' />",
options: [{ labelComponents: ["CustomLabel"] }]
},
],
invalid: ["<input type='text' />", "<textarea type='text'></textarea>"]
invalid: [
"<input type='text' />",
"<textarea type='text'></textarea>",
"<custom-label for='input'>text</custom-label><input type='text' id='input' />"
]
});
41 changes: 32 additions & 9 deletions src/rules/form-control-has-label.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
import type { Rule } from "eslint";
import type { AST } from "vue-eslint-parser";

interface FormControlHasLabelOptions {
labelComponents: string[];
}

import {
defineTemplateBodyVisitor,
getElementAttributeValue,
getElementType,
hasAriaLabel,
isAriaHidden,
makeDocsURL
isMatchingElement,
makeDocsURL,
} from "../utils";

function isLabelElement(
node:
| AST.VElement
| AST.VDocumentFragment
| AST.VText
| AST.VExpressionContainer
| AST.VExpressionContainer,
{ labelComponents = [] }: FormControlHasLabelOptions
) {
return node.type === "VElement" && getElementType(node) === "label";
const allLabelComponents = labelComponents.concat("label");
return isMatchingElement(node, allLabelComponents);
}

function hasLabelElement(node: AST.VElement): boolean {
function hasLabelElement(node: AST.VElement, options: FormControlHasLabelOptions): boolean {
const { parent } = node;

return (
[parent, ...parent.children].some(isLabelElement) ||
(parent && parent.type === "VElement" && hasLabelElement(parent))
[parent, ...parent.children].some((node) => isLabelElement(node, options)) ||
(parent && parent.type === "VElement" && hasLabelElement(parent, options))
);
}

Expand All @@ -37,13 +44,29 @@ const rule: Rule.RuleModule = {
messages: {
default:
"Each form element must have a programmatically associated label element."
}
},
schema: [
{
type: "object",
properties: {
labelComponents: {
type: "array",
items: {
type: "string"
},
uniqueItems: true
}
}
}
]
},
create(context) {
return defineTemplateBodyVisitor(context, {
VElement(node) {
const options = context.options[0] || {};
const elementType = getElementType(node);
if (!["input", "textarea"].includes(elementType)) {

if (!["input", "textarea", "select"].includes(elementType)) {
return;
}

Expand All @@ -63,7 +86,7 @@ const rule: Rule.RuleModule = {
if (
!isAriaHidden(node) &&
!hasAriaLabel(node) &&
!hasLabelElement(node)
!hasLabelElement(node, options)
) {
context.report({ node: node as any, messageId: "default" });
}
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { default as isAriaHidden } from "./utils/isAriaHidden";
export { default as isAttribute } from "./utils/isAttribute";
export { default as isHiddenFromScreenReader } from "./utils/isHiddenFromScreenReader";
export { default as isInteractiveElement } from "./utils/isInteractiveElement";
export { default as isMatchingElement } from "./utils/isMatchingElement";
export { default as isPresentationRole } from "./utils/isPresentationRole";
export { default as makeDocsURL } from "./utils/makeDocsURL";
export { default as makeKebabCase } from "./utils/makeKebabCase";
Expand Down
23 changes: 23 additions & 0 deletions src/utils/isMatchingElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { AST } from "vue-eslint-parser";

import getElementType from "./getElementType";
import makeKebabCase from "./makeKebabCase";

function isMatchingElement(
node:
| AST.VElement
| AST.VDocumentFragment
| AST.VText
| AST.VExpressionContainer,
searchArray: string[]
) {
if (!(node.type === "VElement")) return false;

const elementType = getElementType(node);

return searchArray.some((item: string) => {
return makeKebabCase(item) === elementType;
});
}

export default isMatchingElement;