Skip to content

Commit

Permalink
fix: 增加树状模式自定义宽度的容错 (#2519)
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 authored Jan 18, 2024
1 parent c037628 commit a28322a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SpreadSheet Resize Active Tests should render correctly layout when tree row width is invalid number 1`] = `
Array [
Object {
"height": 30,
"id": "root[&]浙江",
"width": 120,
},
Object {
"height": 30,
"id": "root[&]浙江[&]义乌",
"width": 120,
},
Object {
"height": 30,
"id": "root[&]浙江[&]杭州",
"width": 120,
},
]
`;
30 changes: 30 additions & 0 deletions packages/s2-core/__tests__/spreadsheet/spread-sheet-resize-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,34 @@ describe('SpreadSheet Resize Active Tests', () => {
expect(group.findById(KEY_GROUP_ROW_RESIZE_AREA)).toBeNull();
expect(group.findById(KEY_GROUP_CORNER_RESIZE_AREA)).toBeNull();
});

test('should render correctly layout when tree row width is invalid number', () => {
const s2 = renderSheet(null);

s2.setOptions({
hierarchyType: 'tree',
style: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
treeRowsWidth: '#',
rowCfg: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
treeRowsWidth: '@',
},
},
});

s2.render(false);

const nodes = s2.getRowNodes().map((node) => {
return {
id: node.id,
width: node.width,
height: node.height,
};
});

expect(nodes).toMatchSnapshot();
});
});
9 changes: 6 additions & 3 deletions packages/s2-core/src/facet/pivot-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,20 +854,21 @@ export class PivotFacet extends FrozenFacet {
// 1. 用户拖拽或手动指定的行头宽度优先级最高
// TODO: 由于历史原因, 存在两个行头宽度, (1. style.rowCfg.treeRowsWidth 2.style.treeRowsWidth) 暂时保持兼容
const currentTreeRowsWidth = treeRowsWidth ?? rowCfg?.treeRowsWidth;
if (currentTreeRowsWidth) {
if (isNumber(currentTreeRowsWidth)) {
return currentTreeRowsWidth;
}

// 2. 其次是自定义
const customRowWidth = this.getCellCustomWidth(null, rowCfg?.width);
if (customRowWidth) {
if (isNumber(customRowWidth)) {
return customRowWidth;
}

// 3. 然后是计算 (+ icon province/city/level)
const treeHeaderLabel = rows
.map((key: string): string => dataSet.getFieldName(key))
.join('/');

const { bolderText: cornerCellTextStyle, icon: cornerIconStyle } =
this.spreadsheet.theme.cornerCell;
// 初始化角头时,保证其在树形模式下不换行
Expand All @@ -882,10 +883,12 @@ export class PivotFacet extends FrozenFacet {
this.rowCellTheme.padding?.left +
this.rowCellTheme.padding?.right;

return Math.max(
const width = Math.max(
currentTreeRowsWidth ?? DEFAULT_TREE_ROW_WIDTH,
maxLabelWidth,
);

return Number.isNaN(width) ? DEFAULT_TREE_ROW_WIDTH : width;
}

/**
Expand Down

0 comments on commit a28322a

Please sign in to comment.