Skip to content

Commit

Permalink
fix: 修复树状模式当一组数据只有一条数据时, 叶子节点判断错误, 也渲染了展开/收起图标 close #2804 (#2806)
Browse files Browse the repository at this point in the history
fix: 修复树状模式下一组数据只有一条数据时, 叶子节点判断错误, 也渲染了展开/收起图标 close #2804
  • Loading branch information
lijinke666 authored Jul 5, 2024
1 parent 2c28e84 commit fa5bb9b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
33 changes: 33 additions & 0 deletions packages/s2-core/__tests__/bugs/issue-2804-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/**
* 透视表树状模式当某一组数据只有一条数据时, 叶子节点判断错误, 也渲染了展开/收起图标.
* @description spec for issue #2804
* https://github.com/antvis/S2/issues/2804
*/
import { S2Options } from '../../src';
import * as mockDataConfig from '../data/data-issue-2804.json';
import { getContainer } from '../util/helpers';
import { PivotSheet } from '@/sheet-type';

const s2Options: S2Options = {
width: 800,
height: 600,
hierarchyType: 'tree',
};

describe('Tree Leaf Node Status Tests', () => {
test('should get correctly tree icon and leaf node status', () => {
const s2 = new PivotSheet(getContainer(), mockDataConfig, s2Options);

s2.render();

const [a1, a2] = s2.getRowNodes();

expect(a1.isLeaf).toBeTruthy();
expect(a1.isTotals).toBeFalsy();
expect(a2.isLeaf).toBeFalsy();
expect(a2.isTotals).toBeTruthy();
expect(a1.belongsCell.treeIcon).not.toBeDefined();
expect(a2.belongsCell.treeIcon).toBeDefined();
});
});
21 changes: 21 additions & 0 deletions packages/s2-core/__tests__/data/data-issue-2804.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"fields": {
"rows": ["row0", "row1"],
"columns": ["col0"],
"values": ["cost"],
"valueInCols": true
},
"data": [
{
"row0": "a-1",
"col0": "b-1",
"cost": 2
},
{
"row0": "a-2",
"row1": "a-2-1",
"col0": "b-2",
"cost": 3
}
]
}
19 changes: 13 additions & 6 deletions packages/s2-core/src/facet/layout/build-row-tree-hierarchy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNumber } from 'lodash';
import { i18n } from '../../common';
import { isEmpty, isNumber } from 'lodash';
import { i18n, TOTAL_VALUE } from '../../common';
import type { SpreadSheet } from '../../sheet-type';
import { filterOutDetail } from '../../utils/data-set-operate';
import { generateId } from '../../utils/layout/generate-id';
Expand Down Expand Up @@ -118,11 +118,18 @@ export const buildRowTreeHierarchy = (params: TreeHeaderParams) => {
hierarchy.maxLevel = level;
}

const emptyChildren = !pivotMetaValue?.children?.size;
if (emptyChildren || isTotals) {
/**
* 除了虚拟行小计节点外, 如果为空, 说明当前分组只有一条数据, 应该标记为叶子节点.
* https://github.com/antvis/S2/issues/2804
*/
const children = [...(pivotMetaValue?.children?.keys() || [])].filter(
(child) => child !== TOTAL_VALUE,
);
const isEmptyChildren = isEmpty(children);
if (isEmptyChildren || isTotals) {
node.isLeaf = true;
}
if (!emptyChildren) {
if (!isEmptyChildren) {
node.isTotals = true;
}

Expand All @@ -133,7 +140,7 @@ export const buildRowTreeHierarchy = (params: TreeHeaderParams) => {
hierarchy,
);

if (!emptyChildren && !isCollapse && !isTotals && expandCurrentNode) {
if (!isEmptyChildren && !isCollapse && !isTotals && expandCurrentNode) {
buildRowTreeHierarchy({
level: level + 1,
currentField: pivotMetaValue.childField,
Expand Down

0 comments on commit fa5bb9b

Please sign in to comment.