Skip to content

Commit

Permalink
fix: 修复自定义 dataCell 错误的传参写法 (#2748)
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 authored May 31, 2024
1 parent 535d2e0 commit c54ca82
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 54 deletions.
3 changes: 2 additions & 1 deletion packages/s2-core/__tests__/unit/facet/pivot-facet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ jest.mock('@/sheet-type', () => {
return {
dataCfg: assembleDataCfg(),
options: assembleOptions({
dataCell: (viewMeta) => new DataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new DataCell(viewMeta, spreadsheet),
}),
container,
theme: getTheme({}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,8 @@ describe('<StrategySheet/> Tests', () => {
}

const s2Options: SheetComponentOptions = {
dataCell: (viewMeta) =>
new CustomDataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new CustomDataCell(viewMeta, spreadsheet),
};

renderStrategySheet(s2Options, StrategySheetDataConfig);
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-react/src/components/sheets/chart-sheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export const ChartSheet: React.FC<SheetComponentsProps> = React.memo(

const s2Options = React.useMemo<SheetComponentOptions>(() => {
const options: SheetComponentOptions = {
dataCell: (viewMeta) =>
new ChartSheetDataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new ChartSheetDataCell(viewMeta, spreadsheet),
showDefaultHeaderActionIcon: false,
interaction: {
hoverFocus: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export const GridAnalysisSheet: React.FC<SheetComponentsProps> = React.memo(

const s2Options = React.useMemo<SheetComponentOptions>(() => {
const options: SheetComponentOptions = {
dataCell: (viewMeta) =>
new GridAnalysisSheetDataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new GridAnalysisSheetDataCell(viewMeta, spreadsheet),
showDefaultHeaderActionIcon: false,
style: {
colCell: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type ViewMeta } from '@antv/s2';
import { isEmpty, size } from 'lodash';
import React from 'react';
import { customMerge, Node, SpreadSheet, type ColHeaderConfig } from '@antv/s2';
Expand Down Expand Up @@ -37,8 +36,8 @@ export const StrategySheet: React.FC<SheetComponentsProps> = React.memo(

return {
hierarchyType: 'tree',
dataCell: (viewMeta: ViewMeta) =>
new StrategySheetDataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new StrategySheetDataCell(viewMeta, spreadsheet),
colCell: (
node: Node,
spreadsheet: SpreadSheet,
Expand Down
2 changes: 1 addition & 1 deletion s2-site/docs/api/general/S2Options.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const s2Options = {
## DataCellCallback

```js
DataCellCallback = (viewMeta: ViewMeta, s2: Spreadsheet) => G.Group;
DataCellCallback = (viewMeta: ViewMeta, spreadsheet: SpreadSheet) => G.Group;
```

功能描述:自定义数值单元格。[查看示例](/examples/custom/custom-cell#data-cell)
Expand Down
2 changes: 1 addition & 1 deletion s2-site/docs/common/custom/dataCellCallback.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ order: 2
## DataCellCallback

```js
DataCellCallback = (viewMeta: ViewMeta) => DataCell;
DataCellCallback = (viewMeta: ViewMeta, spreadsheet: SpreadSheet) => DataCell | TableDataCell;
```

功能描述:自定义数值单元格,[ViewMeta](#viewmeta)
Expand Down
6 changes: 3 additions & 3 deletions s2-site/docs/manual/advanced/chart-in-cell.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ class CustomDataCell extends DataCell {
}

const s2Options = {
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta?.spreadsheet);
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet);
},
};
```
Expand Down Expand Up @@ -348,7 +348,7 @@ class ChartSheetDataCell extends DataCell {
}

const s2 = new PivotSheet(container, s2DataConfig, {
dataCell: (viewMeta) => new ChartSheetDataCell(viewMeta, viewMeta.spreadsheet)
dataCell: (viewMeta, spreadsheet) => new ChartSheetDataCell(viewMeta, spreadsheet)
});

await s2.render();
Expand Down
16 changes: 16 additions & 0 deletions s2-site/docs/manual/migration-v2.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,22 @@ const s2Options = {

如有消费请注意修改,具体请查看 [源代码定义](https://github.com/antvis/S2/tree/next/packages/s2-core/src/common/constant).

#### 自定义数值单元格参数变更

增加第二个参数 `spreadsheet`, 和其他单元格保持一致。

```diff
const s2Options = {
width: 600,
- dataCell: (viewMeta) => {
- return new CustomDataCell(viewMeta, viewMeta.spreadsheet);
- }
+ dataCell: (viewMeta, spreadsheet) => {
+ return new CustomDataCell(viewMeta, spreadsheet);
+ }
}
```

### 组件层 <Badge>@antv/s2-react</Badge>

#### 支持 React 18 和 Ant Design 5.0
Expand Down
4 changes: 2 additions & 2 deletions s2-site/examples/case/art/demo/time-spend-abstract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ fetch('https://assets.antv.antgroup.com/s2/time-spend.json')
width: 1150,
height: 720,
showDefaultHeaderActionIcon: false,
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta?.spreadsheet);
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet);
},
interaction: {
hoverHighlight: false,
Expand Down
30 changes: 14 additions & 16 deletions s2-site/examples/case/comparison/demo/measure-comparison.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,10 @@ class CustomDataCell extends DataCell {

// 自定义icon显示
getIconStyle() {
const tagName = Object.keys(this.customConditions).find(
(item) =>
this.meta.colId?.includes(
`root${NODE_ID_SEPARATOR}${item}${NODE_ID_SEPARATOR}`,
),
const tagName = Object.keys(this.customConditions).find((item) =>
this.meta.colId?.includes(
`root${NODE_ID_SEPARATOR}${item}${NODE_ID_SEPARATOR}`,
),
);

if (tagName) {
Expand All @@ -218,11 +217,10 @@ class CustomDataCell extends DataCell {

drawTextShape() {
const { fieldValue } = this.meta;
const tagName = Object.keys(this.textConfig).find(
(item) =>
this.meta.colId?.includes(
`root${NODE_ID_SEPARATOR}${item}${NODE_ID_SEPARATOR}`,
),
const tagName = Object.keys(this.textConfig).find((item) =>
this.meta.colId?.includes(
`root${NODE_ID_SEPARATOR}${item}${NODE_ID_SEPARATOR}`,
),
);

if (!tagName) {
Expand Down Expand Up @@ -511,8 +509,8 @@ fetch(
return parseFloat(fieldValue) > 0
? 'CellUp'
: fieldValue < 0
? 'CellDown'
: '';
? 'CellDown'
: '';
};

const conditionsIcon = [
Expand Down Expand Up @@ -548,8 +546,8 @@ fetch(
parseFloat(value) > 0
? UP_COLOR
: parseFloat(value) < 0
? DOWN_COLOR
: textStyle?.fill,
? DOWN_COLOR
: textStyle?.fill,
};
};

Expand Down Expand Up @@ -585,8 +583,8 @@ fetch(
lineConfigStyle,
);
},
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta.spreadsheet, {
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet, {
lineConfig,
lineConfigStyle,
conditions: {
Expand Down
4 changes: 2 additions & 2 deletions s2-site/examples/case/comparison/demo/time-spend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ fetch('https://assets.antv.antgroup.com/s2/time-spend.json')
width: 1150,
height: 420,
showDefaultHeaderActionIcon: false,
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta?.spreadsheet);
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet);
},
frame: (cfg) => {
return new CustomFrame(cfg);
Expand Down
4 changes: 2 additions & 2 deletions s2-site/examples/case/kpi-strategy/demo/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ fetch('https://assets.antv.antgroup.com/s2/kpi-strategy.json')
// 自定义角头文本
cornerText: '指标',
// 覆盖默认数值单元格, 额外绘制衍生指标和子弹图
dataCell: (viewMeta) =>
new KpiStrategyDataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new KpiStrategyDataCell(viewMeta, spreadsheet),
};

// 覆盖默认主题, 让单元格文字靠左显示
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ fetch(
rowCell: (node, spreadsheet, headerConfig) => {
return new CustomRowCell(node, spreadsheet, headerConfig);
},
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta?.spreadsheet);
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet);
},
};

Expand Down
17 changes: 12 additions & 5 deletions s2-site/examples/custom/custom-cell/demo/custom-table-cell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/* eslint-disable max-classes-per-file */
import { TableColCell, TableDataCell, TableSheet } from '@antv/s2';
import {
TableColCell,
TableDataCell,
TableSheet,
type S2DataConfig,
type S2Options,
} from '@antv/s2';

/**
* 自定义 TableDataCell,通过复写基类方法, 给特定单元格设置背景色, 文字大小, 颜色等...
Expand Down Expand Up @@ -115,14 +121,15 @@ fetch(
.then((res) => res.json())
.then((res) => {
const container = document.getElementById('container');
const s2DataConfig = {
const s2DataConfig: S2DataConfig = {
fields: {
columns: ['province', 'city', 'type', 'sub_type', 'number'],
},
meta: res.meta,
data: res.data,
};
const s2Options = {

const s2Options: S2Options = {
width: 600,
height: 480,
seriesNumber: {
Expand All @@ -131,8 +138,8 @@ fetch(
colCell: (node, spreadsheet, headerConfig) => {
return new CustomColCell(node, spreadsheet, headerConfig);
},
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta?.spreadsheet);
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet);
},
};

Expand Down
4 changes: 2 additions & 2 deletions s2-site/examples/custom/custom-cell/demo/data-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ fetch(
// 关闭 hover 十字高亮, 为了视觉效果,可不设置
hoverHighlight: false,
},
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta?.spreadsheet);
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet);
},
};

Expand Down
4 changes: 2 additions & 2 deletions s2-site/examples/custom/custom-cell/demo/mini-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ fetch(
},
],
},
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta?.spreadsheet);
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet);
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ fetch(
height: 480,
hierarchyType: 'tree',
dataSet: (spreadsheet) => new CustomDataSet(spreadsheet),
dataCell: (viewMeta) =>
new CustomDataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new CustomDataCell(viewMeta, spreadsheet),
};

const s2 = new PivotSheet(container, s2DataConfig, s2Options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ fetch(
colCell: (node, spreadsheet, headerConfig) => {
return new CustomColCell(node, spreadsheet, headerConfig);
},
dataCell: (viewMeta) => {
return new CustomDataCell(viewMeta, viewMeta?.spreadsheet);
dataCell: (viewMeta, spreadsheet) => {
return new CustomDataCell(viewMeta, spreadsheet);
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const s2Options: S2Options = {
height: 400,
},
},
dataCell: (viewMeta) =>
new ChartSheetDataCell(viewMeta, viewMeta.spreadsheet),
dataCell: (viewMeta, spreadsheet) =>
new ChartSheetDataCell(viewMeta, spreadsheet),
};

const s2DataConfig: S2DataConfig = {
Expand Down

0 comments on commit c54ca82

Please sign in to comment.