FormItem.vue
3.59 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
<script setup lang="ts">
import { FormPlus, FormSchema } from '@/components/FormPlus'
import { useForm } from '@/hooks/useForm'
import { propTypes } from "@/utils/propTypes";
import { reactive, unref, ref, computed } from 'vue'
import { ElButton, ElInput, FormItemProp } from 'element-plus'
import { useValidator } from '@/hooks/useValidator'
import { ComponentRef } from '@/types/global';
const props = defineProps({
schemaParam: {
type: Object as PropType<FormSchema[]>,
default: () => {
return []
}
},
labelPosition: propTypes.string.def('top'),
disabled: propTypes.bool.def(false),
model: {
type: Object as PropType<any>,
default: () => {
return {}
}
}
})
const formModel = computed(() => {
console.log(unref(props.model), 'model');
return props.model
})
const schemaData = computed(() => reactive<FormSchema[]>(unref(props.schemaParam)))
const { scrollToError } = useValidator()
const { formRegister, formMethods } = useForm()
const {
getFormResult,
setProps,
setValues,
setSchema,
getComponentExpose,
getFormItemExpose,
getElFormExpose,
getFormData
} = formMethods
/**
* 表单根据字段校验
* @param fields 待验证的字段
*/
const formValidation = async (fields: string[] = []) => { // 表单验证
const elFormExpose = await getElFormExpose()
return new Promise(async (resolve) => {
elFormExpose?.validateField(fields, (isValid) => {
if (isValid) {
resolve(isValid)
} else {
resolve(isValid)
}
})
})
}
const clearValidate = async (fields: string[] = []) => { // 表单验证
const elFormExpose = await getElFormExpose()
elFormExpose?.clearValidate(fields)
}
const inoutFocus = async () => {
const inputEl: ComponentRef<typeof ElInput> = await getComponentExpose('field1')
inputEl?.focus()
}
const inoutValidation = async () => {
const formItem = await getFormItemExpose('field1')
const inputEl: ComponentRef<typeof ElInput> = await getComponentExpose('field1')
inputEl?.focus()
formItem?.validate('focus', (val: boolean) => {
console.log(val)
})
}
const formValidate = (prop: FormItemProp, isValid: boolean, message: string) => {
// console.log(prop, isValid, message)
}
/**
* @param filterEmptyVal 是否过滤空值
*/
const getData = (filterEmptyVal = true) => {
if (filterEmptyVal) {
return new Promise(async (resolve) => {
const resultForm = await getFormData(filterEmptyVal)
resolve(resultForm)
})
} else {
return new Promise(async (resolve) => {
const resultForm = await getFormData(filterEmptyVal)
let res = getFormResult(resultForm, schemaData.value)
resolve(res)
})
}
}
const setValue = async (param = {}, reset = false) => {
const elFormExpose = await getElFormExpose()
if (reset) {
elFormExpose?.resetFields()
} else {
setValues(param)
}
}
const submitForm = () => {
return new Promise(async (resolve) => {
const elFormExpose = await getElFormExpose()
scrollToError()
elFormExpose?.validate((isValid) => {
if (isValid) {
resolve(isValid)
} else {
resolve(isValid)
}
})
})
}
// 子组建暴露的方法
defineExpose({
formValidation,
getFormResult,
clearValidate,
setSchema,
submitForm,
getData,
setValue: setValue,
setValues: setValue,
formMethods
})
</script>
<template>
<div>
<FormPlus :schema="schemaData" :disabled="disabled" :model="formModel" :isCol="true" :labelPosition="labelPosition"
@register="formRegister" @validate="formValidate" />
</div>
</template>
<style lang="scss" scoped>
.el-button {
margin-top: 10px;
}
</style>