Skip to content

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

Merged
adamviktora merged 5 commits intopatternfly:mainfrom
wise-king-sullyman:button-icon-extend-applicability
Aug 26, 2024
Merged

feat(Button): Extend icon moving to support Icon and react-icon children#735
adamviktora merged 5 commits intopatternfly:mainfrom
wise-king-sullyman:button-icon-extend-applicability

Conversation

@wise-king-sullyman
Copy link
Copy Markdown
Collaborator

Closes #716

Copy link
Copy Markdown
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.

);
childrenValue = childrenPropExpression
? context.getSourceCode().getText(childrenPropExpression)
? source.getText(childrenPropExpression)
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@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