From 25be9eb03005517fed16234f0bf2457588f6f1ee Mon Sep 17 00:00:00 2001 From: Kyrielin Date: Tue, 10 Jan 2023 12:33:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20popup=20=E6=BB=9A?= =?UTF-8?q?=E5=8A=A8=E4=BA=8B=E4=BB=B6=E6=89=A7=E8=A1=8C=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#1870)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/{_util => hooks}/usePrevious.ts | 1 + src/popup/Popup.tsx | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) rename src/{_util => hooks}/usePrevious.ts (81%) diff --git a/src/_util/usePrevious.ts b/src/hooks/usePrevious.ts similarity index 81% rename from src/_util/usePrevious.ts rename to src/hooks/usePrevious.ts index a5233144ca..9778a1a176 100644 --- a/src/_util/usePrevious.ts +++ b/src/hooks/usePrevious.ts @@ -1,5 +1,6 @@ import { useEffect, useRef } from 'react'; +// 缓存上一次的 state 用于前后比较 export default function usePrevious(state: T): T | undefined { const ref = useRef(); diff --git a/src/popup/Popup.tsx b/src/popup/Popup.tsx index 70805c7482..9000c026f4 100644 --- a/src/popup/Popup.tsx +++ b/src/popup/Popup.tsx @@ -7,6 +7,7 @@ import { Placement } from '@popperjs/core'; import useControlled from '../hooks/useControlled'; import useAnimation from '../_util/useAnimation'; import useConfig from '../hooks/useConfig'; +import usePrevious from '../hooks/usePrevious'; import { TdPopupProps } from './type'; import Portal from '../common/Portal'; import useTrigger from './hooks/useTrigger'; @@ -61,6 +62,7 @@ const Popup = forwardRef((props: PopupProps, ref: React.RefObject) => const { keepExpand, keepFade } = useAnimation(); const { height: windowHeight, width: windowWidth } = useWindowSize(); const [visible, onVisibleChange] = useControlled(props, 'visible', props.onVisibleChange); + const prevVisible = usePrevious(visible); const [popupElement, setPopupElement] = useState(null); const triggerRef = useRef(null); // 记录 trigger 元素 @@ -112,8 +114,10 @@ const Popup = forwardRef((props: PopupProps, ref: React.RefObject) => // 下拉展开时更新内部滚动条 useEffect(() => { if (!triggerRef.current) triggerRef.current = getTriggerDom(); - visible && updateScrollTop?.(contentRef.current); - }, [visible, updateScrollTop, getTriggerDom]); + if (prevVisible !== visible && visible) { + updateScrollTop?.(contentRef.current); + } + }, [visible, prevVisible, updateScrollTop, getTriggerDom]); function handleExited() { !destroyOnClose && popupElement && (popupElement.style.display = 'none');