index.vue
2.08 KB
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
58
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
<script setup lang="ts">
import { computed, ref } from 'vue';
import { ElButton, ElMessageBox } from 'element-plus';
import { useDebounceFn } from '@vueuse/core';
import type { VButtonProps } from '../../types';
/**
* VButton - 增强型按钮组件
* 基于 Element Plus ElButton 的二次封装
* 提供加载状态、权限控制、确认提示、防抖等功能
*/
defineOptions({
name: 'VButton',
});
const props = withDefaults(defineProps<VButtonProps>(), {
loading: false,
disabled: false,
permission: undefined,
confirmText: undefined,
debounce: 0,
});
const emit = defineEmits<{
click: [event: MouseEvent];
}>();
// 内部加载状态
const internalLoading = ref(false);
// 计算最终的加载状态
const isLoading = computed(() => props.loading || internalLoading.value);
// 计算是否禁用
const isDisabled = computed(() => {
if (props.disabled) return true;
// 这里可以添加权限判断逻辑
// if (props.permission && !hasPermission(props.permission)) return true;
return false;
});
// 提取所有需要传递给 ElButton 的原生属性
const elButtonProps = computed(() => {
const {
permission,
confirmText,
debounce,
loading,
disabled,
...restProps
} = props;
return {
...restProps,
loading: isLoading.value,
disabled: isDisabled.value,
};
});
// 处理点击事件
const handleClick = async (event: MouseEvent) => {
if (isLoading.value || isDisabled.value) return;
// 如果设置了确认文本,则显示确认框
if (props.confirmText) {
try {
await ElMessageBox.confirm(props.confirmText, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
});
} catch {
// 用户取消
return;
}
}
emit('click', event);
};
// 防抖处理
const debouncedClick = props.debounce
? useDebounceFn(handleClick, props.debounce)
: handleClick;
</script>
<template>
<!-- 用计算属性过滤并批量绑定 -->
<ElButton
v-bind="elButtonProps"
@click="debouncedClick"
>
<slot />
</ElButton>
</template>