react-native-sg-basic-carousel 1.0.5 → 1.0.6
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/package.json +5 -18
- package/index.tsx +0 -158
package/package.json
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-sg-basic-carousel",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"repository": {
|
|
9
|
-
"type": "git",
|
|
10
|
-
"url": "git+https://github.com/siddhant124/react-native-sg-basic-carousel.git"
|
|
11
|
-
},
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/siddhant124/react-native-sg-basic-carousel/issues"
|
|
14
|
-
},
|
|
15
|
-
"homepage": "https://github.com/siddhant124/react-native-sg-basic-carousel#readme",
|
|
16
|
-
"keywords": [
|
|
17
|
-
"react-native",
|
|
18
|
-
"carousel",
|
|
19
|
-
"flatlist",
|
|
20
|
-
"mobile"
|
|
3
|
+
"version": "1.0.6",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
21
8
|
],
|
|
22
9
|
"peerDependencies": {
|
|
23
10
|
"react": ">=17.0.0",
|
package/index.tsx
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
/* eslint-disable react-native/no-inline-styles */
|
|
2
|
-
import React, { useState, useRef, useEffect, JSX } from "react";
|
|
3
|
-
import {
|
|
4
|
-
View,
|
|
5
|
-
FlatList,
|
|
6
|
-
Dimensions,
|
|
7
|
-
StyleSheet,
|
|
8
|
-
ViewStyle,
|
|
9
|
-
StyleProp,
|
|
10
|
-
ImageBackground,
|
|
11
|
-
} from "react-native";
|
|
12
|
-
|
|
13
|
-
interface CarouselProps {
|
|
14
|
-
data: any[]; // Array of items with flexible structure but image is mandatory
|
|
15
|
-
renderItem?: (item: any) => JSX.Element; // Custom renderer
|
|
16
|
-
autoScroll?: boolean;
|
|
17
|
-
scrollInterval?: number;
|
|
18
|
-
showPagination?: boolean;
|
|
19
|
-
containerStyle?: StyleProp<ViewStyle>;
|
|
20
|
-
paginationContainerStyle?: StyleProp<ViewStyle>;
|
|
21
|
-
paginationDotStyle?: StyleProp<ViewStyle>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const { width: screenWidth } = Dimensions.get("window");
|
|
25
|
-
|
|
26
|
-
const BasicCarousel = ({
|
|
27
|
-
data,
|
|
28
|
-
renderItem,
|
|
29
|
-
autoScroll = true,
|
|
30
|
-
scrollInterval = 3000,
|
|
31
|
-
showPagination = true,
|
|
32
|
-
containerStyle,
|
|
33
|
-
paginationContainerStyle,
|
|
34
|
-
paginationDotStyle,
|
|
35
|
-
}: CarouselProps) => {
|
|
36
|
-
const [activeIndex, setActiveIndex] = useState(0);
|
|
37
|
-
const flatListRef = useRef<FlatList<any>>(null);
|
|
38
|
-
|
|
39
|
-
useEffect(() => {
|
|
40
|
-
if (autoScroll) {
|
|
41
|
-
const timerId = setInterval(() => {
|
|
42
|
-
if (activeIndex === data.length - 1) {
|
|
43
|
-
setActiveIndex(0);
|
|
44
|
-
flatListRef.current?.scrollToIndex({ index: 0, animated: true });
|
|
45
|
-
} else {
|
|
46
|
-
setActiveIndex(activeIndex + 1);
|
|
47
|
-
flatListRef.current?.scrollToIndex({
|
|
48
|
-
index: activeIndex + 1,
|
|
49
|
-
animated: true,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
}, scrollInterval);
|
|
53
|
-
|
|
54
|
-
return () => clearInterval(timerId);
|
|
55
|
-
}
|
|
56
|
-
}, [activeIndex, autoScroll, scrollInterval, data.length]);
|
|
57
|
-
|
|
58
|
-
const onViewableItemsChanged = useRef(
|
|
59
|
-
({ viewableItems }: { viewableItems: any }) => {
|
|
60
|
-
if (viewableItems.length > 0) {
|
|
61
|
-
setActiveIndex(viewableItems[0].index);
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
).current;
|
|
65
|
-
|
|
66
|
-
if (!data || data.length === 0) {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return (
|
|
71
|
-
<View style={containerStyle}>
|
|
72
|
-
<FlatList
|
|
73
|
-
ref={flatListRef}
|
|
74
|
-
data={data}
|
|
75
|
-
renderItem={({ item }) =>
|
|
76
|
-
renderItem ? renderItem(item) : <DefaultCarouselCard item={item} />
|
|
77
|
-
}
|
|
78
|
-
horizontal
|
|
79
|
-
pagingEnabled
|
|
80
|
-
showsHorizontalScrollIndicator={false}
|
|
81
|
-
onViewableItemsChanged={onViewableItemsChanged}
|
|
82
|
-
scrollEventThrottle={16}
|
|
83
|
-
viewabilityConfig={{ viewAreaCoveragePercentThreshold: 50 }}
|
|
84
|
-
keyExtractor={(_, index) => index.toString()}
|
|
85
|
-
/>
|
|
86
|
-
|
|
87
|
-
{showPagination && (
|
|
88
|
-
<View style={[styles.paginationContainer, paginationContainerStyle]}>
|
|
89
|
-
{data.map((_, index) => (
|
|
90
|
-
<View
|
|
91
|
-
key={index}
|
|
92
|
-
style={[
|
|
93
|
-
styles.paginationDot,
|
|
94
|
-
paginationDotStyle,
|
|
95
|
-
{
|
|
96
|
-
width: index === activeIndex ? 20 : 8,
|
|
97
|
-
opacity: index === activeIndex ? 1 : 0.5,
|
|
98
|
-
},
|
|
99
|
-
]}
|
|
100
|
-
/>
|
|
101
|
-
))}
|
|
102
|
-
</View>
|
|
103
|
-
)}
|
|
104
|
-
</View>
|
|
105
|
-
);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// Default Carousel Card: Used if no custom renderer is provided
|
|
109
|
-
const DefaultCarouselCard = ({ item }: { item: any }) => {
|
|
110
|
-
const imageUri =
|
|
111
|
-
typeof item === "object" && item?.image
|
|
112
|
-
? item.image
|
|
113
|
-
: "https://www.sciencealert.com/images/2026/01/ai_psychosis_header-3.jpg";
|
|
114
|
-
|
|
115
|
-
return (
|
|
116
|
-
<View style={styles.defaultCard}>
|
|
117
|
-
<ImageBackground
|
|
118
|
-
source={{ uri: imageUri }}
|
|
119
|
-
style={styles.imageBackground}
|
|
120
|
-
resizeMode="cover"
|
|
121
|
-
/>
|
|
122
|
-
</View>
|
|
123
|
-
);
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
const styles = StyleSheet.create({
|
|
127
|
-
paginationContainer: {
|
|
128
|
-
flexDirection: "row",
|
|
129
|
-
justifyContent: "center",
|
|
130
|
-
alignItems: "center",
|
|
131
|
-
marginTop: 15,
|
|
132
|
-
},
|
|
133
|
-
paginationDot: {
|
|
134
|
-
height: 8,
|
|
135
|
-
backgroundColor: "#2196F3",
|
|
136
|
-
marginHorizontal: 4,
|
|
137
|
-
borderRadius: 4,
|
|
138
|
-
},
|
|
139
|
-
defaultCard: {
|
|
140
|
-
width: screenWidth * 0.9,
|
|
141
|
-
height: 200,
|
|
142
|
-
marginHorizontal: screenWidth * 0.05,
|
|
143
|
-
justifyContent: "center",
|
|
144
|
-
alignItems: "center",
|
|
145
|
-
borderRadius: 12,
|
|
146
|
-
padding: 1,
|
|
147
|
-
borderWidth: 0.5,
|
|
148
|
-
overflow: "hidden",
|
|
149
|
-
},
|
|
150
|
-
imageBackground: {
|
|
151
|
-
width: "100%",
|
|
152
|
-
borderRadius: 12,
|
|
153
|
-
overflow: "hidden",
|
|
154
|
-
height: "100%",
|
|
155
|
-
},
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
export default BasicCarousel;
|