Skip to content

fix(XYChart, Network): Preserve passed onMouseLeave and onMouseMove #195

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion packages/network/src/chart/Network.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ class Network extends React.PureComponent {
children,
className,
height,
onMouseLeave,
onMouseMove,
renderLink,
renderNode,
renderTooltip,
Expand All @@ -335,7 +337,11 @@ class Network extends React.PureComponent {

if (renderTooltip) {
return (
<WithTooltip renderTooltip={renderTooltip}>
<WithTooltip
renderTooltip={renderTooltip}
onMouseLeave={onMouseLeave} // preserve these as WithTooltip will override them
onMouseMove={onMouseMove}
>
<Network {...this.props} renderTooltip={null} />
</WithTooltip>
);
Expand Down
11 changes: 9 additions & 2 deletions packages/shared/src/enhancer/WithTooltip.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const propTypes = {
children: PropTypes.oneOfType([PropTypes.object, PropTypes.func]).isRequired,
className: PropTypes.string,
HoverStyles: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
onMouseMove: PropTypes.func,
onMouseLeave: PropTypes.func,
renderTooltip: PropTypes.func,
styles: PropTypes.objectOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
TooltipComponent: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
Expand All @@ -40,6 +42,8 @@ const defaultProps = {
`}
</style>
),
onMouseMove: null,
onMouseLeave: null,
renderTooltip: null,
styles: { display: 'inline-block', position: 'relative' },
TooltipComponent: TooltipWithBounds,
Expand All @@ -62,7 +66,7 @@ class WithTooltip extends React.PureComponent {
}

handleMouseMove({ event, datum, coords, ...rest }) {
const { showTooltip } = this.props;
const { showTooltip, onMouseMove } = this.props;
if (this.tooltipTimeout) {
clearTimeout(this.tooltipTimeout);
}
Expand All @@ -83,13 +87,16 @@ class WithTooltip extends React.PureComponent {
...rest,
},
});

if (onMouseMove) onMouseMove({ event, datum, coords, ...rest });
}

handleMouseLeave() {
const { tooltipTimeout, hideTooltip } = this.props;
const { tooltipTimeout, hideTooltip, onMouseLeave } = this.props;
this.tooltipTimeout = setTimeout(() => {
hideTooltip();
}, tooltipTimeout);
if (onMouseLeave) onMouseLeave();
}

render() {
Expand Down
38 changes: 38 additions & 0 deletions packages/shared/test/enhancer/WithTooltip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,44 @@ describe('<WithTooltip />', () => {
expect(wrapper.find('#test')).toHaveLength(1);
});

it('it should invoke the passed onMouseMove when the provided onMouseMove is invoked', () => {
const passedOnMouseMove = jest.fn();

let providedMouseMove;
const wrapper = mount(
<WithTooltip renderTooltip={() => null} onMouseMove={passedOnMouseMove}>
{({ onMouseMove }) => {
providedMouseMove = onMouseMove;

return <svg />;
}}
</WithTooltip>,
);

providedMouseMove({});
wrapper.update();
expect(passedOnMouseMove).toHaveBeenCalledTimes(1);
});

it('it should invoke the passed onMouseLeave when the provided onMouseLeave is invoked', () => {
const passedOnMouseLeave = jest.fn();

let providedMouseLeave;
const wrapper = mount(
<WithTooltip renderTooltip={() => null} onMouseLeave={passedOnMouseLeave}>
{({ onMouseLeave }) => {
providedMouseLeave = onMouseLeave;

return <svg />;
}}
</WithTooltip>,
);

providedMouseLeave({});
wrapper.update();
expect(passedOnMouseLeave).toHaveBeenCalledTimes(1);
});

it('it should use the provided `coords` if passed to onMouseMove', () => {
let mouseMove;
const wrapper = mount(
Expand Down
3 changes: 3 additions & 0 deletions packages/xy-chart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
"react": "^15.0.0-0 || ^16.0.0-0",
"react-dom": "^15.0.0-0 || ^16.0.0-0"
},
"resolutions": {
"@beemo/core": "1.0.8"
},
"beemo": {
"module": "@data-ui/build-config",
"drivers": [
Expand Down
8 changes: 6 additions & 2 deletions packages/xy-chart/src/chart/XYChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,14 @@ class XYChart extends React.PureComponent {
}

render() {
const { renderTooltip } = this.props;
const { renderTooltip, onMouseLeave, onMouseMove } = this.props;
if (renderTooltip) {
return (
<WithTooltip renderTooltip={renderTooltip}>
<WithTooltip
renderTooltip={renderTooltip}
onMouseLeave={onMouseLeave} // preserve these as WithTooltip will override them
onMouseMove={onMouseMove}
>
<XYChart {...this.props} renderTooltip={null} />
</WithTooltip>
);
Expand Down