Skip to content

fix: Not trigger enter open for portal element #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@testing-library/react": "^14.0.0",
"@types/classnames": "^2.2.10",
"@types/jest": "^29.5.2",
"@types/node": "^20.11.6",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.11",
"@umijs/fabric": "^4.0.1",
Expand Down
9 changes: 6 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ export function generateTrigger(
const hoverToShow = showActions.has('hover');
const hoverToHide = hideActions.has('hover');

let onPopupMouseEnter: VoidFunction;
let onPopupMouseEnter: React.MouseEventHandler<HTMLDivElement>;
let onPopupMouseLeave: VoidFunction;

if (hoverToShow) {
Expand All @@ -578,9 +578,12 @@ export function generateTrigger(
setMousePosByEvent(event);
},
);
onPopupMouseEnter = () => {
onPopupMouseEnter = (event) => {
// Only trigger re-open when popup is visible
if (mergedOpen || inMotion) {
if (
(mergedOpen || inMotion) &&
popupEle?.contains(event.target as HTMLElement)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

popupEle 不存在会是什么场景?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

防止别人手工去调用 onMouseEnter,防御性的。

) {
triggerOpen(true, mouseEnterDelay);
}
};
Expand Down
73 changes: 73 additions & 0 deletions tests/portal.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-disable max-classes-per-file */

import { act, cleanup, fireEvent, render } from '@testing-library/react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import React from 'react';
import ReactDOM from 'react-dom';
import Trigger from '../src';
import { placementAlignMap } from './util';

describe('Trigger.Portal', () => {
beforeAll(() => {
spyElementPrototypes(HTMLElement, {
offsetParent: {
get: () => document.body,
},
});
});

beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
cleanup();
jest.useRealTimers();
});

it('no trigger with portal element', () => {
const PortalBox = () => {
return ReactDOM.createPortal(
<div className="portal-box" />,
document.body,
);
};

const onPopupVisibleChange = jest.fn();

const { container } = render(
<div className="holder">
<Trigger
action={['hover']}
popupAlign={placementAlignMap.left}
onPopupVisibleChange={onPopupVisibleChange}
popup={
<strong className="x-content">
tooltip2
<PortalBox />
</strong>
}
>
<div className="target">hover</div>
</Trigger>
</div>,
);

// Show the popup
fireEvent.mouseEnter(container.querySelector('.target'));
expect(onPopupVisibleChange).toHaveBeenCalledWith(true);
fireEvent.mouseLeave(container.querySelector('.target'));

// Mouse enter popup
fireEvent.mouseEnter(document.querySelector('.x-content'));
fireEvent.mouseLeave(document.querySelector('.x-content'));

// To Portal
fireEvent.mouseEnter(document.querySelector('.portal-box'));
act(() => {
jest.runAllTimers();
});

expect(onPopupVisibleChange).toHaveBeenCalledWith(false);
});
});