-
-
Notifications
You must be signed in to change notification settings - Fork 636
[New] no-disabled rule #830
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
c95e07b
aa41427
1d89958
b2cfb4b
b95ebd2
167c235
a556263
23d6ef9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* eslint-env jest */ | ||
/** | ||
* @fileoverview Enforce disabled prop is not used. | ||
* @author Courtney Nguyen <@courtyenn> | ||
*/ | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
import { RuleTester } from 'eslint'; | ||
import rule from '../../../src/rules/no-disabled'; | ||
import parserOptionsMapper from '../../__util__/parserOptionsMapper'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
const expectedWarning = { | ||
message: 'The disabled prop removes the element from being detected by screen readers.', | ||
type: 'JSXAttribute', | ||
}; | ||
|
||
ruleTester.run('no-disabled', rule, { | ||
valid: [ | ||
{ code: '<div />' }, | ||
{ code: '<div disabled />' }, | ||
{ code: '<input />' }, | ||
{ code: '<button />' }, | ||
{ code: '<select />' }, | ||
{ code: '<textarea />' }, | ||
{ code: '<option />' }, | ||
{ code: '<command />' }, | ||
{ code: '<fieldset />' }, | ||
{ code: '<keygen />' }, | ||
{ code: '<optgroup />' }, | ||
].map(parserOptionsMapper), | ||
invalid: [ | ||
{ code: '<input disabled />', errors: [expectedWarning] }, | ||
{ code: '<input disabled="true" />', errors: [expectedWarning] }, | ||
{ code: '<input disabled="false" />', errors: [expectedWarning] }, | ||
{ code: '<button disabled />', errors: [expectedWarning] }, | ||
{ code: '<select disabled />', errors: [expectedWarning] }, | ||
{ code: '<textarea disabled />', errors: [expectedWarning] }, | ||
{ code: '<option disabled />', errors: [expectedWarning] }, | ||
{ code: '<command disabled />', errors: [expectedWarning] }, | ||
{ code: '<fieldset disabled />', errors: [expectedWarning] }, | ||
{ code: '<keygen disabled />', errors: [expectedWarning] }, | ||
{ code: '<optgroup disabled />', errors: [expectedWarning] }, | ||
].map(parserOptionsMapper), | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# no-disabled | ||
|
||
Rule that `disabled` prop should be cautioned on elements. Disabling interactive elements removes the element from the accessibility tree. Consider using `aria-disabled`. | ||
|
||
## Rule details | ||
|
||
Warns usage of `disabled` property. | ||
|
||
### Succeed | ||
```jsx | ||
<div /> | ||
<div disabled /> | ||
<input /> | ||
<select /> | ||
<textarea /> | ||
<button /> | ||
``` | ||
|
||
### Fail | ||
```jsx | ||
<input disabled /> | ||
<input disabled="true" /> | ||
<input disabled="false" /> | ||
<input disabled={undefined} /> | ||
``` | ||
|
||
## Accessibility guidelines | ||
General best practice (reference resources) | ||
|
||
### Resources | ||
- [MDN aria-disabled](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-disabled) | ||
- [W3 KBD Disabled Controls](https://www.w3.org/TR/wai-aria-practices/#kbd_disabled_controls) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @fileoverview Rule to flag 'disabled' prop | ||
* @author Courtney Nguyen <@courtyenn> | ||
*/ | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ---------------------------------------------------------------------------- | ||
|
||
import { propName, elementType } from 'jsx-ast-utils'; | ||
|
||
const warningMessage = 'The disabled prop removes the element from being detected by screen readers.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "The disabled attribute makes the element imperceivable to assistive technology agents, like screen readers." Just a small nit that an Also, we should avoid being screen-reader-centric. There are many assistive technology agents. Screen readers are common and probably what a developer is familiar with, so it helps to mention them. We just want to be sure we leave room for the full spectrum of agents that we need to support. |
||
|
||
const DEFAULT_ELEMENTS = [ | ||
jessebeach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'button', | ||
'command', | ||
'fieldset', | ||
'keygen', | ||
'optgroup', | ||
'option', | ||
'select', | ||
'textarea', | ||
'input', | ||
]; | ||
|
||
export default { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
recommended: false, | ||
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-disabled.md', | ||
}, | ||
schema: [], | ||
}, | ||
|
||
create: (context) => ({ | ||
JSXAttribute: (attribute) => { | ||
// Only monitor eligible elements for "disabled". | ||
const type = elementType(attribute.parent); | ||
if (!DEFAULT_ELEMENTS.includes(type)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recall that we have support issues with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ljharb do you have an issue with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a much larger issue with loops :-) we support down to eslint 3, which requires node 4+, which lacks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. I will endeavor to commit this detail to memory for the next time =D |
||
return; | ||
} | ||
|
||
if (propName(attribute) === 'disabled') { | ||
context.report({ | ||
node: attribute, | ||
message: warningMessage, | ||
}); | ||
} | ||
}, | ||
}), | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.