Skip to content

Commit

Permalink
Merge pull request #33 from umangloria/main
Browse files Browse the repository at this point in the history
feat: add callback for onIndexChange
  • Loading branch information
Skipperlla authored Aug 28, 2024
2 parents d6c9866 + fd1c546 commit 12b823d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ yarn add react-native-reanimated react-native-gesture-handler

## Event callbacks

| Props | type | description | default |
| :------------ | :--- | :--------------------------------------------------------------------------------------------- | :------------------ |
| onSwipeLeft | func | Function called when a card is swiped left. It receives the index of the card as a parameter. | `(cardIndex) => {}` |
| onSwipeRight | func | Function called when a card is swiped right. It receives the index of the card as a parameter. | `(cardIndex) => {}` |
| onSwipeTop | func | Function called when a card is swiped top. It receives the index of the card as a parameter. | `(cardIndex) => {}` |
| onSwipedAll | func | Function called when all cards have been swiped. | `() => {}` |
| onSwipeStart | func | Function called when a swipe event starts. | `() => {}` |
| onSwipeEnd | func | Function called when a swipe event ends. | `() => {}` |
| onSwipeActive | func | Function called when a swipe event is active. | `() => {}` |
| Props | type | description | default |
| :------------ | :--- | :---------------------------------------------------------------------------------------------------- | :------------------ |
| onSwipeLeft | func | Function called when a card is swiped left. It receives the index of the card as a parameter. | `(cardIndex) => {}` |
| onSwipeRight | func | Function called when a card is swiped right. It receives the index of the card as a parameter. | `(cardIndex) => {}` |
| onSwipeTop | func | Function called when a card is swiped top. It receives the index of the card as a parameter. | `(cardIndex) => {}` |
| onSwipedAll | func | Function called when all cards have been swiped. | `() => {}` |
| onSwipeStart | func | Function called when a swipe event starts. | `() => {}` |
| onSwipeEnd | func | Function called when a swipe event ends. | `() => {}` |
| onSwipeActive | func | Function called when a swipe event is active. | `() => {}` |
| onIndexChange | func | Function called when the index of the card changes. It receives the index of the card as a parameter. | `(cardIndex) => {}` |

## Swipe Animation Props

Expand Down Expand Up @@ -182,6 +183,9 @@ const App = () => {
cardStyle={styles.cardStyle}
data={IMAGES}
renderCard={renderCard}
onIndexChange={(index) => {
console.log('Current Active index', index);
}}
onSwipeRight={(cardIndex) => {
console.log('cardIndex', cardIndex);
}}
Expand Down
3 changes: 3 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ const App = () => {
cardStyle={styles.cardStyle}
data={IMAGES}
renderCard={renderCard}
onIndexChange={(index) => {
console.log('Current Active index', index);
}}
onSwipeRight={(cardIndex) => {
console.log('cardIndex', cardIndex);
}}
Expand Down
14 changes: 14 additions & 0 deletions src/Swiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const Swiper = <T,>(
onSwipedAll,
onSwipeTop,
onSwipeBottom,
onIndexChange,
cardStyle,
disableRightSwipe,
disableLeftSwipe,
Expand Down Expand Up @@ -80,6 +81,19 @@ const Swiper = <T,>(
[data]
);

//Listen to the activeIndex value
useAnimatedReaction(
() => {
return activeIndex.value;
},
(currentValue, previousValue) => {
if (currentValue !== previousValue && onIndexChange) {
runOnJS(onIndexChange)(currentValue);
}
},
[]
);

return data.map((item, index) => {
return (
<SwiperCard
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type SwiperOptions<T> = {
onSwipeStart?: () => void;
onSwipeEnd?: () => void;
onSwipeActive?: () => void;
//* Active Index Change Callback
onIndexChange?: (index: number) => void;
//* Swipe Animation Props
disableRightSwipe?: boolean;
disableLeftSwipe?: boolean;
Expand Down

0 comments on commit 12b823d

Please sign in to comment.