-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathRowMeasurer.js
88 lines (77 loc) · 1.9 KB
/
RowMeasurer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React, { createRef } from 'react';
import PropTypes from 'prop-types';
class RowMeasurer extends React.Component {
static propTypes = {
children: PropTypes.node,
className: PropTypes.string,
heightCache: PropTypes.object,
measure: PropTypes.bool,
onBlur: PropTypes.func,
onClick: PropTypes.func,
onFocus: PropTypes.func,
onMeasure: PropTypes.func,
positionedRowStyle: PropTypes.object,
rowIndex: PropTypes.number,
staticPosition: PropTypes.bool
}
static defaultProps = {
onMeasure: () => {}
}
constructor(props) {
super(props);
this.element = createRef(null);
}
componentDidMount() {
if (!this.props.staticPosition) this.measureNode();
}
componentDidUpdate() {
if (!this.props.staticPosition) this.measureNode();
}
componentWillUnmount() {
const {
heightCache,
rowIndex,
} = this.props;
heightCache.clearRequest(rowIndex);
}
handleFocus = () => {
this.props.onFocus(this.props.rowIndex);
}
measureNode = () => {
const { rowIndex, heightCache, measure, onMeasure } = this.props;
if (measure) {
if (!heightCache.request(rowIndex)) {
if (this.element.current === null) return;
const elemHeight = this.element.current.offsetHeight;
heightCache.set(rowIndex, elemHeight);
onMeasure(elemHeight);
}
}
}
render() {
const {
rowIndex,
onClick,
className,
onFocus,
onBlur,
positionedRowStyle
} = this.props;
return (
<div // eslint-disable-line
data-row-index={`row-${rowIndex}`}
className={className}
aria-rowindex={rowIndex + 2}
onClick={onClick}
onFocus={onFocus}
onBlur={onBlur}
style={positionedRowStyle}
role="row"
ref={this.element}
>
{this.props.children}
</div>
);
}
}
export default RowMeasurer;