Blame view

src/components/Button/index.vue 2.92 KB
陈文彬 authored
1
2
<template>
  <Button v-bind="getBindValue" :class="[getColor, $attrs.class]">
3
    <!-- <template #[item]="data" v-for="item in Object.keys($slots)">
4
      <slot :name="item" v-bind="data" />
5
6
    </template> -->
    <template #default="data">
7
      <Icon :icon="preIcon" class="mr-1" v-if="preIcon" />
8
      <slot v-bind="data" />
9
      <Icon :icon="postIcon" class="ml-1" v-if="postIcon" />
陈文彬 authored
10
11
12
13
14
15
16
17
18
19
20
    </template>
  </Button>
</template>
<script lang="ts">
  import { PropType } from 'vue';

  import { defineComponent, computed, unref } from 'vue';
  import { Button } from 'ant-design-vue';
  // import { extendSlots } from '/@/utils/helper/tsxHelper';
  import { useThrottle } from '/@/hooks/core/useThrottle';
  import { isFunction } from '/@/utils/is';
21
  import Icon from '/@/components/Icon';
陈文彬 authored
22
23
24
  export default defineComponent({
    name: 'AButton',
    inheritAttrs: false,
25
    components: { Button, Icon },
陈文彬 authored
26
27
28
29
30
31
32
33
34
35
36
37
    props: {
      // 按钮类型
      type: {
        type: String as PropType<'primary' | 'default' | 'danger' | 'dashed' | 'link'>,
        default: 'default',
      },
      // 节流防抖类型 throttle debounce
      throttle: {
        type: String as PropType<'throttle' | 'debounce'>,
        default: 'throttle',
      },
      color: {
vben authored
38
        type: String as PropType<'error' | 'warning' | 'success' | ''>,
陈文彬 authored
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      },
      // 防抖节流时间
      throttleTime: {
        type: Number as PropType<number>,
        default: 0,
      },
      loading: {
        type: Boolean as PropType<boolean>,
        default: false,
      },
      disabled: {
        type: Boolean as PropType<boolean>,
        default: false,
      },
53
54
55
56
57
58
      preIcon: {
        type: String as PropType<string>,
      },
      postIcon: {
        type: String as PropType<string>,
      },
陈文彬 authored
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    },
    setup(props, { attrs }) {
      const getListeners = computed(() => {
        const { throttle, throttleTime = 0 } = props;
        // 是否开启节流防抖
        const throttleType = throttle!.toLowerCase();
        const isDebounce = throttleType === 'debounce';
        const openThrottle = ['throttle', 'debounce'].includes(throttleType) && throttleTime > 0;

        const on: {
          onClick?: Fn;
        } = {};

        if (attrs.onClick && isFunction(attrs.onClick) && openThrottle) {
          const [handler] = useThrottle(attrs.onClick as any, throttleTime!, {
            debounce: isDebounce,
            immediate: true,
          });
          on.onClick = handler;
        }

        return {
          ...attrs,
          ...on,
        };
      });

      const getColor = computed(() => {
        const res: string[] = [];
        const { color, disabled } = props;
        color && res.push(`ant-btn-${color}`);
        disabled && res.push('is-disabled');
        return res;
      });

      const getBindValue = computed((): any => {
        return { ...unref(getListeners), ...props };
      });
      return { getBindValue, getColor };
    },
  });
</script>