-
Notifications
You must be signed in to change notification settings - Fork 99
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
fix(Icon): correctly parse function with default props #1713
Conversation
Preview is ready. |
Visual Tests Report is ready. |
src/components/Icon/Icon.tsx
Outdated
@@ -71,7 +71,7 @@ export const Icon: React.ForwardRefExoticComponent<IconProps & React.RefAttribut | |||
} else if (isComponentSvgData(data)) { | |||
({viewBox} = data.defaultProps); | |||
} else if (isSvgrData(data)) { | |||
const el = data({}) as React.ReactElement; | |||
const el = React.createElement(data, {}); |
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.
As you can see in line 125, we don't want the default props to be applied. It was intentional
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.
Yes, I understand.
But on line 61, you would like to get the width and height from the viewBox in case they were not specified.
And the viewBox is passed to defaultProps in svgr transformed icons.
sorry, if I miss something
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.
The problem is data({})
and React.createElement(data, {})
produce different results. Return type of React.createElement(data, {})
is not svg element and it hasn't the viewBox
prop.
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.
Also, I can try a alternative solution- extends available types for component svg data
export function isComponentSvgData(data: SVGIconData): data is SVGIconComponentData {
return (typeof data === 'object' || typeof data === 'function') && 'defaultProps' in data;
}
@arteria32 I tried to get same result as yours when transforming via SVGR in the playground but I couldn't. How do you get function component + defaultProps code? |
Example:
2 step: We build project with gravity-ui/app-builder
3 step: Get a bad component in build package in build/esm/components/.../IconWithoutSize/IconWithoutSize.js
|
d056ff9
to
55006e2
Compare
Now, we can catch a bag when the size or width and height are not specified directly and transform the input data for the icon by svgr.
**Example of transformed by svgr data svg from gravity-ui/icons/ **
Creating a component as a usual function skips the default props of the component.
Let's change the method of creating the SVGR component to getting props.
I hereby agree to the terms of the CLA available at: CLA.