zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { iconType } from './types';
import { h, defineComponent, Component } from 'vue';
import { IconifyIconOffline, FontIcon, SvgIcon } from '../index';
 
/**
 * 支持fontawesome4、5+、iconfont、remixicon、element-plus的icons、自定义svg
 * @param icon 必传 string 图标
 * @param attrs 可选 iconType 属性
 * @returns Component
 */
export function useRenderIcon(icon: string, attrs?: iconType, isSvgIcon = true): Component {
  // iconfont
  const ifReg = /^IF-/;
  // typeof icon === "function" 属于SVG
  if (ifReg.test(icon)) {
    // iconfont
    const name = icon.split(ifReg)[1];
    const iconName = name.slice(0, name.indexOf(' ') === -1 ? name.length : name.indexOf(' '));
    const iconType = name.slice(name.indexOf(' ') + 1, name.length);
    return defineComponent({
      name: 'FontIcon',
      render() {
        return h(FontIcon, {
          icon: iconName,
          iconType,
          ...attrs,
        });
      },
    });
  } else if (typeof icon === 'function') {
    // svg
    return icon;
  } else if (isSvgIcon) {
    return defineComponent({
      name: 'SvgIcon',
      render() {
        if (!icon) {
          return;
        }
        return h(SvgIcon, {
          iconClass: icon,
          ...attrs,
        });
      },
    });
  } else {
    return defineComponent({
      name: 'Icon',
      render() {
        return h(IconifyIconOffline, {
          icon: icon,
          ...attrs,
        });
      },
    });
  }
}