From 1dc0bc7e32c805087dd9c2578be184ca06da941e Mon Sep 17 00:00:00 2001 From: zombiej Date: Mon, 12 Oct 2020 14:01:41 +0800 Subject: [PATCH] fix: Hide popup no need re-align --- src/index.tsx | 4 +-- tests/point.test.jsx | 63 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index c29bffed..57f6b93c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -583,7 +583,7 @@ export function generateTrigger( * @param popupVisible Show or not the popup element * @param event SyntheticEvent, used for `pointAlign` */ - setPopupVisible(popupVisible, event?) { + setPopupVisible(popupVisible: boolean, event?: { pageX: number, pageY: number }) { const { alignPoint } = this.props; const { popupVisible: prevPopupVisible } = this.state; @@ -597,7 +597,7 @@ export function generateTrigger( } // Always record the point position since mouseEnterDelay will delay the show - if (alignPoint && event) { + if (alignPoint && event && popupVisible) { this.setPoint(event); } } diff --git a/tests/point.test.jsx b/tests/point.test.jsx index e414a18c..ed30a817 100644 --- a/tests/point.test.jsx +++ b/tests/point.test.jsx @@ -16,11 +16,16 @@ describe('Trigger.Point', () => { }); class Demo extends React.Component { - popup =
POPUP
; + popup = (
POPUP
); render() { return ( - +
); @@ -36,7 +41,9 @@ describe('Trigger.Point', () => { .first() .getDOMNode(); - expect(popup.style).toEqual(expect.objectContaining({ left: '-989px', top: '-979px' })); + expect(popup.style).toEqual( + expect.objectContaining({ left: '-989px', top: '-979px' }), + ); }); it('hover', () => { @@ -48,12 +55,16 @@ describe('Trigger.Point', () => { .first() .getDOMNode(); - expect(popup.style).toEqual(expect.objectContaining({ left: '-989px', top: '-979px' })); + expect(popup.style).toEqual( + expect.objectContaining({ left: '-989px', top: '-979px' }), + ); }); describe('contextMenu', () => { it('basic', () => { - const wrapper = mount(); + const wrapper = mount( + , + ); wrapper.trigger('contextMenu', { pageX: 10, pageY: 20 }); const popup = wrapper @@ -61,12 +72,32 @@ describe('Trigger.Point', () => { .first() .getDOMNode(); - expect(popup.style).toEqual(expect.objectContaining({ left: '-989px', top: '-979px' })); + expect(popup.style).toEqual( + expect.objectContaining({ left: '-989px', top: '-979px' }), + ); + + // Not trigger point update when close + const clickEvent = {}; + const pagePropDefine = { + get: () => { + throw new Error('should not read when close'); + }, + }; + Object.defineProperties(clickEvent, { + pageX: pagePropDefine, + pageY: pagePropDefine, + }); + wrapper + .find('Trigger') + .instance() + .onClick(clickEvent); }); // https://github.com/ant-design/ant-design/issues/17043 it('not prevent default', done => { - const wrapper = mount(); + const wrapper = mount( + , + ); wrapper.trigger('contextMenu', { pageX: 10, pageY: 20 }); const popup = wrapper @@ -74,7 +105,9 @@ describe('Trigger.Point', () => { .first() .getDOMNode(); - expect(popup.style).toEqual(expect.objectContaining({ left: '-989px', top: '-979px' })); + expect(popup.style).toEqual( + expect.objectContaining({ left: '-989px', top: '-979px' }), + ); // Click to close wrapper.trigger('click', { @@ -91,7 +124,11 @@ describe('Trigger.Point', () => { function testPlacement(name, builtinPlacements, afterAll) { it(name, () => { const wrapper = mount( - , + , ); wrapper.trigger('click', { pageX: 10, pageY: 20 }); @@ -100,7 +137,9 @@ describe('Trigger.Point', () => { .first() .getDOMNode(); - expect(popup.style).toEqual(expect.objectContaining({ left: '-989px', top: '-979px' })); + expect(popup.style).toEqual( + expect.objectContaining({ left: '-989px', top: '-979px' }), + ); if (afterAll) { afterAll(wrapper); @@ -124,7 +163,9 @@ describe('Trigger.Point', () => { }, wrapper => { expect( - wrapper.find('div.rc-trigger-popup').hasClass('rc-trigger-popup-placement-left'), + wrapper + .find('div.rc-trigger-popup') + .hasClass('rc-trigger-popup-placement-left'), ).toBeTruthy(); }, );