form.ts 3.4 KB

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