index.vue 1.3 KB
Newer Older
richard_1015's avatar
richard_1015 已提交
1
<script lang="ts">
richard_1015's avatar
richard_1015 已提交
2
import { h, PropType } from 'vue';
richard_1015's avatar
richard_1015 已提交
3 4 5 6 7
import { createComponent } from '@/utils/create';
const { componentName, create } = createComponent('icon');

export default create({
  props: {
richard_1015's avatar
richard_1015 已提交
8 9 10 11 12
    name: { type: String, default: '' },
    size: { type: String, default: '' },
    classPrefix: { type: String, default: 'nutui-iconfont' },
    color: { type: String, default: '' },
    tag: { type: String as PropType<keyof HTMLElementTagNameMap>, default: 'i' }
richard_1015's avatar
richard_1015 已提交
13 14
  },
  emits: ['click'],
richard_1015's avatar
richard_1015 已提交
15 16

  setup(props, { emit, slots }) {
richard_1015's avatar
richard_1015 已提交
17 18 19
    const handleClick = (event: Event) => {
      emit('click', event);
    };
richard_1015's avatar
richard_1015 已提交
20 21 22 23 24
    const isImage = () => {
      return props.name ? props.name.indexOf('/') !== -1 : false;
    };
    const styleOptions = {
      class: `${props.classPrefix} ${componentName}-${props.name}`,
richard_1015's avatar
richard_1015 已提交
25 26 27 28
      style: { color: props.color, fontSize: props.size, width: '', height: '' },
      onClick: handleClick,
      src: ''
    };
richard_1015's avatar
richard_1015 已提交
29 30 31 32 33 34 35
    if (isImage()) {
      styleOptions.class = `${componentName}__img`;
      styleOptions.src = props.name;
      styleOptions.style['width'] = props.size;
      styleOptions.style['height'] = props.size;
    }
    return () => h(isImage() ? 'img' : props.tag, styleOptions, slots.default?.());
richard_1015's avatar
richard_1015 已提交
36 37 38 39 40 41 42
  }
});
</script>

<style lang="scss">
@import 'index.scss';
</style>