import * as React from 'react'; import classNames from 'classnames'; import Context from './Context'; import { svgBaseProps, warning, useInsertStyles } from '../utils'; export interface IconBaseProps extends React.HTMLProps { spin?: boolean; rotate?: number; } export interface CustomIconComponentProps { width: string | number; height: string | number; fill: string; viewBox?: string; className?: string; style?: React.CSSProperties; } export interface IconComponentProps extends IconBaseProps { viewBox?: string; component?: React.ComponentType> | React.ForwardRefExoticComponent; ariaLabel?: React.AriaAttributes['aria-label']; } const Icon = React.forwardRef((props, ref) => { const { // affect outter ... className, // affect inner ... component: Component, viewBox, spin, rotate, tabIndex, onClick, // children children, ...restProps } = props; warning( Boolean(Component || children), 'Should have `component` prop or `children`.', ); useInsertStyles(); const { prefixCls = 'anticon' } = React.useContext(Context); const classString = classNames( prefixCls, className, ); const svgClassString = classNames({ [`${prefixCls}-spin`]: !!spin, }); const svgStyle = rotate ? { msTransform: `rotate(${rotate}deg)`, transform: `rotate(${rotate}deg)`, } : undefined; const innerSvgProps: CustomIconComponentProps = { ...svgBaseProps, className: svgClassString, style: svgStyle, viewBox, }; if (!viewBox) { delete innerSvgProps.viewBox; } // component > children const renderInnerNode = () => { if (Component) { return {children}; } if (children) { warning( Boolean(viewBox) || (React.Children.count(children) === 1 && React.isValidElement(children) && React.Children.only(children).type === 'use'), 'Make sure that you provide correct `viewBox`' + ' prop (default `0 0 1024 1024`) to the icon.', ); return ( {children} ); } return null; }; let iconTabIndex = tabIndex; if (iconTabIndex === undefined && onClick) { iconTabIndex = -1; } return ( {renderInnerNode()} ); }); Icon.displayName = 'Icon'; export default Icon;