2026-04-04 23:40:27 +08:00
|
|
|
import type {
|
|
|
|
|
VbenFormProps as FormProps,
|
|
|
|
|
VbenFormSchema as FormSchema,
|
2026-07-22 19:47:07 +08:00
|
|
|
FormValues,
|
2026-04-04 23:40:27 +08:00
|
|
|
} from '@vben/common-ui';
|
2024-09-10 21:48:51 +08:00
|
|
|
|
2026-04-04 23:40:27 +08:00
|
|
|
import type { ComponentPropsMap, ComponentType } from './component';
|
2024-09-10 21:48:51 +08:00
|
|
|
|
|
|
|
|
import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
|
|
|
|
|
import { $t } from '@vben/locales';
|
|
|
|
|
|
2025-05-23 15:21:09 +08:00
|
|
|
async function initSetupVbenForm() {
|
|
|
|
|
setupVbenForm<ComponentType>({
|
|
|
|
|
config: {
|
|
|
|
|
// ant design vue组件库默认都是 v-model:value
|
|
|
|
|
baseModelPropName: 'value',
|
2024-09-22 14:16:06 +08:00
|
|
|
|
2025-05-23 15:21:09 +08:00
|
|
|
// 一些组件是 v-model:checked 或者 v-model:fileList
|
|
|
|
|
modelPropNameMap: {
|
|
|
|
|
Checkbox: 'checked',
|
|
|
|
|
Radio: 'checked',
|
|
|
|
|
Switch: 'checked',
|
|
|
|
|
Upload: 'fileList',
|
|
|
|
|
},
|
2024-09-10 21:48:51 +08:00
|
|
|
},
|
2026-07-22 19:47:07 +08:00
|
|
|
rules: {
|
2025-05-23 15:21:09 +08:00
|
|
|
// 输入项目必填国际化适配
|
|
|
|
|
required: (value, _params, ctx) => {
|
|
|
|
|
if (value === undefined || value === null || value.length === 0) {
|
|
|
|
|
return $t('ui.formRules.required', [ctx.label]);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
// 选择项目必填国际化适配
|
|
|
|
|
selectRequired: (value, _params, ctx) => {
|
|
|
|
|
if (value === undefined || value === null) {
|
|
|
|
|
return $t('ui.formRules.selectRequired', [ctx.label]);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
2024-09-10 21:48:51 +08:00
|
|
|
},
|
2025-05-23 15:21:09 +08:00
|
|
|
});
|
|
|
|
|
}
|
2024-09-10 21:48:51 +08:00
|
|
|
|
2026-07-23 15:09:31 +08:00
|
|
|
function useVbenForm<
|
|
|
|
|
TFormValues extends FormValues = FormValues,
|
|
|
|
|
TSubmitValues extends FormValues = TFormValues,
|
|
|
|
|
>(
|
|
|
|
|
options: FormProps<
|
|
|
|
|
ComponentType,
|
|
|
|
|
ComponentPropsMap,
|
|
|
|
|
TFormValues,
|
|
|
|
|
TSubmitValues
|
|
|
|
|
>,
|
2026-07-22 19:47:07 +08:00
|
|
|
) {
|
2026-07-23 15:09:31 +08:00
|
|
|
return useForm<TFormValues, ComponentType, ComponentPropsMap, TSubmitValues>(
|
|
|
|
|
options,
|
|
|
|
|
);
|
2026-07-22 19:47:07 +08:00
|
|
|
}
|
2024-09-10 21:48:51 +08:00
|
|
|
|
2025-05-23 15:21:09 +08:00
|
|
|
export { initSetupVbenForm, useVbenForm, z };
|
2024-09-10 21:48:51 +08:00
|
|
|
|
2026-07-22 19:47:07 +08:00
|
|
|
export type VbenFormSchema<TValues extends FormValues = FormValues> =
|
|
|
|
|
FormSchema<ComponentType, ComponentPropsMap, TValues>;
|
2026-07-23 15:09:31 +08:00
|
|
|
export type VbenFormProps<
|
|
|
|
|
TFormValues extends FormValues = FormValues,
|
|
|
|
|
TSubmitValues extends FormValues = TFormValues,
|
|
|
|
|
> = FormProps<ComponentType, ComponentPropsMap, TFormValues, TSubmitValues>;
|