Skip to content

Commit 3ebd812

Browse files
authored
fix: should hide popup when set alignPoint after scrolling (#417)
1 parent afd2782 commit 3ebd812

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

src/hooks/useWatch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default function useWatch(
66
target: HTMLElement,
77
popup: HTMLElement,
88
onAlign: VoidFunction,
9+
onScroll: VoidFunction,
910
) {
1011
useLayoutEffect(() => {
1112
if (open && target && popup) {
@@ -24,6 +25,7 @@ export default function useWatch(
2425

2526
function notifyScroll() {
2627
onAlign();
28+
onScroll();
2729
}
2830

2931
mergedList.forEach((scroller) => {

src/index.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,30 @@ export function generateTrigger(
387387
onPopupAlign,
388388
);
389389

390+
const [showActions, hideActions] = useAction(
391+
mobile,
392+
action,
393+
showAction,
394+
hideAction,
395+
);
396+
397+
const clickToShow = showActions.has('click');
398+
const clickToHide =
399+
hideActions.has('click') || hideActions.has('contextMenu');
400+
390401
const triggerAlign = useEvent(() => {
391402
if (!inMotion) {
392403
onAlign();
393404
}
394405
});
395406

396-
useWatch(mergedOpen, targetEle, popupEle, triggerAlign);
407+
const onScroll = () => {
408+
if (openRef.current && alignPoint && clickToHide) {
409+
triggerOpen(false);
410+
}
411+
};
412+
413+
useWatch(mergedOpen, targetEle, popupEle, triggerAlign, onScroll);
397414

398415
useLayoutEffect(() => {
399416
triggerAlign();
@@ -463,13 +480,6 @@ export function generateTrigger(
463480
};
464481

465482
// =========================== Action ===========================
466-
const [showActions, hideActions] = useAction(
467-
mobile,
468-
action,
469-
showAction,
470-
hideAction,
471-
);
472-
473483
/**
474484
* Util wrapper for trigger action
475485
*/
@@ -489,10 +499,6 @@ export function generateTrigger(
489499
}
490500

491501
// ======================= Action: Click ========================
492-
const clickToShow = showActions.has('click');
493-
const clickToHide =
494-
hideActions.has('click') || hideActions.has('contextMenu');
495-
496502
if (clickToShow || clickToHide) {
497503
cloneProps.onClick = (
498504
event: React.MouseEvent<HTMLElement>,

tests/point.test.jsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ describe('Trigger.Point', () => {
2222

2323
render() {
2424
return (
25-
<Trigger
26-
ref={this.props.triggerRef}
27-
popup={this.popup}
28-
popupAlign={{ points: ['tl'] }}
29-
alignPoint
30-
{...this.props}
25+
<div
26+
className="scroll"
27+
// Jest can not get calculated style in jsdom. So we need to set it manually
28+
style={{ overflowX: 'hidden', overflowY: 'hidden' }}
3129
>
32-
<div className="point-region" />
33-
</Trigger>
30+
<Trigger
31+
ref={this.props.triggerRef}
32+
popup={this.popup}
33+
popupAlign={{ points: ['tl'] }}
34+
alignPoint
35+
{...this.props}
36+
>
37+
<div className="point-region" />
38+
</Trigger>
39+
</div>
3440
);
3541
}
3642
}
@@ -120,6 +126,21 @@ describe('Trigger.Point', () => {
120126
done();
121127
})();
122128
});
129+
130+
it('should hide popup when set alignPoint after scrolling', async () => {
131+
const { container } = render(<Demo action={['contextMenu']} />);
132+
await trigger(container, 'contextmenu', { clientX: 10, clientY: 20 });
133+
134+
const popup = document.querySelector('.rc-trigger-popup');
135+
expect(popup.style).toEqual(
136+
expect.objectContaining({ left: '10px', top: '20px' }),
137+
);
138+
139+
const scrollDiv = container.querySelector('.scroll');
140+
fireEvent.scroll(scrollDiv);
141+
142+
expect(document.querySelector('.rc-trigger-popup-hidden')).toBeTruthy();
143+
});
123144
});
124145

125146
describe('placement', () => {

0 commit comments

Comments
 (0)