Skip to content
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
3 changes: 3 additions & 0 deletions docs/demo/colspan-rowspan-legacy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## colspan-rowspan-legacy

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

interface RecordType {
a?: string;
b?: string;
c?: string;
d?: string;
e?: string;
key?: string;
}

const columns: ColumnsType<RecordType> = [
{
title: '手机号',
dataIndex: 'a',
colSpan: 2,
width: 100,
key: 'a',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};
// 设置第一行为链接
if (index === 0) {
obj.children = <a href="#">{o}</a>;
}
// 第5行合并两列
if (index === 4) {
obj.props.colSpan = 2;
}

if (index === 5) {
obj.props.colSpan = 6;
}
return obj;
},
},
{
title: '电话',
dataIndex: 'b',
colSpan: 0,
width: 100,
key: 'b',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};
// 列合并掉的表格设置colSpan=0,不会去渲染
if (index === 4 || index === 5) {
obj.props.colSpan = 0;
}
return obj;
},
},
{
title: 'Name',
dataIndex: 'c',
width: 100,
key: 'c',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};

if (index === 5) {
obj.props.colSpan = 0;
}
return obj;
},
},
{
title: 'Address',
dataIndex: 'd',
width: 200,
key: 'd',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};
if (index === 0) {
obj.props.rowSpan = 2;
}
if (index === 1 || index === 5) {
obj.props.rowSpan = 0;
}

if (index === 5) {
obj.props.colSpan = 0;
}

return obj;
},
},
{
title: 'Gender',
dataIndex: 'e',
width: 200,
key: 'e',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};
if (index === 5) {
obj.props.colSpan = 0;
}
return obj;
},
},
{
title: 'Operations',
dataIndex: '',
key: 'f',
render(o, row, index) {
if (index === 5) {
return {
props: {
colSpan: 0,
},
};
}
return <a href="#">Operations</a>;
},
},
];

const data: RecordType[] = [
{ a: '13812340987', b: '0571-12345678', c: '张三', d: '文一西路', e: 'Male', key: '1' },
{ a: '13812340986', b: '0571-98787658', c: '张夫人', d: '文一西路', e: 'Female', key: '2' },
{ a: '13812988888', b: '0571-099877', c: '李四', d: '文二西路', e: 'Male', key: '3' },
{ a: '1381200008888', b: '0571-099877', c: '王五', d: '文二西路', e: 'Male', key: '4' },
{ a: '0571-88888110', c: '李警官', d: '武林门', e: 'Male', key: '5' },
{ a: '资料统计完毕于xxxx年xxx月xxx日', key: '6' },
];

const Demo = () => (
<div>
<h2>colSpan & rowSpan</h2>
<Table columns={columns} data={data} className="table" />
</div>
);

export default Demo;
78 changes: 30 additions & 48 deletions docs/examples/colspan-rowspan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,21 @@ const columns: ColumnsType<RecordType> = [
width: 100,
key: 'a',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};
// 设置第一行为链接
if (index === 0) {
obj.children = <a href="#">{o}</a>;
}
return index === 0 ? <a href="#">{o}</a> : o;
},
onCell: (_, index) => {
const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};

// 第5行合并两列
if (index === 4) {
obj.props.colSpan = 2;
props.colSpan = 2;
}

if (index === 5) {
obj.props.colSpan = 6;
props.colSpan = 6;
}
return obj;

return props;
},
},
{
Expand All @@ -45,88 +43,72 @@ const columns: ColumnsType<RecordType> = [
colSpan: 0,
width: 100,
key: 'b',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};
onCell(_, index) {
// 列合并掉的表格设置colSpan=0,不会去渲染
if (index === 4 || index === 5) {
obj.props.colSpan = 0;
return { colSpan: 0 };
}
return obj;
return {};
},
},
{
title: 'Name',
dataIndex: 'c',
width: 100,
key: 'c',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};

onCell(_, index) {
if (index === 5) {
obj.props.colSpan = 0;
return { colSpan: 0 };
}
return obj;
return {};
},
},
{
title: 'Address',
dataIndex: 'd',
width: 200,
key: 'd',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};
onCell(_, index) {
const props: React.TdHTMLAttributes<HTMLTableCellElement> = {};
if (index === 0) {
obj.props.rowSpan = 2;
props.rowSpan = 2;
}
if (index === 1 || index === 5) {
obj.props.rowSpan = 0;
props.rowSpan = 0;
}

if (index === 5) {
obj.props.colSpan = 0;
props.colSpan = 0;
}

return obj;
return props;
},
},
{
title: 'Gender',
dataIndex: 'e',
width: 200,
key: 'e',
render(o, row, index) {
const obj: RenderedCell<RecordType> = {
children: o,
props: {},
};
onCell(_, index) {
if (index === 5) {
obj.props.colSpan = 0;
return { colSpan: 0 };
}
return obj;
return {};
},
},
{
title: 'Operations',
dataIndex: '',
key: 'f',
render(o, row, index) {
render() {
return <a href="#">Operations</a>;
},
onCell(_, index) {
if (index === 5) {
return {
props: {
colSpan: 0,
},
colSpan: 0,
};
}
return <a href="#">Operations</a>;
return {};
},
},
];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@types/jest": "^26.0.3",
"@types/react": "^17.0.35",
"@types/react-dom": "^17.0.10",
"@types/shallowequal": "^1.1.1",
"@umijs/fabric": "^2.0.0",
"cross-env": "^7.0.0",
"dumi": "^1.1.9",
Expand Down
38 changes: 20 additions & 18 deletions src/Body/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ function Body<RecordType>({
emptyNode,
childrenColumnName,
}: BodyProps<RecordType>) {
const [startRow, setStartRow] = React.useState(-1);
const [endRow, setEndRow] = React.useState(-1);
const { onColumnResize } = React.useContext(ResizeContext);
const { prefixCls, getComponent } = React.useContext(TableContext);
const { flattenColumns } = React.useContext(BodyContext);

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

// ====================== Hover =======================
const [startRow, setStartRow] = React.useState(-1);
const [endRow, setEndRow] = React.useState(-1);

const onHover = React.useCallback((start: number, end: number) => {
setStartRow(start);
setEndRow(end);
Expand All @@ -50,7 +52,8 @@ function Body<RecordType>({
[onHover, startRow, endRow],
);

return React.useMemo(() => {
// ====================== Render ======================
const bodyNode = React.useMemo(() => {
const WrapperComponent = getComponent(['body', 'wrapper'], 'tbody');
const trComponent = getComponent(['body', 'row'], 'tr');
const tdComponent = getComponent(['body', 'cell'], 'td');
Expand Down Expand Up @@ -98,20 +101,18 @@ function Body<RecordType>({
const columnsKey = getColumnsKey(flattenColumns);

return (
<HoverContext.Provider value={hoverContext}>
<WrapperComponent className={`${prefixCls}-tbody`}>
{/* Measure body column width with additional hidden col */}
{measureColumnWidth && (
<MeasureRow
prefixCls={prefixCls}
columnsKey={columnsKey}
onColumnResize={onColumnResize}
/>
)}

{rows}
</WrapperComponent>
</HoverContext.Provider>
<WrapperComponent className={`${prefixCls}-tbody`}>
{/* Measure body column width with additional hidden col */}
{measureColumnWidth && (
<MeasureRow
prefixCls={prefixCls}
columnsKey={columnsKey}
onColumnResize={onColumnResize}
/>
)}

{rows}
</WrapperComponent>
);
}, [
data,
Expand All @@ -127,8 +128,9 @@ function Body<RecordType>({
onColumnResize,
rowExpandable,
flattenData,
hoverContext,
]);

return <HoverContext.Provider value={hoverContext}>{bodyNode}</HoverContext.Provider>;
}

const MemoBody = React.memo(Body);
Expand Down
Loading