Skip to content

Commit 898f3df

Browse files
author
嘤嘤嘤
authored
feat: 支持回调函数调整 Tooltip 位置 (#1350)
* feat: 支持回调函数调整 Tooltip 位置 * style: 代码优化
1 parent f6e5e8c commit 898f3df

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

packages/s2-core/__tests__/spreadsheet/tooltip-spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ describe('Tooltip Tests', () => {
5353
s2.destroy();
5454
});
5555

56+
test('should render tooltip in custom position', () => {
57+
const container = document.createElement('div');
58+
container.id = 'custom-container';
59+
document.body.appendChild(container);
60+
61+
const s2 = createS2({
62+
showTooltip: true,
63+
adjustPosition: (position) => {
64+
return { x: position.x + 100, y: position.y + 100 };
65+
},
66+
});
67+
68+
s2.render();
69+
70+
s2.showTooltip({ position: { x: 0, y: 0 } });
71+
72+
expect(s2.tooltip.position).toEqual({ x: 115, y: 110 });
73+
74+
s2.destroy();
75+
});
76+
5677
test('should render tooltip in custom container', () => {
5778
const container = document.createElement('div');
5879
container.id = 'custom-container';

packages/s2-core/src/common/interface/tooltip.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ export interface BaseTooltipConfig<T = TooltipContentType> {
153153
// Tooltip operation
154154
readonly operation?: TooltipOperation;
155155
readonly autoAdjustBoundary?: TooltipAutoAdjustBoundary;
156+
// Custom tooltip position
157+
readonly adjustPosition?: (position: TooltipPosition) => TooltipPosition;
158+
// Custom tooltip content
156159
readonly renderTooltip?: (spreadsheet: SpreadSheet) => BaseTooltip;
157160
// Custom tooltip mount container
158161
readonly getContainer?: () => HTMLElement;

packages/s2-core/src/ui/tooltip/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export class BaseTooltip {
4646
const { position, options, content } = showOptions;
4747
const { enterable } = getTooltipDefaultOptions(options);
4848
const container = this.getContainer();
49-
const { autoAdjustBoundary } = this.spreadsheet.options.tooltip || {};
50-
49+
const { autoAdjustBoundary, adjustPosition } =
50+
this.spreadsheet.options.tooltip || {};
5151
this.visible = true;
5252
this.options = showOptions as unknown as TooltipShowOptions;
5353

@@ -59,16 +59,14 @@ export class BaseTooltip {
5959
tooltipContainer: container,
6060
autoAdjustBoundary,
6161
});
62-
63-
this.position = {
62+
this.position = adjustPosition?.({ x, y }) ?? {
6463
x,
6564
y,
6665
};
67-
6866
setContainerStyle(container, {
6967
style: {
70-
left: `${x}px`,
71-
top: `${y}px`,
68+
left: `${this.position?.x}px`,
69+
top: `${this.position?.y}px`,
7270
pointerEvents: enterable ? 'all' : 'none',
7371
},
7472
className: `${TOOLTIP_CONTAINER_CLS}-show`,

packages/s2-core/src/utils/tooltip.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,10 @@ export const getTooltipOptions = (
595595
spreadsheet: SpreadSheet,
596596
event: CanvasEvent | MouseEvent | Event,
597597
): Tooltip => {
598-
const cellType = spreadsheet.getCellType?.(event.target);
598+
if (!event || !spreadsheet) {
599+
return;
600+
}
601+
const cellType = spreadsheet.getCellType?.(event?.target);
599602
return getTooltipOptionsByCellType(spreadsheet.options.tooltip, cellType);
600603
};
601604

s2-site/docs/common/tooltip.zh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ object **必选**,_default:null_ 功能描述: tooltip 配置
1818
| renderTooltip | 自定义整个 tooltip, 可以继承 BaseTooltip 自己重写一些方法 | [RenderTooltip](#rendertooltip) | - | |
1919
| content | 自定义 tooltip 内容 | `React.ReactNode | Element | string |` 或者 `(cell, defaultTooltipShowOptions) => React.ReactNode | Element | string` | - | |
2020
| autoAdjustBoundary | 当 tooltip 超过边界时自动调整显示位置,container: 图表区域,body: 整个浏览器窗口,设置为 `null` 可关闭此功能 | `container` \| `body` | `body` | |
21+
| adjustPosition | 自定义 tooltip 位置,| `({x: number, y: number}) => {x: number, y: number}` | | |
2122
| getContainer | 自定义 tooltip 挂载容器,| `() => HTMLElement` | `document.body` | |
2223

2324
### BaseTooltipConfig

0 commit comments

Comments
 (0)