2025-05-15 12:13:11 +08:00
|
|
|
|
<!-- add by 星语:参考 vben2 的方式,增加 TableAction 组件 -->
|
2025-05-15 11:49:44 +08:00
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
|
import type { PropType } from 'vue';
|
|
|
|
|
|
|
|
|
|
|
|
import type { ActionItem, PopConfirm } from './typing';
|
|
|
|
|
|
|
2025-07-19 17:22:50 +08:00
|
|
|
|
import { computed, unref, watch } from 'vue';
|
2025-05-15 11:49:44 +08:00
|
|
|
|
|
|
|
|
|
|
import { useAccess } from '@vben/access';
|
|
|
|
|
|
import { IconifyIcon } from '@vben/icons';
|
|
|
|
|
|
import { $t } from '@vben/locales';
|
|
|
|
|
|
import { isBoolean, isFunction } from '@vben/utils';
|
|
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
|
Button,
|
|
|
|
|
|
Dropdown,
|
|
|
|
|
|
Menu,
|
|
|
|
|
|
Popconfirm,
|
|
|
|
|
|
Space,
|
|
|
|
|
|
Tooltip,
|
|
|
|
|
|
} from 'ant-design-vue';
|
|
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
|
actions: {
|
|
|
|
|
|
type: Array as PropType<ActionItem[]>,
|
|
|
|
|
|
default() {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
dropDownActions: {
|
|
|
|
|
|
type: Array as PropType<ActionItem[]>,
|
|
|
|
|
|
default() {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
divider: {
|
|
|
|
|
|
type: Boolean,
|
|
|
|
|
|
default: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const { hasAccessByCodes } = useAccess();
|
|
|
|
|
|
|
2025-06-28 00:16:15 +08:00
|
|
|
|
/** 检查是否显示 */
|
2025-05-15 11:49:44 +08:00
|
|
|
|
function isIfShow(action: ActionItem): boolean {
|
|
|
|
|
|
const ifShow = action.ifShow;
|
|
|
|
|
|
let isIfShow = true;
|
|
|
|
|
|
if (isBoolean(ifShow)) {
|
|
|
|
|
|
isIfShow = ifShow;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isFunction(ifShow)) {
|
|
|
|
|
|
isIfShow = ifShow(action);
|
|
|
|
|
|
}
|
2025-06-12 16:11:54 +08:00
|
|
|
|
if (isIfShow) {
|
|
|
|
|
|
isIfShow =
|
|
|
|
|
|
hasAccessByCodes(action.auth || []) || (action.auth || []).length === 0;
|
|
|
|
|
|
}
|
2025-05-15 11:49:44 +08:00
|
|
|
|
return isIfShow;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-28 00:16:15 +08:00
|
|
|
|
/** 处理按钮 actions */
|
|
|
|
|
|
const getActions = computed(() => {
|
2025-07-19 17:22:50 +08:00
|
|
|
|
const actions = props.actions || [];
|
|
|
|
|
|
return actions.filter((action: ActionItem) => isIfShow(action));
|
2025-06-28 00:16:15 +08:00
|
|
|
|
});
|
2025-05-15 11:49:44 +08:00
|
|
|
|
|
2025-06-28 00:16:15 +08:00
|
|
|
|
/** 处理下拉菜单 actions */
|
|
|
|
|
|
const getDropdownList = computed(() => {
|
2025-07-19 17:22:50 +08:00
|
|
|
|
const dropDownActions = props.dropDownActions || [];
|
|
|
|
|
|
return dropDownActions.filter((action: ActionItem) => isIfShow(action));
|
2025-06-20 18:19:09 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-28 00:16:15 +08:00
|
|
|
|
/** Space 组件的 size */
|
2025-06-20 18:19:09 +08:00
|
|
|
|
const spaceSize = computed(() => {
|
2025-07-19 17:22:50 +08:00
|
|
|
|
const actions = unref(getActions);
|
|
|
|
|
|
return actions?.some((item: ActionItem) => item.type === 'link') ? 0 : 8;
|
2025-05-15 11:49:44 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-28 00:16:15 +08:00
|
|
|
|
/** 获取 PopConfirm 属性 */
|
2025-07-09 17:48:49 +08:00
|
|
|
|
function getPopConfirmProps(popConfirm: PopConfirm) {
|
|
|
|
|
|
if (!popConfirm) return {};
|
|
|
|
|
|
|
|
|
|
|
|
const attrs: Record<string, any> = {};
|
|
|
|
|
|
|
|
|
|
|
|
// 复制基本属性,排除函数
|
|
|
|
|
|
Object.keys(popConfirm).forEach((key) => {
|
|
|
|
|
|
if (key !== 'confirm' && key !== 'cancel' && key !== 'icon') {
|
|
|
|
|
|
attrs[key] = popConfirm[key as keyof PopConfirm];
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 单独处理事件函数
|
|
|
|
|
|
if (popConfirm.confirm && isFunction(popConfirm.confirm)) {
|
|
|
|
|
|
attrs.onConfirm = popConfirm.confirm;
|
2025-05-15 11:49:44 +08:00
|
|
|
|
}
|
2025-07-09 17:48:49 +08:00
|
|
|
|
if (popConfirm.cancel && isFunction(popConfirm.cancel)) {
|
|
|
|
|
|
attrs.onCancel = popConfirm.cancel;
|
2025-05-15 11:49:44 +08:00
|
|
|
|
}
|
2025-07-09 17:48:49 +08:00
|
|
|
|
|
|
|
|
|
|
return attrs;
|
2025-05-15 11:49:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-28 00:16:15 +08:00
|
|
|
|
/** 获取 Button 属性 */
|
2025-05-15 11:49:44 +08:00
|
|
|
|
function getButtonProps(action: ActionItem) {
|
2025-06-28 00:16:15 +08:00
|
|
|
|
return {
|
2025-06-26 15:39:36 +08:00
|
|
|
|
type: action.type || 'link',
|
|
|
|
|
|
danger: action.danger || false,
|
2025-06-20 18:19:09 +08:00
|
|
|
|
disabled: action.disabled,
|
|
|
|
|
|
loading: action.loading,
|
|
|
|
|
|
size: action.size,
|
2025-05-15 11:49:44 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-28 00:16:15 +08:00
|
|
|
|
/** 获取 Tooltip 属性 */
|
2025-06-20 18:19:09 +08:00
|
|
|
|
function getTooltipProps(tooltip: any | string) {
|
|
|
|
|
|
if (!tooltip) return {};
|
2025-06-28 00:16:15 +08:00
|
|
|
|
return typeof tooltip === 'string' ? { title: tooltip } : { ...tooltip };
|
2025-06-20 18:19:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-28 00:16:15 +08:00
|
|
|
|
/** 处理菜单点击 */
|
2025-05-15 11:49:44 +08:00
|
|
|
|
function handleMenuClick(e: any) {
|
2025-06-21 16:21:48 +08:00
|
|
|
|
const action = getDropdownList.value[e.key];
|
2025-06-28 00:16:15 +08:00
|
|
|
|
if (action && action.onClick && isFunction(action.onClick)) {
|
2025-05-15 11:49:44 +08:00
|
|
|
|
action.onClick();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-28 00:16:15 +08:00
|
|
|
|
|
|
|
|
|
|
/** 生成稳定的 key */
|
|
|
|
|
|
function getActionKey(action: ActionItem, index: number) {
|
|
|
|
|
|
return `${action.label || ''}-${action.type || ''}-${index}`;
|
|
|
|
|
|
}
|
2025-07-09 17:48:49 +08:00
|
|
|
|
|
|
|
|
|
|
/** 处理按钮点击 */
|
|
|
|
|
|
function handleButtonClick(action: ActionItem) {
|
|
|
|
|
|
if (action.onClick && isFunction(action.onClick)) {
|
|
|
|
|
|
action.onClick();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-19 17:22:50 +08:00
|
|
|
|
|
2025-07-19 17:40:27 +08:00
|
|
|
|
/** 监听props变化,强制重新计算 */
|
2025-07-19 17:22:50 +08:00
|
|
|
|
watch(
|
|
|
|
|
|
() => [props.actions, props.dropDownActions],
|
|
|
|
|
|
() => {
|
|
|
|
|
|
// 这里不需要额外处理,computed会自动重新计算
|
|
|
|
|
|
},
|
|
|
|
|
|
{ deep: true },
|
|
|
|
|
|
);
|
2025-05-15 11:49:44 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-05-19 10:45:14 +08:00
|
|
|
|
<div class="table-actions">
|
2025-06-20 18:19:09 +08:00
|
|
|
|
<Space :size="spaceSize">
|
2025-06-28 00:16:15 +08:00
|
|
|
|
<template
|
|
|
|
|
|
v-for="(action, index) in getActions"
|
|
|
|
|
|
:key="getActionKey(action, index)"
|
|
|
|
|
|
>
|
2025-05-15 11:49:44 +08:00
|
|
|
|
<Popconfirm
|
|
|
|
|
|
v-if="action.popConfirm"
|
|
|
|
|
|
v-bind="getPopConfirmProps(action.popConfirm)"
|
|
|
|
|
|
>
|
|
|
|
|
|
<template v-if="action.popConfirm.icon" #icon>
|
|
|
|
|
|
<IconifyIcon :icon="action.popConfirm.icon" />
|
|
|
|
|
|
</template>
|
2025-06-28 00:16:15 +08:00
|
|
|
|
<Tooltip v-bind="getTooltipProps(action.tooltip)">
|
2025-05-15 11:49:44 +08:00
|
|
|
|
<Button v-bind="getButtonProps(action)">
|
|
|
|
|
|
<template v-if="action.icon" #icon>
|
|
|
|
|
|
<IconifyIcon :icon="action.icon" />
|
|
|
|
|
|
</template>
|
|
|
|
|
|
{{ action.label }}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
</Popconfirm>
|
2025-06-28 00:16:15 +08:00
|
|
|
|
<Tooltip v-else v-bind="getTooltipProps(action.tooltip)">
|
2025-07-09 17:48:49 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
v-bind="getButtonProps(action)"
|
|
|
|
|
|
@click="handleButtonClick(action)"
|
|
|
|
|
|
>
|
2025-05-15 11:49:44 +08:00
|
|
|
|
<template v-if="action.icon" #icon>
|
|
|
|
|
|
<IconifyIcon :icon="action.icon" />
|
|
|
|
|
|
</template>
|
|
|
|
|
|
{{ action.label }}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
|
|
|
|
|
|
<Dropdown v-if="getDropdownList.length > 0" :trigger="['hover']">
|
|
|
|
|
|
<slot name="more">
|
2025-07-09 17:48:49 +08:00
|
|
|
|
<Button type="link">
|
2025-05-15 11:49:44 +08:00
|
|
|
|
<template #icon>
|
|
|
|
|
|
{{ $t('page.action.more') }}
|
2025-05-19 16:18:48 +08:00
|
|
|
|
<IconifyIcon icon="lucide:ellipsis-vertical" />
|
2025-05-15 11:49:44 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</slot>
|
|
|
|
|
|
<template #overlay>
|
2025-06-21 16:23:10 +08:00
|
|
|
|
<Menu>
|
|
|
|
|
|
<Menu.Item
|
|
|
|
|
|
v-for="(action, index) in getDropdownList"
|
|
|
|
|
|
:key="index"
|
|
|
|
|
|
:disabled="action.disabled"
|
|
|
|
|
|
@click="!action.popConfirm && handleMenuClick({ key: index })"
|
|
|
|
|
|
>
|
2025-05-15 11:49:44 +08:00
|
|
|
|
<template v-if="action.popConfirm">
|
|
|
|
|
|
<Popconfirm v-bind="getPopConfirmProps(action.popConfirm)">
|
|
|
|
|
|
<template v-if="action.popConfirm.icon" #icon>
|
|
|
|
|
|
<IconifyIcon :icon="action.popConfirm.icon" />
|
|
|
|
|
|
</template>
|
|
|
|
|
|
<div
|
|
|
|
|
|
:class="
|
|
|
|
|
|
action.disabled === true
|
|
|
|
|
|
? 'cursor-not-allowed text-gray-300'
|
|
|
|
|
|
: ''
|
|
|
|
|
|
"
|
|
|
|
|
|
>
|
|
|
|
|
|
<IconifyIcon v-if="action.icon" :icon="action.icon" />
|
2025-06-04 16:50:20 +08:00
|
|
|
|
<span :class="action.icon ? 'ml-1' : ''">
|
2025-07-09 17:48:49 +08:00
|
|
|
|
{{ action.label }}
|
2025-06-04 16:50:20 +08:00
|
|
|
|
</span>
|
2025-05-15 11:49:44 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</Popconfirm>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
<template v-else>
|
|
|
|
|
|
<div
|
|
|
|
|
|
:class="
|
|
|
|
|
|
action.disabled === true
|
|
|
|
|
|
? 'cursor-not-allowed text-gray-300'
|
|
|
|
|
|
: ''
|
|
|
|
|
|
"
|
|
|
|
|
|
>
|
|
|
|
|
|
<IconifyIcon v-if="action.icon" :icon="action.icon" />
|
|
|
|
|
|
{{ action.label }}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</Menu.Item>
|
|
|
|
|
|
</Menu>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</Dropdown>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
2025-06-28 00:16:15 +08:00
|
|
|
|
|
2025-05-15 11:49:44 +08:00
|
|
|
|
<style lang="scss">
|
2025-05-19 10:45:14 +08:00
|
|
|
|
.table-actions {
|
2025-05-19 16:18:48 +08:00
|
|
|
|
.ant-btn-link {
|
2025-05-15 11:49:44 +08:00
|
|
|
|
padding: 4px;
|
|
|
|
|
|
margin-left: 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.ant-btn > .iconify + span,
|
|
|
|
|
|
.ant-btn > span + .iconify {
|
|
|
|
|
|
margin-inline-start: 4px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.iconify {
|
|
|
|
|
|
display: inline-flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
width: 1em;
|
|
|
|
|
|
height: 1em;
|
|
|
|
|
|
font-style: normal;
|
|
|
|
|
|
line-height: 0;
|
|
|
|
|
|
vertical-align: -0.125em;
|
|
|
|
|
|
color: inherit;
|
|
|
|
|
|
text-align: center;
|
|
|
|
|
|
text-transform: none;
|
|
|
|
|
|
text-rendering: optimizelegibility;
|
|
|
|
|
|
-webkit-font-smoothing: antialiased;
|
|
|
|
|
|
-moz-osx-font-smoothing: grayscale;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.ant-popconfirm {
|
|
|
|
|
|
.ant-popconfirm-buttons {
|
|
|
|
|
|
.ant-btn {
|
|
|
|
|
|
margin-inline-start: 4px !important;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|