form.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import type {
  2. VbenFormSchema as FormSchema,
  3. VbenFormProps,
  4. } from '@vben/common-ui';
  5. import type { ComponentType } from './component';
  6. import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
  7. import { $t } from '@vben/locales';
  8. async function initSetupVbenForm() {
  9. setupVbenForm<ComponentType>({
  10. config: {
  11. // tdesign组件库默认都是 v-model:value
  12. baseModelPropName: 'value',
  13. // 一些组件是 v-model:checked 或者 v-model:fileList
  14. modelPropNameMap: {
  15. Checkbox: 'checked',
  16. Radio: 'checked',
  17. Switch: 'checked',
  18. Upload: 'fileList',
  19. },
  20. },
  21. defineRules: {
  22. // 输入项目必填国际化适配
  23. required: (value, _params, ctx) => {
  24. if (value === undefined || value === null || value.length === 0) {
  25. return $t('ui.formRules.required', [ctx.label]);
  26. }
  27. return true;
  28. },
  29. // 选择项目必填国际化适配
  30. selectRequired: (value, _params, ctx) => {
  31. if (value === undefined || value === null) {
  32. return $t('ui.formRules.selectRequired', [ctx.label]);
  33. }
  34. return true;
  35. },
  36. },
  37. });
  38. }
  39. const useVbenForm = useForm<ComponentType>;
  40. export { initSetupVbenForm, useVbenForm, z };
  41. export type VbenFormSchema = FormSchema<ComponentType>;
  42. export type { VbenFormProps };