Skip to content

Commit 92616be

Browse files
committed
chore: back of part
1 parent 9aef85d commit 92616be

File tree

4 files changed

+76
-30
lines changed

4 files changed

+76
-30
lines changed

docs/examples/expandedSticky.tsx

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,45 @@ const Demo = () => {
2727
{ key: 'b', a: '张三', d: '文一西路' },
2828
{ key: 'c', a: '张三', d: '文二西路' },
2929
];
30-
const rowKeys = data.map(item => item.key);
30+
// const rowKeys = data.map(item => item.key);
3131

32-
const rowSpanList = getRowSpan(data.map(item => item.a));
32+
// const rowSpanList = getRowSpan(data.map(item => item.a));
3333

3434
const columns: ColumnType<Record<string, any>>[] = [
3535
{
3636
title: '手机号',
3737
dataIndex: 'a',
3838
width: 100,
39+
colSpan: 2,
3940
// fixed: 'left',
4041
onCell: (_, index) => {
41-
const { rowSpan = 1 } = rowSpanList[index];
42-
const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};
43-
props.rowSpan = rowSpan;
44-
if (rowSpan >= 1) {
45-
let currentRowSpan = rowSpan;
46-
for (let i = index; i < index + rowSpan; i += 1) {
47-
const rowKey = rowKeys[i];
48-
if (expandedRowKeys.includes(rowKey)) {
49-
currentRowSpan += 1;
50-
}
51-
}
52-
props.rowSpan = currentRowSpan;
42+
// const { rowSpan = 1 } = rowSpanList[index];
43+
// const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};
44+
// props.rowSpan = rowSpan;
45+
// if (rowSpan >= 1) {
46+
// let currentRowSpan = rowSpan;
47+
// for (let i = index; i < index + rowSpan; i += 1) {
48+
// const rowKey = rowKeys[i];
49+
// if (expandedRowKeys.includes(rowKey)) {
50+
// currentRowSpan += 1;
51+
// }
52+
// }
53+
// props.rowSpan = currentRowSpan;
54+
// }
55+
// return props;
56+
57+
if (index === 1) {
58+
return {
59+
rowSpan: 2,
60+
};
61+
} else if (index === 2) {
62+
return {
63+
rowSpan: 0,
64+
};
5365
}
54-
return props;
5566
},
5667
},
68+
{ title: 'key', dataIndex: 'key2', colSpan: 0, width: 100 },
5769
Table.EXPAND_COLUMN,
5870
{ title: 'key', dataIndex: 'key' },
5971
{ title: 'Address', fixed: 'right', dataIndex: 'd', width: 200 },
@@ -65,11 +77,11 @@ const Demo = () => {
6577
<Table<Record<string, any>>
6678
rowKey="key"
6779
sticky
68-
scroll={{ x: 2000 }}
80+
scroll={{ x: 1000 }}
6981
columns={columns}
7082
data={data}
7183
expandable={{
72-
expandedRowOffset: 1,
84+
expandedRowOffset: 2,
7385
expandedRowKeys,
7486
onExpandedRowsChange: keys => setExpandedRowKeys(keys),
7587
expandedRowRender: record => <p style={{ margin: 0 }}>expandedRowRender: {record.key}</p>,

src/Body/BodyRow.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ export interface BodyRowProps<RecordType> {
2222
scopeCellComponent: CustomizeComponent;
2323
indent?: number;
2424
rowKey: React.Key;
25+
rowKeys: React.Key[];
2526

2627
// Expanded Row
2728
expandedRowInfo?: {
29+
offset: number;
2830
colSpan: number;
2931
sticky: number;
3032
};
@@ -39,6 +41,8 @@ export function getCellProps<RecordType>(
3941
colIndex: number,
4042
indent: number,
4143
index: number,
44+
rowKeys: React.Key[],
45+
expandedRowOffset = 0,
4246
) {
4347
const {
4448
record,
@@ -52,6 +56,8 @@ export function getCellProps<RecordType>(
5256
expanded,
5357
hasNestChildren,
5458
onTriggerExpand,
59+
expandable,
60+
expandedKeys,
5561
} = rowInfo;
5662

5763
const key = columnsKey[colIndex];
@@ -77,16 +83,32 @@ export function getCellProps<RecordType>(
7783
);
7884
}
7985

80-
let additionalCellProps: React.TdHTMLAttributes<HTMLElement>;
81-
if (column.onCell) {
82-
additionalCellProps = column.onCell(record, index);
86+
const additionalCellProps = column.onCell?.(record, index) || {};
87+
88+
// Expandable row has offset
89+
if (expandedRowOffset) {
90+
const { rowSpan = 1 } = additionalCellProps;
91+
92+
// For expandable row with rowSpan,
93+
// We should increase the rowSpan if the row is expanded
94+
if (expandable && rowSpan && colIndex < expandedRowOffset) {
95+
let currentRowSpan = rowSpan;
96+
97+
for (let i = index; i < index + rowSpan; i += 1) {
98+
const rowKey = rowKeys[i];
99+
if (expandedKeys.has(rowKey)) {
100+
currentRowSpan += 1;
101+
}
102+
}
103+
additionalCellProps.rowSpan = currentRowSpan;
104+
}
83105
}
84106

85107
return {
86108
key,
87109
fixedInfo,
88110
appendCellNode,
89-
additionalCellProps: additionalCellProps || {},
111+
additionalCellProps: additionalCellProps,
90112
};
91113
}
92114

@@ -109,6 +131,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
109131
index,
110132
renderIndex,
111133
rowKey,
134+
rowKeys,
112135
indent = 0,
113136
rowComponent: RowComponent,
114137
cellComponent,
@@ -171,6 +194,8 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
171194
colIndex,
172195
indent,
173196
index,
197+
rowKeys,
198+
expandedRowInfo?.offset,
174199
);
175200

176201
return (

src/Body/index.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
5454
const { body: bodyCls = {} } = classNames || {};
5555
const { body: bodyStyles = {} } = styles || {};
5656

57-
const flattenData: { record: RecordType; indent: number; index: number }[] =
58-
useFlattenRecords<RecordType>(data, childrenColumnName, expandedKeys, getRowKey);
57+
const flattenData = useFlattenRecords<RecordType>(
58+
data,
59+
childrenColumnName,
60+
expandedKeys,
61+
getRowKey,
62+
);
63+
64+
const rowKeys = React.useMemo(() => flattenData.map(item => item.rowKey), [flattenData]);
5965

6066
// =================== Performance ====================
6167
const perfRef = React.useRef<PerfRecord>({
@@ -74,6 +80,7 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
7480
}
7581

7682
return {
83+
offset: expandedRowOffset,
7784
colSpan: expandedColSpan,
7885
sticky: expandedStickyStart,
7986
};
@@ -88,16 +95,15 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
8895
let rows: React.ReactNode;
8996
if (data.length) {
9097
rows = flattenData.map((item, idx) => {
91-
const { record, indent, index: renderIndex } = item;
92-
93-
const key = getRowKey(record, idx);
98+
const { record, indent, index: renderIndex, rowKey } = item;
9499

95100
return (
96101
<BodyRow
97102
classNames={bodyCls}
98103
styles={bodyStyles}
99-
key={key}
100-
rowKey={key}
104+
key={rowKey}
105+
rowKey={rowKey}
106+
rowKeys={rowKeys}
101107
record={record}
102108
index={idx}
103109
renderIndex={renderIndex}

src/hooks/useFlattenRecords.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ function fillRecords<T>(
1111
getRowKey: GetRowKey<T>,
1212
index: number,
1313
) {
14+
const key = getRowKey(record, index);
15+
1416
list.push({
1517
record,
1618
indent,
1719
index,
20+
rowKey: key,
1821
});
1922

20-
const key = getRowKey(record);
21-
2223
const expanded = expandedKeys?.has(key);
2324

2425
if (record && Array.isArray(record[childrenColumnName]) && expanded) {
@@ -41,6 +42,7 @@ export interface FlattenData<RecordType> {
4142
record: RecordType;
4243
indent: number;
4344
index: number;
45+
rowKey: Key;
4446
}
4547

4648
/**
@@ -80,6 +82,7 @@ export default function useFlattenRecords<T>(
8082
record: item,
8183
indent: 0,
8284
index,
85+
rowKey: getRowKey(item, index),
8386
};
8487
});
8588
}, [data, childrenColumnName, expandedKeys, getRowKey]);

0 commit comments

Comments
 (0)