Skip to content

Commit

Permalink
fix: 修复刷选时无法获取正确的 tooltip 配置
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Nov 30, 2023
1 parent 9c8c2d7 commit 23d15ff
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Data Cell Tests Data Cell Formatter & Method Tests should get empty chart data 1`] = `
exports[`Data Cell Tests Data Cell Formatter & Method Tests should get empty chart data and default options 1`] = `
Object {
"autoFit": true,
"height": 83,
Expand All @@ -13,7 +13,7 @@ Object {
}
`;

exports[`Data Cell Tests Data Cell Formatter & Method Tests should get multiple chart data 1`] = `
exports[`Data Cell Tests Data Cell Formatter & Method Tests should get multiple chart data and all options 1`] = `
Object {
"autoFit": true,
"data": Array [
Expand All @@ -31,7 +31,7 @@ Object {
}
`;

exports[`Data Cell Tests Data Cell Formatter & Method Tests should get multiple chart data 2`] = `
exports[`Data Cell Tests Data Cell Formatter & Method Tests should get multiple chart data and all options 2`] = `
Object {
"autoFit": true,
"data": Array [
Expand Down
15 changes: 15 additions & 0 deletions packages/s2-core/__tests__/unit/cell/data-cell-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,24 @@ describe('Data Cell Tests', () => {
const dataCell = new DataCell(meta, s2);

expect(dataCell.isMultiData()).toBeFalsy();
expect(dataCell.isChartData()).toBeFalsy();
expect(dataCell.getRenderChartData()).toBeUndefined();
expect(dataCell.getRenderChartOptions()).toMatchSnapshot();
});

test('should get correctly cell data status', () => {
const multipleMeta = {
fieldValue: {
values: [1, 2, 3],
},
} as unknown as ViewMeta;

const dataCell = new DataCell(multipleMeta, s2);

expect(dataCell.isMultiData()).toBeTruthy();
expect(dataCell.isChartData()).toBeFalsy();
});

test('should get multiple chart data and all options', () => {
s2.setThemeCfg({ name: 'dark' });

Expand Down Expand Up @@ -177,6 +191,7 @@ describe('Data Cell Tests', () => {
const dataCell = new DataCell(multipleMeta, s2);

expect(dataCell.isMultiData()).toBeTruthy();
expect(dataCell.isChartData()).toBeTruthy();
expect(dataCell.getRenderChartData()).toMatchSnapshot();
expect(dataCell.getRenderChartOptions()).toMatchSnapshot();
});
Expand Down
38 changes: 36 additions & 2 deletions packages/s2-core/__tests__/unit/utils/tooltip-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
PivotSheet,
type S2DataConfig,
type S2Options,
type TooltipOperatorMenuItems,
} from '@/index';
import type { BaseFacet } from '@/facet/base-facet';
import type { BBox } from '@/engine';
Expand Down Expand Up @@ -256,6 +257,9 @@ describe('Tooltip Utils Tests', () => {
options: {
tooltip,
},
interaction: {
getInteractedCells: jest.fn(() => []),
},
} as unknown as SpreadSheet;

expect(getTooltipOptions(spreadsheet, {} as Event)).toEqual({
Expand Down Expand Up @@ -306,6 +310,9 @@ describe('Tooltip Utils Tests', () => {
options: {
tooltip,
},
interaction: {
getInteractedCells: jest.fn(() => []),
},
} as unknown as SpreadSheet;

const tooltipOptions = omit(
Expand All @@ -330,16 +337,43 @@ describe('Tooltip Utils Tests', () => {
},
);

test('should use interacted cell type if cannot get current cell type', () => {
const mockInteractedCell = { cellType: CellType.DATA_CELL };

const tooltip: Tooltip = {
enable: true,
[CellType.DATA_CELL]: {
enable: false,
},
content: '',
operation: {},
};

const spreadsheet = {
getCellType: () => undefined,
options: {
tooltip,
},
interaction: {
getInteractedCells: jest.fn(() => [mockInteractedCell]),
},
} as unknown as SpreadSheet;

const tooltipOptions = getTooltipOptions(spreadsheet, {} as Event);

expect(tooltipOptions?.enable).toBeFalsy();
});

test('should filter not displayed tooltip operation menus', () => {
const mockCell = {
cellType: CellType.DATA_CELL,
} as unknown as S2CellType;
const onClick = jest.fn();

const defaultMenus = [
const defaultMenus: TooltipOperatorMenuItems = [
{
key: 'default-menu',
text: 'default-menu',
label: 'default-menu',
},
];

Expand Down
46 changes: 29 additions & 17 deletions packages/s2-core/src/cell/data-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,38 @@ import {
isEmpty,
isEqual,
isObject,
isPlainObject,
merge,
} from 'lodash';
import { BaseCell } from '../cell/base-cell';
import { EMPTY_PLACEHOLDER } from '../common/constant/basic';
import {
CellType,
InteractionStateName,
SHAPE_STYLE_MAP,
} from '../common/constant/interaction';
import {
CellBorderPosition,
CellClipBox,
type HeaderActionNameOptions,
type IconCondition,
type InteractionStateTheme,
type RenderTextShapeOptions,
} from '../common/interface';
import type {
BaseChartData,
CellMeta,
Condition,
FormatResult,
ConditionMappingResult,
FormatResult,
MiniChartData,
MultiData,
TextTheme,
ThemeName,
ViewMeta,
ViewMetaIndexType,
MultiData,
BaseChartData,
ThemeName,
} from '../common/interface';
import {
CellBorderPosition,
CellClipBox,
type HeaderActionNameOptions,
type IconCondition,
type InteractionStateTheme,
type RenderTextShapeOptions,
} from '../common/interface';
import { getFieldValueOfViewMetaData } from '../data-set/cell-data';
import {
getHorizontalTextIconPosition,
getVerticalIconPosition,
Expand All @@ -45,12 +49,10 @@ import {
shouldUpdateBySelectedCellsHighlight,
updateBySelectedCellsHighlight,
} from '../utils/cell/data-cell';
import { groupIconsByPosition } from '../utils/cell/header-cell';
import { getIconPosition } from '../utils/condition/condition';
import { updateShapeAttr } from '../utils/g-renders';
import { EMPTY_PLACEHOLDER } from '../common/constant/basic';
import { drawInterval } from '../utils/g-mini-charts';
import { getFieldValueOfViewMetaData } from '../data-set/cell-data';
import { groupIconsByPosition } from '../utils/cell/header-cell';
import { updateShapeAttr } from '../utils/g-renders';
import type { RawData } from './../common/interface/s2DataConfig';

/**
Expand All @@ -74,7 +76,17 @@ export class DataCell extends BaseCell<ViewMeta> {
}

public isMultiData() {
return isObject(this.getMeta().fieldValue);
const fieldValue = this.getFieldValue();

return isObject(fieldValue);
}

public isChartData() {
const fieldValue = this.getFieldValue();

return isPlainObject(
(fieldValue as unknown as MultiData<MiniChartData>)?.values,
);
}

public getRenderChartData(): BaseChartData {
Expand Down
20 changes: 14 additions & 6 deletions packages/s2-core/src/utils/export/copy/pivot-data-cell-copy.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { find, isEmpty, map, slice, zip } from 'lodash';
import { find, isEmpty, isPlainObject, map, slice, zip } from 'lodash';
import {
AsyncRenderThreshold,
type CellMeta,
type DataItem,
EXTRA_FIELD,
VALUE_FIELD,
type CellMeta,
type DataItem,
type MiniChartData,
type MultiData,
} from '../../../common';
import type { Node } from '../../../facet/layout/node';
import type { SpreadSheet } from '../../../sheet-type';
import type {
CopyableList,
CopyAllDataParams,
CopyableList,
MeasureQuery,
SheetCopyConstructorParams,
} from '../interface';
Expand All @@ -22,6 +24,7 @@ import {
getSelectedRows,
} from '../method';
import type { BaseDataSet } from './../../../data-set/base-data-set';
import { BaseDataCellCopy } from './base-data-cell-copy';
import {
assembleMatrix,
completeMatrix,
Expand All @@ -30,7 +33,6 @@ import {
getNodeFormatData,
} from './common';
import { getHeaderNodeFromMeta } from './core';
import { BaseDataCellCopy } from './base-data-cell-copy';

export class PivotDataCellCopy extends BaseDataCellCopy {
protected leafRowNodes: Node[] = [];
Expand Down Expand Up @@ -229,7 +231,13 @@ export class PivotDataCellCopy extends BaseDataCellCopy {
dataSet,
);

return formatter(cellData?.[VALUE_FIELD] ?? '');
const fieldValue = cellData?.[VALUE_FIELD];
const isChartData = isPlainObject(
(fieldValue as MultiData<MiniChartData>)?.values,
);
const value = isChartData ? '' : fieldValue;

return formatter(value ?? '');
};

protected getCornerMatrix = (rowMatrix?: string[][]): string[][] => {
Expand Down
9 changes: 8 additions & 1 deletion packages/s2-core/src/utils/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,16 @@ export const getTooltipOptions = (
return null;
}

const { options, interaction } = spreadsheet;
const cellType = spreadsheet.getCellType?.(event?.target);

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

return getTooltipOptionsByCellType(
options.tooltip!,
cellType || sampleCell.cellType!,
);
};

export const getTooltipVisibleOperator = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { waitFor } from '@testing-library/react';
import React from 'react';
import type { Root } from 'react-dom/client';
import { ChartDataConfig } from '../../../../data/data-g2-chart';
import { renderComponent, sleep } from '../../../../util/helpers';
import { renderComponent } from '../../../../util/helpers';
import {
SheetComponent,
type SheetComponentOptions,
Expand Down Expand Up @@ -79,27 +79,4 @@ describe('<ChartSheet/> Tests', () => {
});
});
});

test('should not throw g2 error after render chart', async () => {
const errorSpy = jest
.spyOn(console, 'error')
.mockImplementationOnce(() => {});

renderChartSheet(null, {
onDataCellRender,
});

await waitFor(async () => {
s2.updateScrollOffset({ offsetY: { value: 800, animate: true } });

await sleep(1000);

expect(errorSpy).not.toHaveBeenCalledWith(
`Uncaught (in promise) TypeError: Cannot read property 'defaultView' of null`,
);
expect(errorSpy).not.toHaveBeenCalledWith(
`Uncaught (in promise) TypeError: Cannot read properties of null (reading 'createElement')`,
);
});
});
});
1 change: 1 addition & 0 deletions packages/s2-react/playground/components/ChartSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const options: SheetComponentOptions = {
height: 900,
interaction: {
enableCopy: true,
copyWithFormat: true,
brushSelection: {
rowCell: true,
colCell: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/s2-react/playground/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export const TableSheetFrozenOptions: S2TableSheetFrozenOptions = {
};

export const S2TooltipOptions: SheetComponentOptions['tooltip'] = {
enable: true,
operation: {
hiddenColumns: true,
menu: {
Expand Down Expand Up @@ -287,6 +288,7 @@ export const s2Options: SheetComponentOptions = {
cornerText: '测试测试测试测试测试测试测试测试测试测试',
interaction: {
enableCopy: true,
copyWithFormat: true,
// 防止 mac 触摸板横向滚动触发浏览器返回, 和移动端下拉刷新
overscrollBehavior: 'none',
brushSelection: {
Expand Down
Loading

0 comments on commit 23d15ff

Please sign in to comment.