Skip to content

Commit

Permalink
Update props with flow-types
Browse files Browse the repository at this point in the history
  • Loading branch information
theo-cerutti committed Apr 9, 2021
1 parent c090662 commit 5483142
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/lib/components/icon/Icon.component.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React, { Suspense } from "react";
// @flow
import React, { Suspense } from 'react';
import styled, { css } from 'styled-components';
import { brand } from '../../style/theme';
import { getTheme } from '../../utils';
import Loader from '../loader/Loader.component';
import type { SizeProp } from '@fortawesome/react-fontawesome';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

export const iconTable = {
Expand Down Expand Up @@ -89,23 +92,17 @@ const IconStyled = styled(FontAwesomeIcon)`
`;

type Props = {
name: string,
size?: string,
color?: string,
name: $Keys<typeof iconTable>,
size?: SizeProp,
color?: $Keys<typeof brand>,
};

function getLazyStyledIcon(iconInfo) {
const [iconType, iconClass] = iconInfo.split(' ');
return React.lazy(async () => {
try {
let icon = null;
if (iconType === "fas") {
icon = await import(`@fortawesome/free-solid-svg-icons/${iconClass}.js`);
} else if (iconType === "far") {
icon = await import(`@fortawesome/free-regular-svg-icons/${iconClass}.js`);
} else {
icon = await import(`@fortawesome/free-solid-svg-icons/${iconClass}.js`);
}
const fontAwesomeType = iconType === "far" ? "free-regular-svg-icons" : "free-solid-svg-icons";
const icon = await import(`@fortawesome/${fontAwesomeType}/${iconClass}.js`);
return { default: ({ color, size, ...rest }) => <IconStyled color={ color } icon={ icon[iconClass] } size={ size } { ...rest } /> }
} catch {
return null;
Expand All @@ -114,15 +111,15 @@ function getLazyStyledIcon(iconInfo) {
}

function Icon({
name = "Theme",
name,
size = "1x",
color = null,
...rest
}: Props) {
const iconClass = iconTable[name];

if (!iconClass)
return null;
throw new Error(`${name}: is not a valid icon.`);

const LazyStyledIcon = getLazyStyledIcon(iconClass);

Expand Down

0 comments on commit 5483142

Please sign in to comment.