Skip to content

Commit

Permalink
fix: 修复 popup 滚动事件执行时机问题 (#1870)
Browse files Browse the repository at this point in the history
  • Loading branch information
honkinglin authored Jan 10, 2023
1 parent 26b23a8 commit 25be9eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/_util/usePrevious.ts → src/hooks/usePrevious.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useRef } from 'react';

// 缓存上一次的 state 用于前后比较
export default function usePrevious<T>(state: T): T | undefined {
const ref = useRef<T>();

Expand Down
8 changes: 6 additions & 2 deletions src/popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,6 +62,7 @@ const Popup = forwardRef((props: PopupProps, ref: React.RefObject<PopupRef>) =>
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 元素
Expand Down Expand Up @@ -112,8 +114,10 @@ const Popup = forwardRef((props: PopupProps, ref: React.RefObject<PopupRef>) =>
// 下拉展开时更新内部滚动条
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');
Expand Down

0 comments on commit 25be9eb

Please sign in to comment.