|
1 | 1 | import type {
|
2 |
| - BaseFormComponentType, |
3 | 2 | VbenFormSchema as FormSchema,
|
4 | 3 | VbenFormProps,
|
5 | 4 | } from '@vben/common-ui';
|
6 | 5 |
|
7 |
| -import type { Component, SetupContext } from 'vue'; |
8 |
| -import { h } from 'vue'; |
| 6 | +import type { ComponentType } from './component'; |
9 | 7 |
|
10 | 8 | import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui';
|
11 | 9 | import { $t } from '@vben/locales';
|
12 | 10 |
|
13 |
| -import { |
14 |
| - AutoComplete, |
15 |
| - Button, |
16 |
| - Checkbox, |
17 |
| - CheckboxGroup, |
18 |
| - DatePicker, |
19 |
| - Divider, |
20 |
| - Input, |
21 |
| - InputNumber, |
22 |
| - InputPassword, |
23 |
| - Mentions, |
24 |
| - Radio, |
25 |
| - RadioGroup, |
26 |
| - RangePicker, |
27 |
| - Rate, |
28 |
| - Select, |
29 |
| - Space, |
30 |
| - Switch, |
31 |
| - Textarea, |
32 |
| - TimePicker, |
33 |
| - TreeSelect, |
34 |
| - Upload, |
35 |
| -} from 'ant-design-vue'; |
36 |
| - |
37 |
| -// 这里需要自行根据业务组件库进行适配,需要用到的组件都需要在这里类型说明 |
38 |
| -export type FormComponentType = |
39 |
| - | 'AutoComplete' |
40 |
| - | 'Checkbox' |
41 |
| - | 'CheckboxGroup' |
42 |
| - | 'DatePicker' |
43 |
| - | 'Divider' |
44 |
| - | 'Input' |
45 |
| - | 'InputNumber' |
46 |
| - | 'InputPassword' |
47 |
| - | 'Mentions' |
48 |
| - | 'Radio' |
49 |
| - | 'RadioGroup' |
50 |
| - | 'RangePicker' |
51 |
| - | 'Rate' |
52 |
| - | 'Select' |
53 |
| - | 'Space' |
54 |
| - | 'Switch' |
55 |
| - | 'Textarea' |
56 |
| - | 'TimePicker' |
57 |
| - | 'TreeSelect' |
58 |
| - | 'Upload' |
59 |
| - | BaseFormComponentType; |
60 |
| - |
61 |
| -const withDefaultPlaceholder = <T extends Component>( |
62 |
| - component: T, |
63 |
| - type: 'input' | 'select', |
64 |
| -) => { |
65 |
| - return (props: any, { attrs, slots }: Omit<SetupContext, 'expose'>) => { |
66 |
| - const placeholder = props?.placeholder || $t(`placeholder.${type}`); |
67 |
| - return h(component, { ...props, ...attrs, placeholder }, slots); |
68 |
| - }; |
69 |
| -}; |
70 |
| - |
71 |
| -// 初始化表单组件,并注册到form组件内部 |
72 |
| -setupVbenForm<FormComponentType>({ |
73 |
| - components: { |
74 |
| - AutoComplete, |
75 |
| - Checkbox, |
76 |
| - CheckboxGroup, |
77 |
| - DatePicker, |
78 |
| - // 自定义默认的重置按钮 |
79 |
| - DefaultResetActionButton: (props, { attrs, slots }) => { |
80 |
| - return h(Button, { ...props, attrs, type: 'default' }, slots); |
81 |
| - }, |
82 |
| - // 自定义默认的提交按钮 |
83 |
| - DefaultSubmitActionButton: (props, { attrs, slots }) => { |
84 |
| - return h(Button, { ...props, attrs, type: 'primary' }, slots); |
85 |
| - }, |
86 |
| - Divider, |
87 |
| - Input: withDefaultPlaceholder(Input, 'input'), |
88 |
| - InputNumber: withDefaultPlaceholder(InputNumber, 'input'), |
89 |
| - InputPassword: withDefaultPlaceholder(InputPassword, 'input'), |
90 |
| - Mentions: withDefaultPlaceholder(Mentions, 'input'), |
91 |
| - Radio, |
92 |
| - RadioGroup, |
93 |
| - RangePicker, |
94 |
| - Rate, |
95 |
| - Select: withDefaultPlaceholder(Select, 'select'), |
96 |
| - Space, |
97 |
| - Switch, |
98 |
| - Textarea: withDefaultPlaceholder(Textarea, 'input'), |
99 |
| - TimePicker, |
100 |
| - TreeSelect: withDefaultPlaceholder(TreeSelect, 'select'), |
101 |
| - Upload, |
102 |
| - }, |
| 11 | +setupVbenForm<ComponentType>({ |
103 | 12 | config: {
|
104 | 13 | // ant design vue组件库默认都是 v-model:value
|
105 | 14 | baseModelPropName: 'value',
|
@@ -130,9 +39,9 @@ setupVbenForm<FormComponentType>({
|
130 | 39 | },
|
131 | 40 | });
|
132 | 41 |
|
133 |
| -const useVbenForm = useForm<FormComponentType>; |
| 42 | +const useVbenForm = useForm<ComponentType>; |
134 | 43 |
|
135 | 44 | export { useVbenForm, z };
|
136 | 45 |
|
137 |
| -export type VbenFormSchema = FormSchema<FormComponentType>; |
| 46 | +export type VbenFormSchema = FormSchema<ComponentType>; |
138 | 47 | export type { VbenFormProps };
|
0 commit comments