Skip to content

Commit

Permalink
Merge pull request #209 from yaob421123/master
Browse files Browse the repository at this point in the history
fix(ProForm): 修复ref暴露方法逻辑异常
  • Loading branch information
ChenlingasMx committed Apr 11, 2023
2 parents 3c2828f + 0e695a0 commit 7a99c2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/ProForm/formdom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function FormDom({
if (baseRef && baseRef.current) {
formInstanceRef.current = baseRef;
}
}, [baseRef, formInstanceRef]);
}, [baseRef]);

const styles: React.CSSProperties = {
background: '#fff',
Expand Down
18 changes: 10 additions & 8 deletions packages/components/src/ProForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ function ProForm(
// 通过ref导出实例方法
useImperativeHandle(ref, () => {
// 表单验证(同时兼容老api submitvalidate和新api onSubmit )
const submitvalidate = () => {
const { onSubmit } = formInstanceRef?.current?.current || {};
onSubmit?.() || null;
};
const submitvalidate = () =>
formInstanceRef?.current?.current?.onSubmit() || null;
// 获取表单的值
const getFieldValues = () =>
formInstanceRef?.current?.current?.getFieldValues() || {};
// 获取表单错误信息
const getError = () => formInstanceRef?.current?.current?.getError() || {};
// 验证并获取表单值
const validateFieldsAndGetValue = () => {
return new Promise(async function (resolve, reject) {
const { getError, getFieldValues } =
formInstanceRef?.current?.current || {};
await submitvalidate();
const errors = getError?.() || {};
const errors = (await getError()) || {};
if (isObjectEmpty(errors)) {
const value = (await getFieldValues?.()) || {};
const value = (await getFieldValues()) || {};
resolve(value);
} else {
reject(errors);
Expand All @@ -63,6 +64,7 @@ function ProForm(
};
return {
...formInstanceRef.current?.current,

submitvalidate,
validateFieldsAndGetValue,
};
Expand Down

0 comments on commit 7a99c2b

Please sign in to comment.