Skip to content

Commit 17f4879

Browse files
fix: wrong index in tree table (#707)
* fix: wrong index in tree table * add a test: render index in tree table
1 parent 3cb4d10 commit 17f4879

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

docs/examples/subTable.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ class Demo extends React.Component {
4545

4646
render() {
4747
const columns = [
48-
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 },
48+
{
49+
title: 'title1',
50+
dataIndex: 'a',
51+
key: 'a',
52+
width: 100,
53+
render(text, record, index) {
54+
return index;
55+
},
56+
},
4957
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 },
5058
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 },
5159
{

src/Body/index.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ function Body<RecordType>({
3737
const { prefixCls, getComponent } = React.useContext(TableContext);
3838
const { flattenColumns } = React.useContext(BodyContext);
3939

40-
const flattenData: { record: RecordType; indent: number }[] = useFlattenRecords<RecordType>(
41-
data,
42-
childrenColumnName,
43-
expandedKeys,
44-
getRowKey,
45-
);
40+
const flattenData: { record: RecordType; indent: number; index: number }[] =
41+
useFlattenRecords<RecordType>(data, childrenColumnName, expandedKeys, getRowKey);
4642

4743
const onHover = React.useCallback((start: number, end: number) => {
4844
setStartRow(start);
@@ -61,18 +57,18 @@ function Body<RecordType>({
6157

6258
let rows: React.ReactNode;
6359
if (data.length) {
64-
rows = flattenData.map((item, index) => {
65-
const { record, indent } = item;
60+
rows = flattenData.map((item, idx) => {
61+
const { record, indent, index: renderIndex } = item;
6662

67-
const key = getRowKey(record, index);
63+
const key = getRowKey(record, idx);
6864

6965
return (
7066
<BodyRow
7167
key={key}
7268
rowKey={key}
7369
record={record}
7470
recordKey={key}
75-
index={index}
71+
index={renderIndex}
7672
rowComponent={trComponent}
7773
cellComponent={tdComponent}
7874
expandedKeys={expandedKeys}

src/hooks/useFlattenRecords.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ function flatRecord<T>(
88
childrenColumnName: string,
99
expandedKeys: Set<Key>,
1010
getRowKey: GetRowKey<T>,
11+
index: number,
1112
) {
1213
const arr = [];
1314

1415
arr.push({
1516
record,
1617
indent,
18+
index,
1719
});
1820

1921
const key = getRowKey(record);
@@ -29,6 +31,7 @@ function flatRecord<T>(
2931
childrenColumnName,
3032
expandedKeys,
3133
getRowKey,
34+
i,
3235
);
3336

3437
arr.push(...tempArr);
@@ -55,24 +58,25 @@ export default function useFlattenRecords<T>(
5558
expandedKeys: Set<Key>,
5659
getRowKey: GetRowKey<T>,
5760
) {
58-
const arr: { record: T; indent: number }[] = React.useMemo(() => {
61+
const arr: { record: T; indent: number; index: number }[] = React.useMemo(() => {
5962
if (expandedKeys?.size) {
60-
const temp: { record: T; indent: number }[] = [];
63+
const temp: { record: T; indent: number; index: number }[] = [];
6164

6265
// collect flattened record
6366
for (let i = 0; i < data?.length; i += 1) {
6467
const record = data[i];
6568

66-
temp.push(...flatRecord<T>(record, 0, childrenColumnName, expandedKeys, getRowKey));
69+
temp.push(...flatRecord<T>(record, 0, childrenColumnName, expandedKeys, getRowKey, i));
6770
}
6871

6972
return temp;
7073
}
7174

72-
return data?.map(item => {
75+
return data?.map((item, index) => {
7376
return {
7477
record: item,
7578
indent: 0,
79+
index,
7680
};
7781
});
7882
}, [data, childrenColumnName, expandedKeys, getRowKey]);

tests/Table.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,4 +982,35 @@ describe('Table.Basic', () => {
982982
expect(wrapper.exists('.rc-table-cell-row-hover')).toBeFalsy();
983983
});
984984
});
985+
986+
it('render index in tree table', () => {
987+
const tColumns = [
988+
{
989+
title: 'Key',
990+
dataIndex: 'key',
991+
},
992+
{
993+
title: '行索引',
994+
key: 'xxx',
995+
render: (value, record, index) => index,
996+
},
997+
];
998+
999+
const tData = [
1000+
{ key: 'row0', children: [{ key: 'row0-0' }, { key: 'row0-1' }] },
1001+
{ key: 'row1', children: [{ key: 'row1-0' }, { key: 'row1-1' }] },
1002+
];
1003+
const wrapper = mount(
1004+
<Table columns={tColumns} expandable={{ defaultExpandAllRows: true }} data={tData} />,
1005+
);
1006+
1007+
const trs = wrapper.find('BodyRow');
1008+
1009+
expect(trs.at(0).find('Cell').at(1).text()).toEqual('0');
1010+
expect(trs.at(1).find('Cell').at(1).text()).toEqual('0');
1011+
expect(trs.at(2).find('Cell').at(1).text()).toEqual('1');
1012+
expect(trs.at(3).find('Cell').at(1).text()).toEqual('1');
1013+
expect(trs.at(4).find('Cell').at(1).text()).toEqual('0');
1014+
expect(trs.at(5).find('Cell').at(1).text()).toEqual('1');
1015+
});
9851016
});

0 commit comments

Comments
 (0)