Skip to content

Commit b297a4b

Browse files
committed
Added 'onSwipedX' callbacks to Swiper
1 parent 9f93f80 commit b297a4b

File tree

2 files changed

+95
-42
lines changed

2 files changed

+95
-42
lines changed

example/src/SwiperExample.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ function SwiperExample({ theme }) {
3636
style={{ width: "100%", height: 300 }}
3737
dotColor="green"
3838
dotActiveColor="red"
39-
onIndexChanged={index => console.log("onIndexChanged: ", index)}
39+
onSwipedLeft={(index) => console.log("Swiped left", index)}
40+
onSwipedRight={(index) => console.log("Swiped right", index)}
41+
onIndexChanged={(index) => console.log("onIndexChanged: ", index)}
4042
>
4143
<SwiperItem style={[style.item, { backgroundColor: "#fdd3d3" }]}>
4244
<Text>Test Slide 1</Text>
@@ -55,6 +57,8 @@ function SwiperExample({ theme }) {
5557
style={{ width: "100%", height: 300 }}
5658
dotColor="#86939e"
5759
dotActiveColor="#2089dc"
60+
onSwipedDown={(index) => console.log("Swiped down", index)}
61+
onSwipedUp={(index) => console.log("Swiped up", index)}
5862
>
5963
<SwiperItem style={[style.item, { backgroundColor: "#fdd3d3" }]}>
6064
<Text>Test Slide 1</Text>
@@ -67,7 +71,7 @@ function SwiperExample({ theme }) {
6771
</SwiperItem>
6872
</Swiper>
6973
</Section>
70-
<Section title="Data-Driven Example">
74+
{/* <Section title="Data-Driven Example">
7175
<Swiper
7276
vertical={false}
7377
loop={true}
@@ -93,7 +97,7 @@ function SwiperExample({ theme }) {
9397
</SwiperItem>
9498
)}
9599
/>
96-
</Section>
100+
</Section> */}
97101
</Container>
98102
);
99103
}

packages/core/src/components/Swiper/Swiper.tsx

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { View, StyleProp, ViewStyle } from "react-native";
33
import SwiperComponent from "react-native-web-swiper";
44

55
export 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

84133
export default Swiper;

0 commit comments

Comments
 (0)