Skip to content
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

Scroll To Last Index After Layout Change Of List #15662

Closed
wants to merge 10 commits into from
7 changes: 7 additions & 0 deletions src/libs/ReportScrollManager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ function scrollToIndex(index, isEditing) {
flatListRef.current.scrollToIndex(index);
}

/**
* Scroll to the last saved index on layout change
* The is a fallback handler for asyncronius scrolling and layout changes.
*/
function scrollToLastIndex() {}

/**
* Scroll to the bottom of the flatlist.
*
Expand All @@ -35,4 +41,5 @@ export {
flatListRef,
scrollToIndex,
scrollToBottom,
scrollToLastIndex,
};
22 changes: 22 additions & 0 deletions src/libs/ReportScrollManager/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,33 @@ import React from 'react';
// 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
* The is a fallback handler for asyncronius scrolling and layout changes.
*/
function scrollToLastIndex() {
if (!flatListRef.current) {
return;
}
if (lastIndex === undefined) {
return;
}

flatListRef.current.scrollToIndex(lastIndex);
lastIndex = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do this? I might be wrong, but I think we don't need to set lastIndex to undefined and don't need the if (lastIndex === undefined) check. Could you verify that, please?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uncontrolled behavior requires this because the events aren't chained and can happen asynchronously the check validates and clearance of this value validates the action won't happen again if not actually needed

}

/**
Expand All @@ -23,10 +43,12 @@ function scrollToBottom() {
}

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

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