-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathMeasureRow.tsx
More file actions
60 lines (53 loc) · 1.87 KB
/
MeasureRow.tsx
File metadata and controls
60 lines (53 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as React from 'react';
import ResizeObserver from '@rc-component/resize-observer';
import MeasureCell from './MeasureCell';
import isVisible from '@rc-component/util/lib/Dom/isVisible';
import { useContext } from '@rc-component/context';
import TableContext from '../context/TableContext';
import type { ColumnType } from '../interface';
export interface MeasureRowProps {
prefixCls: string;
onColumnResize: (key: React.Key, width: number) => void;
columnsKey: React.Key[];
columns: readonly ColumnType<any>[];
}
const MeasureRow: React.FC<MeasureRowProps> = ({
prefixCls,
columnsKey,
onColumnResize,
columns,
}) => {
const ref = React.useRef<HTMLTableRowElement>(null);
const { measureRowRender } = useContext(TableContext, ['measureRowRender']);
const measureRow = (
<tr aria-hidden="true" className={`${prefixCls}-measure-row`} style={{ height: 0 }} ref={ref}>
<ResizeObserver.Collection
onBatchResize={infoList => {
if (isVisible(ref.current)) {
infoList.forEach(({ data: columnKey, size }) => {
onColumnResize(columnKey, size.offsetWidth);
});
}
}}
>
{columnsKey.map(columnKey => {
const column = columns.find(col => col.key === columnKey);
const rawTitle = column?.title;
const titleForMeasure = React.isValidElement<React.RefAttributes<any>>(rawTitle)
? React.cloneElement(rawTitle, { ref: null })
: rawTitle;
return (
<MeasureCell
key={columnKey}
columnKey={columnKey}
onColumnResize={onColumnResize}
title={titleForMeasure}
/>
);
})}
</ResizeObserver.Collection>
</tr>
);
return typeof measureRowRender === 'function' ? measureRowRender(measureRow) : measureRow;
};
export default MeasureRow;