Skip to content

Commit

Permalink
Added a property to disable swipe gesture. (#59)
Browse files Browse the repository at this point in the history
* Added a property to disable swipe gesture

Added a property to disable swipe gesture to only allow index change programmatically.

* Cleanning code

Cleanning code

* Added missing proptype

Added missing proptype

* Update snapshot for test new property

Update snapshot for test new property

* Update snapshot test

Update snapshot test

* Updated docs

Updated docs
  • Loading branch information
ebellumat authored and gusgard committed Oct 11, 2019
1 parent 363b190 commit a8c070d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ const styles = StyleSheet.create({
| autoplay | false | `bool` | Change index automatically |
| autoplayDelay | 3 | `number` | Delay between every page in seconds |
| autoplayLoop | false | `bool` | Continue playing after reach end |
| autoplayInvertDirection | false | `bool` | Invert auto play direction |
| autoplayInvertDirection | false | `bool` | Invert auto play direction
| disableGesture | false | `bool` | Disable swipe gesture |

**More props**

Expand Down
11 changes: 11 additions & 0 deletions src/components/SwiperFlatList/SwiperFlatList.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const SwiperFlatList = React.forwardRef(
onMomentumScrollEnd,
onViewableItemsChanged,
viewabilityConfig,
disableGesture,
...props
},
ref,
Expand All @@ -57,6 +58,7 @@ const SwiperFlatList = React.forwardRef(
const [paginationIndexes, setPaginationIndexes] = React.useState({ index, prevIndex: index });
const [ignoreOnMomentumScrollEnd, setIgnoreOnMomentumScrollEnd] = React.useState(false);
const flatListElement = React.useRef(null);
const [scrollEnabled, setScrollEnabled] = React.useState(!disableGesture);

const _onChangeIndex = React.useCallback(
({ index: _index, prevIndex: _prevIndex }) => {
Expand Down Expand Up @@ -105,15 +107,21 @@ const SwiperFlatList = React.forwardRef(

React.useImperativeHandle(ref, () => ({
scrollToIndex: (...args) => {
setScrollEnabled(true);
_scrollToIndex(...args);
setScrollEnabled(!disableGesture);
},
getCurrentIndex: () => paginationIndex,
getPrevIndex: () => prevIndex,
goToLastIndex: () => {
setScrollEnabled(true);
_scrollToIndex({ index: size - 1 });
setScrollEnabled(!disableGesture);
},
goToFirstIndex: () => {
setScrollEnabled(true);
_scrollToIndex({ index: FIRST_INDEX });
setScrollEnabled(!disableGesture);
},
}));

Expand Down Expand Up @@ -170,6 +178,7 @@ const SwiperFlatList = React.forwardRef(
);

const flatListProps = {
scrollEnabled,
ref: flatListElement,
keyExtractor: (_item, _index) => _index.toString(),
horizontal: !vertical,
Expand Down Expand Up @@ -250,6 +259,7 @@ SwiperFlatList.propTypes = {
onMomentumScrollEnd: PropTypes.func,
onViewableItemsChanged: PropTypes.func,
viewabilityConfig: PropTypes.object,
disableGesture: PropTypes.bool,
};

SwiperFlatList.defaultProps = {
Expand All @@ -268,6 +278,7 @@ SwiperFlatList.defaultProps = {
onMomentumScrollEnd: undefined,
onViewableItemsChanged: undefined,
viewabilityConfig: {},
disableGesture: false,
};

export default SwiperFlatList;
2 changes: 2 additions & 0 deletions src/components/SwiperFlatList/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ exports[`swiper flatlist renders children 1`] = `
pagingEnabled={true}
removeClippedSubviews={false}
renderItem={[Function]}
scrollEnabled={true}
scrollEventThrottle={50}
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
Expand Down Expand Up @@ -135,6 +136,7 @@ exports[`swiper flatlist renders correctly 1`] = `
pagingEnabled={true}
removeClippedSubviews={false}
renderItem={[Function]}
scrollEnabled={true}
scrollEventThrottle={50}
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
Expand Down

0 comments on commit a8c070d

Please sign in to comment.