react-native-sg-basic-carousel 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.tsx +24 -33
- package/package.json +1 -1
package/index.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable react-native/no-inline-styles */
|
|
2
|
-
import React, {useState, useRef, useEffect} from
|
|
2
|
+
import React, { useState, useRef, useEffect } from "react";
|
|
3
3
|
import {
|
|
4
4
|
View,
|
|
5
5
|
FlatList,
|
|
@@ -8,17 +8,11 @@ import {
|
|
|
8
8
|
ViewStyle,
|
|
9
9
|
StyleProp,
|
|
10
10
|
ImageBackground,
|
|
11
|
-
} from
|
|
12
|
-
|
|
13
|
-
// CarouselItem must have at least an image, but other fields can be dynamic
|
|
14
|
-
interface CarouselItem {
|
|
15
|
-
image: string; // The image field is required
|
|
16
|
-
[key: string]: any; // Other properties are flexible
|
|
17
|
-
}
|
|
11
|
+
} from "react-native";
|
|
18
12
|
|
|
19
13
|
interface CarouselProps {
|
|
20
|
-
data:
|
|
21
|
-
renderItem?: (item:
|
|
14
|
+
data: any[]; // Array of items with flexible structure but image is mandatory
|
|
15
|
+
renderItem?: (item: any) => JSX.Element; // Custom renderer
|
|
22
16
|
autoScroll?: boolean;
|
|
23
17
|
scrollInterval?: number;
|
|
24
18
|
showPagination?: boolean;
|
|
@@ -27,7 +21,7 @@ interface CarouselProps {
|
|
|
27
21
|
paginationDotStyle?: StyleProp<ViewStyle>;
|
|
28
22
|
}
|
|
29
23
|
|
|
30
|
-
const {width: screenWidth} = Dimensions.get(
|
|
24
|
+
const { width: screenWidth } = Dimensions.get("window");
|
|
31
25
|
|
|
32
26
|
const BasicCarousel = ({
|
|
33
27
|
data,
|
|
@@ -40,14 +34,14 @@ const BasicCarousel = ({
|
|
|
40
34
|
paginationDotStyle,
|
|
41
35
|
}: CarouselProps) => {
|
|
42
36
|
const [activeIndex, setActiveIndex] = useState(0);
|
|
43
|
-
const flatListRef = useRef<FlatList<
|
|
37
|
+
const flatListRef = useRef<FlatList<any>>(null);
|
|
44
38
|
|
|
45
39
|
useEffect(() => {
|
|
46
40
|
if (autoScroll) {
|
|
47
41
|
const timerId = setInterval(() => {
|
|
48
42
|
if (activeIndex === data.length - 1) {
|
|
49
43
|
setActiveIndex(0);
|
|
50
|
-
flatListRef.current?.scrollToIndex({index: 0, animated: true});
|
|
44
|
+
flatListRef.current?.scrollToIndex({ index: 0, animated: true });
|
|
51
45
|
} else {
|
|
52
46
|
setActiveIndex(activeIndex + 1);
|
|
53
47
|
flatListRef.current?.scrollToIndex({
|
|
@@ -62,11 +56,11 @@ const BasicCarousel = ({
|
|
|
62
56
|
}, [activeIndex, autoScroll, scrollInterval, data.length]);
|
|
63
57
|
|
|
64
58
|
const onViewableItemsChanged = useRef(
|
|
65
|
-
({viewableItems}: {viewableItems: any}) => {
|
|
59
|
+
({ viewableItems }: { viewableItems: any }) => {
|
|
66
60
|
if (viewableItems.length > 0) {
|
|
67
61
|
setActiveIndex(viewableItems[0].index);
|
|
68
62
|
}
|
|
69
|
-
}
|
|
63
|
+
}
|
|
70
64
|
).current;
|
|
71
65
|
|
|
72
66
|
return (
|
|
@@ -74,7 +68,7 @@ const BasicCarousel = ({
|
|
|
74
68
|
<FlatList
|
|
75
69
|
ref={flatListRef}
|
|
76
70
|
data={data}
|
|
77
|
-
renderItem={({item}) =>
|
|
71
|
+
renderItem={({ item }) =>
|
|
78
72
|
renderItem ? renderItem(item) : <DefaultCarouselCard item={item} />
|
|
79
73
|
}
|
|
80
74
|
horizontal
|
|
@@ -82,7 +76,7 @@ const BasicCarousel = ({
|
|
|
82
76
|
showsHorizontalScrollIndicator={false}
|
|
83
77
|
onViewableItemsChanged={onViewableItemsChanged}
|
|
84
78
|
scrollEventThrottle={16}
|
|
85
|
-
viewabilityConfig={{viewAreaCoveragePercentThreshold: 50}}
|
|
79
|
+
viewabilityConfig={{ viewAreaCoveragePercentThreshold: 50 }}
|
|
86
80
|
keyExtractor={(_, index) => index.toString()}
|
|
87
81
|
/>
|
|
88
82
|
|
|
@@ -108,28 +102,24 @@ const BasicCarousel = ({
|
|
|
108
102
|
};
|
|
109
103
|
|
|
110
104
|
// Default Carousel Card: Used if no custom renderer is provided
|
|
111
|
-
const DefaultCarouselCard = ({item}: {item:
|
|
105
|
+
const DefaultCarouselCard = ({ item }: { item: any }) => {
|
|
112
106
|
return (
|
|
113
107
|
<View style={styles.defaultCard}>
|
|
114
|
-
{
|
|
115
|
-
<ImageBackground
|
|
116
|
-
source={{uri: item.image}}
|
|
117
|
-
style={styles.imageBackground}
|
|
118
|
-
/>
|
|
108
|
+
<View style={styles.imageBackground} />
|
|
119
109
|
</View>
|
|
120
110
|
);
|
|
121
111
|
};
|
|
122
112
|
|
|
123
113
|
const styles = StyleSheet.create({
|
|
124
114
|
paginationContainer: {
|
|
125
|
-
flexDirection:
|
|
126
|
-
justifyContent:
|
|
127
|
-
alignItems:
|
|
115
|
+
flexDirection: "row",
|
|
116
|
+
justifyContent: "center",
|
|
117
|
+
alignItems: "center",
|
|
128
118
|
marginTop: 15,
|
|
129
119
|
},
|
|
130
120
|
paginationDot: {
|
|
131
121
|
height: 8,
|
|
132
|
-
backgroundColor:
|
|
122
|
+
backgroundColor: "#2196F3",
|
|
133
123
|
marginHorizontal: 4,
|
|
134
124
|
borderRadius: 4,
|
|
135
125
|
},
|
|
@@ -137,18 +127,19 @@ const styles = StyleSheet.create({
|
|
|
137
127
|
width: screenWidth * 0.9,
|
|
138
128
|
height: 200,
|
|
139
129
|
marginHorizontal: screenWidth * 0.05,
|
|
140
|
-
justifyContent:
|
|
141
|
-
alignItems:
|
|
130
|
+
justifyContent: "center",
|
|
131
|
+
alignItems: "center",
|
|
142
132
|
borderRadius: 12,
|
|
143
133
|
padding: 1,
|
|
144
134
|
borderWidth: 0.5,
|
|
145
|
-
overflow:
|
|
135
|
+
overflow: "hidden",
|
|
146
136
|
},
|
|
147
137
|
imageBackground: {
|
|
148
|
-
width:
|
|
138
|
+
width: "100%",
|
|
149
139
|
borderRadius: 12,
|
|
150
|
-
overflow:
|
|
151
|
-
height:
|
|
140
|
+
overflow: "hidden",
|
|
141
|
+
height: "100%",
|
|
142
|
+
backgroundColor: "red",
|
|
152
143
|
},
|
|
153
144
|
});
|
|
154
145
|
|