1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
| <script lang="ts" setup>
| import { Page } from '@vben/common-ui';
|
| import { Button, Card, message } from 'ant-design-vue';
|
| import { useVbenForm } from '#/adapter/form';
|
| const [Form, formApi] = useVbenForm({
| // 提交函数
| handleSubmit: onSubmit,
| schema: [
| {
| component: 'Input',
| defaultValue: 'hidden value',
| dependencies: {
| show: false,
| // 随意一个字段改变时,都会触发
| triggerFields: ['field1Switch'],
| },
| fieldName: 'hiddenField',
| label: '隐藏字段',
| },
| {
| component: 'Switch',
| defaultValue: true,
| fieldName: 'field1Switch',
| help: '通过Dom控制销毁',
| label: '显示字段1',
| },
| {
| component: 'Switch',
| defaultValue: true,
| fieldName: 'field2Switch',
| help: '通过css控制隐藏',
| label: '显示字段2',
| },
| {
| component: 'Switch',
| fieldName: 'field3Switch',
| label: '禁用字段3',
| },
| {
| component: 'Switch',
| fieldName: 'field4Switch',
| label: '字段4必填',
| },
| {
| component: 'Input',
| dependencies: {
| if(values) {
| return !!values.field1Switch;
| },
| // 只有指定的字段改变时,才会触发
| triggerFields: ['field1Switch'],
| },
| // 字段名
| fieldName: 'field1',
| // 界面显示的label
| label: '字段1',
| },
| {
| component: 'Input',
| dependencies: {
| show(values) {
| return !!values.field2Switch;
| },
| triggerFields: ['field2Switch'],
| },
| fieldName: 'field2',
| label: '字段2',
| },
| {
| component: 'Input',
| dependencies: {
| disabled(values) {
| return !!values.field3Switch;
| },
| triggerFields: ['field3Switch'],
| },
| fieldName: 'field3',
| label: '字段3',
| },
| {
| component: 'Input',
| dependencies: {
| required(values) {
| return !!values.field4Switch;
| },
| triggerFields: ['field4Switch'],
| },
| fieldName: 'field4',
| label: '字段4',
| },
| {
| component: 'Input',
| dependencies: {
| rules(values) {
| if (values.field1 === '123') {
| return 'required';
| }
| return null;
| },
| triggerFields: ['field1'],
| },
| fieldName: 'field5',
| help: '当字段1的值为`123`时,必填',
| label: '动态rules',
| },
| {
| component: 'Select',
| componentProps: {
| allowClear: true,
| class: 'w-full',
| filterOption: true,
| options: [
| {
| label: '选项1',
| value: '1',
| },
| {
| label: '选项2',
| value: '2',
| },
| ],
| placeholder: '请选择',
| showSearch: true,
| },
| dependencies: {
| componentProps(values) {
| if (values.field2 === '123') {
| return {
| options: [
| {
| label: '选项1',
| value: '1',
| },
| {
| label: '选项2',
| value: '2',
| },
| {
| label: '选项3',
| value: '3',
| },
| ],
| };
| }
| return {};
| },
| triggerFields: ['field2'],
| },
| fieldName: 'field6',
| help: '当字段2的值为`123`时,更改下拉选项',
| label: '动态配置',
| },
| {
| component: 'Input',
| fieldName: 'field7',
| label: '字段7',
| },
| ],
| // 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
| wrapperClass: 'grid-cols-1 md:grid-cols-3 lg:grid-cols-4',
| });
|
| const [SyncForm] = useVbenForm({
| handleSubmit: onSubmit,
| schema: [
| {
| component: 'Input',
| // 字段名
| fieldName: 'field1',
| // 界面显示的label
| label: '字段1',
| },
| {
| component: 'Input',
| componentProps: {
| disabled: true,
| },
| dependencies: {
| trigger(values, form) {
| form.setFieldValue('field2', values.field1);
| },
| // 只有指定的字段改变时,才会触发
| triggerFields: ['field1'],
| },
| // 字段名
| fieldName: 'field2',
| // 界面显示的label
| label: '字段2',
| },
| ],
| // 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
| wrapperClass: 'grid-cols-1 md:grid-cols-3 lg:grid-cols-4',
| });
|
| function onSubmit(values: Record<string, any>) {
| message.success({
| content: `form values: ${JSON.stringify(values)}`,
| });
| }
|
| function handleDelete() {
| formApi.setState((prev) => {
| return {
| schema: prev.schema?.filter((item) => item.fieldName !== 'field7'),
| };
| });
| }
|
| function handleAdd() {
| formApi.setState((prev) => {
| return {
| schema: [
| ...(prev?.schema ?? []),
| {
| component: 'Input',
| fieldName: `field${Date.now()}`,
| label: '字段+',
| },
| ],
| };
| });
| }
|
| function handleUpdate() {
| formApi.setState((prev) => {
| return {
| schema: prev.schema?.map((item) => {
| if (item.fieldName === 'field3') {
| return {
| ...item,
| label: '字段3-修改',
| };
| }
| return item;
| }),
| };
| });
| }
| </script>
|
| <template>
| <Page
| description="表单组件动态联动示例,包含了常用的场景。增删改,本质上是修改schema,你也可以通过 `setState` 动态修改schema。"
| title="表单组件"
| >
| <Card title="表单动态联动示例">
| <template #extra>
| <Button class="mr-2" @click="handleUpdate">修改字段3</Button>
| <Button class="mr-2" @click="handleDelete">删除字段7</Button>
| <Button @click="handleAdd">添加字段</Button>
| </template>
| <Form />
| </Card>
|
| <Card class="mt-5" title="字段同步,字段1数据与字段2数据同步">
| <SyncForm />
| </Card>
| </Page>
| </template>
|
|