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: 增加树状模式自定义宽度的容错 #2519

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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,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 @@ -54,8 +54,8 @@
[FrozenGroup.FROZEN_ROW].forEach((key) => {
if (!this.frozenGroupInfo[key].range) {
return;
}
let cols = [];

Check warning on line 58 in packages/s2-core/src/facet/pivot-facet.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/facet/pivot-facet.ts#L57-L58

Added lines #L57 - L58 were not covered by tests
let rows = [];
if (key.toLowerCase().includes('row')) {
const [rowMin, rowMax] = this.frozenGroupInfo[key].range;
Expand Down Expand Up @@ -336,7 +336,7 @@
hierarchy: colsHierarchy,
isRowHeader: false,
isSubTotal: true,
});

Check warning on line 339 in packages/s2-core/src/facet/pivot-facet.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/facet/pivot-facet.ts#L339

Added line #L339 was not covered by tests
this.adjustTotalNodesCoordinate({
hierarchy: colsHierarchy,
isRowHeader: false,
Expand Down Expand Up @@ -717,7 +717,7 @@
let res = 0;
for (let i = 0; i < multiple; i++) {
res += hierarchy.sampleNodesForAllLevels.find(
(sampleNode) => sampleNode.level === node.level + i,

Check warning on line 720 in packages/s2-core/src/facet/pivot-facet.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/facet/pivot-facet.ts#L720

Added line #L720 was not covered by tests
)[key];
}
node[key] = res;
Expand Down Expand Up @@ -854,20 +854,21 @@
// 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 @@
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
Loading