forked from bvaughn/react-virtualized
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCellMeasurer.DynamicHeightList.example.js
76 lines (65 loc) · 1.91 KB
/
CellMeasurer.DynamicHeightList.example.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
import Immutable from 'immutable';
import PropTypes from 'prop-types';
import * as React from 'react';
import CellMeasurer from './CellMeasurer';
import CellMeasurerCache from './CellMeasurerCache';
import List from '../List';
import styles from './CellMeasurer.example.css';
export default class DynamicHeightList extends React.PureComponent {
static propTypes = {
getClassName: PropTypes.func.isRequired,
list: PropTypes.instanceOf(Immutable.List).isRequired,
width: PropTypes.number.isRequired,
};
constructor(props, context) {
super(props, context);
this._cache = new CellMeasurerCache({
fixedWidth: true,
minHeight: 50,
});
this._rowRenderer = this._rowRenderer.bind(this);
}
render() {
const {width} = this.props;
return (
<List
className={styles.BodyGrid}
deferredMeasurementCache={this._cache}
height={400}
overscanRowCount={0}
rowCount={1000}
rowHeight={this._cache.rowHeight}
rowRenderer={this._rowRenderer}
width={width}
/>
);
}
_rowRenderer({index, key, parent, style}) {
const {getClassName, list} = this.props;
const datum = list.get(index % list.size);
const classNames = getClassName({columnIndex: 0, rowIndex: index});
const imageWidth = 300;
const imageHeight = datum.size * (1 + (index % 3));
const source = `https://www.fillmurray.com/${imageWidth}/${imageHeight}`;
return (
<CellMeasurer
cache={this._cache}
columnIndex={0}
key={key}
rowIndex={index}
parent={parent}>
{({measure, registerChild}) => (
<div ref={registerChild} className={classNames} style={style}>
<img
onLoad={measure}
src={source}
style={{
width: imageWidth,
}}
/>
</div>
)}
</CellMeasurer>
);
}
}