-
Notifications
You must be signed in to change notification settings - Fork 19
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
feat(Button): Extend icon moving to support Icon and react-icon children #735
Conversation
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.
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) |
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.
? 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", | ||
}, | ||
], | ||
}, |
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.
Can we also add a test for the children attribute mentioned above?
}, | |
}, | |
{ | |
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"); |
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.
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"; |
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.
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; |
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.
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.
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.
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"; |
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.
import { Button } from "@patternfly/react-core"; | |
import { Button, Icon } from "@patternfly/react-core"; |
@@ -1,5 +1,12 @@ | |||
import { Button } from "@patternfly/react-core"; |
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.
import { Button } from "@patternfly/react-core"; | |
import { Button, Icon } from "@patternfly/react-core"; |
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.
Ah yep, great catches!
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.
🎉
Closes #716