@@ -3,6 +3,8 @@ import { View, StyleProp, ViewStyle } from "react-native";
33import SwiperComponent from "react-native-web-swiper" ;
44
55export interface SwiperProps < T > {
6+ onSwipedNext ?: ( index : number ) => void ;
7+ onSwipedPrevious ?: ( index : number ) => void ;
68 vertical ?: boolean ;
79 loop ?: boolean ;
810 from ?: number ;
@@ -38,47 +40,74 @@ const Swiper = ({
3840 keyExtractor,
3941 renderItem,
4042 children,
41- onIndexChanged,
43+ onIndexChanged : onIndexChangedProp ,
44+ onSwipedNext,
45+ onSwipedPrevious,
4246 style,
43- } : SwiperProps < any > ) => (
44- < View style = { style } >
45- { /* @ts -ignore */ }
46- < SwiperComponent
47- from = { from }
48- loop = { loop }
49- timeout = { timeout }
50- vertical = { vertical }
51- onIndexChanged = { onIndexChanged }
52- controlsProps = { {
53- prevTitle,
54- nextTitle,
55- prevTitleStyle : { color : prevTitleColor } ,
56- nextTitleStyle : { color : nextTitleColor } ,
57- dotsTouchable,
58- ...( dotColor
59- ? { dotProps : { badgeStyle : { backgroundColor : dotColor } } }
60- : { } ) ,
61- ...( dotActiveColor
62- ? { dotActiveStyle : { backgroundColor : dotActiveColor } }
63- : { } ) ,
64- } }
65- >
66- { data && renderItem
67- ? data . map ( ( item , index ) => {
68- const component = renderItem ( { item, index } ) ;
47+ } : SwiperProps < any > ) => {
48+ const [ currentIndex , setCurrentIndex ] = React . useState ( 0 ) ;
49+ const numberOfItems = data ?. length ?? React . Children . count ( children ) ;
6950
70- if ( ! component ) {
71- return null ;
72- }
51+ const onIndexChanged = ( index : number ) => {
52+ const previous = currentIndex ;
53+ const current = index ;
7354
74- const key = keyExtractor ? keyExtractor ( item , index ) : index ;
75- return React . cloneElement ( component , {
76- key,
77- } ) ;
78- } )
79- : children }
80- </ SwiperComponent >
81- </ View >
82- ) ;
55+ onIndexChangedProp ?.( index ) ;
56+ setCurrentIndex ( index ) ;
57+
58+ if ( previous === numberOfItems - 1 && current === 0 ) {
59+ //Last -> first swipe
60+ onSwipedNext ?.( previous ) ;
61+ } else if ( previous === 0 && current === numberOfItems - 1 ) {
62+ //First -> last swipe
63+ onSwipedPrevious ?.( previous ) ;
64+ } else if ( current > previous ) {
65+ onSwipedNext ?.( previous ) ;
66+ } else if ( current < previous ) {
67+ onSwipedPrevious ?.( previous ) ;
68+ }
69+ } ;
70+
71+ return (
72+ < View style = { style } >
73+ { /* @ts -ignore */ }
74+ < SwiperComponent
75+ from = { from }
76+ loop = { loop }
77+ timeout = { timeout }
78+ vertical = { vertical }
79+ onIndexChanged = { onIndexChanged }
80+ controlsProps = { {
81+ prevTitle,
82+ nextTitle,
83+ prevTitleStyle : { color : prevTitleColor } ,
84+ nextTitleStyle : { color : nextTitleColor } ,
85+ dotsTouchable,
86+ ...( dotColor
87+ ? { dotProps : { badgeStyle : { backgroundColor : dotColor } } }
88+ : { } ) ,
89+ ...( dotActiveColor
90+ ? { dotActiveStyle : { backgroundColor : dotActiveColor } }
91+ : { } ) ,
92+ } }
93+ >
94+ { data && renderItem
95+ ? data . map ( ( item , index ) => {
96+ const component = renderItem ( { item, index } ) ;
97+
98+ if ( ! component ) {
99+ return null ;
100+ }
101+
102+ const key = keyExtractor ? keyExtractor ( item , index ) : index ;
103+ return React . cloneElement ( component , {
104+ key,
105+ } ) ;
106+ } )
107+ : children }
108+ </ SwiperComponent >
109+ </ View >
110+ ) ;
111+ } ;
83112
84113export default Swiper ;
0 commit comments