Skip to content

Commit 0592c28

Browse files
authored
refactor: Move all measure into table body (#409)
* move measure out of first row * remove Header measure logic * update snapshot
1 parent 6055b67 commit 0592c28

File tree

9 files changed

+227
-69
lines changed

9 files changed

+227
-69
lines changed

src/Body/BodyRow.tsx

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as React from 'react';
22
import classNames from 'classnames';
3-
import ResizeObserver from 'rc-resize-observer';
43
import Cell from '../Cell';
54
import TableContext from '../context/TableContext';
65
import BodyContext from '../context/BodyContext';
@@ -13,7 +12,6 @@ import {
1312
Key,
1413
GetRowKey,
1514
} from '../interface';
16-
import ResizeContext from '../context/ResizeContext';
1715
import { getCellFixedInfo } from '../utils/fixUtil';
1816
import ExpandedRow from './ExpandedRow';
1917

@@ -22,8 +20,6 @@ export interface BodyRowProps<RecordType> {
2220
index: number;
2321
className?: string;
2422
style?: React.CSSProperties;
25-
/** Set if need collect column width info */
26-
measureColumnWidth: boolean;
2723
stickyOffsets: StickyOffsets;
2824
recordKey: Key;
2925
expandedKeys: Set<Key>;
@@ -51,7 +47,6 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
5147
indent = 0,
5248
rowComponent: RowComponent,
5349
cellComponent,
54-
measureColumnWidth,
5550
childrenColumnName,
5651
} = props;
5752
const { prefixCls } = React.useContext(TableContext);
@@ -70,7 +65,6 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
7065
expandedRowRender,
7166
expandIconColumnIndex,
7267
} = React.useContext(BodyContext);
73-
const { onColumnResize } = React.useContext(ResizeContext);
7468
const [expandRended, setExpandRended] = React.useState(false);
7569

7670
const expanded = props.expandedKeys.has(props.recordKey);
@@ -165,7 +159,7 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
165159
additionalCellProps = column.onCell(record, index);
166160
}
167161

168-
const cellNode = (
162+
return (
169163
<Cell
170164
className={columnClassName}
171165
ellipsis={column.ellipsis}
@@ -182,21 +176,6 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
182176
additionalProps={additionalCellProps}
183177
/>
184178
);
185-
186-
if (measureColumnWidth) {
187-
return (
188-
<ResizeObserver
189-
key={key}
190-
onResize={({ width }) => {
191-
onColumnResize(key, width);
192-
}}
193-
>
194-
{cellNode}
195-
</ResizeObserver>
196-
);
197-
}
198-
199-
return cellNode;
200179
})}
201180
</RowComponent>
202181
);
@@ -243,7 +222,6 @@ function BodyRow<RecordType extends { children?: RecordType[] }>(props: BodyRowP
243222
recordKey={subKey}
244223
index={subIndex}
245224
indent={indent + 1}
246-
measureColumnWidth={false}
247225
/>
248226
);
249227
},

src/Body/index.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import * as React from 'react';
2+
import ResizeObserver from 'rc-resize-observer';
23
import BodyRow from './BodyRow';
34
import TableContext from '../context/TableContext';
45
import { GetRowKey, StickyOffsets, Key, GetComponentProps } from '../interface';
56
import ExpandedRow from './ExpandedRow';
67
import BodyContext from '../context/BodyContext';
8+
import { getColumnsKey } from '../utils/valueUtil';
9+
import ResizeContext from '../context/ResizeContext';
710

811
export interface BodyProps<RecordType> {
912
data: RecordType[];
@@ -28,6 +31,7 @@ function Body<RecordType>({
2831
emptyNode,
2932
childrenColumnName,
3033
}: BodyProps<RecordType>) {
34+
const { onColumnResize } = React.useContext(ResizeContext);
3135
const { prefixCls, getComponent } = React.useContext(TableContext);
3236
const { fixHeader, fixColumn, flattenColumns, componentWidth } = React.useContext(BodyContext);
3337

@@ -48,7 +52,6 @@ function Body<RecordType>({
4852
record={record}
4953
recordKey={key}
5054
index={index}
51-
measureColumnWidth={measureColumnWidth && index === 0}
5255
rowComponent={trComponent}
5356
cellComponent={tdComponent}
5457
stickyOffsets={stickyOffsets}
@@ -78,7 +81,29 @@ function Body<RecordType>({
7881
);
7982
}
8083

81-
return <WrapperComponent className={`${prefixCls}-tbody`}>{rows}</WrapperComponent>;
84+
const columnsKey = getColumnsKey(flattenColumns);
85+
86+
return (
87+
<WrapperComponent className={`${prefixCls}-tbody`}>
88+
{/* Measure body column width with additional hidden col */}
89+
{measureColumnWidth && (
90+
<tr aria-hidden="true" className={`${prefixCls}-measure-row`}>
91+
{columnsKey.map(columnKey => (
92+
<ResizeObserver
93+
key={columnKey}
94+
onResize={({ width }) => {
95+
onColumnResize(columnKey, width);
96+
}}
97+
>
98+
<td style={{ padding: 0, border: 0, height: 0 }} />
99+
</ResizeObserver>
100+
))}
101+
</tr>
102+
)}
103+
104+
{rows}
105+
</WrapperComponent>
106+
);
82107
}, [
83108
data,
84109
prefixCls,

src/Header/Header.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ import TableContext from '../context/TableContext';
55

66
function parseHeaderRows<RecordType>(
77
rootColumns: ColumnsType<RecordType>,
8-
measureColumnWidth: boolean,
98
): CellType<RecordType>[][] {
109
const rows: CellType<RecordType>[][] = [];
1110

12-
// Record which cell should wrapped with resize observer used for fixed
13-
const measureCells: boolean[] = [];
14-
1511
function fillRowCells(
1612
columns: ColumnsType<RecordType>,
1713
colIndex: number,
@@ -54,12 +50,6 @@ function parseHeaderRows<RecordType>(
5450

5551
currentColIndex += colSpan;
5652

57-
// Measure only happen on single node
58-
if (measureColumnWidth && colSpan === 1 && !measureCells[cell.colStart]) {
59-
cell.measure = true;
60-
measureCells[cell.colStart] = true;
61-
}
62-
6353
return colSpan;
6454
});
6555

@@ -87,22 +77,17 @@ export interface HeaderProps<RecordType> {
8777
columns: ColumnsType<RecordType>;
8878
flattenColumns: ColumnType<RecordType>[];
8979
stickyOffsets: StickyOffsets;
90-
measureColumnWidth?: boolean;
9180
onHeaderRow: GetComponentProps<ColumnType<RecordType>[]>;
9281
}
9382

9483
function Header<RecordType>({
9584
stickyOffsets,
9685
columns,
9786
flattenColumns,
98-
measureColumnWidth,
9987
onHeaderRow,
10088
}: HeaderProps<RecordType>): React.ReactElement {
10189
const { getComponent } = React.useContext(TableContext);
102-
const rows: CellType<RecordType>[][] = React.useMemo(
103-
() => parseHeaderRows(columns, measureColumnWidth),
104-
[columns],
105-
);
90+
const rows: CellType<RecordType>[][] = React.useMemo(() => parseHeaderRows(columns), [columns]);
10691

10792
const WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
10893
const trComponent = getComponent(['header', 'row'], 'tr');

src/Header/HeaderRow.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import ResizeObserver from 'rc-resize-observer';
32
import Cell from '../Cell';
43
import {
54
CellType,
@@ -10,7 +9,6 @@ import {
109
} from '../interface';
1110
import TableContext from '../context/TableContext';
1211
import { getCellFixedInfo } from '../utils/fixUtil';
13-
import ResizeContext from '../context/ResizeContext';
1412
import { getColumnsKey } from '../utils/valueUtil';
1513

1614
export interface RowProps<RecordType> {
@@ -33,7 +31,6 @@ function HeaderRow<RecordType>({
3331
index,
3432
}: RowProps<RecordType>) {
3533
const { prefixCls } = React.useContext(TableContext);
36-
const { onColumnResize } = React.useContext(ResizeContext);
3734

3835
let rowProps: React.HTMLAttributes<HTMLElement>;
3936
if (onHeaderRow) {
@@ -45,7 +42,7 @@ function HeaderRow<RecordType>({
4542
return (
4643
<RowComponent {...rowProps}>
4744
{cells.map((cell: CellType<RecordType>, cellIndex) => {
48-
const { column, measure } = cell;
45+
const { column } = cell;
4946
const fixedInfo = getCellFixedInfo(
5047
cell.colStart,
5148
cell.colEnd,
@@ -58,7 +55,7 @@ function HeaderRow<RecordType>({
5855
additionalProps = cell.column.onHeaderCell(column);
5956
}
6057

61-
let cellNode = (
58+
return (
6259
<Cell
6360
{...cell}
6461
ellipsis={column.ellipsis}
@@ -70,21 +67,6 @@ function HeaderRow<RecordType>({
7067
additionalProps={additionalProps}
7168
/>
7269
);
73-
74-
if (measure) {
75-
cellNode = (
76-
<ResizeObserver
77-
key={cellIndex}
78-
onResize={({ width }) => {
79-
onColumnResize(columnsKey[cell.colStart], width);
80-
}}
81-
>
82-
{cellNode}
83-
</ResizeObserver>
84-
);
85-
}
86-
87-
return cellNode;
8870
})}
8971
</RowComponent>
9072
);

src/Table.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,7 @@ function Table<RecordType extends DefaultRecordType>(props: TableProps<RecordTyp
560560
>
561561
<TableComponent style={{ ...scrollTableStyle, tableLayout: mergedTableLayout }}>
562562
{bodyColGroup}
563-
{showHeader !== false && (
564-
<Header
565-
{...headerProps}
566-
{...columnContext}
567-
measureColumnWidth={!hasData && fixColumn}
568-
/>
569-
)}
563+
{showHeader !== false && <Header {...headerProps} {...columnContext} />}
570564
{bodyTable}
571565
{footerTable}
572566
</TableComponent>

src/interface.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export interface CellType<RecordType> {
4646
hasSubColumns?: boolean;
4747
colStart?: number;
4848
colEnd?: number;
49-
measure?: boolean;
5049
}
5150

5251
export interface RenderedCell<RecordType> {

tests/__snapshots__/ExpandRow.spec.js.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ exports[`Table.Expand renders fixed column correctly work 1`] = `
154154
<tbody
155155
class="rc-table-tbody"
156156
>
157+
<tr
158+
aria-hidden="true"
159+
class="rc-table-measure-row"
160+
>
161+
<td
162+
style="padding: 0px; border: 0px; height: 0px;"
163+
/>
164+
<td
165+
style="padding: 0px; border: 0px; height: 0px;"
166+
/>
167+
<td
168+
style="padding: 0px; border: 0px; height: 0px;"
169+
/>
170+
<td
171+
style="padding: 0px; border: 0px; height: 0px;"
172+
/>
173+
</tr>
157174
<tr
158175
class="rc-table-row rc-table-row-level-0"
159176
data-row-key="0"

0 commit comments

Comments
 (0)