Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复自定义 tooltip 时, 刷选时无法获取到单元格信息 #2738

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Up @@ -14,6 +14,7 @@ import {
isEmpty,
isFunction,
isString,
last,
memoize,
some,
values,
Expand Down Expand Up @@ -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());
lijinke666 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 展示 Tooltip 提示
* @alias s2.tooltip.show()
Expand All @@ -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;
Expand All @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion packages/s2-core/src/utils/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
isNil,
isNumber,
isObject,
last,
map,
mapKeys,
noop,
Expand Down Expand Up @@ -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!,
Expand Down
Loading