-
Notifications
You must be signed in to change notification settings - Fork 37
/
CellMeasurer.js
56 lines (46 loc) · 1.34 KB
/
CellMeasurer.js
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
import React, { createRef } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import noop from 'lodash/noop';
class CellMeasurer extends React.Component {
static propTypes = {
children: PropTypes.func,
columnName: PropTypes.string,
onMeasure: PropTypes.func,
rowIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
shouldMeasure: PropTypes.bool,
widthCache: PropTypes.object,
}
static defaultProps = {
onMeasure: noop
}
constructor(props) {
super(props);
this.element = createRef(null);
}
componentDidMount() {
this.measureNode();
}
componentDidUpdate() {
this.measureNode();
}
measureNode = () => {
const { widthCache, rowIndex, shouldMeasure, onMeasure, columnName } = this.props;
if (shouldMeasure) {
// const elem = ReactDOM.findDOMNode(this); // eslint-disable-line react/no-find-dom-node
if (this.element.current && this.element.current.offsetParent !== null) {
const width = this.element.current.offsetWidth;
widthCache.set(rowIndex, width);
if (onMeasure) onMeasure(rowIndex, columnName, width);
}
}
};
render() {
const { children } = this.props;
if (typeof children === 'function') {
return children(this.element);
}
return null;
}
}
export default CellMeasurer;