Skip to content

Commit

Permalink
refactor: ♻️ separate TableSheet and PivotSheet from the SpreadSheet …
Browse files Browse the repository at this point in the history
…and close  #301 (#310)

* refactor: ♻️ add TableSheet and PivotSheet based on the SpreadSheet

* refactor: ♻️ remove the mode from S2Options

* chore: ✅ add test for checking sheetType switch

* chore: ✏️ correct type defination

* fix: optimize the pagination
  • Loading branch information
xingwanying authored Sep 18, 2021
1 parent e5e961e commit 2d14ebb
Show file tree
Hide file tree
Showing 42 changed files with 1,436 additions and 1,168 deletions.
4 changes: 2 additions & 2 deletions README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ const s2options = {
```

```ts
import { SpreadSheet } from '@antv/s2';
import { PivotSheet } from '@antv/s2';
import '@antv/s2/dist/s2.min.css'

const container = document.getElementById('container');

const s2 = new SpreadSheet(container, s2DataCfg, s2options)
const s2 = new PivotSheet(container, s2DataCfg, s2options)

s2.render()
```
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ const s2options = {
```

```ts
import { SpreadSheet } from '@antv/s2';
import { PivotSheet } from '@antv/s2';
import '@antv/s2/dist/s2.min.css'

const container = document.getElementById('container');

const s2 = new SpreadSheet(container, s2DataConfig, s2options)
const s2 = new PivotSheet(container, s2DataConfig, s2options)

s2.render()
```
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/README.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ const s2options = {
```

```ts
import { SpreadSheet } from '@antv/s2';
import { PivotSheet } from '@antv/s2';
import '@antv/s2/dist/s2.min.css'

const container = document.getElementById('container');

const s2 = new SpreadSheet(container, s2DataCfg, s2options)
const s2 = new PivotSheet(container, s2DataCfg, s2options)

s2.render()
```
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ const s2options = {
```

```ts
import { SpreadSheet } from '@antv/s2';
import { PivotSheet } from '@antv/s2';
import '@antv/s2/dist/s2.min.css'

const container = document.getElementById('container');

const s2 = new SpreadSheet(container, s2DataConfig, s2options)
const s2 = new PivotSheet(container, s2DataConfig, s2options)

s2.render()
```
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/__tests__/bugs/issue-285-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function MainLayout() {
dataCfg={dataCfg}
options={{}}
themeCfg={{ name: 'default' }}
forceUpdate={true}
forceUpdateDataCfg={true}
header={
<Switch
checkedChildren="有列头"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ import 'antd/dist/antd.min.css';
import ReactDOM from 'react-dom';
import React from 'react';
import { getContainer } from '../util/helpers';
import {
S2DataConfig,
S2Options,
SheetComponent,
SpreadSheet,
} from '../../src';
import { customTreeItems } from '../data/custom-tree-items';
import { dataCustomTrees } from '../data/data-custom-trees';
import { PivotSheet } from '@/sheet-type';
import { S2DataConfig, S2Options } from '@/common/interface';
import { SheetComponent } from '@/components';
import { transformCustomTreeItems } from '@/utils/layout/transform-custom-tree-items';

const getSpreadSheet = (
dom: string | HTMLElement,
dataCfg: S2DataConfig,
options: S2Options,
) => {
return new SpreadSheet(dom, dataCfg, options);
return new PivotSheet(dom, dataCfg, options);
};

const getDataCfg = () => {
Expand Down Expand Up @@ -50,7 +47,6 @@ const getOptions = () => {
hierarchyType: 'customTree',
hierarchyCollapse: false,
freezeRowHeader: false,
mode: 'pivot',
style: {
treeRowsWidth: 120,
collapsedRows: {},
Expand Down
8 changes: 4 additions & 4 deletions packages/s2-core/__tests__/spreadsheet/layout-hooks-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { act } from 'react-dom/test-utils';
import 'antd/dist/antd.min.css';
import ReactDOM from 'react-dom';
import React from 'react';
import { getContainer, getMockData } from '../util/helpers';
import {
GetCellMeta,
Node,
S2DataConfig,
S2Options,
SheetComponent,
SpreadSheet,
PivotSheet,
SpreadSheetFacetCfg,
ViewMeta,
} from '../../src';
import { getContainer, getMockData } from '../util/helpers';
} from '@/index';
import { LayoutHierarchyReturnType } from '@/common/interface/hooks';
import { generateId } from '@/utils/layout/generate-id';
const data = getMockData('../data/tableau-supermarket.csv');
Expand All @@ -23,7 +24,7 @@ const getSpreadSheet = (
dataCfg: S2DataConfig,
options: S2Options,
) => {
innerSS = new SpreadSheet(dom, dataCfg, options);
innerSS = new PivotSheet(dom, dataCfg, options);
return innerSS;
};

Expand Down Expand Up @@ -160,7 +161,6 @@ const getOptions = () => {
hierarchyType: 'grid',
hierarchyCollapse: false,
freezeRowHeader: true,
mode: 'pivot',
style: {
treeRowsWidth: 120,
collapsedRows: {},
Expand Down
22 changes: 11 additions & 11 deletions packages/s2-core/__tests__/spreadsheet/merge-cells-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
S2DataConfig,
S2Options,
SheetComponent,
SpreadSheet,
PivotSheet,
SheetType,
} from '@/index';

let data = getMockData('../data/tableau-supermarket.csv');
Expand All @@ -30,7 +31,7 @@ const getSpreadSheet = (
dataCfg: S2DataConfig,
options: S2Options,
) => {
return new SpreadSheet(dom, dataCfg, options);
return new PivotSheet(dom, dataCfg, options);
};

const baseDataCfg: S2DataConfig = {
Expand Down Expand Up @@ -103,7 +104,6 @@ const baseOptions = {
hierarchyCollapse: false,
showSeriesNumber: true,
freezeRowHeader: false,
mode: 'pivot',
valueInCols: true,
conditions: {
text: [],
Expand Down Expand Up @@ -339,31 +339,31 @@ const tabularOptions = {
],
} as S2Options;

const getDataCfg = (sheetType: 'base' | 'tabular') => {
const getDataCfg = (sheetType: SheetType) => {
switch (sheetType) {
case 'tabular':
return tabularDataCfg;
case 'base':
case 'pivot':
default:
return baseDataCfg;
}
};

const getOptions = (sheetType: 'base' | 'tabular') => {
const getOptions = (sheetType: SheetType) => {
switch (sheetType) {
case 'tabular':
return tabularOptions;
case 'base':
case 'pivot':
default:
return baseOptions;
}
};

function MainLayout() {
const [sheetType, setSheetType] = React.useState<'base' | 'tabular'>('base');
const [options, setOptions] = React.useState<S2Options>(getOptions('base'));
const [sheetType, setSheetType] = React.useState<SheetType>('pivot');
const [options, setOptions] = React.useState<S2Options>(getOptions('pivot'));
const [dataCfg, setDataCfg] = React.useState<S2DataConfig>(
getDataCfg('base'),
getDataCfg('pivot'),
);

let sheet;
Expand Down Expand Up @@ -408,7 +408,7 @@ function MainLayout() {
};

const onCheckChanged = (checked) => {
const type = checked ? 'base' : 'tabular';
const type = checked ? 'pivot' : 'tabular';
setSheetType(type);
setDataCfg(getDataCfg(type));
setOptions(getOptions(type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
S2Options,
SheetComponent,
SpreadSheet,
PivotSheet,
} from '../../src';
import {
multipleDataWithBottom,
Expand All @@ -23,7 +24,7 @@ const getSpreadSheet = (
dataCfg: S2DataConfig,
options: S2Options,
) => {
sheet = new SpreadSheet(dom, dataCfg, options);
sheet = new PivotSheet(dom, dataCfg, options);
(window as any).sheet = sheet;
return sheet;
};
Expand Down Expand Up @@ -84,7 +85,6 @@ const getOptions = (): S2Options => {
hierarchyCollapse: false,
showSeriesNumber: true,
freezeRowHeader: false,
mode: 'pivot',
conditions: {
text: [],
interval: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const getOptions = () => {
hierarchyCollapse: false,
showSeriesNumber: false,
freezeRowHeader: false,
mode: 'pivot',
style: {
treeRowsWidth: 100,
collapsedRows: {},
Expand Down
5 changes: 2 additions & 3 deletions packages/s2-core/__tests__/spreadsheet/pressure-test-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
S2DataConfig,
S2Options,
SheetComponent,
SpreadSheet,
PivotSheet,
} from '../../src';
import { getContainer } from '../util/helpers';

Expand All @@ -29,7 +29,7 @@ const getSpreadSheet = (
dataCfg: S2DataConfig,
options: S2Options,
) => {
return new SpreadSheet(dom, dataCfg, options);
return new PivotSheet(dom, dataCfg, options);
};

const getDataCfg = () => {
Expand Down Expand Up @@ -61,7 +61,6 @@ const getOptions = () => {
hierarchyCollapse: false,
showSeriesNumber: false,
freezeRowHeader: false,
mode: 'pivot',
totals: {
row: {
showGrandTotals: true,
Expand Down
39 changes: 34 additions & 5 deletions packages/s2-core/__tests__/spreadsheet/spread-sheet-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ import React from 'react';
import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';
import { getContainer } from '../util/helpers';
import { SheetEntry } from '../util/sheet-entry';
import { SheetEntry, assembleDataCfg } from '../util/sheet-entry';
// import * as tableData from '../data/mock-dataset.json';
import { CustomTooltip } from './custom/custom-tooltip';
import { ThemeName } from '@/index';
import { ThemeName, SheetType } from '@/index';

const tableDataFields = {
fields: {
columns: ['province', 'city', 'type', 'sub_type', 'price'],
valueInCols: true,
},
};

function MainLayout() {
const [dataCfg, setDataCfg] = React.useState(assembleDataCfg({}));
const [render, setRender] = React.useState(true);

const [sheetType, setSheetType] = React.useState<SheetType>('pivot');
const [spotLight, setSpotLight] = React.useState(true);
const [isPivotSheet, setIsPivotSheet] = React.useState(true);
const [hoverHighlight, setHoverHighlight] = React.useState(true);
const [showPagination, setShowPagination] = React.useState(false);
const [showTooltip, setShowTooltip] = React.useState(true);

const [themeName, setThemeName] = React.useState<ThemeName>('default');

const onToggleRender = () => {
Expand All @@ -38,6 +47,19 @@ function MainLayout() {
selectedCellsSpotlight: spotLight,
hoverHighlight: hoverHighlight,
};

const onSheetTypeChange = (checked) => {
setIsPivotSheet(checked);
// 透视表
if (checked) {
setSheetType('pivot');
setDataCfg(assembleDataCfg({}));
} else {
setSheetType('table');
setDataCfg(assembleDataCfg(tableDataFields));
}
};

return (
<div>
<Space size="middle" style={{ marginBottom: 20 }}>
Expand All @@ -50,9 +72,10 @@ function MainLayout() {
</Space>
{render && (
<SheetEntry
dataCfg={{}}
dataCfg={dataCfg}
options={mergedOptions}
themeCfg={{ name: themeName }}
sheetType={sheetType}
header={
<Space size="middle" style={{ marginBottom: 20 }}>
<Radio.Group onChange={onRadioChange} defaultValue="default">
Expand Down Expand Up @@ -86,6 +109,12 @@ function MainLayout() {
checked={showTooltip}
onChange={setShowTooltip}
/>
<Switch
checkedChildren="透视表"
unCheckedChildren="明细表"
checked={isPivotSheet}
onChange={onSheetTypeChange}
/>
</Space>
}
/>
Expand Down
10 changes: 5 additions & 5 deletions packages/s2-core/__tests__/spreadsheet/table-sheet-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import 'antd/dist/antd.min.css';
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';
import { getContainer, getMockData } from '../util/helpers';
import {
S2DataConfig,
S2Event,
S2Options,
SheetComponent,
SpreadSheet,
} from '../../src';
import { getContainer, getMockData } from '../util/helpers';

TableSheet,
} from '@/index';
const data = getMockData('../data/tableau-supermarket.csv');

const getSpreadSheet =
(ref) =>
(dom: string | HTMLElement, dataCfg: S2DataConfig, options: S2Options) => {
const s2 = new SpreadSheet(dom, dataCfg, options);
const s2 = new TableSheet(dom, dataCfg, options);
ref.current = s2;
return s2;
};
Expand Down Expand Up @@ -85,7 +85,6 @@ function MainLayout() {
width: 800,
height: 600,
showSeriesNumber: true,
mode: 'table',
enableCopy: true,
style: {
colCfg: {
Expand Down Expand Up @@ -141,6 +140,7 @@ function MainLayout() {
dataCfg={dataCfg}
adaptive={false}
options={options}
sheetType={'table'}
spreadsheet={getSpreadSheet(s2Ref)}
/>
</div>
Expand Down
Loading

0 comments on commit 2d14ebb

Please sign in to comment.