mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-16 01:41:02 +08:00
e46d414b11
* support getFieldInstance * update doc * fix lint * move func * move into hooks * update ref logic * fix lint * rm only * fix docs
49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import * as React from 'react';
|
|
|
|
/**
|
|
* Always debounce error to avoid [error -> null -> error] blink
|
|
*/
|
|
export default function useCacheErrors(
|
|
errors: React.ReactNode[],
|
|
changeTrigger: (visible: boolean) => void,
|
|
directly: boolean,
|
|
): [boolean, React.ReactNode[]] {
|
|
const cacheRef = React.useRef({
|
|
errors,
|
|
visible: !!errors.length,
|
|
});
|
|
|
|
const [, forceUpdate] = React.useState({});
|
|
|
|
const update = () => {
|
|
const prevVisible = cacheRef.current.visible;
|
|
const newVisible = !!errors.length;
|
|
|
|
const prevErrors = cacheRef.current.errors;
|
|
cacheRef.current.errors = errors;
|
|
cacheRef.current.visible = newVisible;
|
|
|
|
if (prevVisible !== newVisible) {
|
|
changeTrigger(newVisible);
|
|
} else if (
|
|
prevErrors.length !== errors.length ||
|
|
prevErrors.some((prevErr, index) => prevErr !== errors[index])
|
|
) {
|
|
forceUpdate({});
|
|
}
|
|
};
|
|
|
|
React.useEffect(() => {
|
|
if (!directly) {
|
|
const timeout = setTimeout(update, 10);
|
|
return () => clearTimeout(timeout);
|
|
}
|
|
}, [errors]);
|
|
|
|
if (directly) {
|
|
update();
|
|
}
|
|
|
|
return [cacheRef.current.visible, cacheRef.current.errors];
|
|
}
|