import { computed, defineComponent, h, ref } from 'vue'; import type { Component, PropType } from 'vue'; import { ElButton, ElPopover, ElTooltip } from 'element-plus'; import { $t } from '@/locales'; export type BusinessTableAction = { key: string; label: string; buttonType?: 'primary' | 'success' | 'warning' | 'danger' | 'info'; icon?: Component; disabled?: boolean; onClick: () => void | Promise; }; export default defineComponent({ name: 'BusinessTableActionCell', props: { actions: { type: Array as PropType, required: true }, variant: { type: String as PropType<'button' | 'icon'>, default: 'button' } }, setup(props) { const popoverVisible = ref(false); const directActions = computed(() => { if (props.variant === 'icon') { return props.actions; } if (props.actions.length <= 2) { return props.actions; } return props.actions.slice(0, 1); }); const moreActions = computed(() => { if (props.variant === 'icon') { return []; } if (props.actions.length <= 2) { return []; } return props.actions.slice(1); }); async function handleAction(action: BusinessTableAction) { if (action.disabled) { return; } popoverVisible.value = false; await action.onClick(); } function renderIcon(action: BusinessTableAction) { if (!action.icon) return null; return h(action.icon, { class: 'business-table-action-icon' }); } function renderButtonAction(action: BusinessTableAction) { return ( handleAction(action)} > {action.label} ); } function renderIconAction(action: BusinessTableAction) { return ( handleAction(action)} > {renderIcon(action)} ); } function renderMenuButton(action: BusinessTableAction) { if (props.variant === 'icon') { return ( handleAction(action)} > {renderIcon(action)} {action.label} ); } return ( handleAction(action)} > {action.label} ); } return () => (
event.stopPropagation()}> {directActions.value.map(action => props.variant === 'icon' ? renderIconAction(action) : renderButtonAction(action) )} {moreActions.value.length > 0 && ( {{ reference: () => ( event.stopPropagation()} > {props.variant === 'icon' ? ( ) : ( {$t('common.more')} )} ), default: () => (
{moreActions.value.map(action => renderMenuButton(action))}
) }}
)}
); } });