From 3ebd812629d06865942c7008b2bdfabfe96a1f86 Mon Sep 17 00:00:00 2001 From: daisy <47104575+linxianxi@users.noreply.github.com> Date: Wed, 9 Aug 2023 11:33:40 +0800 Subject: [PATCH] fix: should hide popup when set alignPoint after scrolling (#417) --- src/hooks/useWatch.ts | 2 ++ src/index.tsx | 30 ++++++++++++++++++------------ tests/point.test.jsx | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/hooks/useWatch.ts b/src/hooks/useWatch.ts index 5067675d..eba0fd80 100644 --- a/src/hooks/useWatch.ts +++ b/src/hooks/useWatch.ts @@ -6,6 +6,7 @@ export default function useWatch( target: HTMLElement, popup: HTMLElement, onAlign: VoidFunction, + onScroll: VoidFunction, ) { useLayoutEffect(() => { if (open && target && popup) { @@ -24,6 +25,7 @@ export default function useWatch( function notifyScroll() { onAlign(); + onScroll(); } mergedList.forEach((scroller) => { diff --git a/src/index.tsx b/src/index.tsx index 9426d54b..ed88b2c4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -387,13 +387,30 @@ export function generateTrigger( onPopupAlign, ); + const [showActions, hideActions] = useAction( + mobile, + action, + showAction, + hideAction, + ); + + const clickToShow = showActions.has('click'); + const clickToHide = + hideActions.has('click') || hideActions.has('contextMenu'); + const triggerAlign = useEvent(() => { if (!inMotion) { onAlign(); } }); - useWatch(mergedOpen, targetEle, popupEle, triggerAlign); + const onScroll = () => { + if (openRef.current && alignPoint && clickToHide) { + triggerOpen(false); + } + }; + + useWatch(mergedOpen, targetEle, popupEle, triggerAlign, onScroll); useLayoutEffect(() => { triggerAlign(); @@ -463,13 +480,6 @@ export function generateTrigger( }; // =========================== Action =========================== - const [showActions, hideActions] = useAction( - mobile, - action, - showAction, - hideAction, - ); - /** * Util wrapper for trigger action */ @@ -489,10 +499,6 @@ export function generateTrigger( } // ======================= Action: Click ======================== - const clickToShow = showActions.has('click'); - const clickToHide = - hideActions.has('click') || hideActions.has('contextMenu'); - if (clickToShow || clickToHide) { cloneProps.onClick = ( event: React.MouseEvent, diff --git a/tests/point.test.jsx b/tests/point.test.jsx index 6c1dd534..30c2a350 100644 --- a/tests/point.test.jsx +++ b/tests/point.test.jsx @@ -22,15 +22,21 @@ describe('Trigger.Point', () => { render() { return ( - -
- + +
+ +
); } } @@ -120,6 +126,21 @@ describe('Trigger.Point', () => { done(); })(); }); + + it('should hide popup when set alignPoint after scrolling', async () => { + const { container } = render(); + await trigger(container, 'contextmenu', { clientX: 10, clientY: 20 }); + + const popup = document.querySelector('.rc-trigger-popup'); + expect(popup.style).toEqual( + expect.objectContaining({ left: '10px', top: '20px' }), + ); + + const scrollDiv = container.querySelector('.scroll'); + fireEvent.scroll(scrollDiv); + + expect(document.querySelector('.rc-trigger-popup-hidden')).toBeTruthy(); + }); }); describe('placement', () => {