From 03982f9e1c84a00c16c85ae7e9b29fe350d3b2f0 Mon Sep 17 00:00:00 2001 From: orangii Date: Wed, 4 Sep 2024 00:35:12 +0800 Subject: [PATCH] fix: correctly hide tooltip when point out Signed-off-by: orangii --- packages/g6/__tests__/demos/plugin-tooltip.ts | 16 ++++++++++++++++ packages/g6/src/plugins/tooltip.ts | 6 ++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/g6/__tests__/demos/plugin-tooltip.ts b/packages/g6/__tests__/demos/plugin-tooltip.ts index 7e3d88c9067..fa08e4fd499 100644 --- a/packages/g6/__tests__/demos/plugin-tooltip.ts +++ b/packages/g6/__tests__/demos/plugin-tooltip.ts @@ -1,4 +1,5 @@ import data from '@@/dataset/combo.json'; +import type { IElementEvent } from '@antv/g6'; import { Graph } from '@antv/g6'; export const pluginTooltip: TestCase = async (context) => { @@ -33,6 +34,7 @@ export const pluginTooltip: TestCase = async (context) => { pluginTooltip.form = (panel) => { const config = { trigger: 'click', + enable: 'all', }; return [ panel @@ -46,6 +48,20 @@ export const pluginTooltip: TestCase = async (context) => { }), ); }), + panel + .add(config, 'enable', ['all', 'node', 'edge', 'combo']) + .name('Change Enable Target') + .onChange((enable: string) => { + graph.setPlugins((plugins) => + plugins.map((plugin) => { + if (typeof plugin === 'object' && plugin.type === 'tooltip') { + if (enable === 'all') return { ...plugin, enable: true }; + else return { ...plugin, enable: (event: IElementEvent) => event.targetType === enable }; + } + return plugin; + }), + ); + }), ]; }; diff --git a/packages/g6/src/plugins/tooltip.ts b/packages/g6/src/plugins/tooltip.ts index 2bf52b76d88..89066a3b363 100644 --- a/packages/g6/src/plugins/tooltip.ts +++ b/packages/g6/src/plugins/tooltip.ts @@ -151,8 +151,8 @@ export class Tooltip extends BasePlugin { } = event; // click the same item twice, tooltip will be hidden if (this.currentTarget === id) { - this.currentTarget = null; this.hide(event); + this.currentTarget = null; } else { this.currentTarget = id; this.show(event); @@ -294,7 +294,9 @@ export class Tooltip extends BasePlugin { this.tooltipElement?.hide(); return; } - if (!this.tooltipElement || !this.isEnable(event)) return; + if (!this.tooltipElement) return; + // No target node: tooltip has been hidden. No need for duplicated call. + if (!this.currentTarget) return; const { client: { x, y }, } = event;