-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathUserFieldFormDialog.vue
More file actions
168 lines (160 loc) · 5.23 KB
/
UserFieldFormDialog.vue
File metadata and controls
168 lines (160 loc) · 5.23 KB
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
<template>
<el-dialog
:title="
isEdit
? $t('views.template.templateForm.title.editParam')
: $t('views.template.templateForm.title.addParam')
"
v-model="dialogVisible"
:close-on-click-modal="false"
:close-on-press-escape="false"
:destroy-on-close="true"
:before-close="close"
append-to-body
>
<DynamicsFormConstructor
v-model="currentRow"
label-position="top"
require-asterisk-position="right"
:input_type_list="inputTypeList"
ref="DynamicsFormConstructorRef"
></DynamicsFormConstructor>
<template #footer>
<span class="dialog-footer">
<el-button @click.prevent="close"> {{ $t('common.cancel') }} </el-button>
<el-button type="primary" @click="submit()" :loading="loading">
{{ isEdit ? $t('common.save') : $t('common.add') }}
</el-button>
</span>
</template>
</el-dialog>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
import { cloneDeep } from 'lodash'
import DynamicsFormConstructor from '@/components/dynamics-form/constructor/index.vue'
import type { FormField } from '@/components/dynamics-form/type'
import _ from 'lodash'
import { t } from '@/locales'
const emit = defineEmits(['refresh'])
const DynamicsFormConstructorRef = ref()
const loading = ref<boolean>(false)
const isEdit = ref(false)
const currentItem = ref<FormField | any>()
const check_field = (field_list: Array<string>, obj: any) => {
return field_list.every((field) => _.get(obj, field, undefined) !== undefined)
}
const currentRow = computed(() => {
if (currentItem.value) {
const row = currentItem.value
switch (row.type) {
case 'input':
if (check_field(['field', 'input_type', 'label', 'required', 'attrs'], currentItem.value)) {
return currentItem.value
}
return {
attrs: row.attrs || { maxlength: 20, minlength: 0 },
field: row.field || row.variable,
input_type: 'TextInput',
label: row.label || row.name,
default_value: row.default_value,
required: row.required != undefined ? row.required : row.is_required
}
case 'select':
if (
check_field(
['field', 'input_type', 'label', 'required', 'option_list'],
currentItem.value
)
) {
return currentItem.value
}
return {
attrs: row.attrs || {},
field: row.field || row.variable,
input_type: 'SingleSelect',
label: row.label || row.name,
default_value: row.default_value,
required: row.required != undefined ? row.required : row.is_required,
option_list: row.option_list
? row.option_list
: row.optionList.map((o: any) => {
return { key: o, value: o }
})
}
case 'date':
if (
check_field(
[
'field',
'input_type',
'label',
'required',
'attrs.format',
'attrs.value-format',
'attrs.type'
],
currentItem.value
)
) {
return currentItem.value
}
return {
field: row.field || row.variable,
input_type: 'DatePicker',
label: row.label || row.name,
default_value: row.default_value,
required: row.required != undefined ? row.required : row.is_required,
attrs: {
format: 'YYYY-MM-DD HH:mm:ss',
'value-format': 'YYYY-MM-DD HH:mm:ss',
type: 'datetime'
}
}
default:
return currentItem.value
}
} else {
return { input_type: 'TextInput', required: false, attrs: { maxlength: 20, minlength: 0 } }
}
})
const currentIndex = ref(null)
const inputTypeList = ref([
{ label: t('dynamicsForm.input_type_list.TextInput'), value: 'TextInputConstructor' },
{ label: t('dynamicsForm.input_type_list.PasswordInput'), value: 'PasswordInputConstructor' },
{ label: t('dynamicsForm.input_type_list.SingleSelect'), value: 'SingleSelectConstructor' },
{ label: t('dynamicsForm.input_type_list.MultiSelect'), value: 'MultiSelectConstructor' },
{ label: t('dynamicsForm.input_type_list.RadioCard'), value: 'RadioCardConstructor' },
{ label: t('dynamicsForm.input_type_list.DatePicker'), value: 'DatePickerConstructor' },
{ label: t('dynamicsForm.input_type_list.SwitchInput'), value: 'SwitchInputConstructor' },
])
const dialogVisible = ref<boolean>(false)
const open = (row: any, index: any) => {
dialogVisible.value = true
if (row) {
isEdit.value = true
currentItem.value = cloneDeep(row)
currentIndex.value = index
} else {
currentItem.value = null
}
}
const close = () => {
dialogVisible.value = false
isEdit.value = false
currentIndex.value = null
currentItem.value = null as any
}
const submit = async () => {
const formEl = DynamicsFormConstructorRef.value
if (!formEl) return
await formEl.validate().then(() => {
emit('refresh', formEl?.getData(), currentIndex.value)
isEdit.value = false
currentItem.value = null as any
currentIndex.value = null
})
}
defineExpose({ open, close })
</script>
<style lang="scss" scoped></style>