Skip to content

Commit

Permalink
Added bring to focus functionality (#585)
Browse files Browse the repository at this point in the history
* Added bring to focus functionality

* Added return type for bring to focus

* Added doc for bringToFocus

* Fixed tslint issue

Co-authored-by: Anirudh Agarwal <anirudh.agarwal@flipkart.com>
Co-authored-by: Talha Naqvi <naqvitalha@gmail.com>
  • Loading branch information
3 people authored Jan 25, 2021
1 parent e481e07 commit 782e6eb
Showing 1 changed file with 27 additions and 0 deletions.
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 {
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

0 comments on commit 782e6eb

Please sign in to comment.