diff --git a/packages/s2-core/__tests__/bugs/issue-2804-spec.ts b/packages/s2-core/__tests__/bugs/issue-2804-spec.ts new file mode 100644 index 0000000000..209152b0eb --- /dev/null +++ b/packages/s2-core/__tests__/bugs/issue-2804-spec.ts @@ -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(); + }); +}); diff --git a/packages/s2-core/__tests__/data/data-issue-2804.json b/packages/s2-core/__tests__/data/data-issue-2804.json new file mode 100644 index 0000000000..36479a1da9 --- /dev/null +++ b/packages/s2-core/__tests__/data/data-issue-2804.json @@ -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 + } + ] +} diff --git a/packages/s2-core/src/facet/layout/build-row-tree-hierarchy.ts b/packages/s2-core/src/facet/layout/build-row-tree-hierarchy.ts index 1dc795ef1b..4cc15df0e6 100644 --- a/packages/s2-core/src/facet/layout/build-row-tree-hierarchy.ts +++ b/packages/s2-core/src/facet/layout/build-row-tree-hierarchy.ts @@ -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'; @@ -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; } @@ -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,