Skip to content

Commit d91920e

Browse files
committed
chore: simplify code
1 parent 0b53334 commit d91920e

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

docs/examples/expandedRowSpan.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ const columns: ColumnsType = [
1111
width: 100,
1212
onCell: (_, index) => {
1313
const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};
14-
if (index === 0) {
14+
if (index === 1) {
1515
props.rowSpan = 4;
16-
} else if (index === 1) {
17-
props.rowSpan = 0;
1816
} else if (index === 2) {
1917
props.rowSpan = 0;
2018
} else if (index === 3) {
2119
props.rowSpan = 0;
2220
} else if (index === 4) {
23-
props.rowSpan = 1;
21+
props.rowSpan = 0;
2422
}
2523

2624
return props;
@@ -33,6 +31,7 @@ const columns: ColumnsType = [
3331
];
3432

3533
const data = [
34+
{ a: '12313132132', b: '0571-43243256', c: '小二', d: '文零西路', e: 'Male', key: 'z' },
3635
{ a: '13812340987', b: '0571-12345678', c: '张三', d: '文一西路', e: 'Male', key: 'a' },
3736
{ a: '13812340987', b: '0571-12345678', c: '张夫人', d: '文一西路', e: 'Female', key: 'b' },
3837
{ a: '13812340987', b: '0571-099877', c: '李四', d: '文二西路', e: 'Male', key: 'c' },

src/Body/BodyRow.tsx

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,35 +75,20 @@ export function getCellProps<RecordType>(
7575
let additionalCellProps: React.TdHTMLAttributes<HTMLElement>;
7676
if (column.onCell) {
7777
additionalCellProps = column.onCell(record, index);
78-
// 开启 expanded 的增加下面逻辑
79-
if (expandable) {
80-
// 当前为合并单元格开始
81-
if (additionalCellProps.rowSpan > 0) {
82-
// 本身展开 +1
83-
if (expanded) {
84-
additionalCellProps.rowSpan = additionalCellProps.rowSpan + 1;
78+
const { rowSpan } = additionalCellProps;
79+
80+
// For expandable row with rowSpan,
81+
// We should increase the rowSpan if the row is expanded
82+
if (expandable && rowSpan > 1) {
83+
let currentRowSpan = rowSpan;
84+
85+
for (let i = index; i < rowSpan; i += 1) {
86+
const rowKey = rowKeys[i];
87+
if (expandedKeys.has(rowKey)) {
88+
currentRowSpan += 1;
8589
}
86-
// 下面如果是 0 的,增加 +1 逻辑
87-
const addChildrenRowSpan = (rowSpan: number, idx: number) => {
88-
const nextIndex = idx + 1;
89-
const rowKey = rowKeys[nextIndex];
90-
let _rowSpan = rowSpan;
91-
if (rowKey !== undefined) {
92-
const thisCellProps = column.onCell(record, nextIndex);
93-
if (thisCellProps.rowSpan === 0) {
94-
const thisExpanded = expandedKeys.has(rowKey);
95-
if (thisExpanded) {
96-
_rowSpan = _rowSpan + 1;
97-
}
98-
// 继续往下找
99-
return addChildrenRowSpan(_rowSpan, nextIndex);
100-
}
101-
}
102-
// 找不到后返回
103-
return _rowSpan;
104-
};
105-
additionalCellProps.rowSpan = addChildrenRowSpan(additionalCellProps.rowSpan, index);
10690
}
91+
additionalCellProps.rowSpan = currentRowSpan;
10792
}
10893
}
10994

0 commit comments

Comments
 (0)