diff --git a/examples/nested.js b/examples/nested.js index 84529e67..a205bf21 100644 --- a/examples/nested.js +++ b/examples/nested.js @@ -1,6 +1,7 @@ /* eslint no-console:0 */ import React from 'react'; +import ReactDOM from 'react-dom'; import Trigger from '../src'; import '../assets/index.less'; @@ -36,66 +37,96 @@ const popupBorderStyle = { padding: 10, }; -class Test extends React.Component { - saveContainerRef = (node) => { - this.containerInstanceNode = node; - }; +const OuterContent = ({ getContainer }) => { + return ReactDOM.createPortal( +
+ I am outer content + +
, + getContainer(), + ); +}; + +const Test = () => { + const containerRef = React.useRef(); + const outerDivRef = React.useRef(); - render() { - const innerTrigger = ( -
-
+ const innerTrigger = ( +
+
+ containerRef.current} + popup={
I am inner Trigger Popup
} + > + + clickToShowInnerTrigger + +
+
+ ); + return ( +
+
this.containerInstanceNode} - popup={
I am inner Trigger Popup
} + popup={ +
+ i am a click popup + outerDivRef.current} /> +
+ } > - - clickToShowInnerTrigger + + i am a hover popup
} + > + + trigger + +
- ); - return ( -
-
- i am a click popup
} - > - - i am a hover popup
} - > - - trigger - - - - -
-
- - - trigger - - -
+
+ + + trigger + +
- ); - } -} + +
+
+ ); +}; export default Test; diff --git a/src/Popup/PopupInner.tsx b/src/Popup/PopupInner.tsx index df24bd76..4cf10d5e 100644 --- a/src/Popup/PopupInner.tsx +++ b/src/Popup/PopupInner.tsx @@ -218,8 +218,8 @@ const PopupInner = React.forwardRef( className={mergedClassName} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} - onMouseDown={onMouseDown} - onTouchStart={onTouchStart} + onMouseDownCapture={onMouseDown} + onTouchStartCapture={onTouchStart} style={{ ...motionStyle, ...mergedStyle, diff --git a/tests/basic.test.jsx b/tests/basic.test.jsx index baecc375..dd6c737b 100644 --- a/tests/basic.test.jsx +++ b/tests/basic.test.jsx @@ -1,67 +1,13 @@ +/* eslint-disable max-classes-per-file */ + import React from 'react'; +import ReactDOM from 'react-dom'; import { mount } from 'enzyme'; import { act } from 'react-dom/test-utils'; import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; import Portal from 'rc-util/lib/Portal'; import Trigger from '../src'; - -const autoAdjustOverflow = { - adjustX: 1, - adjustY: 1, -}; - -const targetOffsetG = [0, 0]; - -export const placementAlignMap = { - left: { - points: ['cr', 'cl'], - overflow: autoAdjustOverflow, - offset: [-3, 0], - targetOffsetG, - }, - right: { - points: ['cl', 'cr'], - overflow: autoAdjustOverflow, - offset: [3, 0], - targetOffsetG, - }, - top: { - points: ['bc', 'tc'], - overflow: autoAdjustOverflow, - offset: [0, -3], - targetOffsetG, - }, - bottom: { - points: ['tc', 'bc'], - overflow: autoAdjustOverflow, - offset: [0, 3], - targetOffsetG, - }, - topLeft: { - points: ['bl', 'tl'], - overflow: autoAdjustOverflow, - offset: [0, -3], - targetOffsetG, - }, - topRight: { - points: ['br', 'tr'], - overflow: autoAdjustOverflow, - offset: [0, -3], - targetOffsetG, - }, - bottomRight: { - points: ['tr', 'br'], - overflow: autoAdjustOverflow, - offset: [0, 3], - targetOffsetG, - }, - bottomLeft: { - points: ['tl', 'bl'], - overflow: autoAdjustOverflow, - offset: [0, 3], - targetOffsetG, - }, -}; +import { placementAlignMap } from './util'; describe('Trigger.Basic', () => { beforeEach(() => { @@ -147,12 +93,7 @@ describe('Trigger.Basic', () => { ); wrapper.trigger(); - expect( - wrapper - .find('Popup') - .find('.x-content') - .text(), - ).toBe('tooltip2'); + expect(wrapper.find('Popup').find('.x-content').text()).toBe('tooltip2'); wrapper.trigger(); expect(wrapper.isHidden()).toBeTruthy(); @@ -173,12 +114,7 @@ describe('Trigger.Basic', () => { ); wrapper.trigger(); - expect( - wrapper - .find('Popup') - .find('.x-content') - .text(), - ).toBe('tooltip3'); + expect(wrapper.find('Popup').find('.x-content').text()).toBe('tooltip3'); wrapper.trigger(); expect(wrapper.isHidden()).toBeTruthy(); @@ -509,7 +445,7 @@ describe('Trigger.Basic', () => { }); describe('stretch', () => { - const createTrigger = stretch => + const createTrigger = (stretch) => mount( { domSpy.mockRestore(); }); - ['width', 'height', 'minWidth', 'minHeight'].forEach(prop => { + ['width', 'height', 'minWidth', 'minHeight'].forEach((prop) => { it(prop, () => { const wrapper = createTrigger(prop); @@ -643,7 +579,7 @@ describe('Trigger.Basic', () => {
, + container, + ); + }; + + const Demo = () => { + return ( + + tooltip2 + + + } + > +
click
+
+ ); + }; + + const wrapper = mount(, { attachTo: root }); + + wrapper.find('.target').simulate('click'); + expect(wrapper.isHidden()).toBeFalsy(); + + // Click should not close + wrapper.find('button').simulate('mouseDown'); + + // Mock document mouse click event + act(() => { + const mouseEvent = new MouseEvent('mousedown'); + document.dispatchEvent(mouseEvent); + wrapper.update(); + }); + + wrapper.update(); + expect(wrapper.isHidden()).toBeFalsy(); + + wrapper.unmount(); + + document.body.removeChild(div); + document.body.removeChild(root); + }); }); diff --git a/tests/mask.test.jsx b/tests/mask.test.jsx index 484eb04c..2d51ba69 100644 --- a/tests/mask.test.jsx +++ b/tests/mask.test.jsx @@ -1,7 +1,7 @@ import React from 'react'; import { mount } from 'enzyme'; import Trigger from '../src'; -import { placementAlignMap } from './basic.test'; +import { placementAlignMap } from './util'; describe('Trigger.Mask', () => { beforeEach(() => { diff --git a/tests/mobile.test.tsx b/tests/mobile.test.tsx index c8fe3a7f..0aa37d04 100644 --- a/tests/mobile.test.tsx +++ b/tests/mobile.test.tsx @@ -2,8 +2,9 @@ import React from 'react'; import { act } from 'react-dom/test-utils'; import isMobile from 'rc-util/lib/isMobile'; import { mount } from 'enzyme'; -import Trigger, { TriggerProps } from '../src'; -import { placementAlignMap } from './basic.test'; +import type { TriggerProps } from '../src'; +import Trigger from '../src'; +import { placementAlignMap } from './util'; jest.mock('rc-util/lib/isMobile'); @@ -53,7 +54,7 @@ describe('Trigger.Mobile', () => { const wrapper = mount( getTrigger({ mobile: { - popupRender: node => ( + popupRender: (node) => ( <>
Light
{node} diff --git a/tests/motion.test.jsx b/tests/motion.test.jsx index 033c2669..13a66c77 100644 --- a/tests/motion.test.jsx +++ b/tests/motion.test.jsx @@ -1,7 +1,7 @@ import React from 'react'; import { mount } from 'enzyme'; import Trigger from '../src'; -import { placementAlignMap } from './basic.test'; +import { placementAlignMap } from './util'; describe('Trigger.Motion', () => { beforeEach(() => { diff --git a/tests/util.tsx b/tests/util.tsx new file mode 100644 index 00000000..918578b4 --- /dev/null +++ b/tests/util.tsx @@ -0,0 +1,57 @@ +const autoAdjustOverflow = { + adjustX: 1, + adjustY: 1, +}; + +const targetOffsetG = [0, 0]; + +export const placementAlignMap = { + left: { + points: ['cr', 'cl'], + overflow: autoAdjustOverflow, + offset: [-3, 0], + targetOffsetG, + }, + right: { + points: ['cl', 'cr'], + overflow: autoAdjustOverflow, + offset: [3, 0], + targetOffsetG, + }, + top: { + points: ['bc', 'tc'], + overflow: autoAdjustOverflow, + offset: [0, -3], + targetOffsetG, + }, + bottom: { + points: ['tc', 'bc'], + overflow: autoAdjustOverflow, + offset: [0, 3], + targetOffsetG, + }, + topLeft: { + points: ['bl', 'tl'], + overflow: autoAdjustOverflow, + offset: [0, -3], + targetOffsetG, + }, + topRight: { + points: ['br', 'tr'], + overflow: autoAdjustOverflow, + offset: [0, -3], + targetOffsetG, + }, + bottomRight: { + points: ['tr', 'br'], + overflow: autoAdjustOverflow, + offset: [0, 3], + targetOffsetG, + }, + bottomLeft: { + points: ['tl', 'bl'], + overflow: autoAdjustOverflow, + offset: [0, 3], + targetOffsetG, + }, +};