Skip to content

Commit 0d56929

Browse files
committed
fix(ProForm): 优化ts
1 parent 114d323 commit 0d56929

File tree

2 files changed

+13
-37
lines changed

2 files changed

+13
-37
lines changed

packages/components/src/ProForm/formdom.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useRef, useEffect } from 'react';
22
import { Form, Button, Col, Row, FormFieldsProps } from 'uiw';
3-
import { ProFormProps } from './type';
3+
import { ProFormProps, UseFormStateProps } from './type';
44
import { useStore, useColPropsContext } from './hooks/store';
55
import { fromValidate } from './utils';
66
import './style/form-item.less';
@@ -19,9 +19,11 @@ function FormDom({
1919
formInstanceRef,
2020
}: ProFormProps & {
2121
formfields: Record<string, FormFieldsProps<{}>> | undefined;
22-
formInstanceRef: any;
22+
formInstanceRef: React.MutableRefObject<
23+
{ current: UseFormStateProps | null } | undefined
24+
>;
2325
}) {
24-
const baseRef = useRef(null);
26+
const baseRef: React.MutableRefObject<null> = useRef(null);
2527
const store = useStore();
2628
const colProps = useColPropsContext();
2729

packages/components/src/ProForm/index.tsx

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ import { Collapse, Card } from 'uiw';
33
import FormDom from './formdom';
44
import ReadFormDom from './readform';
55
import { getFormFields } from './widgets';
6-
import { ProFormProps, UseFormStateProps } from './type';
6+
import { ProFormProps, UseFormStateProps, UseFormProps } from './type';
77
import { StoreCtx, ColPropsContext } from './hooks/store';
8-
import { isObjectEmpty } from './utils';
98
import './style/form-item.less';
109

11-
function ProForm(props: ProFormProps, ref: any) {
10+
function ProForm(
11+
props: ProFormProps,
12+
ref: React.ForwardedRef<
13+
Partial<React.LegacyRef<UseFormProps>> | undefined | null
14+
>,
15+
) {
1216
const {
1317
formDatas = [],
1418
title = '',
@@ -34,37 +38,7 @@ function ProForm(props: ProFormProps, ref: any) {
3438

3539
// 通过ref导出实例方法
3640
const formInstanceRef = useRef<{ current: UseFormStateProps }>();
37-
useImperativeHandle(ref, () => {
38-
// 表单验证(同时兼容老api submitvalidate和新api onSubmit )
39-
const submitvalidate = () =>
40-
formInstanceRef.current?.current?.onSubmit() || null;
41-
// 获取表单的值
42-
const getFieldValues = () =>
43-
formInstanceRef.current?.current?.getFieldValues() || {};
44-
// 获取表单错误信息
45-
const getError = () => formInstanceRef?.current?.current?.getError() || {};
46-
47-
// 验证并获取表单值
48-
const validateFieldsAndGetValue = () => {
49-
return new Promise(async function (resolve, reject) {
50-
await submitvalidate();
51-
const errors = getError();
52-
if (isObjectEmpty(errors)) {
53-
const value = getFieldValues();
54-
resolve(value);
55-
} else {
56-
reject(errors);
57-
}
58-
});
59-
};
60-
return {
61-
...formInstanceRef.current?.current,
62-
submitvalidate,
63-
getFieldValues,
64-
getError,
65-
validateFieldsAndGetValue,
66-
};
67-
});
41+
useImperativeHandle(ref, () => ({ ...form }));
6842

6943
let children: React.ReactNode;
7044
const formDomProps = { ...props, formfields, formInstanceRef };

0 commit comments

Comments
 (0)