Skip to content
Closed
Show file tree
Hide file tree
Changes from 7 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
8 changes: 8 additions & 0 deletions docs/demo/expandedRowSpan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: expandedRowSpan
nav:
title: Demo
path: /demo
---

<code src="../examples/expandedRowSpan.tsx"></code>
56 changes: 56 additions & 0 deletions docs/examples/expandedRowSpan.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import Table from 'rc-table';
import '../../assets/index.less';
import type { ColumnsType } from '@/interface';

const columns: ColumnsType = [
{
title: '手机号',
dataIndex: 'a',
colSpan: 2,
width: 100,
onCell: (_, index) => {
const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};
if (index === 0) {
props.rowSpan = 4;
} else if (index === 1) {
props.rowSpan = 0;
} else if (index === 2) {
props.rowSpan = 0;
} else if (index === 3) {
props.rowSpan = 0;
} else if (index === 4) {
props.rowSpan = 1;
}

return props;
},
},
{ title: '电话', dataIndex: 'b', colSpan: 0, width: 100 },
Table.EXPAND_COLUMN,
{ title: 'Name', dataIndex: 'c', width: 100 },
{ title: 'Address', dataIndex: 'd', width: 200 },
];

const data = [
{ a: '13812340987', b: '0571-12345678', c: '张三', d: '文一西路', e: 'Male', key: 'a' },
{ a: '13812340987', b: '0571-12345678', c: '张夫人', d: '文一西路', e: 'Female', key: 'b' },
{ a: '13812340987', b: '0571-099877', c: '李四', d: '文二西路', e: 'Male', key: 'c' },
{ a: '13812340987', b: '0571-099877', c: '李四', d: '文二西路', e: 'Male', key: 'd' },
{ a: '1381200008888', b: '0571-099877', c: '王五', d: '文二西路', e: 'Male', key: 'e' },
];

const Demo = () => (
<div>
<h2>expanded & rowSpan</h2>
<Table<Record<string, any>>
rowKey="key"
columns={columns}
data={data}
expandable={{ expandedRowRender: record => <p style={{ margin: 0 }}>{record.key}</p> }}
className="table"
/>
</div>
);

export default Demo;
34 changes: 34 additions & 0 deletions src/Body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
scopeCellComponent: CustomizeComponent;
indent?: number;
rowKey: React.Key;
getRowKey: (index: number) => React.Key;
}

// ==================================================================================
Expand All @@ -30,6 +31,7 @@
colIndex: number,
indent: number,
index: number,
getRowKey?: (index: number) => React.Key,
) {
const {
record,
Expand All @@ -43,6 +45,7 @@
expanded,
hasNestChildren,
onTriggerExpand,
expandedKeys,
} = rowInfo;

const key = columnsKey[colIndex];
Expand All @@ -68,9 +71,37 @@
);
}

const addChildrenRowSpan = (rowSpan: number, index2: number) => {
const _index = index2 + 1;
let _rowSpan = rowSpan;
const rowKey = getRowKey?.(_index);
if (rowKey !== undefined) {
// 下面如果是 0 的,增加 +1 逻辑
const thisCellProps = column.onCell(record, _index);
if (thisCellProps.rowSpan === 0) {
const thisExpanded = expandedKeys.has(getRowKey?.(_index));
if (thisExpanded) {
_rowSpan = _rowSpan + 1;
}

Check warning on line 85 in src/Body/BodyRow.tsx

View check run for this annotation

Codecov / codecov/patch

src/Body/BodyRow.tsx#L84-L85

Added lines #L84 - L85 were not covered by tests
// 继续往下找
return addChildrenRowSpan(_rowSpan, _index);
}
}
// 找不到后返回
return _rowSpan;
};

let additionalCellProps: React.TdHTMLAttributes<HTMLElement>;
if (column.onCell) {
additionalCellProps = column.onCell(record, index);
if (additionalCellProps.rowSpan > 0) {
// 本身展开 +1
if (expanded) {
additionalCellProps.rowSpan = additionalCellProps.rowSpan + 1;
}

Check warning on line 101 in src/Body/BodyRow.tsx

View check run for this annotation

Codecov / codecov/patch

src/Body/BodyRow.tsx#L100-L101

Added lines #L100 - L101 were not covered by tests
additionalCellProps.rowSpan = addChildrenRowSpan(additionalCellProps.rowSpan, index);
}
console.log('additionalCellProps.rowSpan', additionalCellProps.rowSpan);
}

return {
Expand Down Expand Up @@ -102,8 +133,10 @@
rowComponent: RowComponent,
cellComponent,
scopeCellComponent,
getRowKey,
} = props;
const rowInfo = useRowInfo(record, rowKey, index, indent);

const {
prefixCls,
flattenColumns,
Expand Down Expand Up @@ -153,6 +186,7 @@
colIndex,
indent,
index,
getRowKey,
);

return (
Expand Down
20 changes: 13 additions & 7 deletions src/Body/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
'emptyNode',
]);

const flattenData: { record: RecordType; indent: number; index: number }[] =
useFlattenRecords<RecordType>(data, childrenColumnName, expandedKeys, getRowKey);
const flattenData = useFlattenRecords<RecordType>(
data,
childrenColumnName,
expandedKeys,
getRowKey,
);

// =================== Performance ====================
const perfRef = React.useRef<PerfRecord>({
Expand All @@ -59,14 +63,16 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
let rows: React.ReactNode;
if (data.length) {
rows = flattenData.map((item, idx) => {
const { record, indent, index: renderIndex } = item;

const key = getRowKey(record, idx);
const { record, indent, index: renderIndex, rowKey } = item;

return (
<BodyRow
key={key}
rowKey={key}
key={rowKey}
rowKey={rowKey}
getRowKey={index => {
const thisRecord = flattenData[index];
return thisRecord?.rowKey;
}}
record={record}
index={idx}
renderIndex={renderIndex}
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useFlattenRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function fillRecords<T>(
record,
indent,
index,
rowKey: getRowKey(record, index),
});

const key = getRowKey(record);
Expand All @@ -41,6 +42,7 @@ export interface FlattenData<RecordType> {
record: RecordType;
indent: number;
index: number;
rowKey: Key;
}

/**
Expand Down Expand Up @@ -80,6 +82,7 @@ export default function useFlattenRecords<T>(
record: item,
indent: 0,
index,
rowKey: getRowKey(item, index),
};
});
}, [data, childrenColumnName, expandedKeys, getRowKey]);
Expand Down