mirror of
https://github.com/imdap/ruoyi-plus-vben5.git
synced 2026-04-23 00:38:34 +08:00
- 将 useTableForm 从箭头函数改为普通函数声明 - 简化表单工厂函数的获取逻辑,支持上下文注入 - 移除初始化时的重复上下文注入代码 - 改进错误提示信息的准确性 - 调整代码结构以提高可读性和维护性 - 将 SetupVxeTable 接口中的 useVbenForm 字段改为可选参数
139 lines
3.5 KiB
TypeScript
139 lines
3.5 KiB
TypeScript
import type { SetupVxeTable } from "./types";
|
||
|
||
import { defineComponent, watch } from "vue";
|
||
|
||
import { usePreferences } from "@vben/preferences";
|
||
|
||
import { injectPluginsOptions } from "../plugins-context";
|
||
|
||
import {
|
||
VxeButton,
|
||
VxeCheckbox,
|
||
VxeIcon,
|
||
VxeInput,
|
||
VxeLoading,
|
||
VxeModal,
|
||
VxeNumberInput,
|
||
VxePager,
|
||
VxeRadioGroup,
|
||
VxeSelect,
|
||
VxeTooltip,
|
||
VxeUI,
|
||
VxeUpload
|
||
} from "vxe-pc-ui";
|
||
import enUS from "vxe-pc-ui/lib/language/en-US";
|
||
// 导入默认的语言
|
||
import zhCN from "vxe-pc-ui/lib/language/zh-CN";
|
||
import { VxeColgroup, VxeColumn, VxeGrid, VxeTable, VxeToolbar } from "vxe-table";
|
||
|
||
import { extendsDefaultFormatter } from "./extends";
|
||
|
||
// 是否加载过
|
||
let isInit = false;
|
||
|
||
let tableFormFactory: ((...args: any[]) => any) | undefined;
|
||
|
||
function normalizeVxeLocale<T extends Record<string, any>>(localeModule: T) {
|
||
return (
|
||
localeModule &&
|
||
typeof localeModule === 'object' &&
|
||
'default' in localeModule
|
||
? localeModule.default
|
||
: localeModule
|
||
) as T;
|
||
}
|
||
|
||
export function useTableForm(...args: any[]) {
|
||
const pluginsOptions = injectPluginsOptions();
|
||
const contextFormFactory = pluginsOptions?.form?.useVbenForm;
|
||
|
||
const factory = tableFormFactory || contextFormFactory;
|
||
if (!factory) {
|
||
throw new Error(
|
||
'useTableForm is not initialized. Please provide useVbenForm via setupVbenVxeTable() or providePluginsOptions()',
|
||
);
|
||
}
|
||
|
||
return factory(...args);
|
||
}
|
||
|
||
// 部分组件,如果没注册,vxe-table 会报错,这里实际没用组件,只是为了不报错,同时可以减少打包体积
|
||
const createVirtualComponent = (name = '') => {
|
||
return defineComponent({
|
||
name,
|
||
});
|
||
};
|
||
|
||
export function initVxeTable() {
|
||
if (isInit) {
|
||
return;
|
||
}
|
||
|
||
VxeUI.component(VxeTable);
|
||
VxeUI.component(VxeColumn);
|
||
VxeUI.component(VxeColgroup);
|
||
VxeUI.component(VxeGrid);
|
||
VxeUI.component(VxeToolbar);
|
||
|
||
VxeUI.component(VxeButton);
|
||
// VxeUI.component(VxeButtonGroup);
|
||
VxeUI.component(VxeCheckbox);
|
||
// VxeUI.component(VxeCheckboxGroup);
|
||
VxeUI.component(createVirtualComponent('VxeForm'));
|
||
// VxeUI.component(VxeFormGather);
|
||
// VxeUI.component(VxeFormItem);
|
||
VxeUI.component(VxeIcon);
|
||
VxeUI.component(VxeInput);
|
||
// VxeUI.component(VxeList);
|
||
VxeUI.component(VxeLoading);
|
||
VxeUI.component(VxeModal);
|
||
VxeUI.component(VxeNumberInput);
|
||
// VxeUI.component(VxeOptgroup);
|
||
// VxeUI.component(VxeOption);
|
||
VxeUI.component(VxePager);
|
||
// VxeUI.component(VxePulldown);
|
||
// VxeUI.component(VxeRadio);
|
||
// VxeUI.component(VxeRadioButton);
|
||
VxeUI.component(VxeRadioGroup);
|
||
VxeUI.component(VxeSelect);
|
||
// VxeUI.component(VxeSwitch);
|
||
// VxeUI.component(VxeTextarea);
|
||
VxeUI.component(VxeTooltip);
|
||
VxeUI.component(VxeUpload);
|
||
|
||
isInit = true;
|
||
}
|
||
|
||
export function setupVbenVxeTable(setupOptions: SetupVxeTable) {
|
||
const { configVxeTable, useVbenForm: useVbenFormFromParam } = setupOptions;
|
||
|
||
initVxeTable();
|
||
|
||
// 优先使用参数传入的 useVbenForm,context 注入在 useTableForm 中获取
|
||
if (useVbenFormFromParam) {
|
||
tableFormFactory = useVbenFormFromParam;
|
||
}
|
||
const { isDark, locale } = usePreferences();
|
||
|
||
const localMap = {
|
||
'zh-CN': normalizeVxeLocale(zhCN),
|
||
'en-US': normalizeVxeLocale(enUS),
|
||
};
|
||
|
||
watch(
|
||
[() => isDark.value, () => locale.value],
|
||
([isDarkValue, localeValue]) => {
|
||
VxeUI.setTheme(isDarkValue ? 'dark' : 'light');
|
||
VxeUI.setI18n(localeValue, localMap[localeValue]);
|
||
VxeUI.setLanguage(localeValue);
|
||
},
|
||
{
|
||
immediate: true,
|
||
},
|
||
);
|
||
|
||
extendsDefaultFormatter(VxeUI);
|
||
|
||
configVxeTable(VxeUI);
|
||
}
|