2024-09-22 14:16:06 +08:00
|
|
|
|
import type {
|
2026-07-22 19:47:07 +08:00
|
|
|
|
VbenFormProps as FormProps,
|
2024-09-22 14:16:06 +08:00
|
|
|
|
VbenFormSchema as FormSchema,
|
2026-07-22 19:47:07 +08:00
|
|
|
|
FormValues,
|
2024-09-22 14:16:06 +08:00
|
|
|
|
} from '@vben/common-ui';
|
|
|
|
|
|
|
2024-10-13 18:33:43 +08:00
|
|
|
|
import type { ComponentType } from './component';
|
2024-09-22 14:16:06 +08:00
|
|
|
|
|
|
|
|
|
|
import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
|
|
|
|
|
|
import { $t } from '@vben/locales';
|
|
|
|
|
|
|
2024-10-13 18:33:43 +08:00
|
|
|
|
import { initComponentAdapter } from './component';
|
2024-09-22 14:16:06 +08:00
|
|
|
|
|
2024-10-13 18:33:43 +08:00
|
|
|
|
initComponentAdapter();
|
2026-03-14 21:34:48 +08:00
|
|
|
|
|
2024-10-13 18:33:43 +08:00
|
|
|
|
setupVbenForm<ComponentType>({
|
2024-09-22 14:16:06 +08:00
|
|
|
|
config: {
|
|
|
|
|
|
baseModelPropName: 'value',
|
2024-10-13 18:33:43 +08:00
|
|
|
|
// naive-ui组件的空值为null,不能是undefined,否则重置表单时不生效
|
|
|
|
|
|
emptyStateValue: null,
|
2024-09-22 14:16:06 +08:00
|
|
|
|
modelPropNameMap: {
|
|
|
|
|
|
Checkbox: 'checked',
|
|
|
|
|
|
Radio: 'checked',
|
|
|
|
|
|
Switch: 'checked',
|
|
|
|
|
|
Upload: 'fileList',
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2026-07-22 19:47:07 +08:00
|
|
|
|
rules: {
|
2024-09-22 14:16:06 +08:00
|
|
|
|
required: (value, _params, ctx) => {
|
|
|
|
|
|
if (value === undefined || value === null || value.length === 0) {
|
2024-10-19 14:28:21 +08:00
|
|
|
|
return $t('ui.formRules.required', [ctx.label]);
|
2024-09-22 14:16:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
},
|
|
|
|
|
|
selectRequired: (value, _params, ctx) => {
|
|
|
|
|
|
if (value === undefined || value === null) {
|
2024-10-19 14:28:21 +08:00
|
|
|
|
return $t('ui.formRules.selectRequired', [ctx.label]);
|
2024-09-22 14:16:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-07-22 19:47:07 +08:00
|
|
|
|
function useVbenForm<TValues extends FormValues = FormValues>(
|
|
|
|
|
|
options: FormProps<ComponentType, Record<never, never>, TValues>,
|
|
|
|
|
|
) {
|
|
|
|
|
|
return useForm<TValues, ComponentType, Record<never, never>>(options);
|
|
|
|
|
|
}
|
2024-09-22 14:16:06 +08:00
|
|
|
|
|
|
|
|
|
|
export { useVbenForm, z };
|
|
|
|
|
|
|
2026-07-22 19:47:07 +08:00
|
|
|
|
export type VbenFormSchema<TValues extends FormValues = FormValues> =
|
|
|
|
|
|
FormSchema<ComponentType, Record<never, never>, TValues>;
|
|
|
|
|
|
export type VbenFormProps<TValues extends FormValues = FormValues> = FormProps<
|
|
|
|
|
|
ComponentType,
|
|
|
|
|
|
Record<never, never>,
|
|
|
|
|
|
TValues
|
|
|
|
|
|
>;
|