-
Notifications
You must be signed in to change notification settings - Fork 98
/
SwiperWithChildren.tsx
102 lines (97 loc) · 3.16 KB
/
SwiperWithChildren.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React from 'react';
import { Alert, TouchableOpacity, Text, Dimensions, StyleSheet } from 'react-native';
import { SwiperFlatList } from 'react-native-swiper-flatlist';
const { width, height } = Dimensions.get('window');
export default () => {
const scrollRef = React.useRef<SwiperFlatList>(null);
const goToLastIndex = () => {
scrollRef.current?.goToLastIndex();
};
const goToFirstIndex = () => {
scrollRef.current?.goToFirstIndex();
};
const goToSecondIndex = () => {
scrollRef.current?.scrollToIndex({ index: 1 });
};
const getCurrentIndex = () => {
const currentIndex = scrollRef.current?.getCurrentIndex();
Alert.alert(`the current index is ${currentIndex}`);
};
const getPrevIndex = () => {
const prevIndex = scrollRef.current?.getPrevIndex();
Alert.alert(`the previous index is ${prevIndex}`);
};
const [currentIndex, setCurrentIndex] = React.useState(0);
const [currentPrevIndex, setPrevIndex] = React.useState(0);
return (
<>
<Text style={styles.indexText}>
index: {currentIndex} - prevIndex: {currentPrevIndex}
</Text>
<SwiperFlatList
showPagination
ref={scrollRef}
onChangeIndex={({ index, prevIndex }) => {
console.log('example', { index, prevIndex });
setPrevIndex(prevIndex);
setCurrentIndex(index);
}}
// TODO: rename it to children eg: "container_swiper_children"
e2eID="container_swiper"
>
<TouchableOpacity
style={[styles.child, { backgroundColor: 'salmon' }]}
onPress={goToLastIndex}
testID="container_swiper_screen_0"
>
<Text style={styles.text}>0 - Go to last index</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.child, { backgroundColor: 'skyblue' }]}
onPress={getPrevIndex}
testID="container_swiper_screen_1"
>
<Text style={styles.text}>1 - Press to get the previous index</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.child, { backgroundColor: 'tomato' }]}
onPress={getCurrentIndex}
testID="container_swiper_screen_2"
>
<Text style={styles.text}>2 - Press to get the current index</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.child, { backgroundColor: 'skyblue' }]}
onPress={goToSecondIndex}
testID="container_swiper_screen_3"
>
<Text style={styles.text}>3 - Go to the second index</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.child, { backgroundColor: 'teal' }]}
onPress={goToFirstIndex}
testID="container_swiper_screen_4"
>
<Text style={styles.text}>4 - Go to first index</Text>
</TouchableOpacity>
</SwiperFlatList>
</>
);
};
const styles = StyleSheet.create({
child: {
height: height * 0.5,
width,
justifyContent: 'center',
},
text: {
fontSize: width * 0.1,
textAlign: 'center',
},
indexText: {
fontSize: 11,
backgroundColor: 'trasparent',
textAlign: 'center',
fontWeight: 'bold',
},
});