Skip to content

Commit 6a3c10a

Browse files
committed
chore: perf
1 parent 7142cdd commit 6a3c10a

File tree

6 files changed

+42
-20
lines changed

6 files changed

+42
-20
lines changed

docs/examples/expandedSticky.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const Demo = () => {
6565
<Table<Record<string, any>>
6666
rowKey="key"
6767
sticky
68-
scroll={{ x: 800 }}
68+
scroll={{ x: 2000 }}
6969
columns={columns}
7070
data={data}
7171
expandable={{

src/Body/BodyRow.tsx

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import Cell from '../Cell';
44
import { responseImmutable } from '../context/TableContext';
55
import devRenderTimes from '../hooks/useRenderTimes';
66
import useRowInfo from '../hooks/useRowInfo';
7-
import type { ColumnType, CustomizeComponent, ExpandableConfig } from '../interface';
7+
import type { ColumnType, CustomizeComponent } from '../interface';
88
import ExpandedRow from './ExpandedRow';
99
import { computedExpandedClassName } from '../utils/expandUtil';
10-
import { TableProps } from '..';
10+
import type { TableProps } from '..';
1111

1212
export interface BodyRowProps<RecordType> {
1313
record: RecordType;
@@ -22,7 +22,12 @@ export interface BodyRowProps<RecordType> {
2222
scopeCellComponent: CustomizeComponent;
2323
indent?: number;
2424
rowKey: React.Key;
25-
expandedRowOffset?: ExpandableConfig<RecordType>['expandedRowOffset'];
25+
26+
// Expanded Row
27+
expandedRowInfo: {
28+
colSpan: number;
29+
sticky: number;
30+
};
2631
}
2732

2833
// ==================================================================================
@@ -108,7 +113,7 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
108113
rowComponent: RowComponent,
109114
cellComponent,
110115
scopeCellComponent,
111-
expandedRowOffset = 0,
116+
expandedRowInfo,
112117
} = props;
113118

114119
const rowInfo = useRowInfo(record, rowKey, index, indent);
@@ -198,14 +203,6 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
198203
if (rowSupportExpand && (expandedRef.current || expanded)) {
199204
const expandContent = expandedRowRender(record, index, indent + 1, expanded);
200205

201-
const offsetColumns = flattenColumns.filter((_, idx) => idx < expandedRowOffset);
202-
let offsetWidth = 0;
203-
offsetColumns.forEach(item => {
204-
if (typeof item.width === 'number') {
205-
offsetWidth = offsetWidth + (item.width ?? 0);
206-
}
207-
});
208-
209206
expandRowNode = (
210207
<ExpandedRow
211208
expanded={expanded}
@@ -217,9 +214,9 @@ function BodyRow<RecordType extends { children?: readonly RecordType[] }>(
217214
prefixCls={prefixCls}
218215
component={RowComponent}
219216
cellComponent={cellComponent}
220-
offsetWidth={offsetWidth}
221-
colSpan={flattenColumns.length - expandedRowOffset}
217+
colSpan={expandedRowInfo.colSpan}
222218
isEmpty={false}
219+
stickyOffset={expandedRowInfo.sticky}
223220
>
224221
{expandContent}
225222
</ExpandedRow>

src/Body/ExpandedRow.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface ExpandedRowProps {
1414
children: React.ReactNode;
1515
colSpan: number;
1616
isEmpty: boolean;
17-
offsetWidth?: number;
17+
stickyOffset?: number;
1818
}
1919

2020
function ExpandedRow(props: ExpandedRowProps) {
@@ -31,7 +31,7 @@ function ExpandedRow(props: ExpandedRowProps) {
3131
expanded,
3232
colSpan,
3333
isEmpty,
34-
offsetWidth = 0,
34+
stickyOffset = 0,
3535
} = props;
3636

3737
const { scrollbarSize, fixHeader, fixColumn, componentWidth, horizonScroll } = useContext(
@@ -46,9 +46,9 @@ function ExpandedRow(props: ExpandedRowProps) {
4646
contentNode = (
4747
<div
4848
style={{
49-
width: componentWidth - offsetWidth - (fixHeader && !isEmpty ? scrollbarSize : 0),
49+
width: componentWidth - stickyOffset - (fixHeader && !isEmpty ? scrollbarSize : 0),
5050
position: 'sticky',
51-
left: 0,
51+
left: stickyOffset,
5252
overflow: 'hidden',
5353
}}
5454
className={`${prefixCls}-expanded-row-fixed`}

src/Body/index.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
3535
classNames,
3636
styles,
3737
expandedRowOffset,
38+
fixedInfoList,
39+
colWidths,
3840
} = useContext(TableContext, [
3941
'prefixCls',
4042
'getComponent',
@@ -47,6 +49,8 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
4749
'classNames',
4850
'styles',
4951
'expandedRowOffset',
52+
'fixedInfoList',
53+
'colWidths',
5054
]);
5155
const { body: bodyCls = {} } = classNames || {};
5256
const { body: bodyStyles = {} } = styles || {};
@@ -59,6 +63,23 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
5963
renderWithProps: false,
6064
});
6165

66+
// ===================== Expanded =====================
67+
// `expandedRowOffset` data is same for all the rows.
68+
// Let's calc on Body side to save performance.
69+
const expandedRowInfo = React.useMemo(() => {
70+
const expandedColSpan = flattenColumns.length - expandedRowOffset;
71+
72+
let expandedStickyStart = 0;
73+
for (let i = 0; i < expandedRowOffset; i += 1) {
74+
expandedStickyStart += colWidths[i] || 0;
75+
}
76+
77+
return {
78+
colSpan: expandedColSpan,
79+
sticky: expandedStickyStart,
80+
};
81+
}, [flattenColumns.length, expandedRowOffset, colWidths]);
82+
6283
// ====================== Render ======================
6384
const WrapperComponent = getComponent(['body', 'wrapper'], 'tbody');
6485
const trComponent = getComponent(['body', 'row'], 'tr');
@@ -85,7 +106,8 @@ function Body<RecordType>(props: BodyProps<RecordType>) {
85106
cellComponent={tdComponent}
86107
scopeCellComponent={thComponent}
87108
indent={indent}
88-
expandedRowOffset={expandedRowOffset}
109+
// Expanded row info
110+
expandedRowInfo={expandedRowInfo}
89111
/>
90112
);
91113
});

src/Table.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ function Table<RecordType extends DefaultRecordType>(
881881
columns,
882882
flattenColumns,
883883
onColumnResize,
884+
colWidths,
884885

885886
// Row
886887
hoverStartRow: startRow,
@@ -931,6 +932,7 @@ function Table<RecordType extends DefaultRecordType>(
931932
columns,
932933
flattenColumns,
933934
onColumnResize,
935+
colWidths,
934936

935937
// Row
936938
startRow,

src/context/TableContext.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface TableContextProps<RecordType = any> {
6262
columns: ColumnsType<RecordType>;
6363
flattenColumns: readonly ColumnType<RecordType>[];
6464
onColumnResize: (columnKey: React.Key, width: number) => void;
65+
colWidths: number[];
6566

6667
// Row
6768
hoverStartRow: number;

0 commit comments

Comments
 (0)