Commit 8284d106e2ea1c286e634904bac270dc13734468
Committed by
GitHub
1 parent
de305ecd
refactor<useForm>: 重构案例 (#2577)
Showing
1 changed file
with
208 additions
and
112 deletions
src/views/demo/form/UseForm.vue
1 | <template> | 1 | <template> |
2 | <PageWrapper title="UseForm操作示例"> | 2 | <PageWrapper title="UseForm操作示例"> |
3 | - <div class="mb-4"> | ||
4 | - <a-button @click="setProps({ labelWidth: 150 })" class="mr-2"> 更改labelWidth </a-button> | ||
5 | - <a-button @click="setProps({ labelWidth: 120 })" class="mr-2"> 还原labelWidth </a-button> | ||
6 | - <a-button @click="setProps({ size: 'large' })" class="mr-2"> 更改Size </a-button> | ||
7 | - <a-button @click="setProps({ size: 'default' })" class="mr-2"> 还原Size </a-button> | ||
8 | - <a-button @click="setProps({ disabled: true })" class="mr-2"> 禁用表单 </a-button> | ||
9 | - <a-button @click="setProps({ disabled: false })" class="mr-2"> 解除禁用 </a-button> | ||
10 | - <a-button @click="setProps({ compact: true })" class="mr-2"> 紧凑表单 </a-button> | ||
11 | - <a-button @click="setProps({ compact: false })" class="mr-2"> 还原正常间距 </a-button> | ||
12 | - <a-button @click="setProps({ actionColOptions: { span: 8 } })" class="mr-2"> | ||
13 | - 操作按钮位置 | ||
14 | - </a-button> | ||
15 | - </div> | ||
16 | - <div class="mb-4"> | ||
17 | - <a-button @click="setProps({ showActionButtonGroup: false })" class="mr-2"> | ||
18 | - 隐藏操作按钮 | ||
19 | - </a-button> | ||
20 | - <a-button @click="setProps({ showActionButtonGroup: true })" class="mr-2"> | ||
21 | - 显示操作按钮 | ||
22 | - </a-button> | ||
23 | - <a-button @click="setProps({ showResetButton: false })" class="mr-2"> 隐藏重置按钮 </a-button> | ||
24 | - <a-button @click="setProps({ showResetButton: true })" class="mr-2"> 显示重置按钮 </a-button> | ||
25 | - <a-button @click="setProps({ showSubmitButton: false })" class="mr-2"> | ||
26 | - 隐藏查询按钮 | ||
27 | - </a-button> | ||
28 | - <a-button @click="setProps({ showSubmitButton: true })" class="mr-2"> 显示查询按钮 </a-button> | ||
29 | - <a-button | ||
30 | - @click=" | ||
31 | - setProps({ | ||
32 | - resetButtonOptions: { | ||
33 | - disabled: true, | ||
34 | - text: '重置New', | ||
35 | - }, | ||
36 | - }) | ||
37 | - " | ||
38 | - class="mr-2" | ||
39 | - > | 3 | + <Space class="mb-4"> |
4 | + <a-button type="primary" @click="showDrawer"> 更改设置 </a-button> | ||
5 | + <a-button @click="setProps({ resetButtonOptions: { disabled: true, text: '重置New' } })"> | ||
40 | 修改重置按钮 | 6 | 修改重置按钮 |
41 | </a-button> | 7 | </a-button> |
42 | - <a-button | ||
43 | - @click=" | ||
44 | - setProps({ | ||
45 | - submitButtonOptions: { | ||
46 | - disabled: true, | ||
47 | - loading: true, | ||
48 | - }, | ||
49 | - }) | ||
50 | - " | ||
51 | - class="mr-2" | ||
52 | - > | 8 | + <a-button @click="setProps({ submitButtonOptions: { disabled: true, loading: true } })"> |
53 | 修改查询按钮 | 9 | 修改查询按钮 |
54 | </a-button> | 10 | </a-button> |
55 | <a-button @click="handleLoad" class="mr-2"> 联动回显 </a-button> | 11 | <a-button @click="handleLoad" class="mr-2"> 联动回显 </a-button> |
56 | - </div> | 12 | + </Space> |
13 | + | ||
14 | + <Drawer v-model:visible="visible" title="更改设置" placement="right"> | ||
15 | + <BasicForm ref="settingFormRef" @register="registerSetting" @submit="handleSubmitSetting" /> | ||
16 | + <template #extra> | ||
17 | + <Space> | ||
18 | + <a-button @click="resetSettings">重置设置</a-button> | ||
19 | + <a-button type="primary" @click="onSettings">应用</a-button> | ||
20 | + </Space> | ||
21 | + </template> | ||
22 | + </Drawer> | ||
23 | + | ||
57 | <CollapseContainer title="useForm示例"> | 24 | <CollapseContainer title="useForm示例"> |
58 | <BasicForm @register="register" @submit="handleSubmit" /> | 25 | <BasicForm @register="register" @submit="handleSubmit" /> |
59 | </CollapseContainer> | 26 | </CollapseContainer> |
60 | </PageWrapper> | 27 | </PageWrapper> |
61 | </template> | 28 | </template> |
29 | + | ||
62 | <script lang="ts"> | 30 | <script lang="ts"> |
63 | - import { defineComponent } from 'vue'; | ||
64 | - import { BasicForm, FormSchema, useForm } from '/@/components/Form/index'; | ||
65 | - import { CollapseContainer } from '/@/components/Container/index'; | ||
66 | - import { useMessage } from '/@/hooks/web/useMessage'; | 31 | + import { defineComponent, ref } from 'vue'; |
32 | + import { Drawer, Space } from 'ant-design-vue'; | ||
33 | + import { BasicForm, FormSchema, useForm } from '/@/components/Form'; | ||
34 | + import { CollapseContainer } from '/@/components/Container'; | ||
67 | import { PageWrapper } from '/@/components/Page'; | 35 | import { PageWrapper } from '/@/components/Page'; |
68 | import { areaRecord } from '/@/api/demo/cascader'; | 36 | import { areaRecord } from '/@/api/demo/cascader'; |
69 | 37 | ||
38 | + const sizeList = [ | ||
39 | + { value: 'large', label: 'large' }, | ||
40 | + { value: 'middle', label: 'middle' }, | ||
41 | + { value: 'small', label: 'small' }, | ||
42 | + { value: 'default', label: 'defualt' }, | ||
43 | + ]; | ||
44 | + | ||
70 | const schemas: FormSchema[] = [ | 45 | const schemas: FormSchema[] = [ |
71 | { | 46 | { |
72 | field: 'field1', | 47 | field: 'field1', |
73 | component: 'Input', | 48 | component: 'Input', |
74 | label: '字段1', | 49 | label: '字段1', |
75 | - colProps: { | ||
76 | - span: 8, | ||
77 | - }, | 50 | + colProps: { span: 8 }, |
78 | componentProps: { | 51 | componentProps: { |
79 | placeholder: '自定义placeholder', | 52 | placeholder: '自定义placeholder', |
80 | onChange: (e: any) => { | 53 | onChange: (e: any) => { |
@@ -86,45 +59,29 @@ | @@ -86,45 +59,29 @@ | ||
86 | field: 'field2', | 59 | field: 'field2', |
87 | component: 'Input', | 60 | component: 'Input', |
88 | label: '字段2', | 61 | label: '字段2', |
89 | - colProps: { | ||
90 | - span: 8, | ||
91 | - }, | 62 | + colProps: { span: 8 }, |
92 | }, | 63 | }, |
93 | { | 64 | { |
94 | field: 'field3', | 65 | field: 'field3', |
95 | component: 'DatePicker', | 66 | component: 'DatePicker', |
96 | label: '字段3', | 67 | label: '字段3', |
97 | - colProps: { | ||
98 | - span: 8, | ||
99 | - }, | 68 | + colProps: { span: 8 }, |
100 | }, | 69 | }, |
101 | { | 70 | { |
102 | field: 'fieldTime', | 71 | field: 'fieldTime', |
103 | component: 'RangePicker', | 72 | component: 'RangePicker', |
104 | label: '时间字段', | 73 | label: '时间字段', |
105 | - colProps: { | ||
106 | - span: 8, | ||
107 | - }, | 74 | + colProps: { span: 8 }, |
108 | }, | 75 | }, |
109 | { | 76 | { |
110 | field: 'field4', | 77 | field: 'field4', |
111 | component: 'Select', | 78 | component: 'Select', |
112 | label: '字段4', | 79 | label: '字段4', |
113 | - colProps: { | ||
114 | - span: 8, | ||
115 | - }, | 80 | + colProps: { span: 8 }, |
116 | componentProps: { | 81 | componentProps: { |
117 | options: [ | 82 | options: [ |
118 | - { | ||
119 | - label: '选项1', | ||
120 | - value: '1', | ||
121 | - key: '1', | ||
122 | - }, | ||
123 | - { | ||
124 | - label: '选项2', | ||
125 | - value: '2', | ||
126 | - key: '2', | ||
127 | - }, | 83 | + { label: '选项1', value: '1', key: '1' }, |
84 | + { label: '选项2', value: '2', key: '2' }, | ||
128 | ], | 85 | ], |
129 | }, | 86 | }, |
130 | }, | 87 | }, |
@@ -137,14 +94,8 @@ | @@ -137,14 +94,8 @@ | ||
137 | }, | 94 | }, |
138 | componentProps: { | 95 | componentProps: { |
139 | options: [ | 96 | options: [ |
140 | - { | ||
141 | - label: '选项1', | ||
142 | - value: '1', | ||
143 | - }, | ||
144 | - { | ||
145 | - label: '选项2', | ||
146 | - value: '2', | ||
147 | - }, | 97 | + { label: '选项1', value: '1' }, |
98 | + { label: '选项2', value: '2' }, | ||
148 | ], | 99 | ], |
149 | }, | 100 | }, |
150 | }, | 101 | }, |
@@ -152,19 +103,11 @@ | @@ -152,19 +103,11 @@ | ||
152 | field: 'field7', | 103 | field: 'field7', |
153 | component: 'RadioGroup', | 104 | component: 'RadioGroup', |
154 | label: '字段7', | 105 | label: '字段7', |
155 | - colProps: { | ||
156 | - span: 8, | ||
157 | - }, | 106 | + colProps: { span: 8 }, |
158 | componentProps: { | 107 | componentProps: { |
159 | options: [ | 108 | options: [ |
160 | - { | ||
161 | - label: '选项1', | ||
162 | - value: '1', | ||
163 | - }, | ||
164 | - { | ||
165 | - label: '选项2', | ||
166 | - value: '2', | ||
167 | - }, | 109 | + { label: '选项1', value: '1' }, |
110 | + { label: '选项2', value: '2' }, | ||
168 | ], | 111 | ], |
169 | }, | 112 | }, |
170 | }, | 113 | }, |
@@ -172,9 +115,7 @@ | @@ -172,9 +115,7 @@ | ||
172 | field: 'field8', | 115 | field: 'field8', |
173 | component: 'ApiCascader', | 116 | component: 'ApiCascader', |
174 | label: '联动', | 117 | label: '联动', |
175 | - colProps: { | ||
176 | - span: 8, | ||
177 | - }, | 118 | + colProps: { span: 8 }, |
178 | componentProps: { | 119 | componentProps: { |
179 | api: areaRecord, | 120 | api: areaRecord, |
180 | apiParamKey: 'parentCode', | 121 | apiParamKey: 'parentCode', |
@@ -193,9 +134,7 @@ | @@ -193,9 +134,7 @@ | ||
193 | field: 'field9', | 134 | field: 'field9', |
194 | component: 'ApiCascader', | 135 | component: 'ApiCascader', |
195 | label: '联动回显', | 136 | label: '联动回显', |
196 | - colProps: { | ||
197 | - span: 8, | ||
198 | - }, | 137 | + colProps: { span: 8 }, |
199 | componentProps: { | 138 | componentProps: { |
200 | api: areaRecord, | 139 | api: areaRecord, |
201 | apiParamKey: 'parentCode', | 140 | apiParamKey: 'parentCode', |
@@ -212,17 +151,161 @@ | @@ -212,17 +151,161 @@ | ||
212 | }, | 151 | }, |
213 | ]; | 152 | ]; |
214 | 153 | ||
154 | + const formSchemas: FormSchema[] = [ | ||
155 | + { | ||
156 | + field: '', | ||
157 | + component: 'Divider', | ||
158 | + label: '基础属性', | ||
159 | + colProps: { span: 24 }, | ||
160 | + componentProps: { | ||
161 | + orientation: 'center', | ||
162 | + }, | ||
163 | + }, | ||
164 | + { | ||
165 | + field: 'labelWidth', | ||
166 | + defaultValue: 120, | ||
167 | + component: 'InputNumber', | ||
168 | + label: 'labelWidth', | ||
169 | + colProps: { span: 24 }, | ||
170 | + componentProps: { | ||
171 | + size: 'small', | ||
172 | + }, | ||
173 | + }, | ||
174 | + { | ||
175 | + field: 'size', | ||
176 | + defaultValue: 'default', | ||
177 | + component: 'Select', | ||
178 | + label: 'size', | ||
179 | + colProps: { span: 24 }, | ||
180 | + componentProps: { | ||
181 | + options: sizeList, | ||
182 | + size: 'small', | ||
183 | + }, | ||
184 | + }, | ||
185 | + { | ||
186 | + field: 'disabled', | ||
187 | + defaultValue: false, | ||
188 | + component: 'Switch', | ||
189 | + label: 'disabled', | ||
190 | + colProps: { span: 24 }, | ||
191 | + componentProps: { | ||
192 | + size: 'small', | ||
193 | + }, | ||
194 | + }, | ||
195 | + { | ||
196 | + field: 'compact', | ||
197 | + defaultValue: false, | ||
198 | + component: 'Switch', | ||
199 | + label: 'compact', | ||
200 | + colProps: { span: 24 }, | ||
201 | + componentProps: { | ||
202 | + size: 'small', | ||
203 | + }, | ||
204 | + }, | ||
205 | + { | ||
206 | + field: '', | ||
207 | + component: 'Divider', | ||
208 | + label: '网格布局', | ||
209 | + colProps: { span: 24 }, | ||
210 | + componentProps: { | ||
211 | + orientation: 'center', | ||
212 | + }, | ||
213 | + }, | ||
214 | + { | ||
215 | + field: 'actionColOptions.span', | ||
216 | + component: 'Slider', | ||
217 | + defaultValue: 24, | ||
218 | + label: 'span', | ||
219 | + colProps: { span: 24 }, | ||
220 | + componentProps: { | ||
221 | + min: 1, | ||
222 | + max: 24, | ||
223 | + }, | ||
224 | + }, | ||
225 | + { | ||
226 | + field: '', | ||
227 | + component: 'Divider', | ||
228 | + label: '操作按钮', | ||
229 | + colProps: { span: 24 }, | ||
230 | + componentProps: { | ||
231 | + orientation: 'center', | ||
232 | + }, | ||
233 | + }, | ||
234 | + { | ||
235 | + field: 'showActionButtonGroup', | ||
236 | + defaultValue: true, | ||
237 | + component: 'Switch', | ||
238 | + label: '操作按钮', | ||
239 | + colProps: { span: 24 }, | ||
240 | + componentProps: ({ formActionType }) => { | ||
241 | + return { | ||
242 | + size: 'small', | ||
243 | + onChange: async (val: boolean) => { | ||
244 | + formActionType.updateSchema([ | ||
245 | + { field: 'showResetButton', componentProps: { disabled: !val } }, | ||
246 | + { | ||
247 | + field: 'showSubmitButton', | ||
248 | + componentProps: { disabled: !val }, | ||
249 | + }, | ||
250 | + ]); | ||
251 | + }, | ||
252 | + }; | ||
253 | + }, | ||
254 | + }, | ||
255 | + { | ||
256 | + field: 'showResetButton', | ||
257 | + defaultValue: true, | ||
258 | + component: 'Switch', | ||
259 | + label: '重置按钮', | ||
260 | + colProps: { span: 24 }, | ||
261 | + componentProps: { | ||
262 | + size: 'small', | ||
263 | + }, | ||
264 | + }, | ||
265 | + { | ||
266 | + field: 'showSubmitButton', | ||
267 | + defaultValue: true, | ||
268 | + component: 'Switch', | ||
269 | + label: '提交按钮', | ||
270 | + colProps: { span: 24 }, | ||
271 | + componentProps: { | ||
272 | + size: 'small', | ||
273 | + }, | ||
274 | + }, | ||
275 | + ]; | ||
276 | + | ||
215 | export default defineComponent({ | 277 | export default defineComponent({ |
216 | - components: { BasicForm, CollapseContainer, PageWrapper }, | 278 | + components: { |
279 | + BasicForm, | ||
280 | + CollapseContainer, | ||
281 | + PageWrapper, | ||
282 | + Drawer, | ||
283 | + Space, | ||
284 | + }, | ||
217 | setup() { | 285 | setup() { |
218 | - const { createMessage } = useMessage(); | 286 | + const visible = ref<boolean>(false); |
287 | + const settingFormRef = ref(); | ||
288 | + const [registerSetting] = useForm({ | ||
289 | + labelWidth: 80, | ||
290 | + schemas: formSchemas, | ||
291 | + compact: true, | ||
292 | + actionColOptions: { span: 24 }, | ||
293 | + showActionButtonGroup: false, | ||
294 | + }); | ||
295 | + | ||
296 | + const resetSettings = async () => { | ||
297 | + await settingFormRef.value?.resetFields(); | ||
298 | + }; | ||
299 | + | ||
300 | + const handleSubmitSetting = async (values: Recordable) => { | ||
301 | + await setProps(values); | ||
302 | + visible.value = false; | ||
303 | + }; | ||
219 | 304 | ||
220 | const [register, { setProps, setFieldsValue, updateSchema }] = useForm({ | 305 | const [register, { setProps, setFieldsValue, updateSchema }] = useForm({ |
221 | labelWidth: 120, | 306 | labelWidth: 120, |
222 | schemas, | 307 | schemas, |
223 | - actionColOptions: { | ||
224 | - span: 24, | ||
225 | - }, | 308 | + actionColOptions: { span: 24 }, |
226 | fieldMapToTime: [['fieldTime', ['startTime', 'endTime'], 'YYYY-MM']], | 309 | fieldMapToTime: [['fieldTime', ['startTime', 'endTime'], 'YYYY-MM']], |
227 | }); | 310 | }); |
228 | 311 | ||
@@ -249,19 +332,32 @@ | @@ -249,19 +332,32 @@ | ||
249 | displayRenderArray: [province, city, district], | 332 | displayRenderArray: [province, city, district], |
250 | }, | 333 | }, |
251 | }); | 334 | }); |
252 | - await setFieldsValue({ | ||
253 | - field9, | ||
254 | - }); | 335 | + await setFieldsValue({ field9 }); |
255 | } | 336 | } |
256 | 337 | ||
338 | + const showDrawer = () => { | ||
339 | + visible.value = true; | ||
340 | + }; | ||
341 | + | ||
342 | + const onSettings = () => { | ||
343 | + settingFormRef.value?.submit(); | ||
344 | + }; | ||
345 | + | ||
257 | return { | 346 | return { |
258 | register, | 347 | register, |
259 | schemas, | 348 | schemas, |
260 | handleSubmit: (values: Recordable) => { | 349 | handleSubmit: (values: Recordable) => { |
261 | - createMessage.success('click search,values:' + JSON.stringify(values)); | 350 | + console.log(values); |
262 | }, | 351 | }, |
263 | setProps, | 352 | setProps, |
264 | handleLoad, | 353 | handleLoad, |
354 | + visible, | ||
355 | + showDrawer, | ||
356 | + settingFormRef, | ||
357 | + onSettings, | ||
358 | + resetSettings, | ||
359 | + registerSetting, | ||
360 | + handleSubmitSetting, | ||
265 | }; | 361 | }; |
266 | }, | 362 | }, |
267 | }); | 363 | }); |