Skip to content

Commit fd4091e

Browse files
authored
perf: extract expanded row context (#705)
* docs: Add fixedcolumns resize demo * perf: extract expanded row context
1 parent ff2a010 commit fd4091e

File tree

8 files changed

+156
-43
lines changed

8 files changed

+156
-43
lines changed

docs/demo/fixedColumns-resize.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## fixedColumns-resize
2+
3+
<code src="../examples/fixedColumns-resize.tsx">
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import React, { useState, useCallback } from 'react';
2+
import Table from 'rc-table';
3+
import '../../assets/index.less';
4+
import type { ColumnType } from '@/interface';
5+
6+
interface RecordType {
7+
a: string;
8+
b?: string;
9+
c?: string;
10+
d: number;
11+
key: string;
12+
}
13+
14+
const defaultColumns: ColumnType<RecordType>[] = [
15+
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' },
16+
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100, fixed: 'left', ellipsis: true },
17+
{ title: 'title3', dataIndex: 'c', key: 'c' },
18+
{ title: 'title4', dataIndex: 'b', key: 'd' },
19+
{ title: 'title5', dataIndex: 'b', key: 'e' },
20+
{ title: 'title6', dataIndex: 'b', key: 'f' },
21+
{ title: 'title8', dataIndex: 'b', key: 'h' },
22+
{ title: 'title9', dataIndex: 'b', key: 'i' },
23+
{ title: 'title11', dataIndex: 'b', key: 'j' },
24+
{ title: 'title12', dataIndex: 'b', key: 'j1' },
25+
{ title: 'title13', dataIndex: 'b', key: 'j2' },
26+
{ title: 'title14', dataIndex: 'b', key: 'j3' },
27+
{ title: 'title15', dataIndex: 'b', key: 'j4' },
28+
{ title: 'title16', dataIndex: 'b', key: 'j5' },
29+
{ title: 'title17', dataIndex: 'b', key: 'j6' },
30+
{ title: 'title18', dataIndex: 'b', key: 'j7' },
31+
{ title: 'title19', dataIndex: 'b', key: 'k', width: 50, fixed: 'right' },
32+
{ title: 'title20', dataIndex: 'b', key: 'l', width: 100, fixed: 'right' },
33+
];
34+
35+
const data: RecordType[] = Array.from(new Array(200).fill(1), (v, index) => {
36+
return {
37+
a: '123',
38+
b: 'xxxxx',
39+
d: 3,
40+
key: index + '',
41+
};
42+
});
43+
44+
const Demo = () => {
45+
const [isShown, setIsShown] = useState(false);
46+
const [renderTime, setRenderTime] = useState(0);
47+
const [isFixed, setIsFixed] = useState(true);
48+
const [columns, setColumns] = useState(defaultColumns);
49+
const onToggleSideBar = useCallback(() => {
50+
const s = window.performance.now();
51+
setIsShown(v => !v);
52+
53+
setTimeout(() => {
54+
setRenderTime(+(window.performance.now() - s).toFixed(2));
55+
});
56+
}, []);
57+
58+
const onToggleFixed = useCallback(() => {
59+
setIsFixed(v => !v);
60+
}, []);
61+
62+
const onRemoveColumn = useCallback(() => {
63+
setColumns(state => {
64+
const newState = [...state];
65+
newState.splice(
66+
state.findIndex(({ fixed }) => !fixed),
67+
1,
68+
);
69+
return newState;
70+
});
71+
}, []);
72+
73+
const onAddColumn = useCallback(() => {
74+
setColumns(state => {
75+
const newState = [...state];
76+
newState.splice(
77+
state.findIndex(({ fixed }) => !fixed),
78+
0,
79+
{ title: 'new title', dataIndex: 'b', key: Math.random().toString(16).slice(2) },
80+
);
81+
return newState;
82+
});
83+
}, []);
84+
85+
const expandedRowRender = useCallback(({ b, c }) => b || c, []);
86+
87+
return (
88+
<div>
89+
<div>
90+
<button onClick={onToggleSideBar}>切换侧边栏展开状态</button>
91+
<button onClick={onToggleFixed}>切换固定列</button>
92+
<button onClick={onRemoveColumn}>删除列</button>
93+
<button onClick={onAddColumn}>增加列</button>
94+
<p>更新用时:{renderTime} ms</p>
95+
</div>
96+
<div
97+
style={{
98+
display: 'flex',
99+
width: '800px',
100+
resize: 'both',
101+
padding: '12px',
102+
border: '1px solid #333',
103+
height: '80vh',
104+
overflow: 'auto',
105+
}}
106+
>
107+
<div style={{ flex: `0 0 ${isShown ? '10px' : '80px'}` }} />
108+
<div style={{ flex: 1, overflow: 'hidden' }}>
109+
<Table
110+
columns={columns}
111+
scroll={isFixed ? { x: 1200 } : null}
112+
data={data}
113+
expandedRowRender={expandedRowRender}
114+
/>
115+
</div>
116+
</div>
117+
</div>
118+
);
119+
};
120+
121+
export default Demo;

src/Body/BodyRow.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
4949
} = props;
5050
const { prefixCls, fixedInfoList } = React.useContext(TableContext);
5151
const {
52-
fixHeader,
53-
fixColumn,
54-
horizonScroll,
55-
componentWidth,
5652
flattenColumns,
5753
expandableType,
5854
expandRowByClick,
@@ -197,11 +193,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
197193
computedExpandedRowClassName,
198194
)}
199195
prefixCls={prefixCls}
200-
fixHeader={fixHeader}
201-
fixColumn={fixColumn}
202-
horizonScroll={horizonScroll}
203196
component={RowComponent}
204-
componentWidth={componentWidth}
205197
cellComponent={cellComponent}
206198
colSpan={flattenColumns.length}
207199
>

src/Body/ExpandedRow.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import * as React from 'react';
2-
import { CustomizeComponent } from '../interface';
2+
import type { CustomizeComponent } from '../interface';
33
import Cell from '../Cell';
44
import TableContext from '../context/TableContext';
5+
import ExpandedRowContext from '../context/ExpandedRowContext';
56

67
export interface ExpandedRowProps {
78
prefixCls: string;
89
component: CustomizeComponent;
910
cellComponent: CustomizeComponent;
10-
fixHeader: boolean;
11-
fixColumn: boolean;
12-
horizonScroll: boolean;
13-
componentWidth: number;
1411
className: string;
1512
expanded: boolean;
1613
children: React.ReactNode;
@@ -22,15 +19,12 @@ function ExpandedRow({
2219
children,
2320
component: Component,
2421
cellComponent,
25-
fixHeader,
26-
fixColumn,
27-
horizonScroll,
2822
className,
2923
expanded,
30-
componentWidth,
3124
colSpan,
3225
}: ExpandedRowProps) {
3326
const { scrollbarSize } = React.useContext(TableContext);
27+
const { fixHeader, fixColumn, componentWidth } = React.useContext(ExpandedRowContext);
3428

3529
// Cache render node
3630
return React.useMemo(() => {
@@ -67,13 +61,13 @@ function ExpandedRow({
6761
}, [
6862
children,
6963
Component,
70-
fixHeader,
71-
horizonScroll,
7264
className,
7365
expanded,
74-
componentWidth,
7566
colSpan,
7667
scrollbarSize,
68+
componentWidth,
69+
fixColumn,
70+
fixHeader,
7771
]);
7872
}
7973

src/Body/index.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ function Body<RecordType>({
3535
const [endRow, setEndRow] = React.useState(-1);
3636
const { onColumnResize } = React.useContext(ResizeContext);
3737
const { prefixCls, getComponent } = React.useContext(TableContext);
38-
const { fixHeader, horizonScroll, flattenColumns, componentWidth } =
39-
React.useContext(BodyContext);
38+
const { flattenColumns } = React.useContext(BodyContext);
4039

4140
const flattenData: { record: RecordType; indent: number }[] = useFlattenRecords<RecordType>(
4241
data,
@@ -91,11 +90,7 @@ function Body<RecordType>({
9190
expanded
9291
className={`${prefixCls}-placeholder`}
9392
prefixCls={prefixCls}
94-
fixHeader={fixHeader}
95-
fixColumn={horizonScroll}
96-
horizonScroll={horizonScroll}
9793
component={trComponent}
98-
componentWidth={componentWidth}
9994
cellComponent={tdComponent}
10095
colSpan={flattenColumns.length}
10196
>
@@ -138,12 +133,9 @@ function Body<RecordType>({
138133
expandedKeys,
139134
getRowKey,
140135
getComponent,
141-
componentWidth,
142136
emptyNode,
143137
flattenColumns,
144138
childrenColumnName,
145-
fixHeader,
146-
horizonScroll,
147139
onColumnResize,
148140
rowExpandable,
149141
flattenData,

src/Table.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import FixedHolder from './FixedHolder';
7676
import type { SummaryProps } from './Footer/Summary';
7777
import Summary from './Footer/Summary';
7878
import StickyContext from './context/StickyContext';
79+
import ExpandedRowContext from './context/ExpandedRowContext';
7980
import { EXPAND_COLUMN } from './constant';
8081

8182
// Used for conditions cache
@@ -807,10 +808,6 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
807808
tableLayout: mergedTableLayout,
808809
rowClassName,
809810
expandedRowClassName,
810-
componentWidth,
811-
fixHeader,
812-
fixColumn,
813-
horizonScroll,
814811
expandIcon: mergedExpandIcon,
815812
expandableType,
816813
expandRowByClick,
@@ -824,10 +821,6 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
824821
mergedTableLayout,
825822
rowClassName,
826823
expandedRowClassName,
827-
componentWidth,
828-
fixHeader,
829-
fixColumn,
830-
horizonScroll,
831824
mergedExpandIcon,
832825
expandableType,
833826
expandRowByClick,
@@ -838,13 +831,24 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
838831
],
839832
);
840833

834+
const ExpandedRowContextValue = React.useMemo(
835+
() => ({
836+
componentWidth,
837+
fixHeader,
838+
fixColumn,
839+
}),
840+
[componentWidth, fixHeader, fixColumn],
841+
);
842+
841843
const ResizeContextValue = React.useMemo(() => ({ onColumnResize }), [onColumnResize]);
842844

843845
return (
844846
<StickyContext.Provider value={supportSticky}>
845847
<TableContext.Provider value={TableContextValue}>
846848
<BodyContext.Provider value={BodyContextValue}>
847-
<ResizeContext.Provider value={ResizeContextValue}>{fullTable}</ResizeContext.Provider>
849+
<ExpandedRowContext.Provider value={ExpandedRowContextValue}>
850+
<ResizeContext.Provider value={ResizeContextValue}>{fullTable}</ResizeContext.Provider>
851+
</ExpandedRowContext.Provider>
848852
</BodyContext.Provider>
849853
</TableContext.Provider>
850854
</StickyContext.Provider>

src/context/BodyContext.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import {
2+
import type {
33
ColumnType,
44
DefaultRecordType,
55
ColumnsType,
@@ -18,11 +18,7 @@ export interface BodyContextProps<RecordType = DefaultRecordType> {
1818
columns: ColumnsType<RecordType>;
1919
flattenColumns: readonly ColumnType<RecordType>[];
2020

21-
componentWidth: number;
2221
tableLayout: TableLayout;
23-
fixHeader: boolean;
24-
fixColumn: boolean;
25-
horizonScroll: boolean;
2622

2723
indentSize: number;
2824
expandableType: ExpandableType;

src/context/ExpandedRowContext.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react';
2+
3+
export interface ExpandedRowProps {
4+
componentWidth: number;
5+
fixHeader: boolean;
6+
fixColumn: boolean;
7+
}
8+
9+
const ExpandedRowContext = React.createContext<ExpandedRowProps>(null);
10+
11+
export default ExpandedRowContext;

0 commit comments

Comments
 (0)