geido commented on a change in pull request #14318:
URL: https://github.com/apache/superset/pull/14318#discussion_r621497585
##########
File path: superset-frontend/src/components/Icons/Icon.tsx
##########
@@ -17,34 +17,52 @@
* under the License.
*/
-import React from 'react';
+import React, { useEffect, useRef, useState } from 'react';
import AntdIcon from '@ant-design/icons';
import { styled } from '@superset-ui/core';
-import { CustomIconComponentProps } from
'@ant-design/icons/lib/components/Icon';
+import { ReactComponent as TransparentIcon } from
'images/icons/transparent.svg';
import IconType from './IconType';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
-const EnhancedIcon = ({ iconColor, iconSize, ...rest }: IconType) => (
+const AntdIconComponent = ({ iconColor, iconSize, ...rest }: IconType) => (
<AntdIcon viewBox={rest.viewBox || '0 0 24 24'} {...rest} />
);
-const Icon = styled(EnhancedIcon)<IconType>`
+const StyledIcon = styled(AntdIconComponent)<IconType>`
${({ iconColor }) => iconColor && `color: ${iconColor};`};
font-size: ${({ iconSize, theme }) =>
iconSize ? `${theme.typography.sizes[iconSize]}px` : '24px'};
`;
-export const renderIcon = (
- SVGComponent:
- | React.ComponentClass<
- CustomIconComponentProps | React.SVGProps<SVGSVGElement>,
- any
- >
- | React.FunctionComponent<
- CustomIconComponentProps | React.SVGProps<SVGSVGElement>
- >
- | undefined,
- props: IconType,
-) => <Icon component={SVGComponent} {...props} />;
-
-export default Icon;
+interface IconProps extends IconType {
+ path: string;
+}
+
+export const Icon = (props: IconProps) => {
+ const { path, ...iconProps } = props;
+ const [, setLoaded] = useState(false);
+ const ImportedSVG = useRef<React.FC<React.SVGProps<SVGSVGElement>>>();
+ const name = path.replace('_', '-');
+
+ useEffect(() => {
+ async function importIcon(): Promise<void> {
+ ImportedSVG.current = (
+ await import(
+ `!!@svgr/webpack?-svgo,+titleProp,+ref!images/icons/${path}.svg`
+ )
+ ).default;
+ setLoaded(true);
+ }
+ importIcon();
+ }, [path, ImportedSVG]);
+
+ return (
+ <StyledIcon
+ component={ImportedSVG.current || TransparentIcon}
Review comment:
Probably also with your approach that would work, but Antd will fire
warnings for the type being invalid
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]