forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic-form-item.vue
132 lines (125 loc) · 2.93 KB
/
dynamic-form-item.vue
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
<docs>
---
order: 3
title:
zh-CN: 动态增减表单项
en-US: Dynamic Form Item
---
## zh-CN
动态增加、减少表单项。
## en-US
Add or remove form items dynamically.
</docs>
<template>
<a-form
ref="formRef"
name="dynamic_form_item"
:model="dynamicValidateForm"
v-bind="formItemLayoutWithOutLabel"
>
<a-form-item
v-for="(domain, index) in dynamicValidateForm.domains"
:key="domain.key"
v-bind="index === 0 ? formItemLayout : {}"
:label="index === 0 ? 'Domains' : ''"
:name="['domains', index, 'value']"
:rules="{
required: true,
message: 'domain can not be null',
trigger: 'change',
}"
>
<a-input
v-model:value="domain.value"
placeholder="please input domain"
style="width: 60%; margin-right: 8px"
/>
<MinusCircleOutlined
v-if="dynamicValidateForm.domains.length > 1"
class="dynamic-delete-button"
@click="removeDomain(domain)"
/>
</a-form-item>
<a-form-item v-bind="formItemLayoutWithOutLabel">
<a-button type="dashed" style="width: 60%" @click="addDomain">
<PlusOutlined />
Add field
</a-button>
</a-form-item>
<a-form-item v-bind="formItemLayoutWithOutLabel">
<a-button type="primary" html-type="submit" @click="submitForm">Submit</a-button>
<a-button style="margin-left: 10px" @click="resetForm">Reset</a-button>
</a-form-item>
</a-form>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons-vue';
import type { FormInstance } from 'ant-design-vue';
interface Domain {
value: string;
key: number;
}
const formRef = ref<FormInstance>();
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 4 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 20 },
},
};
const formItemLayoutWithOutLabel = {
wrapperCol: {
xs: { span: 24, offset: 0 },
sm: { span: 20, offset: 4 },
},
};
const dynamicValidateForm = reactive<{ domains: Domain[] }>({
domains: [],
});
const submitForm = () => {
formRef.value
.validate()
.then(() => {
console.log('values', dynamicValidateForm.domains);
})
.catch(error => {
console.log('error', error);
});
};
const resetForm = () => {
formRef.value.resetFields();
};
const removeDomain = (item: Domain) => {
const index = dynamicValidateForm.domains.indexOf(item);
if (index !== -1) {
dynamicValidateForm.domains.splice(index, 1);
}
};
const addDomain = () => {
dynamicValidateForm.domains.push({
value: '',
key: Date.now(),
});
};
</script>
<style scoped>
.dynamic-delete-button {
cursor: pointer;
position: relative;
top: 4px;
font-size: 24px;
color: #999;
transition: all 0.3s;
}
.dynamic-delete-button:hover {
color: #777;
}
.dynamic-delete-button[disabled] {
cursor: not-allowed;
opacity: 0.5;
}
</style>