form.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import type {
  2. BaseFormComponentType,
  3. VbenFormSchema as FormSchema,
  4. VbenFormProps,
  5. } from '@vben/common-ui';
  6. import { type Component, h, type SetupContext } from 'vue';
  7. import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
  8. import { $t } from '@vben/locales';
  9. import {
  10. AutoComplete,
  11. Button,
  12. Checkbox,
  13. CheckboxGroup,
  14. DatePicker,
  15. Divider,
  16. Input,
  17. InputNumber,
  18. InputPassword,
  19. Mentions,
  20. Radio,
  21. RadioGroup,
  22. RangePicker,
  23. Rate,
  24. Select,
  25. Space,
  26. Switch,
  27. Textarea,
  28. TimePicker,
  29. TreeSelect,
  30. Upload,
  31. } from 'ant-design-vue';
  32. // 这里需要自行根据业务组件库进行适配,需要用到的组件都需要在这里类型说明
  33. export type FormComponentType =
  34. | 'AutoComplete'
  35. | 'Checkbox'
  36. | 'CheckboxGroup'
  37. | 'DatePicker'
  38. | 'Divider'
  39. | 'Input'
  40. | 'InputNumber'
  41. | 'InputPassword'
  42. | 'Mentions'
  43. | 'Radio'
  44. | 'RadioGroup'
  45. | 'RangePicker'
  46. | 'Rate'
  47. | 'Select'
  48. | 'Space'
  49. | 'Switch'
  50. | 'Textarea'
  51. | 'TimePicker'
  52. | 'TreeSelect'
  53. | 'Upload'
  54. | BaseFormComponentType;
  55. const withDefaultPlaceholder = <T extends Component>(
  56. component: T,
  57. type: 'input' | 'select',
  58. ) => {
  59. return (props: any, { attrs, slots }: Omit<SetupContext, 'expose'>) => {
  60. const placeholder = props?.placeholder || $t(`placeholder.${type}`);
  61. return h(component, { ...props, attrs, placeholder }, slots);
  62. };
  63. };
  64. // 初始化表单组件,并注册到form组件内部
  65. setupVbenForm<FormComponentType>({
  66. components: {
  67. AutoComplete,
  68. Checkbox,
  69. CheckboxGroup,
  70. DatePicker,
  71. // 自定义默认的重置按钮
  72. DefaultResetActionButton: (props, { attrs, slots }) => {
  73. return h(Button, { ...props, attrs, type: 'default' }, slots);
  74. },
  75. // 自定义默认的提交按钮
  76. DefaultSubmitActionButton: (props, { attrs, slots }) => {
  77. return h(Button, { ...props, attrs, type: 'primary' }, slots);
  78. },
  79. Divider,
  80. Input: withDefaultPlaceholder(Input, 'input'),
  81. InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
  82. InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
  83. Mentions: withDefaultPlaceholder(Mentions, 'input'),
  84. Radio,
  85. RadioGroup,
  86. RangePicker,
  87. Rate,
  88. Select: withDefaultPlaceholder(Select, 'select'),
  89. Space,
  90. Switch,
  91. Textarea: withDefaultPlaceholder(Textarea, 'input'),
  92. TimePicker,
  93. TreeSelect: withDefaultPlaceholder(TreeSelect, 'select'),
  94. Upload,
  95. },
  96. config: {
  97. // ant design vue组件库默认都是 v-model:value
  98. baseModelPropName: 'value',
  99. // 一些组件是 v-model:checked 或者 v-model:fileList
  100. modelPropNameMap: {
  101. Checkbox: 'checked',
  102. Radio: 'checked',
  103. Switch: 'checked',
  104. Upload: 'fileList',
  105. },
  106. },
  107. defineRules: {
  108. // 输入项目必填国际化适配
  109. required: (value, _params, ctx) => {
  110. if (value === undefined || value === null || value.length === 0) {
  111. return $t('formRules.required', [ctx.label]);
  112. }
  113. return true;
  114. },
  115. // 选择项目必填国际化适配
  116. selectRequired: (value, _params, ctx) => {
  117. if (value === undefined || value === null) {
  118. return $t('formRules.selectRequired', [ctx.label]);
  119. }
  120. return true;
  121. },
  122. },
  123. });
  124. const useVbenForm = useForm<FormComponentType>;
  125. export { useVbenForm, z };
  126. export type VbenFormSchema = FormSchema<FormComponentType>;
  127. export type { VbenFormProps };