diff --git a/packages/g6/__tests__/demos/index.ts b/packages/g6/__tests__/demos/index.ts index cb2e13f8057..d1fe8f8743e 100644 --- a/packages/g6/__tests__/demos/index.ts +++ b/packages/g6/__tests__/demos/index.ts @@ -149,7 +149,7 @@ export { pluginToolbarIconfont } from './plugin-toolbar-iconfont'; export { pluginTooltip } from './plugin-tooltip'; export { pluginTooltipAsync } from './plugin-tooltip-async'; export { pluginTooltipDual } from './plugin-tooltip-dual'; -export { pluginTooltipEnable } from './plugin-tooltip-enable'; +export { pluginTooltipEnable, pluginTooltipPrefixClsEnable } from './plugin-tooltip-enable'; export { pluginTooltipWithCustomNode } from './plugin-tooltip-with-custom-node'; export { pluginWatermark } from './plugin-watermark'; export { pluginWatermarkImage } from './plugin-watermark-image'; diff --git a/packages/g6/__tests__/demos/plugin-tooltip-enable.ts b/packages/g6/__tests__/demos/plugin-tooltip-enable.ts index bfdda87c687..0b41a8a3231 100644 --- a/packages/g6/__tests__/demos/plugin-tooltip-enable.ts +++ b/packages/g6/__tests__/demos/plugin-tooltip-enable.ts @@ -35,3 +35,39 @@ export const pluginTooltipEnable: TestCase = async (context) => { return graph; }; + +export const pluginTooltipPrefixClsEnable: TestCase = async (context) => { + const graph = new Graph({ + ...context, + data: { + nodes: [ + { id: 'node11', style: { x: 150, y: 100 }, data: { type: 'test1', desc: 'This is a tooltip' } }, + { id: 'node22', style: { x: 150, y: 200 }, data: { type: 'test1', desc: '' } }, + { id: 'node33', style: { x: 150, y: 300 }, data: { type: 'test2', desc: 'This is a tooltip' } }, + ], + }, + node: { + style: { + labelText: (d) => d.id, + }, + }, + plugins: [ + { + key: 'tooltip', + type: 'tooltip', + trigger: 'click', + prefixCls: 'g6prefix-', + enable: (e: IElementEvent, items: ElementDatum[]) => { + return items[0].data?.type === 'test1'; + }, + getContent: (evt: IElementEvent, items: ElementDatum[]) => { + return items[0].data?.desc || ''; + }, + }, + ], + }); + + await graph.render(); + + return graph; +}; diff --git a/packages/g6/__tests__/unit/plugins/tooltip.spec.ts b/packages/g6/__tests__/unit/plugins/tooltip.spec.ts index 5a6828b5a7f..2aa8b3d9f78 100644 --- a/packages/g6/__tests__/unit/plugins/tooltip.spec.ts +++ b/packages/g6/__tests__/unit/plugins/tooltip.spec.ts @@ -1,6 +1,6 @@ import type { Tooltip } from '@/src'; import { ComboEvent, EdgeEvent, NodeEvent, idOf } from '@/src'; -import { pluginTooltip, pluginTooltipAsync, pluginTooltipEnable } from '@@/demos'; +import { pluginTooltip, pluginTooltipAsync, pluginTooltipEnable, pluginTooltipPrefixClsEnable } from '@@/demos'; import { createDemoGraph } from '@@/utils'; describe('plugin tooltip', () => { @@ -66,6 +66,26 @@ describe('plugin tooltip', () => { graph.destroy(); }); + it('enable-prefixCls', async () => { + const graph = await createDemoGraph(pluginTooltipPrefixClsEnable); + const container = graph.getCanvas().getContainer()!; + const el = container.querySelector('.tooltip'); + expect(el).toBeNull(); + + const prefixEl = container.querySelector('.g6prefix-tooltip') as HTMLDivElement; + expect(prefixEl).not.toBeNull(); + + const plugin = graph.getPluginInstance('tooltip'); + + await plugin.showById('node33'); + expect(prefixEl.style.visibility).toBe('hidden'); + + await plugin.showById('node11'); + expect(prefixEl.style.visibility).toBe('visible'); + + graph.destroy(); + }); + it('get content null', async () => { const graph = await createDemoGraph(pluginTooltipEnable); const container = graph.getCanvas().getContainer()!; diff --git a/packages/g6/src/plugins/tooltip.ts b/packages/g6/src/plugins/tooltip.ts index c363c204e92..f26f3d2ee61 100644 --- a/packages/g6/src/plugins/tooltip.ts +++ b/packages/g6/src/plugins/tooltip.ts @@ -45,6 +45,12 @@ export interface TooltipOptions * Callback executed when visibility of the tooltip card is changed */ onOpenChange: (open: boolean) => void; + /** + * 自定义类名前缀 + * + * Custom class name prefix + */ + prefixCls?: string; } /** @@ -59,18 +65,24 @@ export class Tooltip extends BasePlugin { enterable: false, enable: true, offset: [10, 10], - style: { - '.tooltip': { - visibility: 'hidden', - }, - }, }; private currentTarget: string | null = null; private tooltipElement: TooltipComponent | null = null; private container: HTMLElement | null = null; constructor(context: RuntimeContext, options: TooltipOptions) { - super(context, Object.assign({}, Tooltip.defaultOptions, options)); + const combineOptions = Object.assign( + { + style: { + [`.${options.prefixCls || ''}tooltip`]: { + visibility: 'hidden', + }, + }, + }, + Tooltip.defaultOptions, + options, + ); + super(context, combineOptions); this.render(); this.bindEvents(); } @@ -299,7 +311,7 @@ export class Tooltip extends BasePlugin { x, y, style: { - '.tooltip': { + [`.${this.options.prefixCls || ''}tooltip`]: { visibility: 'visible', }, }, @@ -350,12 +362,14 @@ export class Tooltip extends BasePlugin { enterable, offset, style, + template: { + prefixCls: this.options.prefixCls || '', + }, }; } private initTooltip = () => { const tooltipElement = new TooltipComponent({ - className: 'tooltip', style: this.tooltipStyleProps, }); this.container?.appendChild(tooltipElement.HTMLTooltipElement); diff --git a/packages/site/docs/manual/plugin/Tooltip.en.md b/packages/site/docs/manual/plugin/Tooltip.en.md index 861bc3886e7..d4e2b22cc34 100644 --- a/packages/site/docs/manual/plugin/Tooltip.en.md +++ b/packages/site/docs/manual/plugin/Tooltip.en.md @@ -44,6 +44,7 @@ const graph = new Graph({ | enterable | Whether pointer can enter | boolean | false | | | title | Title | string | - | | style | Style object | Record | {'.tooltip': { visibility: 'hidden'}} | | +| prefixCls | custom class name prefix | string | - | | ## Detailed Configuration diff --git a/packages/site/docs/manual/plugin/Tooltip.zh.md b/packages/site/docs/manual/plugin/Tooltip.zh.md index 9646e136741..a204decc5fc 100644 --- a/packages/site/docs/manual/plugin/Tooltip.zh.md +++ b/packages/site/docs/manual/plugin/Tooltip.zh.md @@ -44,6 +44,7 @@ const graph = new Graph({ | enterable | 指针是否可以进入 | boolean | false | | | title | 标题 | string | - | | style | 样式对象 | Record | {'.tooltip': { visibility: 'hidden'}} | | +| prefixCls | tooltip自定义类名前缀 | string | - | | ## 详细配置说明