Skip to content

Commit

Permalink
fix: make sure initialScrollIndex is bigger than 0 (#36844)
Browse files Browse the repository at this point in the history
Summary:
Hey,

`adjustCellsAroundViewport` function was checking if `props.initialScrollIndex` is truthy and -1 was returning true. This caused bugs with rendering for tvOS: react-native-tvos/react-native-tvos#485 There are warnings in the code about `initalScrollIndex` being smaller than 0 but this if statement would still allow that.

## Changelog:

[General] [Fixed] - Make sure initialScrollToIndex is bigger than 0 when adjusting cells

Pull Request resolved: #36844

Test Plan: Pass -1 as initialScrollToIndex. Check that this code is executed.

Reviewed By: cipolleschi

Differential Revision: D44856266

Pulled By: NickGerleman

fbshipit-source-id: 781a1c0efeae93f00766eede4a42559dcd066d7d
  • Loading branch information
okwasniewski authored and facebook-github-bot committed Apr 18, 2023
1 parent 4264ddb commit eb30a80
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/virtualized-lists/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
),
};
} else {
// If we have a non-zero initialScrollIndex and run this before we've scrolled,
// If we have a positive non-zero initialScrollIndex and run this before we've scrolled,
// we'll wipe out the initialNumToRender rendered elements starting at initialScrollIndex.
// So let's wait until we've scrolled the view to the right place. And until then,
// we will trust the initialScrollIndex suggestion.
Expand All @@ -627,7 +627,8 @@ class VirtualizedList extends StateSafePureComponent<Props, State> {
// - initialScrollIndex > 0 AND the end of the list is visible (this handles the case
// where the list is shorter than the visible area)
if (
props.initialScrollIndex &&
props.initialScrollIndex != null &&
props.initialScrollIndex > 0 &&
!this._scrollMetrics.offset &&
Math.abs(distanceFromEnd) >= Number.EPSILON
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3122,12 +3122,53 @@ exports[`gracefully handles negative initialScrollIndex 1`] = `
/>
</View>
<View
style={
Object {
"height": 60,
}
}
/>
onFocusCapture={[Function]}
style={null}
>
<MockCellItem
value={4}
/>
</View>
<View
onFocusCapture={[Function]}
style={null}
>
<MockCellItem
value={5}
/>
</View>
<View
onFocusCapture={[Function]}
style={null}
>
<MockCellItem
value={6}
/>
</View>
<View
onFocusCapture={[Function]}
style={null}
>
<MockCellItem
value={7}
/>
</View>
<View
onFocusCapture={[Function]}
style={null}
>
<MockCellItem
value={8}
/>
</View>
<View
onFocusCapture={[Function]}
style={null}
>
<MockCellItem
value={9}
/>
</View>
</View>
</RCTScrollView>
`;
Expand Down

0 comments on commit eb30a80

Please sign in to comment.