-
Notifications
You must be signed in to change notification settings - Fork 15
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
Added a rule for Tablist component #117
Open
irina-vas
wants to merge
4
commits into
main
Choose a base branch
from
users/t-ivasylenko/tablist
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+326
−123
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,16 @@ | ||
export namespace meta { | ||
namespace messages { | ||
let missingTabLabel: string; | ||
let missingTablistLabel: string; | ||
} | ||
let type: string; | ||
namespace docs { | ||
let description: string; | ||
let recommended: boolean; | ||
let url: string; | ||
} | ||
let schema: never[]; | ||
} | ||
export function create(context: any): { | ||
JSXOpeningElement(node: any): void; | ||
}; |
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,76 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'This rule aims to ensure that Tabs with icons but no text labels have an accessible name and that Tablist is properly labeled.', | ||
recommended: true, | ||
url: 'https://www.w3.org/WAI/ARIA/apg/patterns/tabs/', // URL to the documentation page for this rule | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
missingTabLabel: 'Accessibility: Tab elements must have an aria-label attribute is there is no visiable text content', | ||
missingTablistLabel: 'Accessibility: Tablist must have an accessible label' | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const { hasTextContentChild } = require('../util/hasTextContentChild'); | ||
const { hasNonEmptyProp } = require('../util/hasNonEmptyProp'); | ||
const { hasAssociatedLabelViaAriaLabelledBy } = require('../util/labelUtils'); | ||
|
||
var elementType = require("jsx-ast-utils").elementType; | ||
|
||
return { | ||
|
||
// visitor functions for different types of nodes | ||
JSXOpeningElement(node) { | ||
const elementTypeValue = elementType(node); | ||
|
||
// if it is not a Tablist or Tab, return | ||
if (elementTypeValue !== 'Tablist' && elementTypeValue !== 'Tab') { | ||
return; | ||
} | ||
|
||
// Check for Tablist elements | ||
if (elementTypeValue === "Tablist") { | ||
if ( | ||
// if the Tablist has a label, if the Tablist has an associated label, return | ||
hasNonEmptyProp(node.attributes, 'aria-label') || //aria-label | ||
hasAssociatedLabelViaAriaLabelledBy(node, context) // aria-labelledby | ||
) { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
messageId: 'missingTablistLabel' | ||
}); | ||
} | ||
|
||
// Check for Tab elements | ||
if (elementTypeValue === 'Tab') { | ||
if ( | ||
hasTextContentChild(node.parent) || // text content | ||
hasNonEmptyProp(node.attributes, 'aria-label') // aria-label | ||
) { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
messageId: 'missingTabLabel' | ||
}); | ||
} | ||
} | ||
}; | ||
}, | ||
}; |
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,63 @@ | ||
# This rule aims to ensure that Tabs with icons but no text labels have an accessible name and that Tablist is properly labeled (`@microsoft/fluentui-jsx-a11y/tablist-and-tabs-need-labelling`) | ||
|
||
💼 This rule is enabled in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
Accessibility: | ||
1. If the Tab is represented by an icon, it must have an 'aria-label' to describe the Tab. | ||
2. If the Tablist has a visible label, it should use aria-labelledby to reference that label. If there is no visible label, the Tablist should have a label provided by aria-label. | ||
|
||
<https://www.w3.org/WAI/ARIA/apg/patterns/tabs/> | ||
|
||
## Ways to fix | ||
1. Add an 'aria-label' to the Tab when it is represented by icon | ||
2. Add an 'aria-labelledby' to the Tablist to reference a visible label | ||
3. If there is no visible label, add an 'aria-label' to the Tablist | ||
|
||
## Rule Details | ||
|
||
This rule aims to ensure that Tabs with icons but no text labels have an accessible name and that Tablist is properly labeled. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<Tab icon={<SettingsIcon />} /> | ||
|
||
<Tab icon={<SettingsIcon />}></Tab> | ||
|
||
<Tablist> | ||
<Tab>Settings Tab</Tab> | ||
</Tablist> | ||
|
||
<Tablist> | ||
<Label id="tablist-settings">Settings Label</Label> | ||
<Tab>Settings Tab</Tab> | ||
</Tablist> | ||
|
||
<Tablist> | ||
<Label>Settings Label</Label> | ||
<Tab>Settings Tab</Tab> | ||
</Tablist> | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
|
||
<Tab icon={<SettingsIcon />} aria-label="Settings" /> | ||
|
||
<Tab icon={<SettingsIcon />}>Settings</Tab> | ||
|
||
<Tab>Settings</Tab> | ||
|
||
<Tablist aria-label="settings"> | ||
<Tab>Settings Tab</Tab> | ||
</Tablist> | ||
|
||
<Tablist aria-labelledby="tablist-settings"> | ||
<Label id="tablist-settings">Settings Label</Label> | ||
<Tab>Settings Tab</Tab> | ||
</Tablist> | ||
|
||
``` |
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,76 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'This rule aims to ensure that Tabs with icons but no text labels have an accessible name and that Tablist is properly labeled.', | ||
recommended: true, | ||
url: 'https://www.w3.org/WAI/ARIA/apg/patterns/tabs/', // URL to the documentation page for this rule | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
missingTabLabel: 'Accessibility: Tab elements must have an aria-label attribute is there is no visiable text content', | ||
missingTablistLabel: 'Accessibility: Tablist must have an accessible label' | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const { hasTextContentChild } = require('../util/hasTextContentChild'); | ||
const { hasNonEmptyProp } = require('../util/hasNonEmptyProp'); | ||
const { hasAssociatedLabelViaAriaLabelledBy } = require('../util/labelUtils'); | ||
|
||
var elementType = require("jsx-ast-utils").elementType; | ||
|
||
return { | ||
|
||
// visitor functions for different types of nodes | ||
JSXOpeningElement(node) { | ||
const elementTypeValue = elementType(node); | ||
|
||
// if it is not a Tablist or Tab, return | ||
if (elementTypeValue !== 'Tablist' && elementTypeValue !== 'Tab') { | ||
return; | ||
} | ||
|
||
// Check for Tablist elements | ||
if (elementTypeValue === "Tablist") { | ||
if ( | ||
// if the Tablist has a label, if the Tablist has an associated label, return | ||
hasNonEmptyProp(node.attributes, 'aria-label') || //aria-label | ||
hasAssociatedLabelViaAriaLabelledBy(node, context) // aria-labelledby | ||
) { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
messageId: 'missingTablistLabel' | ||
}); | ||
} | ||
|
||
// Check for Tab elements | ||
if (elementTypeValue === 'Tab') { | ||
if ( | ||
hasTextContentChild(node.parent) || // text content | ||
hasNonEmptyProp(node.attributes, 'aria-label') // aria-label | ||
) { | ||
return; | ||
} | ||
context.report({ | ||
node, | ||
messageId: 'missingTabLabel' | ||
}); | ||
} | ||
} | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one change: could you please update the coverage.md file too?