@@ -3,6 +3,10 @@ import { View, StyleProp, ViewStyle } from "react-native";
33import SwiperComponent from "react-native-web-swiper" ;
44
55export interface SwiperProps < T > {
6+ onSwipedLeft ?: ( index : number ) => void ;
7+ onSwipedRight ?: ( index : number ) => void ;
8+ onSwipedUp ?: ( index : number ) => void ;
9+ onSwipedDown ?: ( index : number ) => void ;
610 vertical ?: boolean ;
711 loop ?: boolean ;
812 from ?: number ;
@@ -38,47 +42,92 @@ const Swiper = ({
3842 keyExtractor,
3943 renderItem,
4044 children,
41- onIndexChanged,
45+ onIndexChanged : onIndexChangedProp ,
46+ onSwipedLeft,
47+ onSwipedRight,
48+ onSwipedUp,
49+ onSwipedDown,
4250 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 } ) ;
51+ } : SwiperProps < any > ) => {
52+ const [ currentIndex , setCurrentIndex ] = React . useState ( 0 ) ;
53+ const numberOfItems = data ?. length ?? React . Children . count ( children ) ;
6954
70- if ( ! component ) {
71- return null ;
72- }
55+ const onIndexChanged = ( index : number ) => {
56+ const previous = currentIndex ;
57+ const current = index ;
7358
74- const key = keyExtractor ? keyExtractor ( item , index ) : index ;
75- return React . cloneElement ( component , {
76- key,
77- } ) ;
78- } )
79- : children }
80- </ SwiperComponent >
81- </ View >
82- ) ;
59+ onIndexChangedProp ?.( index ) ;
60+ setCurrentIndex ( index ) ;
61+
62+ if ( previous === numberOfItems - 1 && current === 0 ) {
63+ //Last -> first swipe
64+ if ( vertical ) {
65+ onSwipedUp ?.( previous ) ;
66+ } else {
67+ onSwipedLeft ?.( previous ) ;
68+ }
69+ } else if ( previous === 0 && current === numberOfItems - 1 ) {
70+ //First -> last swipe
71+ if ( vertical ) {
72+ onSwipedDown ?.( previous ) ;
73+ } else {
74+ onSwipedRight ?.( previous ) ;
75+ }
76+ } else if ( current > previous ) {
77+ if ( vertical ) {
78+ onSwipedUp ?.( previous ) ;
79+ } else {
80+ onSwipedLeft ?.( previous ) ;
81+ }
82+ } else if ( current < previous ) {
83+ if ( vertical ) {
84+ onSwipedDown ?.( previous ) ;
85+ } else {
86+ onSwipedRight ?.( previous ) ;
87+ }
88+ }
89+ } ;
90+
91+ return (
92+ < View style = { style } >
93+ { /* @ts -ignore */ }
94+ < SwiperComponent
95+ from = { from }
96+ loop = { loop }
97+ timeout = { timeout }
98+ vertical = { vertical }
99+ onIndexChanged = { onIndexChanged }
100+ controlsProps = { {
101+ prevTitle,
102+ nextTitle,
103+ prevTitleStyle : { color : prevTitleColor } ,
104+ nextTitleStyle : { color : nextTitleColor } ,
105+ dotsTouchable,
106+ ...( dotColor
107+ ? { dotProps : { badgeStyle : { backgroundColor : dotColor } } }
108+ : { } ) ,
109+ ...( dotActiveColor
110+ ? { dotActiveStyle : { backgroundColor : dotActiveColor } }
111+ : { } ) ,
112+ } }
113+ >
114+ { data && renderItem
115+ ? data . map ( ( item , index ) => {
116+ const component = renderItem ( { item, index } ) ;
117+
118+ if ( ! component ) {
119+ return null ;
120+ }
121+
122+ const key = keyExtractor ? keyExtractor ( item , index ) : index ;
123+ return React . cloneElement ( component , {
124+ key,
125+ } ) ;
126+ } )
127+ : children }
128+ </ SwiperComponent >
129+ </ View >
130+ ) ;
131+ } ;
83132
84133export default Swiper ;
0 commit comments