Skip to content
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

feat(Button): Extend icon moving to support Icon and react-icon children #735

Conversation

wise-king-sullyman
Copy link
Collaborator

Closes #716

Copy link
Contributor

@adamviktora adamviktora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of comments bellow, also there is a problem that this rule removes all children of the Button (because it was assuming only the plain button variant before), which becomes destructive in case like this:

import { Button } from "@patternfly/react-core";
import { SomeIcon } from "@patternfly/react-icons";

<Button>
  Text
  <SomeIcon />
</Button>

where the "Text" will be removed.

if (childrenProp) {
const childrenPropExpression = getExpression(
childrenProp?.value
);
childrenValue = childrenPropExpression
? context.getSourceCode().getText(childrenPropExpression)
? source.getText(childrenPropExpression)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
? source.getText(childrenPropExpression)
? `{${source.getText(childrenPropExpression)}}`

Not related to a change in this PR, but we should wrap this in an expression bracket, otherwise this: <Button variant="plain" children={<span>Some icon</span>} /> would result to an icon argument not being wrapped: <Button icon=<span>Some icon</span> variant="plain" />

type: "JSXElement",
},
],
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a test for the children attribute mentioned above?

Suggested change
},
},
{
code: `import { Button } from '@patternfly/react-core'; <Button variant="plain" children={<span>Some icon</span>} />`,
output: `import { Button } from '@patternfly/react-core'; <Button icon={<span>Some icon</span>} variant="plain" />`,
errors: [
{
message: `Icons must now be passed to the \`icon\` prop of Button instead of as children. If you are passing anything other than an icon as children, ignore this rule when running fixes.`,
type: "JSXElement",
},
],
},

return;
}

const iconComponentChild = getChildElementByName(node, "Icon");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check an import { Icon } from react-core, otherwise this will get any element named "Icon".

@@ -1,7 +1,18 @@
import { Button } from "@patternfly/react-core";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the comment above, an Icon import should be included here in order for the second element to have the rule applied.

isReactIcon(context, child)
);

const iconChild = iconComponentChild || reactIconChild;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should also handle this icon child in case of children as an attribute. Because currently this is valid:

import { Button } from '@patternfly/react-core'; 
import { SomeIcon } from "@patternfly/react-icons";
<Button children={<SomeIcon />} />

and the rule won't be applied.

I think it is a pretty rare use case and it would complicate the rule, so I wouldn't bother.

Copy link
Contributor

@adamviktora adamviktora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM mostly, there is a forgotten Icon import in the input and output example files.

Also, can you rebase and rename getChildElementByName to getChildJSXElementByName? (Eric's Card PR got merged including renaming this helper)

@@ -1,7 +1,18 @@
import { Button } from "@patternfly/react-core";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { Button } from "@patternfly/react-core";
import { Button, Icon } from "@patternfly/react-core";

@@ -1,5 +1,12 @@
import { Button } from "@patternfly/react-core";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { Button } from "@patternfly/react-core";
import { Button, Icon } from "@patternfly/react-core";

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep, great catches!

Copy link
Contributor

@adamviktora adamviktora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@adamviktora adamviktora merged commit d5633b0 into patternfly:main Aug 26, 2024
3 checks passed
@wise-king-sullyman wise-king-sullyman deleted the button-icon-extend-applicability branch August 26, 2024 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Button - Move icon children to the icon prop
3 participants