Skip to content

Commit

Permalink
fix: form field validate write opreation not check unmoun status (#2206)
Browse files Browse the repository at this point in the history
Co-authored-by: pointhalo <[email protected]>
  • Loading branch information
sylingd and pointhalo authored Apr 26, 2024
1 parent 7553cff commit 53c26e6
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/semi-ui/form/hoc/withField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ function withField<
const [cursor, setCursor, getCursor] = useStateWithGetter(0);
const [status, setStatus] = useState(validateStatus); // use props.validateStatus to init

const isUnmounted = useRef(false);
const rulesRef = useRef(rules);
const validateRef = useRef(validate);
const validatePromise = useRef<Promise<any> | null>(null);
Expand All @@ -159,6 +160,9 @@ function withField<
};

const updateError = (errors: any, callOpts?: CallOpts) => {
if (isUnmounted.current) {
return;
}
if (errors === getError()) {
// When the inspection result is unchanged, no need to update, saving a forceUpdate overhead
// When errors is an array, deepEqual is not used, and it is always treated as a need to update
Expand Down Expand Up @@ -214,7 +218,7 @@ function withField<
(errors, fields) => {}
)
.then(res => {
if (validatePromise.current !== rootPromise) {
if (isUnmounted.current || validatePromise.current !== rootPromise) {
return;
}
// validation passed
Expand All @@ -223,7 +227,7 @@ function withField<
resolve({});
})
.catch(err => {
if (validatePromise.current !== rootPromise) {
if (isUnmounted.current || validatePromise.current !== rootPromise) {
return;
}
let { errors, fields } = err;
Expand Down Expand Up @@ -270,7 +274,7 @@ function withField<
} else if (isPromise(maybePromisedErrors)) {
maybePromisedErrors.then((result: any) => {
// If the async validate is outdated (a newer validate occurs), the result should be discarded
if (validatePromise.current !== rootPromise) {
if (isUnmounted.current || validatePromise.current !== rootPromise) {
return;
}

Expand Down Expand Up @@ -408,11 +412,14 @@ function withField<
validateRef.current = validate;
}, [rules, validate]);

// exec validate once when trigger inlcude 'mount'
useIsomorphicEffect(() => {
// exec validate once when trigger include 'mount'
if (validateOnMount) {
fieldValidate(value);
}
return () => {
isUnmounted.current = true;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down

0 comments on commit 53c26e6

Please sign in to comment.