form.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. ElButton,
  12. ElCheckbox,
  13. ElCheckboxGroup,
  14. ElDivider,
  15. ElInput,
  16. ElInputNumber,
  17. ElRadioGroup,
  18. ElSelect,
  19. ElSpace,
  20. ElSwitch,
  21. ElTimePicker,
  22. ElTreeSelect,
  23. ElUpload,
  24. } from 'element-plus';
  25. // 业务表单组件适配
  26. export type FormComponentType =
  27. | 'Checkbox'
  28. | 'CheckboxGroup'
  29. | 'DatePicker'
  30. | 'Divider'
  31. | 'Input'
  32. | 'InputNumber'
  33. | 'RadioGroup'
  34. | 'Select'
  35. | 'Space'
  36. | 'Switch'
  37. | 'TimePicker'
  38. | 'TreeSelect'
  39. | 'Upload'
  40. | BaseFormComponentType;
  41. const withDefaultPlaceholder = <T extends Component>(
  42. component: T,
  43. type: 'input' | 'select',
  44. ) => {
  45. return (props: any, { attrs, slots }: Omit<SetupContext, 'expose'>) => {
  46. const placeholder = props?.placeholder || $t(`placeholder.${type}`);
  47. return h(component, { ...props, ...attrs, placeholder }, slots);
  48. };
  49. };
  50. // 初始化表单组件,并注册到form组件内部
  51. setupVbenForm<FormComponentType>({
  52. components: {
  53. Checkbox: ElCheckbox,
  54. CheckboxGroup: ElCheckboxGroup,
  55. // 自定义默认的重置按钮
  56. DefaultResetActionButton: (props, { attrs, slots }) => {
  57. return h(ElButton, { ...props, attrs, type: 'info' }, slots);
  58. },
  59. // 自定义默认的提交按钮
  60. DefaultSubmitActionButton: (props, { attrs, slots }) => {
  61. return h(ElButton, { ...props, attrs, type: 'primary' }, slots);
  62. },
  63. Divider: ElDivider,
  64. Input: withDefaultPlaceholder(ElInput, 'input'),
  65. InputNumber: withDefaultPlaceholder(ElInputNumber, 'input'),
  66. RadioGroup: ElRadioGroup,
  67. Select: withDefaultPlaceholder(ElSelect, 'select'),
  68. Space: ElSpace,
  69. Switch: ElSwitch,
  70. TimePicker: ElTimePicker,
  71. TreeSelect: withDefaultPlaceholder(ElTreeSelect, 'select'),
  72. Upload: ElUpload,
  73. },
  74. config: {
  75. modelPropNameMap: {
  76. Upload: 'fileList',
  77. },
  78. },
  79. defineRules: {
  80. required: (value, _params, ctx) => {
  81. if (value === undefined || value === null || value.length === 0) {
  82. return $t('formRules.required', [ctx.label]);
  83. }
  84. return true;
  85. },
  86. selectRequired: (value, _params, ctx) => {
  87. if (value === undefined || value === null) {
  88. return $t('formRules.selectRequired', [ctx.label]);
  89. }
  90. return true;
  91. },
  92. },
  93. });
  94. const useVbenForm = useForm<FormComponentType>;
  95. export { useVbenForm, z };
  96. export type VbenFormSchema = FormSchema<FormComponentType>;
  97. export type { VbenFormProps };