Browse Source

refactor(vxe-table): 重构 useTableForm 函数实现并优化初始化逻辑

- 将 useTableForm 从箭头函数改为普通函数声明
- 简化表单工厂函数的获取逻辑,支持上下文注入
- 移除初始化时的重复上下文注入代码
- 改进错误提示信息的准确性
- 调整代码结构以提高可读性和维护性
- 将 SetupVxeTable 接口中的 useVbenForm 字段改为可选参数
pull/7729/head
Jin Mao 4 days ago
parent
commit
a7ca7cdb9f
  1. 28
      packages/effects/plugins/src/vxe-table/init.ts
  2. 19
      packages/effects/plugins/src/vxe-table/types.ts

28
packages/effects/plugins/src/vxe-table/init.ts

@ -43,19 +43,19 @@ function normalizeVxeLocale<T extends Record<string, any>>(localeModule: T) {
) as T; ) as T;
} }
export const useTableForm = ((...args: any[]) => { export function useTableForm(...args: any[]) {
const pluginsOptions = injectPluginsOptions(); const pluginsOptions = injectPluginsOptions();
const contextFormFactory = pluginsOptions?.form?.useVbenForm;
if (!tableFormFactory) { const factory = tableFormFactory || contextFormFactory;
if (pluginsOptions?.form?.useVbenForm) { if (!factory) {
tableFormFactory = pluginsOptions.form.useVbenForm; throw new Error(
} else { 'useTableForm is not initialized. Please provide useVbenForm via setupVbenVxeTable() or providePluginsOptions()',
throw new Error('useTableForm is not initialized'); );
}
} }
return tableFormFactory(...args); return factory(...args);
}); }
// 部分组件,如果没注册,vxe-table 会报错,这里实际没用组件,只是为了不报错,同时可以减少打包体积 // 部分组件,如果没注册,vxe-table 会报错,这里实际没用组件,只是为了不报错,同时可以减少打包体积
const createVirtualComponent = (name = '') => { const createVirtualComponent = (name = '') => {
@ -109,12 +109,10 @@ export function setupVbenVxeTable(setupOptions: SetupVxeTable) {
initVxeTable(); initVxeTable();
const pluginsOptions = injectPluginsOptions(); // 优先使用参数传入的 useVbenForm,context 注入在 useTableForm 中获取
const useVbenFormFromContext = pluginsOptions?.form?.useVbenForm; if (useVbenFormFromParam) {
tableFormFactory = useVbenFormFromParam;
// 优先级:参数传入 > context 注入 }
tableFormFactory = useVbenFormFromParam || useVbenFormFromContext;
const { isDark, locale } = usePreferences(); const { isDark, locale } = usePreferences();
const localMap = { const localMap = {

19
packages/effects/plugins/src/vxe-table/types.ts

@ -1,19 +1,18 @@
import type { import type {
VxeGridListeners, VxeGridListeners,
VxeGridPropTypes,
VxeGridProps as VxeTableGridProps, VxeGridProps as VxeTableGridProps,
VxeUIExport, VxeGridPropTypes,
} from 'vxe-table'; VxeUIExport
} from "vxe-table";
import type { Ref } from 'vue';
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 { export interface VxePaginationInfo {
currentPage: number; currentPage: number;
@ -95,5 +94,5 @@ export type ExtendedVxeGridApi<
export interface SetupVxeTable { export interface SetupVxeTable {
configVxeTable: (ui: VxeUIExport) => void; configVxeTable: (ui: VxeUIExport) => void;
useVbenForm: typeof useVbenForm; useVbenForm?: typeof useVbenForm;
} }

Loading…
Cancel
Save