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: 修复角头和行头折叠展开 icon 的状态未同步以及展开异常的问题 close #2607 #2620

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SpreadSheet Collapse/Expand Tests Tree Mode should only expand current group row nodes 1`] = `
Array [
"root[&]浙江省",
"root[&]浙江省[&]杭州市",
"root[&]浙江省[&]绍兴市",
"root[&]浙江省[&]宁波市",
"root[&]浙江省[&]舟山市",
"root[&]四川省",
]
`;

exports[`SpreadSheet Collapse/Expand Tests Tree Mode should only expand current group row nodes for custom tree 1`] = `
Array [
"root[&]a-1",
"root[&]a-1[&]a-1-1",
"root[&]a-1[&]a-1-2",
"root[&]a-2",
"root[&]a-3",
]
`;

exports[`SpreadSheet Collapse/Expand Tests Tree Mode should sync corner cell collapse all icon status 1`] = `
Array [
"root[&]浙江省",
"root[&]四川省",
]
`;

exports[`SpreadSheet Collapse/Expand Tests should only expand current group row nodes for custom tree 1`] = `
Array [
"root[&]a-1",
"root[&]a-1[&]a-1-1",
"root[&]a-1[&]a-1-2",
"root[&]a-2",
"root[&]a-3",
]
`;

exports[`SpreadSheet Collapse/Expand Tests should sync corner cell collapse all icon status 1`] = `
Array [
"root[&]浙江省",
"root[&]四川省",
]
`;
138 changes: 125 additions & 13 deletions packages/s2-core/__tests__/spreadsheet/spread-sheet-collapse-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as mockDataConfig from 'tests/data/simple-data.json';
import { getContainer } from 'tests/util/helpers';
import type { RowCellCollapsedParams } from '../../src/common/interface';
import { createPivotSheet, getContainer, sleep } from 'tests/util/helpers';
import {
CornerNodeType,
type RowCellCollapsedParams,
type S2DataConfig,
type S2Options,
} from '../../src/common/interface';
import { customRowGridFields } from '../data/custom-grid-fields';
import { CustomGridData } from '../data/data-custom-grid';
import { S2Event } from './../../src/common/constant/events/basic';
import { PivotSheet, SpreadSheet } from '@/sheet-type';
import type { Node } from '@/facet/layout/node';
Expand All @@ -9,9 +16,30 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
let container: HTMLElement;
let s2: SpreadSheet;

const s2Options: S2Options = {
width: 600,
height: 400,
hierarchyType: 'tree',
style: {
rowCell: {
expandDepth: undefined,
},
},
};

const mapNodes = (spreadsheet: SpreadSheet) =>
spreadsheet.facet.getRowLeafNodes().map((node) => node.id);

const expectCornerIconName = (spreadsheet: SpreadSheet, iconName: string) => {
const cornerCell = spreadsheet.facet
.getCornerCells()
.find((cell) => cell.getMeta().cornerType === CornerNodeType.Row)!;

const cornerIcon = cornerCell.getTreeIcon();

expect(cornerIcon?.name).toEqual(iconName);
};

beforeEach(async () => {
container = getContainer();
s2 = new PivotSheet(
Expand All @@ -25,22 +53,13 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
valueInCols: true,
},
},
{
width: 600,
height: 200,
hierarchyType: 'tree',
style: {
rowCell: {
expandDepth: undefined,
},
},
},
s2Options,
);
await s2.render();
});

afterEach(() => {
s2.destroy();
// s2.destroy();
});

describe('Tree Mode', () => {
Expand Down Expand Up @@ -101,6 +120,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
"root[&]浙江",
]
`);
expectCornerIconName(s2, 'Plus');
});

test('should collapse by field', async () => {
Expand All @@ -121,6 +141,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
"root[&]浙江",
]
`);
expectCornerIconName(s2, 'Plus');
});

test('should collapse by field id', async () => {
Expand All @@ -144,6 +165,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
"root[&]浙江[&]杭州[&]笔",
]
`);
expectCornerIconName(s2, 'Plus');
});

test('should collapse use collapseFields first', async () => {
Expand All @@ -167,6 +189,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
"root[&]浙江[&]杭州",
]
`);
expectCornerIconName(s2, 'Plus');
});

test('should collapse use expandDepth first', async () => {
Expand All @@ -189,6 +212,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
"root[&]浙江[&]杭州[&]笔",
]
`);
expectCornerIconName(s2, 'Minus');
});

test('should collapse use collapseFields first when contain collapseAll and expandDepth config', async () => {
Expand All @@ -213,6 +237,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
"root[&]浙江[&]杭州",
]
`);
expectCornerIconName(s2, 'Plus');
});

test('should collapse use collapseFields by node id first', async () => {
Expand All @@ -233,6 +258,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
"root[&]浙江",
]
`);
expectCornerIconName(s2, 'Plus');
});

test('should collapse all nodes if collapseAll is true and collapseFields is undefined', async () => {
Expand All @@ -251,6 +277,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
"root[&]浙江",
]
`);
expectCornerIconName(s2, 'Plus');
});

test('should emit collapse event', () => {
Expand All @@ -275,6 +302,7 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
s2.emit(S2Event.ROW_CELL_COLLAPSED__PRIVATE, treeRowType);

expect(onCollapsed).toHaveBeenCalledWith(params);
expectCornerIconName(s2, 'Minus');
});

test('should emit collapse all event', () => {
Expand All @@ -285,6 +313,90 @@ describe('SpreadSheet Collapse/Expand Tests', () => {
s2.emit(S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE, false);

expect(onCollapsed).toHaveBeenCalledWith(true);
expectCornerIconName(s2, 'Minus');
});

// https://github.com/antvis/S2/issues/2607
test('should only expand current group row nodes', async () => {
s2 = createPivotSheet(
{
...s2Options,
style: {
rowCell: {
collapseAll: true,
},
},
},
{ useSimpleData: false },
);

await s2.render();

const rowNodes = s2.facet.getRowNodes(0);

s2.emit(S2Event.ROW_CELL_COLLAPSED__PRIVATE, {
isCollapsed: false,
node: rowNodes[0],
});

await sleep(500);

expectCornerIconName(s2, 'Minus');
expect(s2.facet.getRowNodes().map(({ id }) => id)).toMatchSnapshot();
});

test('should sync corner cell collapse all icon status', async () => {
s2 = createPivotSheet(
{
...s2Options,
style: {
rowCell: {
collapseAll: false,
collapseFields: {
'root[&]浙江省': true,
'root[&]四川省': true,
},
},
},
},
{ useSimpleData: false },
);

await s2.render();

expectCornerIconName(s2, 'Plus');
expect(s2.facet.getRowNodes().map(({ id }) => id)).toMatchSnapshot();
});

test('should only expand current group row nodes for custom tree', async () => {
const customRowDataCfg: S2DataConfig = {
data: CustomGridData,
fields: customRowGridFields,
};

s2 = createPivotSheet({
...s2Options,
style: {
rowCell: {
collapseAll: true,
},
},
});

s2.setDataCfg(customRowDataCfg);
await s2.render();

const rowNodes = s2.facet.getRowNodes(0);

s2.emit(S2Event.ROW_CELL_COLLAPSED__PRIVATE, {
isCollapsed: false,
node: rowNodes[0],
});

await sleep(500);

expectCornerIconName(s2, 'Minus');
expect(s2.facet.getRowNodes().map(({ id }) => id)).toMatchSnapshot();
});
});
});
14 changes: 10 additions & 4 deletions packages/s2-core/src/cell/corner-cell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PointLike } from '@antv/g';
import { last } from 'lodash';
import { last, values } from 'lodash';
import {
CellType,
KEY_GROUP_CORNER_RESIZE_AREA,
Expand Down Expand Up @@ -61,10 +61,16 @@ export class CornerCell extends HeaderCell<CornerHeaderConfig> {
return;
}

const { collapseAll } = this.spreadsheet.options.style?.rowCell!;
const { collapseFields = {} } = this.spreadsheet.options.style?.rowCell!;
const { size = 0 } = this.getStyle()!.icon!;
const { fill } = this.getTextStyle();
const area = this.getBBoxByType(CellClipBox.CONTENT_BOX);
const rootRowNodes = this.spreadsheet.facet.getRowNodes(0);
// 任意一级节点的 icon 展开/收起, 同步更新角头 icon 的状态
const isAllCollapsed =
values(collapseFields!).filter(Boolean).length === rootRowNodes.length ||
rootRowNodes.every((node) => node.isCollapsed);
const isCollapsed = isAllCollapsed;

this.treeIcon = renderTreeIcon({
group: this,
Expand All @@ -75,12 +81,12 @@ export class CornerCell extends HeaderCell<CornerHeaderConfig> {
height: size,
fill,
},
isCollapsed: collapseAll,
isCollapsed,
onClick: () => {
this.spreadsheet.facet.resetScrollY();
this.spreadsheet.emit(
S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE,
collapseAll!,
isCollapsed,
);
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const buildCustomTreeHierarchy = (params: CustomTreeHeaderParams) => {

const defaultCollapsed = collapsed ?? false;
const isDefaultCollapsed =
collapseFields?.[nodeId] || collapseFields?.[field];
collapseFields?.[nodeId] ?? collapseFields?.[field];
const isCollapsed = isDefaultCollapsed ?? (collapseAll || defaultCollapsed);

// TODO: 平铺模式支持 折叠/展开
Expand Down
1 change: 0 additions & 1 deletion packages/s2-core/src/sheet-type/pivot-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export class PivotSheet extends SpreadSheet {
this.setOptions({
style: {
rowCell: {
collapseAll: false,
collapseFields,
},
},
Expand Down
5 changes: 5 additions & 0 deletions packages/s2-react/playground/components/CustomTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export const CustomTreeOptions: SheetComponentOptions = {
withFormat: true,
},
},
style: {
rowCell: {
collapseAll: true,
},
},
// cornerText: '指标',
};

Expand Down
2 changes: 1 addition & 1 deletion packages/s2-react/playground/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export const s2Options: SheetComponentOptions = {
debug: true,
width: 800,
height: 600,
hierarchyType: 'grid',
hierarchyType: 'tree',
seriesNumber: {
enable: true,
},
Expand Down
6 changes: 5 additions & 1 deletion s2-site/docs/manual/migration-v2.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,11 @@ const s2DataConfig = {

具体请查看 [自定义行列头分组](/manual/advanced/custom/custom-header) 相关文档。

#### 行列冻结配置调整
#### 树状结构 icon 折叠展开状态同步

现在行头节点的 icon 展开/收起,会同步更新角头 icon(全部展开/收起)的状态。

#### 行列冻结配置

透视表和明细表的行列冻结配置统一收拢到 `frozen`.

Expand Down
Loading