Skip to content

Commit

Permalink
feat: add scroll to item and scroll to offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Cesarato committed Jun 7, 2021
1 parent 0f1f4a8 commit 1824633
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/src/lists/CompareList.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-shadow */
import React from "react";
import {
FlatList,
Expand Down
39 changes: 39 additions & 0 deletions lib/BigList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,45 @@ class BigList extends PureComponent {
return this.scrollTo(options);
}

/**
* Scroll to item.
* @param {object} item
* @param {bool} animated
* @returns {bool}
*/
scrollToItem({ item, animated = false }) {
let index;
if (this.hasSections()) {
const coords = JSON.stringify(
this.map((a) => {
return a[0] + "|" + a[1];
}),
);
index = coords.indexOf(item[0] + "|" + item[1]) !== -1;
} else {
index = this.props.data.indexOf(item);
}
return this.scrollTo({ index, animated });
}

/**
* Scroll to offset.
* @param {number} offset
* @param {bool} animated
* @returns {bool}
*/
scrollToOffset({ offset, animated = false }) {
if (this.scrollView.current != null) {
this.scrollView.current.scrollTo({
x: 0,
y: offset,
animated,
});
return true;
}
return false;
}

/**
* Scroll to top.
* @param {bool} animated
Expand Down

0 comments on commit 1824633

Please sign in to comment.