import React, { memo } from 'react'; import { IconName, Icons } from './generated'; import { Color, ColorName } from '../color/Color'; import { SemanticColor, SemanticColorName, StateColor, TextColor } from '../color/SemanticColor'; import { hasOwnProperty, isNil } from '../../internal/utils/functions'; export interface IconProps { name: IconName; color?: TextColor | StateColor; size: 14 | 16 | 20 | 24 | 36; } export const Icon: React.FC = memo((props) => { const IconElement = Icons[props.name]; if (!IconElement) throw new Error('Unexpected icon types'); const iconColor = (() => { if (isNil(props.color)) return SemanticColor.text_primary; if (isSemanticColor(props.color)) { return SemanticColor[props.color]; } return Color[props.color]; })(); return React.cloneElement(IconElement, { color: iconColor, width: props.size, height: props.size, }); }); /** * 일반 color인지 SemanticColor인지 검사하는 함수 */ const isSemanticColor = (colorName: ColorName | SemanticColorName): colorName is SemanticColorName => { return hasOwnProperty(SemanticColor, colorName); };