You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB

1 year ago
  1. import IconAll, { ALL_ICON_KEYS, IconType, IIconAllProps } from '@icon-park/react/es/all'
  2. import React, { Fragment } from 'react'
  3. import * as AntIcons from '@ant-design/icons/es/icons'
  4. import { IconComponentProps } from '@ant-design/icons/es/components/Icon'
  5. export function Icon(props: Partial<IIconAllProps | IconComponentProps>) {
  6. const { type, ...other } = props
  7. const AntIcon = AntIcons[type as keyof typeof AntIcons]
  8. if (AntIcon) {
  9. return <AntIcon {...other}/>
  10. }
  11. //如果是http或https链接,直接返回图片
  12. if (type && (type.startsWith('http') || type.startsWith('https') || type.startsWith('data:image'))) {
  13. // @ts-ignore 没有办法把所有的属性都传递给img
  14. return <img src={type} alt="icon" width={16} height={16} {...other}/>
  15. }
  16. if (ALL_ICON_KEYS.indexOf(type as IconType) < 0) {
  17. return null
  18. }
  19. return (
  20. <Fragment>
  21. <IconAll type={type as IconType}
  22. theme="outline" size="20" fill="#868686" strokeWidth={3}
  23. {...other}/>
  24. </Fragment>
  25. )
  26. }
  27. // eslint-disable-next-line react-refresh/only-export-components
  28. export const getIcon = (type: string, props?: Partial<IIconAllProps>) => {
  29. if(React.isValidElement(type)){
  30. return type
  31. }
  32. //判断是否为json格式
  33. if (type && type.startsWith('{') && type.endsWith('}')) {
  34. try {
  35. const obj = JSON.parse(type)
  36. type = obj.type
  37. props = obj
  38. } catch (e) { /* empty */
  39. }
  40. }
  41. return <Icon type={type} {...props}/>
  42. }
  43. export default Icon