Skip to content

Commit 77a3866

Browse files
perf: 优化表单组件
1 parent 3e4e27c commit 77a3866

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/components/Form/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export interface FormExpose {
4040
addSchema: (formSchema: FormSchema, index?: number) => void
4141
setSchema: (schemaProps: FormSetProps[]) => void
4242
formModel: Recordable
43-
getElFormRef: () => ComponentRef<typeof ElForm>
4443
getComponentExpose: (field: string) => any
4544
getFormItemExpose: (field: string) => any
4645
}

src/components/Form/src/Form.vue

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ export default defineComponent({
125125
}
126126
}
127127
128-
const getElFormRef = (): ComponentRef<typeof ElForm> => {
129-
return unref(elFormRef) as ComponentRef<typeof ElForm>
130-
}
131-
132128
const getOptions = async (fn: Function, field: string) => {
133129
const options = await fn()
134130
setSchema([
@@ -171,7 +167,6 @@ export default defineComponent({
171167
delSchema,
172168
addSchema,
173169
setSchema,
174-
getElFormRef,
175170
getComponentExpose,
176171
getFormItemExpose
177172
})

src/hooks/web/useForm.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export const useForm = () => {
3030

3131
// 一些内置的方法
3232
const methods = {
33+
/**
34+
* @description 设置form组件的props
35+
* @param field FormItem的field
36+
*/
3337
setProps: async (props: FormProps = {}) => {
3438
const form = await getForm()
3539
form?.setProps(props)
@@ -38,12 +42,17 @@ export const useForm = () => {
3842
}
3943
},
4044

45+
/**
46+
* @description 设置form的值
47+
* @param data 需要设置的数据
48+
*/
4149
setValues: async (data: Recordable) => {
4250
const form = await getForm()
4351
form?.setValues(data)
4452
},
4553

4654
/**
55+
* @description 设置schema
4756
* @param schemaProps 需要设置的schemaProps
4857
*/
4958
setSchema: async (schemaProps: FormSetProps[]) => {
@@ -52,6 +61,7 @@ export const useForm = () => {
5261
},
5362

5463
/**
64+
* @description 新增schema
5565
* @param formSchema 需要新增数据
5666
* @param index 在哪里新增
5767
*/
@@ -61,6 +71,7 @@ export const useForm = () => {
6171
},
6272

6373
/**
74+
* @description 删除schema
6475
* @param field 删除哪个数据
6576
*/
6677
delSchema: async (field: string) => {
@@ -69,23 +80,38 @@ export const useForm = () => {
6980
},
7081

7182
/**
83+
* @description 获取表单数据
7284
* @returns form data
7385
*/
7486
getFormData: async <T = Recordable>(): Promise<T> => {
7587
const form = await getForm()
7688
return form?.formModel as T
7789
},
7890

91+
/**
92+
* @description 获取表单组件的实例
93+
* @param field 表单项唯一标识
94+
* @returns component instance
95+
*/
7996
getComponentExpose: async (field: string) => {
8097
const form = await getForm()
8198
return form?.getComponentExpose(field)
8299
},
83100

101+
/**
102+
* @description 获取formItem组件的实例
103+
* @param field 表单项唯一标识
104+
* @returns formItem instance
105+
*/
84106
getFormItemExpose: async (field: string) => {
85107
const form = await getForm()
86108
return form?.getFormItemExpose(field) as ComponentRef<typeof ElFormItem>
87109
},
88110

111+
/**
112+
* @description 获取ElForm组件的实例
113+
* @returns ElForm instance
114+
*/
89115
getElFormExpose: async () => {
90116
await getForm()
91117
return unref(elFormRef)

src/views/Components/Form/UseFormDemo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ const inoutValidation = async () => {
292292
<ElButton @click="verifyReset"> {{ t('formDemo.verifyReset') }} </ElButton>
293293

294294
<ElButton @click="getDictOne">
295-
{{ t('searchDemo.dynamicOptions') }}
295+
{{ `${t('formDemo.select')} ${t('searchDemo.dynamicOptions')}` }}
296296
</ElButton>
297297

298298
<ElButton @click="inoutFocus">

0 commit comments

Comments
 (0)