Skip to content

Commit

Permalink
feat: tooltip interaction emit events when body hide (#5157)
Browse files Browse the repository at this point in the history
* feat: tooltip interaction support showContent params

* fix: ci

---------

Co-authored-by: MiniPear <pearminipro@gmail.com>
  • Loading branch information
pepper-nice and pearmini authored Jun 27, 2023
1 parent 7bf049c commit b8491c3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { chartEmitItemTooltipHideContent as render } from '../plots/api/chart-emit-item-tooltip-hide-content';
import './utils/useSnapshotMatchers';
import {
dispatchFirstElementEvent,
createPromise,
receiveExpectData,
} from './utils/event';
import { createDOMGCanvas } from './utils/createDOMGCanvas';

describe('chart.emit', () => {
const canvas = createDOMGCanvas(800, 500);

it('chart.tooltip hide body should emit events.', async () => {
const { finished, chart, clear } = render({
canvas,
container: document.createElement('div'),
});
await finished;
clear();

// chart.on("tooltip:hide") should be called when hiding tooltip.
const [tooltipHided, resolveHide] = createPromise();
chart.on('tooltip:hide', receiveExpectData(resolveHide, null));
dispatchFirstElementEvent(canvas, 'pointerout');
await tooltipHided;
});

afterAll(() => {
canvas?.destroy();
});
});
70 changes: 70 additions & 0 deletions __tests__/plots/api/chart-emit-item-tooltip-hide-content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Chart } from '../../../src';

export function chartEmitItemTooltipHideContent(context) {
const { container, canvas } = context;

// wrapperDiv
const wrapperDiv = document.createElement('div');
container.appendChild(wrapperDiv);

// button
const button = document.createElement('button');
button.innerText = 'Hide tooltip';
container.appendChild(button);

// p
const p = document.createElement('p');
p.innerText = '';
container.appendChild(p);

const chart = new Chart({
theme: 'classic',
container: wrapperDiv,
canvas,
});

chart
.interval()
.data([
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
])
.encode('x', 'genre')
.encode('y', 'sold')
.encode('color', 'genre')
.interaction('tooltip', {
body: false,
});

const finished = chart.render();

finished.then((chart) =>
chart.emit('tooltip:show', {
data: { data: { sold: 115 } },
}),
);

chart.on('tooltip:show', ({ data }) => {
p.innerText = JSON.stringify(data);
});

const hide = () => (p.innerText = 'null');

chart.on('tooltip:hide', hide);

button.onclick = () => {
chart.emit('tooltip:hide');
};

return {
chart,
button,
finished,
clear: () => {
chart.off('tooltip:hide', hide);
},
};
}
1 change: 1 addition & 0 deletions __tests__/plots/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export { chartEmitBrushHighlightAxisHorizontal } from './chart-emit-brush-highli
export { chartEmitBrushHighlightAxisCross } from './chart-emit-brush-highlight-axis-cross';
export { chartEmitScrollbarFilter } from './chart-emit-scrollbar-filter';
export { chartOptionsCompositeMark } from './chart-options-composite-mark';
export { chartEmitItemTooltipHideContent } from './chart-emit-item-tooltip-hide-content';
35 changes: 19 additions & 16 deletions src/interaction/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ function showTooltip({
}

function hideTooltip({ root, single, emitter, nativeEvent = true, mount }) {
if (nativeEvent) {
emitter.emit('tooltip:hide', { nativeEvent });
}
const container = single ? getContainer(root, mount) : root;
const { tooltipElement } = container;
if (tooltipElement) {
tooltipElement.hide();
if (nativeEvent) {
emitter.emit('tooltip:hide', { nativeEvent });
}
}
}

Expand Down Expand Up @@ -640,6 +640,7 @@ export function tooltip(
view,
mount,
bounding,
body = true,
}: Record<string, any>,
) {
const elements = elementsof(root);
Expand Down Expand Up @@ -674,19 +675,21 @@ export function tooltip(
}

const { offsetX, offsetY } = event;
showTooltip({
root,
data,
x: offsetX,
y: offsetY,
render,
event,
single,
position,
enterable,
mount,
bounding,
});
if (body) {
showTooltip({
root,
data,
x: offsetX,
y: offsetY,
render,
event,
single,
position,
enterable,
mount,
bounding,
});
}

emitter.emit('tooltip:show', {
...event,
Expand Down

0 comments on commit b8491c3

Please sign in to comment.