WEB/src/views/storage/stockcheck/components/OperationCellRenderer.vue
2026-10-02 18:03:43 +08:00

203 lines
5.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!--
OperationCellRenderer — 表格操作列渲染组件
根据 operationButtons 配置渲染按钮列表。
支持的行为类型:
- actionType='action' → 触发 @operation-action 事件(可选配置 confirmMessage 弹确认框)
- actionType='form' → 打开 COMPONENT_REGISTRY 中注册的表单组件
显隐与禁用:
- visibleWhen(row) 返回 false 时不渲染该按钮(用于按 status 只显示可用动作)
- disabledOnField 对应行字段为空时置灰
使用方式:在 index.vue 中通过插槽 column-operation 引入:
<OperationCellRenderer
:buttons="currentTabConfig?.operationButtons"
:row="row"
@operation-action="handleOperationAction"
/>
-->
<template>
<div class="operation-cell">
<template v-for="btn in buttons" :key="btn.key">
<!-- action 按钮:支持确认弹窗 -->
<el-button
v-if="isVisible(btn) && btn.actionType === 'action'"
link
:type="btn.type || 'primary'"
size="small"
:loading="actionLoadingMap[btn.key]"
:disabled="isDisabled(btn)"
@click.stop="handleActionClick(btn)"
>
{{ resolveLabel(btn) }}
</el-button>
<!-- form 按钮:打开表单弹窗 -->
<template v-else-if="isVisible(btn) && btn.actionType === 'form'">
<el-button
link
:type="btn.type || 'primary'"
size="small"
:disabled="isDisabled(btn)"
@click.stop="openForm(btn)"
>
{{ resolveLabel(btn) }}
</el-button>
<!-- 表单弹窗 -->
<component
:is="resolvedFormComponents[btn.key]"
v-if="resolvedFormComponents[btn.key] && activeFormKey === btn.key"
v-model:visible="formVisibleMap[btn.key]"
v-bind="buildFormProps(btn)"
@success="handleFormSuccess(btn.key)"
/>
</template>
</template>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, markRaw, type Component } from 'vue'
import { ElMessageBox } from 'element-plus'
import { COMPONENT_REGISTRY } from '../config/template-config'
import type { OperationButtonConfig } from '../config/template-config'
defineOptions({ name: 'OperationCellRenderer' })
const props = defineProps<{
/** 操作按钮配置列表(来自 TabPageConfig.operationButtons) */
buttons?: OperationButtonConfig[]
/** 当前行数据 */
row: Record<string, any>
}>()
const emit = defineEmits<{
/** action 按钮点击事件(key: 按钮标识, row: 行数据) */
(e: 'operation-action', key: string, row: Record<string, any>): void
}>()
// ===== form 弹窗管理 =====
const activeFormKey = ref('')
const formVisibleMap = reactive<Record<string, boolean>>({})
const resolvedFormComponents = ref<Record<string, Component>>({})
const actionLoadingMap = reactive<Record<string, boolean>>({})
// ============================================================
// action 按钮:支持确认弹窗
// ============================================================
async function handleActionClick(btn: OperationButtonConfig) {
if (btn.confirmMessage) {
try {
await ElMessageBox.confirm(
btn.confirmMessage,
btn.confirmTitle || '确认操作',
{
confirmButtonText: btn.confirmButtonText || '确定',
cancelButtonText: '取消',
type: (btn.confirmType || 'warning') as any
}
)
} catch {
return // 取消
}
}
actionLoadingMap[btn.key] = true
try {
emit('operation-action', btn.key, props.row)
} finally {
actionLoadingMap[btn.key] = false
}
}
// ============================================================
// form 按钮:打开表单弹窗
// ============================================================
function openForm(btn: OperationButtonConfig) {
if (!btn.formKey) return
const comp = COMPONENT_REGISTRY[btn.formKey]
if (!comp) {
console.warn(`[OperationCellRenderer] 未注册的表单组件: ${btn.formKey}`)
return
}
activeFormKey.value = btn.key
resolvedFormComponents.value[btn.key] = markRaw(comp)
formVisibleMap[btn.key] = true
}
function buildFormProps(btn: OperationButtonConfig): Record<string, any> {
const result: Record<string, any> = {}
// paramMap: { formProp: rowField } → 从行数据取值注入表单
if (btn.paramMap) {
for (const [formProp, rowField] of Object.entries(btn.paramMap)) {
result[formProp] = props.row[rowField]
}
}
// extraParams: 固定参数直接注入
if (btn.extraParams) {
Object.assign(result, btn.extraParams)
}
return result
}
function handleFormSuccess(key: string) {
emit('operation-action', key, props.row)
}
// ============================================================
// 按钮显隐 / 禁用判断
// ============================================================
/** 按行数据决定是否渲染(未配置 visibleWhen 则渲染) */
function isVisible(btn: OperationButtonConfig): boolean {
if (typeof btn.visibleWhen !== 'function') return true
try {
return btn.visibleWhen(props.row) !== false
} catch {
return true
}
}
/** 按钮文案:labelWhen(row) 优先,返回空则回退静态 label */
function resolveLabel(btn: OperationButtonConfig): string {
if (typeof btn.labelWhen === 'function') {
try {
const dynamic = btn.labelWhen(props.row)
if (dynamic) return dynamic
} catch {
/* 忽略,回退静态 label */
}
}
return btn.label
}
function isDisabled(btn: OperationButtonConfig): boolean {
if (btn.disabledOnField) {
const val = props.row[btn.disabledOnField]
if (val === undefined || val === null || val === '' || val === false) {
return true
}
}
return false
}
</script>
<style lang="scss" scoped>
.operation-cell {
display: flex;
flex-wrap: wrap;
gap: 2px;
justify-content: center;
}
</style>