Skip to content

Commit

Permalink
fix(Switch): removed labelOff prop for a11y (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatblindgeye authored Jul 10, 2024
1 parent eff02ee commit cd27b84
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### switch-remove-labelOff [(#10646)](https://github.com/patternfly/patternfly-react/pull/10646)

The `labelOff` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.

#### Examples

In:

```jsx
%inputExample%
```

Out:

```jsx
%outputExample%
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const ruleTester = require("../../ruletester");
import * as rule from "./switch-remove-labelOff";

ruleTester.run("switch-remove-labelOff", rule, {
valid: [
{
code: `<Switch labelOff />`,
},
{
code: `import { Switch } from '@patternfly/react-core'; <Switch someOtherProp />`,
},
],
invalid: [
{
code: `import { Switch } from '@patternfly/react-core'; <Switch labelOff="Some label" />`,
output: `import { Switch } from '@patternfly/react-core'; <Switch />`,
errors: [
{
message: `The \`labelOff\` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { Switch } from '@patternfly/react-core/dist/esm/components/Switch/index.js'; <Switch labelOff="Some label" />`,
output: `import { Switch } from '@patternfly/react-core/dist/esm/components/Switch/index.js'; <Switch />`,
errors: [
{
message: `The \`labelOff\` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { Switch } from '@patternfly/react-core/dist/js/components/Switch/index.js'; <Switch labelOff="Some label" />`,
output: `import { Switch } from '@patternfly/react-core/dist/js/components/Switch/index.js'; <Switch />`,
errors: [
{
message: `The \`labelOff\` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { Switch } from '@patternfly/react-core/dist/dynamic/components/Switch/index.js'; <Switch labelOff="Some label" />`,
output: `import { Switch } from '@patternfly/react-core/dist/dynamic/components/Switch/index.js'; <Switch />`,
errors: [
{
message: `The \`labelOff\` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.`,
type: "JSXOpeningElement",
},
],
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";
import { getFromPackage, getAttribute } from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10646
module.exports = {
meta: { fixable: "code" },
create: function (context: Rule.RuleContext) {
const { imports } = getFromPackage(context, "@patternfly/react-core");

const switchImport = imports.find(
(specifier) => specifier.imported.name === "Switch"
);

return !switchImport
? {}
: {
JSXOpeningElement(node: JSXOpeningElement) {
if (
node.name.type === "JSXIdentifier" &&
switchImport.local.name === node.name.name
) {
const attribute = getAttribute(node, "labelOff");
if (attribute) {
context.report({
node,
message:
"The `labelOff` prop has been removed from Switch. The label for a Switch should not dynamically update based on the on/off state.",
fix(fixer) {
return fixer.replaceText(attribute, "");
},
});
}
}
},
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Switch } from "@patternfly/react-core";

export const SwitchRemoveLabelOffInput = () => <Switch labelOff='Some label' />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Switch } from "@patternfly/react-core";

export const SwitchRemoveLabelOffInput = () => <Switch />

0 comments on commit cd27b84

Please sign in to comment.