Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 修复自定义 tooltip 时, 刷选时无法获取到单元格信息
Browse files Browse the repository at this point in the history
lijinke666 committed May 23, 2024
1 parent 979eeee commit 7544a0c
Showing 3 changed files with 22 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import type {
} from '../../../src';
import { PivotDataSet } from '../../../src/data-set';
import { PivotFacet } from '../../../src/facet';
import { createMockCellInfo } from '../../util/helpers';
import { customMerge, setupDataConfig } from '@/utils';
import { BaseTooltip } from '@/ui/tooltip';
import { PivotSheet, SpreadSheet } from '@/sheet-type';
@@ -1370,4 +1371,15 @@ describe('PivotSheet Tests', () => {
// eslint-disable-next-line no-underscore-dangle
expect(canvas.__s2_instance__).toBe(undefined);
});

test('should get last interacted cell if event target is empty', () => {
const cellA = createMockCellInfo('A');
const cellB = createMockCellInfo('B');

s2.interaction.setInteractedCells(cellA);
s2.interaction.setInteractedCells(cellB);

// @ts-ignore
expect(s2.getTargetCell(null)).toEqual(cellB);
});
});
10 changes: 8 additions & 2 deletions packages/s2-core/src/sheet-type/spread-sheet.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import {
isEmpty,
isFunction,
isString,
last,
memoize,
some,
values,
@@ -240,6 +241,11 @@ export abstract class SpreadSheet extends EE {
return this.options.tooltip?.render?.(this) || new BaseTooltip(this);
}

private getTargetCell(target: CellEventTarget) {
// 刷选等场景, 以最后一个发生交互的单元格为准
return this.getCell(target) || last(this.interaction.getInteractedCells());
}

/**
* 展示 Tooltip 提示
* @alias s2.tooltip.show()
@@ -258,7 +264,7 @@ export abstract class SpreadSheet extends EE {
Menu = BaseTooltipOperatorMenuOptions,
>(showOptions: TooltipShowOptions<T, Menu>): Promise<void> {
const { content, event } = showOptions;
const cell = this.getCell(event?.target);
const cell = this.getTargetCell(event?.target);
const displayContent = isFunction(content)
? content(cell!, showOptions)
: content;
@@ -285,7 +291,7 @@ export abstract class SpreadSheet extends EE {
return;
}

const targetCell = this.getCell(event?.target);
const targetCell = this.getTargetCell(event?.target);
const tooltipData =
options?.data ??
getTooltipData({
3 changes: 2 additions & 1 deletion packages/s2-core/src/utils/tooltip.ts
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import {
isNil,
isNumber,
isObject,
last,
map,
mapKeys,
noop,
@@ -716,7 +717,7 @@ export const getTooltipOptions = (
const cellType = spreadsheet.getCellType?.(event?.target);

// 如果没有 cellType, 说明是刷选丢失 event target 的场景, 此时从产生过交互状态的单元格里取, 避免刷选读取不到争取 tooltip 配置的问题
const sampleCell = interaction.getInteractedCells()[0];
const sampleCell = last(interaction.getInteractedCells());

return getTooltipOptionsByCellType(
options.tooltip!,

0 comments on commit 7544a0c

Please sign in to comment.