1
0

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. import { initComponentAdapter } from './component';
  9. initComponentAdapter();
  10. setupVbenForm<ComponentType>({
  11. config: {
  12. baseModelPropName: 'value',
  13. // naive-ui组件不接受onChang事件,所以需要禁用
  14. disabledOnChangeListener: true,
  15. // naive-ui组件的空值为null,不能是undefined,否则重置表单时不生效
  16. emptyStateValue: null,
  17. modelPropNameMap: {
  18. Checkbox: 'checked',
  19. Radio: 'checked',
  20. Switch: 'checked',
  21. Upload: 'fileList',
  22. },
  23. },
  24. defineRules: {
  25. required: (value, _params, ctx) => {
  26. if (value === undefined || value === null || value.length === 0) {
  27. return $t('ui.formRules.required', [ctx.label]);
  28. }
  29. return true;
  30. },
  31. selectRequired: (value, _params, ctx) => {
  32. if (value === undefined || value === null) {
  33. return $t('ui.formRules.selectRequired', [ctx.label]);
  34. }
  35. return true;
  36. },
  37. },
  38. });
  39. const useVbenForm = useForm<ComponentType>;
  40. export { useVbenForm, z };
  41. export type VbenFormSchema = FormSchema<ComponentType>;
  42. export type { VbenFormProps };