import * as React from 'react'; import { IconContext, DefaultContext } from './iconContext'; export interface IconTree { tag: string; attr: {[key: string]: string}; child: IconTree[]; } function Tree2Element(tree: IconTree[]): React.ReactElement<{}>[] { return tree && tree.map((node, i) => React.createElement(node.tag, {key: i, ...node.attr}, Tree2Element(node.child))); } export function GenIcon(data: IconTree) { return (props: IconBaseProps) => ( {Tree2Element(data.child)} ); } export interface IconBaseProps extends React.SVGAttributes { children?: React.ReactNode; size?: string | number; color?: string; title?: string; } export type IconType = (props: IconBaseProps) => JSX.Element; export function IconBase(props:IconBaseProps & { attr: {} | undefined }): JSX.Element { const elem = (conf: IconContext) => { const computedSize = props.size || conf.size || "1em"; let className; if (conf.className) className = conf.className; if (props.className) className = (className ? className + ' ' : '') + props.className; const {attr, title, ...svgProps} = props; return ( {title && {title}} {props.children} ) }; return IconContext !== undefined ? {(conf: IconContext) => elem(conf)} : elem(DefaultContext); }