perf(button): delete the button component useless code

This commit is contained in:
vben
2020-10-30 00:56:11 +08:00
parent fb0c7763ed
commit bdce84537a
6 changed files with 97 additions and 54 deletions

View File

@@ -1,23 +1,20 @@
<template>
<Button v-bind="getBindValue" :class="[getColor, $attrs.class]">
<!-- <template #[item]="data" v-for="item in Object.keys($slots)">
<slot :name="item" v-bind="data" />
</template> -->
<template #default="data">
<Icon :icon="preIcon" class="mr-1" v-if="preIcon" />
<Icon :icon="preIcon" :class="{ 'mr-1': !getIsCircleBtn }" v-if="preIcon" />
<slot v-bind="data" />
<Icon :icon="postIcon" class="ml-1" v-if="postIcon" />
<Icon :icon="postIcon" :class="{ 'ml-1': !getIsCircleBtn }" v-if="postIcon" />
</template>
</Button>
</template>
<script lang="ts">
import { PropType } from 'vue';
import { defineComponent, computed, unref } from 'vue';
import { defineComponent, computed } 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';
// import { useThrottle } from '/@/hooks/core/useThrottle';
// import { isFunction } from '/@/utils/is';
import Icon from '/@/components/Icon';
export default defineComponent({
name: 'AButton',
@@ -30,18 +27,18 @@
default: 'default',
},
// 节流防抖类型 throttle debounce
throttle: {
type: String as PropType<'throttle' | 'debounce'>,
default: 'throttle',
},
// throttle: {
// type: String as PropType<'throttle' | 'debounce'>,
// default: 'throttle',
// },
color: {
type: String as PropType<'error' | 'warning' | 'success' | ''>,
},
// 防抖节流时间
throttleTime: {
type: Number as PropType<number>,
default: 0,
},
// // 防抖节流时间
// throttleTime: {
// type: Number as PropType<number>,
// default: 50,
// },
loading: {
type: Boolean as PropType<boolean>,
default: false,
@@ -58,30 +55,38 @@
},
},
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 getIsCircleBtn = computed(() => {
return attrs.shape === 'circle';
});
// const getListeners = computed(() => {
// const { throttle, throttleTime = 0 } = props;
// // 是否开启节流防抖
// const throttleType = throttle!.toLowerCase();
// const isDebounce = throttleType === 'debounce';
// const openThrottle = ['throttle', 'debounce'].includes(throttleType) && throttleTime > 0;
// if (!openThrottle) {
// return {
// ...attrs,
// };
// }
// const on: {
// onClick?: Fn;
// } = {};
// if (attrs.onClick && isFunction(attrs.onClick) && openThrottle) {
// const [handler] = useThrottle(attrs.onClick as any, throttleTime!, {
// debounce: isDebounce,
// immediate: false,
// });
// on.onClick = handler;
// }
// return {
// ...attrs,
// ...on,
// };
// });
const getColor = computed(() => {
const res: string[] = [];
@@ -92,9 +97,10 @@
});
const getBindValue = computed((): any => {
return { ...unref(getListeners), ...props };
return { ...attrs, ...props };
});
return { getBindValue, getColor };
return { getBindValue, getColor, getIsCircleBtn };
},
});
</script>