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

Added bring to focus functionality #585

Merged
merged 5 commits into from
Jan 25, 2021
Merged
Changes from 4 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
27 changes: 27 additions & 0 deletions src/core/RecyclerListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,33 @@ export default class RecyclerListView<P extends RecyclerListViewProps, S extends
}
}

/**
* This API is almost similar to scrollToIndex, but differs when the view is already in viewport.
* Instead of bringing the view to the top of the viewport, it will calculate the overflow of the @param index
* and scroll to just bring the entire view to viewport.
*/
public bringToFocus(index: number, animate?: boolean): void {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@anirudhagarwal365 can you also add documentation around this in readme and as a comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

const listSize = this.getRenderedSize();
const itemLayout = this.getLayout(index);
const currentScrollOffset = this.getCurrentScrollOffset();
const {isHorizontal} = this.props;
if (itemLayout) {
const mainAxisLayoutDimen = isHorizontal ? itemLayout.width : itemLayout.height;
const mainAxisLayoutPos = isHorizontal ? itemLayout.x : itemLayout.y;
const mainAxisListDimen = isHorizontal ? listSize.width : listSize.height;
const screenEndPos = mainAxisListDimen + currentScrollOffset;
if (mainAxisLayoutDimen > mainAxisListDimen || mainAxisLayoutPos < currentScrollOffset || mainAxisLayoutPos > screenEndPos) {
this.scrollToIndex(index);
} else {
const viewEndPos = mainAxisLayoutPos + mainAxisLayoutDimen;
if (viewEndPos > screenEndPos) {
const offset = viewEndPos - screenEndPos;
this.scrollToOffset(0, offset + currentScrollOffset, animate);
}
}
}
}

public scrollToItem(data: any, animate?: boolean): void {
const count = this.props.dataProvider.getSize();
for (let i = 0; i < count; i++) {
Expand Down