Skip to content

Commit

Permalink
Add onScrollToIndexFailed support
Browse files Browse the repository at this point in the history
Summary: Allows handling the case of wanting to scroll beyond the measured window.

Reviewed By: TheSavior

Differential Revision: D5915331

fbshipit-source-id: 329927632f4d04f213567ce4bbe547b04b8ea86d
  • Loading branch information
sahrens authored and facebook-github-bot committed Sep 28, 2017
1 parent 1af645b commit e16b514
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ type OptionalProps = {
* sure to also set the `refreshing` prop correctly.
*/
onRefresh?: ?Function,
/**
* Used to handle failures when scrolling to an index that has not been measured yet. Recommended
* action is to either compute your own offset and `scrollTo` it, or scroll as far as possible and
* then try again after more items have been rendered.
*/
onScrollToIndexFailed?: ?(info: {
index: number,
highestMeasuredFrameIndex: number,
averageItemLength: number,
}) => void,
/**
* Called when the viewability of rows changes, as defined by the
* `viewabilityConfig` prop.
Expand Down Expand Up @@ -259,17 +269,31 @@ class VirtualizedList extends React.PureComponent<Props, State> {
viewOffset?: number,
viewPosition?: number,
}) {
const {data, horizontal, getItemCount, getItemLayout} = this.props;
const {
data,
horizontal,
getItemCount,
getItemLayout,
onScrollToIndexFailed,
} = this.props;
const {animated, index, viewOffset, viewPosition} = params;
invariant(
index >= 0 && index < getItemCount(data),
`scrollToIndex out of range: ${index} vs ${getItemCount(data) - 1}`,
);
invariant(
getItemLayout || index <= this._highestMeasuredFrameIndex,
'scrollToIndex should be used in conjunction with getItemLayout, ' +
'otherwise there is no way to know the location of an arbitrary index.',
);
if (!getItemLayout && index > this._highestMeasuredFrameIndex) {
invariant(
!!onScrollToIndexFailed,
'scrollToIndex should be used in conjunction with getItemLayout or onScrollToIndexFailed, ' +
'otherwise there is no way to know the location of offscreen indices or handle failures.',
);
onScrollToIndexFailed({
averageItemLength: this._averageCellLength,
highestMeasuredFrameIndex: this._highestMeasuredFrameIndex,
index,
});
return;
}
const frame = this._getFrameMetricsApprox(index);
const offset =
Math.max(
Expand Down

0 comments on commit e16b514

Please sign in to comment.