From a7ca7cdb9f8a8cefb9537feb7dddbc7abe8ef906 Mon Sep 17 00:00:00 2001 From: Jin Mao Date: Wed, 25 Mar 2026 14:42:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(vxe-table):=20=E9=87=8D=E6=9E=84=20use?= =?UTF-8?q?TableForm=20=E5=87=BD=E6=95=B0=E5=AE=9E=E7=8E=B0=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=9D=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 useTableForm 从箭头函数改为普通函数声明 - 简化表单工厂函数的获取逻辑,支持上下文注入 - 移除初始化时的重复上下文注入代码 - 改进错误提示信息的准确性 - 调整代码结构以提高可读性和维护性 - 将 SetupVxeTable 接口中的 useVbenForm 字段改为可选参数 --- .../effects/plugins/src/vxe-table/init.ts | 28 +++++++++---------- .../effects/plugins/src/vxe-table/types.ts | 19 ++++++------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/packages/effects/plugins/src/vxe-table/init.ts b/packages/effects/plugins/src/vxe-table/init.ts index 48b5044d5..d92136ec6 100644 --- a/packages/effects/plugins/src/vxe-table/init.ts +++ b/packages/effects/plugins/src/vxe-table/init.ts @@ -43,19 +43,19 @@ function normalizeVxeLocale>(localeModule: T) { ) as T; } -export const useTableForm = ((...args: any[]) => { +export function useTableForm(...args: any[]) { const pluginsOptions = injectPluginsOptions(); + const contextFormFactory = pluginsOptions?.form?.useVbenForm; - if (!tableFormFactory) { - if (pluginsOptions?.form?.useVbenForm) { - tableFormFactory = pluginsOptions.form.useVbenForm; - } else { - throw new Error('useTableForm is not initialized'); - } + const factory = tableFormFactory || contextFormFactory; + if (!factory) { + throw new Error( + 'useTableForm is not initialized. Please provide useVbenForm via setupVbenVxeTable() or providePluginsOptions()', + ); } - return tableFormFactory(...args); -}); + return factory(...args); +} // 部分组件,如果没注册,vxe-table 会报错,这里实际没用组件,只是为了不报错,同时可以减少打包体积 const createVirtualComponent = (name = '') => { @@ -109,12 +109,10 @@ export function setupVbenVxeTable(setupOptions: SetupVxeTable) { initVxeTable(); - const pluginsOptions = injectPluginsOptions(); - const useVbenFormFromContext = pluginsOptions?.form?.useVbenForm; - - // 优先级:参数传入 > context 注入 - tableFormFactory = useVbenFormFromParam || useVbenFormFromContext; - + // 优先使用参数传入的 useVbenForm,context 注入在 useTableForm 中获取 + if (useVbenFormFromParam) { + tableFormFactory = useVbenFormFromParam; + } const { isDark, locale } = usePreferences(); const localMap = { diff --git a/packages/effects/plugins/src/vxe-table/types.ts b/packages/effects/plugins/src/vxe-table/types.ts index 8e56ce418..49846b274 100644 --- a/packages/effects/plugins/src/vxe-table/types.ts +++ b/packages/effects/plugins/src/vxe-table/types.ts @@ -1,19 +1,18 @@ import type { VxeGridListeners, - VxeGridPropTypes, VxeGridProps as VxeTableGridProps, - VxeUIExport, -} from 'vxe-table'; - -import type { Ref } from 'vue'; + VxeGridPropTypes, + VxeUIExport +} from "vxe-table"; -import type { ClassType, DeepPartial } from '@vben/types'; +import type { Ref } from "vue"; -import type { BaseFormComponentType, VbenFormProps } from '@vben-core/form-ui'; +import type { ClassType, DeepPartial } from "@vben/types"; -import type { VxeGridApi } from './api'; +import type { BaseFormComponentType, VbenFormProps } from "@vben-core/form-ui"; +import { useVbenForm } from "@vben-core/form-ui"; -import { useVbenForm } from '@vben-core/form-ui'; +import type { VxeGridApi } from "./api"; export interface VxePaginationInfo { currentPage: number; @@ -95,5 +94,5 @@ export type ExtendedVxeGridApi< export interface SetupVxeTable { configVxeTable: (ui: VxeUIExport) => void; - useVbenForm: typeof useVbenForm; + useVbenForm?: typeof useVbenForm; }