Commit 42908a453516457b3039c8f993a5b93f4ec158cb
Committed by
GitHub
1 parent
a0920d28
add TabsForm demo (#1914)
* chore: table size放到settings * chore(TableAction): 操作确认框增加placement属性支持 * chore(Form): 表单field支持a.b.c嵌套写法 * feat(Form): add TabsForm demo Co-authored-by: jinmao88 <50581550+jinmao88@users.noreply.github.com>
Showing
4 changed files
with
146 additions
and
0 deletions
src/locales/lang/en/routes/demo.ts
@@ -111,6 +111,7 @@ export default { | @@ -111,6 +111,7 @@ export default { | ||
111 | dynamicForm: 'Dynamic', | 111 | dynamicForm: 'Dynamic', |
112 | customerForm: 'Custom', | 112 | customerForm: 'Custom', |
113 | appendForm: 'Append', | 113 | appendForm: 'Append', |
114 | + tabsForm: 'TabsForm', | ||
114 | }, | 115 | }, |
115 | iframe: { | 116 | iframe: { |
116 | frame: 'External', | 117 | frame: 'External', |
src/locales/lang/zh-CN/routes/demo.ts
@@ -107,6 +107,7 @@ export default { | @@ -107,6 +107,7 @@ export default { | ||
107 | dynamicForm: '动态表单', | 107 | dynamicForm: '动态表单', |
108 | customerForm: '自定义组件', | 108 | customerForm: '自定义组件', |
109 | appendForm: '表单增删示例', | 109 | appendForm: '表单增删示例', |
110 | + tabsForm: '标签页+多级field', | ||
110 | }, | 111 | }, |
111 | iframe: { | 112 | iframe: { |
112 | frame: '外部页面', | 113 | frame: '外部页面', |
src/router/routes/modules/demo/comp.ts
@@ -98,6 +98,14 @@ const comp: AppRouteModule = { | @@ -98,6 +98,14 @@ const comp: AppRouteModule = { | ||
98 | title: t('routes.demo.form.appendForm'), | 98 | title: t('routes.demo.form.appendForm'), |
99 | }, | 99 | }, |
100 | }, | 100 | }, |
101 | + { | ||
102 | + path: 'tabsForm', | ||
103 | + name: 'tabsFormDemo', | ||
104 | + component: () => import('/@/views/demo/form/TabsForm.vue'), | ||
105 | + meta: { | ||
106 | + title: t('routes.demo.form.tabsForm'), | ||
107 | + }, | ||
108 | + }, | ||
101 | ], | 109 | ], |
102 | }, | 110 | }, |
103 | { | 111 | { |
src/views/demo/form/TabsForm.vue
0 → 100644
1 | +<template> | ||
2 | + <PageWrapper title="标签页+多级field表单" v-loading="loading"> | ||
3 | + <div class="mb-4"> | ||
4 | + <a-button @click="handleReset" class="mr-2"> 重置表单 </a-button> | ||
5 | + <a-button @click="handleSetValues" class="mr-2"> 设置默认值 </a-button> | ||
6 | + <a-button @click="handleSubmit" class="mr-2" type="primary"> 提交表单 </a-button> | ||
7 | + </div> | ||
8 | + <CollapseContainer title="标签页+多级field表单"> | ||
9 | + <Tabs v-model:activeKey="activeKey"> | ||
10 | + <TabPane | ||
11 | + v-for="item in tabsFormSchema" | ||
12 | + :key="item.key" | ||
13 | + v-bind="omit(item, ['Form', 'key'])" | ||
14 | + > | ||
15 | + <BasicForm @register="item.Form[0]" /> | ||
16 | + </TabPane> | ||
17 | + </Tabs> | ||
18 | + </CollapseContainer> | ||
19 | + </PageWrapper> | ||
20 | +</template> | ||
21 | + | ||
22 | +<script lang="ts"> | ||
23 | + import { ref, defineComponent } from 'vue'; | ||
24 | + import { Tabs } from 'ant-design-vue'; | ||
25 | + import { PageWrapper } from '/@/components/Page'; | ||
26 | + import { CollapseContainer } from '/@/components/Container'; | ||
27 | + import { useMessage } from '/@/hooks/web/useMessage'; | ||
28 | + import { omit } from 'lodash-es'; | ||
29 | + import { deepMerge } from '/@/utils'; | ||
30 | + import { BasicForm, FormSchema, useForm, FormProps, UseFormReturnType } from '/@/components/Form'; | ||
31 | + | ||
32 | + export default defineComponent({ | ||
33 | + name: 'TabsFormDemo', | ||
34 | + components: { Tabs, TabPane: Tabs.TabPane, PageWrapper, CollapseContainer, BasicForm }, | ||
35 | + setup() { | ||
36 | + type TabsFormType = { | ||
37 | + key: string; | ||
38 | + tab: string; | ||
39 | + forceRender?: boolean; | ||
40 | + Form: UseFormReturnType; | ||
41 | + }; | ||
42 | + | ||
43 | + const { createMessage } = useMessage(); | ||
44 | + const activeKey = ref('tabs2'); | ||
45 | + const loading = ref(false); | ||
46 | + const tabsFormSchema: TabsFormType[] = []; | ||
47 | + | ||
48 | + // 公共属性 | ||
49 | + const baseFormConfig: Partial<FormProps> = { | ||
50 | + showActionButtonGroup: false, | ||
51 | + labelWidth: 100, | ||
52 | + }; | ||
53 | + | ||
54 | + // 为每个字段模拟默认值, { tabs1: { field1: '', field2: '' }, tabs2: { field1: '' }, ... } | ||
55 | + const mockDefaultValue: Recordable = {}; | ||
56 | + | ||
57 | + // 模拟5个标签页 | ||
58 | + for (let i = 1; i <= 5; ++i) { | ||
59 | + const tabsKey = `tabs${i}`; | ||
60 | + | ||
61 | + // 每个标签页8个字段 | ||
62 | + const schemas: FormSchema[] = []; | ||
63 | + const row: Recordable = {}; | ||
64 | + | ||
65 | + for (let j = 1; j <= 8; ++j) { | ||
66 | + schemas.push({ | ||
67 | + field: `${tabsKey}.field${j}`, | ||
68 | + label: `${tabsKey}-field${j}`, | ||
69 | + component: 'Input', | ||
70 | + colProps: { span: 24 }, | ||
71 | + }); | ||
72 | + row[`field${j}`] = `field: ${tabsKey}.field${j}, default value`; | ||
73 | + } | ||
74 | + | ||
75 | + mockDefaultValue[tabsKey] = row; | ||
76 | + | ||
77 | + tabsFormSchema.push({ | ||
78 | + key: tabsKey, | ||
79 | + tab: tabsKey, | ||
80 | + forceRender: true, | ||
81 | + Form: useForm(Object.assign({ schemas }, baseFormConfig) as FormProps), | ||
82 | + }); | ||
83 | + } | ||
84 | + | ||
85 | + async function handleReset() { | ||
86 | + for (const item of tabsFormSchema) { | ||
87 | + const { resetFields } = item.Form[1]; | ||
88 | + await resetFields(); | ||
89 | + } | ||
90 | + } | ||
91 | + | ||
92 | + async function handleSubmit() { | ||
93 | + let lastKey = ''; | ||
94 | + loading.value = true; | ||
95 | + try { | ||
96 | + const values: Recordable = {}; | ||
97 | + for (const item of tabsFormSchema) { | ||
98 | + lastKey = item.key; | ||
99 | + const { validate, getFieldsValue } = item.Form[1]; | ||
100 | + await validate(); | ||
101 | + // 表单已支持多级key | ||
102 | + deepMerge(values, getFieldsValue()); | ||
103 | + } | ||
104 | + | ||
105 | + console.log('submit values: ', values); | ||
106 | + createMessage.success('提交成功!请打开控制台查看'); | ||
107 | + } catch (e) { | ||
108 | + // 验证失败或出错,切换到对应标签页 | ||
109 | + activeKey.value = lastKey; | ||
110 | + console.log(e); | ||
111 | + } finally { | ||
112 | + loading.value = false; | ||
113 | + } | ||
114 | + } | ||
115 | + | ||
116 | + async function handleSetValues() { | ||
117 | + console.log('默认值为: ', mockDefaultValue); | ||
118 | + for (const item of tabsFormSchema) { | ||
119 | + const { setFieldsValue } = item.Form[1]; | ||
120 | + await setFieldsValue(mockDefaultValue); | ||
121 | + } | ||
122 | + } | ||
123 | + return { | ||
124 | + omit, | ||
125 | + loading, | ||
126 | + activeKey, | ||
127 | + tabsFormSchema, | ||
128 | + handleReset, | ||
129 | + handleSubmit, | ||
130 | + handleSetValues, | ||
131 | + }; | ||
132 | + }, | ||
133 | + }); | ||
134 | +</script> | ||
135 | + | ||
136 | +<style scoped></style> |