Commit 85b92a9add2b560559b4ef60ecf93e22f5941edb
Committed by
GitHub
1 parent
bd83eccd
perf: add AppendFormDemo (#503)
Co-authored-by: haha <admin@qq.com>
Showing
5 changed files
with
131 additions
and
0 deletions
src/locales/lang/en/routes/demo/form.ts
src/locales/lang/zh_CN/routes/demo/form.ts
src/router/menus/modules/demo/comp.ts
@@ -47,6 +47,10 @@ const menu: MenuModule = { | @@ -47,6 +47,10 @@ const menu: MenuModule = { | ||
47 | path: 'customerForm', | 47 | path: 'customerForm', |
48 | name: t('routes.demo.form.customerForm'), | 48 | name: t('routes.demo.form.customerForm'), |
49 | }, | 49 | }, |
50 | + { | ||
51 | + path: 'appendForm', | ||
52 | + name: t('routes.demo.form.appendForm'), | ||
53 | + }, | ||
50 | ], | 54 | ], |
51 | }, | 55 | }, |
52 | { | 56 | { |
src/router/routes/modules/demo/comp.ts
@@ -89,6 +89,14 @@ const comp: AppRouteModule = { | @@ -89,6 +89,14 @@ const comp: AppRouteModule = { | ||
89 | title: t('routes.demo.form.customerForm'), | 89 | title: t('routes.demo.form.customerForm'), |
90 | }, | 90 | }, |
91 | }, | 91 | }, |
92 | + { | ||
93 | + path: 'appendForm', | ||
94 | + name: 'appendFormDemo', | ||
95 | + component: () => import('/@/views/demo/form/AppendForm.vue'), | ||
96 | + meta: { | ||
97 | + title: t('routes.demo.form.appendForm'), | ||
98 | + }, | ||
99 | + }, | ||
92 | ], | 100 | ], |
93 | }, | 101 | }, |
94 | { | 102 | { |
src/views/demo/form/AppendForm.vue
0 → 100644
1 | +<template> | ||
2 | + <PageWrapper title="表单增删示例"> | ||
3 | + <CollapseContainer title="表单增删"> | ||
4 | + <BasicForm @register="register" @submit="handleSubmit"> | ||
5 | + <template #add="{ field }"> | ||
6 | + <Button v-if="field === 1" @click="add">+</Button> | ||
7 | + <Button v-if="field > 1" @click="del(field)">-</Button> | ||
8 | + </template> | ||
9 | + </BasicForm> | ||
10 | + </CollapseContainer> | ||
11 | + </PageWrapper> | ||
12 | +</template> | ||
13 | +<script lang="ts"> | ||
14 | + import { defineComponent, ref } from 'vue'; | ||
15 | + import { BasicForm, useForm } from '/@/components/Form/index'; | ||
16 | + import { CollapseContainer } from '/@/components/Container/index'; | ||
17 | + import { Input } from 'ant-design-vue'; | ||
18 | + import { PageWrapper } from '/@/components/Page'; | ||
19 | + import { Button } from '/@/components/Button'; | ||
20 | + | ||
21 | + export default defineComponent({ | ||
22 | + components: { BasicForm, CollapseContainer, PageWrapper, [Input.name]: Input, Button }, | ||
23 | + setup() { | ||
24 | + const [register, { appendSchemaByField, removeSchemaByFiled, validate }] = useForm({ | ||
25 | + schemas: [ | ||
26 | + { | ||
27 | + field: 'field1a', | ||
28 | + component: 'Input', | ||
29 | + label: '字段1', | ||
30 | + colProps: { | ||
31 | + span: 8, | ||
32 | + }, | ||
33 | + required: true, | ||
34 | + }, | ||
35 | + { | ||
36 | + field: 'field1b', | ||
37 | + component: 'Input', | ||
38 | + label: '字段1', | ||
39 | + colProps: { | ||
40 | + span: 8, | ||
41 | + }, | ||
42 | + required: true, | ||
43 | + }, | ||
44 | + { | ||
45 | + field: '1', | ||
46 | + component: 'Input', | ||
47 | + label: ' ', | ||
48 | + colProps: { | ||
49 | + span: 8, | ||
50 | + }, | ||
51 | + slot: 'add', | ||
52 | + }, | ||
53 | + ], | ||
54 | + labelWidth: 100, | ||
55 | + actionColOptions: { span: 24 }, | ||
56 | + }); | ||
57 | + | ||
58 | + async function handleSubmit() { | ||
59 | + try { | ||
60 | + const data = await validate(); | ||
61 | + console.log(data); | ||
62 | + } catch (e) { | ||
63 | + console.log(e); | ||
64 | + } | ||
65 | + } | ||
66 | + | ||
67 | + const n = ref(2); | ||
68 | + | ||
69 | + function add() { | ||
70 | + appendSchemaByField( | ||
71 | + { | ||
72 | + field: 'field' + n.value + 'a', | ||
73 | + component: 'Input', | ||
74 | + label: '字段2', | ||
75 | + colProps: { | ||
76 | + span: 8, | ||
77 | + }, | ||
78 | + required: true, | ||
79 | + }, | ||
80 | + '' | ||
81 | + ); | ||
82 | + appendSchemaByField( | ||
83 | + { | ||
84 | + field: 'field' + n.value + 'b', | ||
85 | + component: 'Input', | ||
86 | + label: '字段2', | ||
87 | + colProps: { | ||
88 | + span: 8, | ||
89 | + }, | ||
90 | + required: true, | ||
91 | + }, | ||
92 | + '' | ||
93 | + ); | ||
94 | + appendSchemaByField( | ||
95 | + { | ||
96 | + field: `${n.value}`, | ||
97 | + component: 'Input', | ||
98 | + label: ' ', | ||
99 | + colProps: { | ||
100 | + span: 8, | ||
101 | + }, | ||
102 | + slot: 'add', | ||
103 | + }, | ||
104 | + '' | ||
105 | + ); | ||
106 | + n.value++; | ||
107 | + } | ||
108 | + | ||
109 | + function del(field) { | ||
110 | + console.log(field); | ||
111 | + removeSchemaByFiled([`field${field}a`, `field${field}b`, `${field}`]); | ||
112 | + } | ||
113 | + | ||
114 | + return { register, handleSubmit, add, del }; | ||
115 | + }, | ||
116 | + }); | ||
117 | +</script> |