perf: 优化表单组件

This commit is contained in:
kailong321200875 2023-06-21 14:16:48 +08:00
parent 3e4e27c21f
commit 77a3866248
4 changed files with 27 additions and 7 deletions

View File

@ -40,7 +40,6 @@ export interface FormExpose {
addSchema: (formSchema: FormSchema, index?: number) => void
setSchema: (schemaProps: FormSetProps[]) => void
formModel: Recordable
getElFormRef: () => ComponentRef<typeof ElForm>
getComponentExpose: (field: string) => any
getFormItemExpose: (field: string) => any
}

View File

@ -125,10 +125,6 @@ export default defineComponent({
}
}
const getElFormRef = (): ComponentRef<typeof ElForm> => {
return unref(elFormRef) as ComponentRef<typeof ElForm>
}
const getOptions = async (fn: Function, field: string) => {
const options = await fn()
setSchema([
@ -171,7 +167,6 @@ export default defineComponent({
delSchema,
addSchema,
setSchema,
getElFormRef,
getComponentExpose,
getFormItemExpose
})

View File

@ -30,6 +30,10 @@ export const useForm = () => {
// 一些内置的方法
const methods = {
/**
* @description form组件的props
* @param field FormItem的field
*/
setProps: async (props: FormProps = {}) => {
const form = await getForm()
form?.setProps(props)
@ -38,12 +42,17 @@ export const useForm = () => {
}
},
/**
* @description form的值
* @param data
*/
setValues: async (data: Recordable) => {
const form = await getForm()
form?.setValues(data)
},
/**
* @description schema
* @param schemaProps schemaProps
*/
setSchema: async (schemaProps: FormSetProps[]) => {
@ -52,6 +61,7 @@ export const useForm = () => {
},
/**
* @description schema
* @param formSchema
* @param index
*/
@ -61,6 +71,7 @@ export const useForm = () => {
},
/**
* @description schema
* @param field
*/
delSchema: async (field: string) => {
@ -69,6 +80,7 @@ export const useForm = () => {
},
/**
* @description
* @returns form data
*/
getFormData: async <T = Recordable>(): Promise<T> => {
@ -76,16 +88,30 @@ export const useForm = () => {
return form?.formModel as T
},
/**
* @description
* @param field
* @returns component instance
*/
getComponentExpose: async (field: string) => {
const form = await getForm()
return form?.getComponentExpose(field)
},
/**
* @description formItem组件的实例
* @param field
* @returns formItem instance
*/
getFormItemExpose: async (field: string) => {
const form = await getForm()
return form?.getFormItemExpose(field) as ComponentRef<typeof ElFormItem>
},
/**
* @description ElForm组件的实例
* @returns ElForm instance
*/
getElFormExpose: async () => {
await getForm()
return unref(elFormRef)

View File

@ -292,7 +292,7 @@ const inoutValidation = async () => {
<ElButton @click="verifyReset"> {{ t('formDemo.verifyReset') }} </ElButton>
<ElButton @click="getDictOne">
{{ t('searchDemo.dynamicOptions') }}
{{ `${t('formDemo.select')} ${t('searchDemo.dynamicOptions')}` }}
</ElButton>
<ElButton @click="inoutFocus">