Skip to content

Scroll To Last Index After Layout Change Of List #15662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/libs/ReportScrollManager/index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';

// This ref is created using React.createRef here because this function is used by a component that doesn't have access
// to the original ref.
const flatListRef = React.createRef();

// A reference to the last index required to scroll to, for async layout change handler.
let lastIndex;

/**
* Scroll to the provided index.
*
* @param {Object} index
*/
function scrollToIndex(index) {
flatListRef.current.scrollToIndex(index);
lastIndex = index;
}

/**
* Scroll to the last saved index on layout change
* See https://github.com/Expensify/App/issues/15303 for more context
*/
function scrollToLastIndex() {
if (!flatListRef.current) {
return;
}
if (lastIndex === undefined) {
return;
}

flatListRef.current.scrollToIndex(lastIndex);
lastIndex = undefined;
}

/**
* Scroll to the bottom of the flatlist.
*
*/
function scrollToBottom() {
if (!flatListRef.current) {
return;
}

flatListRef.current.scrollToOffset({animated: false, offset: 0});
lastIndex = 0;
}

export {
flatListRef,
scrollToIndex,
scrollToBottom,
scrollToLastIndex,
};
6 changes: 6 additions & 0 deletions src/libs/ReportScrollManager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ function scrollToIndex(index, isEditing) {
flatListRef.current.scrollToIndex(index);
}

/**
* Noop as this is only needed on iOS
*/
function scrollToLastIndex() {}

/**
* Scroll to the bottom of the flatlist.
*
Expand All @@ -35,4 +40,5 @@ export {
flatListRef,
scrollToIndex,
scrollToBottom,
scrollToLastIndex,
};
6 changes: 6 additions & 0 deletions src/libs/ReportScrollManager/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ function scrollToIndex(index) {
flatListRef.current.scrollToIndex(index);
}

/**
* Noop as this is only needed on iOS
*/
function scrollToLastIndex() {}

/**
* Scroll to the bottom of the flatlist.
*
Expand All @@ -29,4 +34,5 @@ export {
flatListRef,
scrollToIndex,
scrollToBottom,
scrollToLastIndex,
};
1 change: 1 addition & 0 deletions src/pages/home/report/ReportActionsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class ReportActionsList extends React.Component {
this.setState({
skeletonViewHeight: event.nativeEvent.layout.height,
});
ReportScrollManager.scrollToLastIndex();
this.props.onLayout(event);
}}
onScroll={this.props.onScroll}
Expand Down