Skip to content

Commit

Permalink
fix: fix virtualizedList scrollToEnd for 0 items (#36067)
Browse files Browse the repository at this point in the history
Summary:
Fixes #36066

## Changelog

[GENERAL] [FIXED] - VirtualizedList scrollToEnd with no data

Pull Request resolved: #36067

Test Plan: Run `yarn test VirtualizedList-test`

Reviewed By: jacdebug

Differential Revision: D43041763

Pulled By: javache

fbshipit-source-id: d4d5e871284708a89bf9911d82e9aa97d7625aca
  • Loading branch information
Andarius authored and facebook-github-bot committed Feb 6, 2023
1 parent 34604fa commit 98009ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ export default class VirtualizedList extends StateSafePureComponent<
scrollToEnd(params?: ?{animated?: ?boolean, ...}) {
const animated = params ? params.animated : true;
const veryLast = this.props.getItemCount(this.props.data) - 1;
if (veryLast < 0) {
return;
}
const frame = this.__getFrameMetricsApprox(veryLast, this.props);
const offset = Math.max(
0,
Expand Down
14 changes: 14 additions & 0 deletions Libraries/Lists/__tests__/VirtualizedList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ describe('VirtualizedList', () => {
expect(component).toMatchSnapshot();
});

it('scrollToEnd works with null list', () => {
const listRef = React.createRef(null);
ReactTestRenderer.create(
<VirtualizedList
data={undefined}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => 0}
ref={listRef}
/>,
);
listRef.current.scrollToEnd();
});

it('renders empty list with empty component', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
Expand Down

0 comments on commit 98009ad

Please sign in to comment.