Skip to content

Commit 3d92a23

Browse files
committed
Close #113 Add itemSizeEstimator prop
1 parent 9b0d2c7 commit 3d92a23

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ The axis that this list scrolls on.
7070

7171
An index to scroll to after mounting.
7272

73-
##### itemSizeGetter(index)
74-
75-
A function that receives an item index and returns the size (height for y-axis
76-
lists and width for x-axis lists) of that item at that index. This prop is only
77-
used when the prop `type` is set to `variable`.
78-
7973
##### itemRenderer(index, key)
8074

8175
A function that receives an index and a key and returns the content to be
@@ -88,6 +82,21 @@ is just a `<div>`. Generally it only needs to be overridden for use in a
8882
`<table>` or other special case. **NOTE: You must set ref={ref} on the component
8983
that contains the `items` so the correct item sizing calculations can be made.**
9084

85+
##### itemSizeEstimator(index, cache)
86+
87+
A function that receives an item index and the cached known item sizes and
88+
returns an estimated size (height for y-axis lists and width for x-axis lists)
89+
of that item at that index. This prop is only used when the prop `type` is set
90+
to `variable` and `itemSizeGetter` is not defined. Use this property when you
91+
can't know the exact size of an item before rendering it, but want it to take up
92+
space in the list regardless.
93+
94+
##### itemSizeGetter(index)
95+
96+
A function that receives an item index and returns the size (height for y-axis
97+
lists and width for x-axis lists) of that item at that index. This prop is only
98+
used when the prop `type` is set to `variable`.
99+
91100
##### length (defaults to `0`)
92101

93102
The number of items in the list.

react-list.es6

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ export default class extends Component {
2828
static propTypes = {
2929
axis: PropTypes.oneOf(['x', 'y']),
3030
initialIndex: PropTypes.number,
31-
itemSizeGetter: PropTypes.func,
3231
itemRenderer: PropTypes.func,
32+
itemSizeEstimator: PropTypes.func,
33+
itemSizeGetter: PropTypes.func,
3334
itemsRenderer: PropTypes.func,
3435
length: PropTypes.number,
3536
pageSize: PropTypes.number,
@@ -41,13 +42,10 @@ export default class extends Component {
4142

4243
static defaultProps = {
4344
axis: 'y',
44-
initialIndex: null,
45-
itemSizeGetter: null,
4645
itemRenderer: (index, key) => <div key={key}>{index}</div>,
4746
itemsRenderer: (items, ref) => <div ref={ref}>{items}</div>,
4847
length: 0,
4948
pageSize: 10,
50-
scrollParentGetter: null,
5149
threshold: 100,
5250
type: 'simple',
5351
useTranslate3d: false
@@ -323,7 +321,7 @@ export default class extends Component {
323321

324322
getSizeOf(index) {
325323
const {cache, items} = this;
326-
const {axis, itemSizeGetter, type} = this.props;
324+
const {axis, itemSizeGetter, itemSizeEstimator, type} = this.props;
327325
const {from, itemSize, size} = this.state;
328326

329327
// Try the static itemSize.
@@ -340,6 +338,9 @@ export default class extends Component {
340338
const itemEl = findDOMNode(items).children[index - from];
341339
if (itemEl) return itemEl[OFFSET_SIZE_KEYS[axis]];
342340
}
341+
342+
// Try the itemSizeEstimator.
343+
if (itemSizeEstimator) return itemSizeEstimator(index, cache);
343344
}
344345

345346
constrain(from, size, itemsPerRow, {length, pageSize, type}) {

react-list.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@
6363
value: {
6464
axis: _react.PropTypes.oneOf(['x', 'y']),
6565
initialIndex: _react.PropTypes.number,
66-
itemSizeGetter: _react.PropTypes.func,
6766
itemRenderer: _react.PropTypes.func,
67+
itemSizeEstimator: _react.PropTypes.func,
68+
itemSizeGetter: _react.PropTypes.func,
6869
itemsRenderer: _react.PropTypes.func,
6970
length: _react.PropTypes.number,
7071
pageSize: _react.PropTypes.number,
@@ -78,8 +79,6 @@
7879
key: 'defaultProps',
7980
value: {
8081
axis: 'y',
81-
initialIndex: null,
82-
itemSizeGetter: null,
8382
itemRenderer: function itemRenderer(index, key) {
8483
return _React['default'].createElement(
8584
'div',
@@ -96,7 +95,6 @@
9695
},
9796
length: 0,
9897
pageSize: 10,
99-
scrollParentGetter: null,
10098
threshold: 100,
10199
type: 'simple',
102100
useTranslate3d: false
@@ -445,6 +443,7 @@
445443
var _props6 = this.props;
446444
var axis = _props6.axis;
447445
var itemSizeGetter = _props6.itemSizeGetter;
446+
var itemSizeEstimator = _props6.itemSizeEstimator;
448447
var type = _props6.type;
449448
var _state3 = this.state;
450449
var from = _state3.from;
@@ -465,6 +464,9 @@
465464
var itemEl = findDOMNode(items).children[index - from];
466465
if (itemEl) return itemEl[OFFSET_SIZE_KEYS[axis]];
467466
}
467+
468+
// Try the itemSizeEstimator.
469+
if (itemSizeEstimator) return itemSizeEstimator(index, cache);
468470
}
469471
}, {
470472
key: 'constrain',

0 commit comments

Comments
 (0)