-
-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathMeasureCell.tsx
More file actions
42 lines (35 loc) · 1.23 KB
/
MeasureCell.tsx
File metadata and controls
42 lines (35 loc) · 1.23 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
import * as React from 'react';
import ResizeObserver from '@rc-component/resize-observer';
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
import { cleanMeasureRowAttributes } from '../utils/measureUtil';
export interface MeasureCellProps {
columnKey: React.Key;
onColumnResize: (key: React.Key, width: number) => void;
title?: React.ReactNode;
}
const MeasureCell: React.FC<MeasureCellProps> = props => {
const { columnKey, onColumnResize, title } = props;
const cellRef = React.useRef<HTMLTableCellElement>(null);
const contentRef = React.useRef<HTMLDivElement>(null);
useLayoutEffect(() => {
if (cellRef.current) {
onColumnResize(columnKey, cellRef.current.offsetWidth);
}
if (contentRef.current) {
cleanMeasureRowAttributes(contentRef.current);
}
}, [title, columnKey, onColumnResize]);
return (
<ResizeObserver data={columnKey}>
<td
ref={cellRef}
style={{ paddingTop: 0, paddingBottom: 0, borderTop: 0, borderBottom: 0, height: 0 }}
>
<div ref={contentRef} style={{ height: 0, overflow: 'hidden', fontWeight: 'bold' }}>
{title || '\xa0'}
</div>
</td>
</ResizeObserver>
);
};
export default MeasureCell;