-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nikitasingla
authored and
nikitasingla
committed
Mar 13, 2024
1 parent
113e5dd
commit a2ad534
Showing
6 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,4 @@ We currently cover the following components: | |
- [x] TextArea | ||
- [x] Toolbar | ||
- [] Tooltip | ||
- [x] Breadcrumb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# All interactive elements must have an accessible name (`@microsoft/fluentui-jsx-a11y/breadcrumb-needs-labelling-v9`) | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Provide labels to identify all form controls, including text fields, checkboxes, radio buttons, and drop-down menus. In most cases, this is done by using the label element. | ||
|
||
<https://www.w3.org/WAI/tutorials/forms/labels/> | ||
|
||
All interactive elements must have an accessible name. | ||
|
||
## Rule Details | ||
|
||
This rule aims to... | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<div> | ||
<label>Breadcrumb default example<label> | ||
<Breadcrumb ></BreadCrumb> | ||
</div> | ||
<Breadcrumb></Breadcrumb> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
<Breadcrumb aria-label="Breadcrumb default example"> | ||
<div> | ||
<label id="my-label">Breadcrumb default example<label> | ||
<Breadcrumb aria-labelledby="my-label"><BreadCrumb> | ||
</div> | ||
<label>Breadcrumb default example<Breadcrumb></Breadcrumb></label> | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
"use strict"; | ||
|
||
const { hasNonEmptyProp } = require("../util/hasNonEmptyProp"); | ||
var elementType = require("jsx-ast-utils").elementType; | ||
const { hasAssociatedLabelViaAriaLabelledBy, isInsideLabelTag } = require("../util/labelUtils"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
// possible error messages for the rule | ||
messages: { | ||
noUnlabelledBreadcrumb: "Accessibility: Breadcrumb must have an accessible label" | ||
}, | ||
// "problem" means the rule is identifying code that either will cause an error or may cause a confusing behavior: https://eslint.org/docs/latest/developer-guide/working-with-rules | ||
type: "problem", | ||
docs: { | ||
description: "All interactive elements must have an accessible name", | ||
recommended: false, | ||
url: "https://www.w3.org/TR/html-aria/" // URL to the documentation page for this rule | ||
}, | ||
fixable: null, // Or `code` or `whitespace` | ||
schema: [] // Add a schema if the rule has options | ||
}, | ||
|
||
create(context) { | ||
return { | ||
// visitor functions for different types of nodes | ||
JSXOpeningElement(node) { | ||
// if it is not a Breadcrumb, return | ||
if (elementType(node) !== "Breadcrumb") { | ||
return; | ||
} | ||
|
||
// if the Breadcrumb has a label, if the Breadcrumb has an associated label, return | ||
if ( | ||
hasNonEmptyProp(node.attributes, "aria-label") || //aria-label | ||
isInsideLabelTag(context) || // wrapped in label | ||
hasAssociatedLabelViaAriaLabelledBy(node, context) // aria-labelledby | ||
) { | ||
return; | ||
} | ||
|
||
// if it has no visual labelling, report error | ||
context.report({ | ||
node, | ||
messageId: `noUnlabelledBreadcrumb` | ||
}); | ||
} | ||
}; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/breadcrumb-needs-labelling-v9"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
} | ||
}); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
ruleTester.run("breadcrumb-needs-labelling-v9", rule, { | ||
valid: [ | ||
// give me some code that won't trigger a warning | ||
'<Breadcrumb aria-label="Breadcrumb default example"></Breadcrumb>', | ||
"<label>Breadcrumb default example<Breadcrumb></Breadcrumb></label>", | ||
'<div><label id="my-label">Breadcrumb default example</label><Breadcrumb aria-labelledby="my-label"></Breadcrumb></div>', | ||
'<div><Label id="my-label">Breadcrumb default example</Label><Breadcrumb aria-labelledby="my-label"></Breadcrumb></div>' | ||
], | ||
invalid: [ | ||
{ | ||
code: '<div><Label id="my-label">Breadcrumb default example</Label><Breadcrumb></Breadcrumb></div>', | ||
errors: [{ messageId: "noUnlabelledBreadcrumb" }] | ||
}, | ||
{ | ||
code: "<Breadcrumb></Breadcrumb>", | ||
errors: [{ messageId: "noUnlabelledBreadcrumb" }] | ||
} | ||
] | ||
}); | ||
|