react-native-sg-basic-carousel 1.0.1 → 1.0.4
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 +23 -33
- package/package.json +4 -5
package/index.tsx
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable react-native/no-inline-styles */
|
|
2
|
-
import
|
|
2
|
+
import { useState, useRef, useEffect, JSX } from "react";
|
|
3
3
|
import {
|
|
4
4
|
View,
|
|
5
5
|
FlatList,
|
|
@@ -7,18 +7,11 @@ import {
|
|
|
7
7
|
StyleSheet,
|
|
8
8
|
ViewStyle,
|
|
9
9
|
StyleProp,
|
|
10
|
-
|
|
11
|
-
} from 'react-native';
|
|
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
|
-
}
|
|
10
|
+
} from "react-native";
|
|
18
11
|
|
|
19
12
|
interface CarouselProps {
|
|
20
|
-
data:
|
|
21
|
-
renderItem?: (item:
|
|
13
|
+
data: any[]; // Array of items with flexible structure but image is mandatory
|
|
14
|
+
renderItem?: (item: any) => JSX.Element; // Custom renderer
|
|
22
15
|
autoScroll?: boolean;
|
|
23
16
|
scrollInterval?: number;
|
|
24
17
|
showPagination?: boolean;
|
|
@@ -27,7 +20,7 @@ interface CarouselProps {
|
|
|
27
20
|
paginationDotStyle?: StyleProp<ViewStyle>;
|
|
28
21
|
}
|
|
29
22
|
|
|
30
|
-
const {width: screenWidth} = Dimensions.get(
|
|
23
|
+
const { width: screenWidth } = Dimensions.get("window");
|
|
31
24
|
|
|
32
25
|
const BasicCarousel = ({
|
|
33
26
|
data,
|
|
@@ -40,14 +33,14 @@ const BasicCarousel = ({
|
|
|
40
33
|
paginationDotStyle,
|
|
41
34
|
}: CarouselProps) => {
|
|
42
35
|
const [activeIndex, setActiveIndex] = useState(0);
|
|
43
|
-
const flatListRef = useRef<FlatList<
|
|
36
|
+
const flatListRef = useRef<FlatList<any>>(null);
|
|
44
37
|
|
|
45
38
|
useEffect(() => {
|
|
46
39
|
if (autoScroll) {
|
|
47
40
|
const timerId = setInterval(() => {
|
|
48
41
|
if (activeIndex === data.length - 1) {
|
|
49
42
|
setActiveIndex(0);
|
|
50
|
-
flatListRef.current?.scrollToIndex({index: 0, animated: true});
|
|
43
|
+
flatListRef.current?.scrollToIndex({ index: 0, animated: true });
|
|
51
44
|
} else {
|
|
52
45
|
setActiveIndex(activeIndex + 1);
|
|
53
46
|
flatListRef.current?.scrollToIndex({
|
|
@@ -62,7 +55,7 @@ const BasicCarousel = ({
|
|
|
62
55
|
}, [activeIndex, autoScroll, scrollInterval, data.length]);
|
|
63
56
|
|
|
64
57
|
const onViewableItemsChanged = useRef(
|
|
65
|
-
({viewableItems}: {viewableItems: any}) => {
|
|
58
|
+
({ viewableItems }: { viewableItems: any }) => {
|
|
66
59
|
if (viewableItems.length > 0) {
|
|
67
60
|
setActiveIndex(viewableItems[0].index);
|
|
68
61
|
}
|
|
@@ -74,7 +67,7 @@ const BasicCarousel = ({
|
|
|
74
67
|
<FlatList
|
|
75
68
|
ref={flatListRef}
|
|
76
69
|
data={data}
|
|
77
|
-
renderItem={({item}) =>
|
|
70
|
+
renderItem={({ item }) =>
|
|
78
71
|
renderItem ? renderItem(item) : <DefaultCarouselCard item={item} />
|
|
79
72
|
}
|
|
80
73
|
horizontal
|
|
@@ -82,7 +75,7 @@ const BasicCarousel = ({
|
|
|
82
75
|
showsHorizontalScrollIndicator={false}
|
|
83
76
|
onViewableItemsChanged={onViewableItemsChanged}
|
|
84
77
|
scrollEventThrottle={16}
|
|
85
|
-
viewabilityConfig={{viewAreaCoveragePercentThreshold: 50}}
|
|
78
|
+
viewabilityConfig={{ viewAreaCoveragePercentThreshold: 50 }}
|
|
86
79
|
keyExtractor={(_, index) => index.toString()}
|
|
87
80
|
/>
|
|
88
81
|
|
|
@@ -108,28 +101,24 @@ const BasicCarousel = ({
|
|
|
108
101
|
};
|
|
109
102
|
|
|
110
103
|
// Default Carousel Card: Used if no custom renderer is provided
|
|
111
|
-
const DefaultCarouselCard = ({item}: {item:
|
|
104
|
+
const DefaultCarouselCard = ({ item }: { item: any }) => {
|
|
112
105
|
return (
|
|
113
106
|
<View style={styles.defaultCard}>
|
|
114
|
-
{
|
|
115
|
-
<ImageBackground
|
|
116
|
-
source={{uri: item.image}}
|
|
117
|
-
style={styles.imageBackground}
|
|
118
|
-
/>
|
|
107
|
+
<View style={styles.imageBackground} />
|
|
119
108
|
</View>
|
|
120
109
|
);
|
|
121
110
|
};
|
|
122
111
|
|
|
123
112
|
const styles = StyleSheet.create({
|
|
124
113
|
paginationContainer: {
|
|
125
|
-
flexDirection:
|
|
126
|
-
justifyContent:
|
|
127
|
-
alignItems:
|
|
114
|
+
flexDirection: "row",
|
|
115
|
+
justifyContent: "center",
|
|
116
|
+
alignItems: "center",
|
|
128
117
|
marginTop: 15,
|
|
129
118
|
},
|
|
130
119
|
paginationDot: {
|
|
131
120
|
height: 8,
|
|
132
|
-
backgroundColor:
|
|
121
|
+
backgroundColor: "#2196F3",
|
|
133
122
|
marginHorizontal: 4,
|
|
134
123
|
borderRadius: 4,
|
|
135
124
|
},
|
|
@@ -137,18 +126,19 @@ const styles = StyleSheet.create({
|
|
|
137
126
|
width: screenWidth * 0.9,
|
|
138
127
|
height: 200,
|
|
139
128
|
marginHorizontal: screenWidth * 0.05,
|
|
140
|
-
justifyContent:
|
|
141
|
-
alignItems:
|
|
129
|
+
justifyContent: "center",
|
|
130
|
+
alignItems: "center",
|
|
142
131
|
borderRadius: 12,
|
|
143
132
|
padding: 1,
|
|
144
133
|
borderWidth: 0.5,
|
|
145
|
-
overflow:
|
|
134
|
+
overflow: "hidden",
|
|
146
135
|
},
|
|
147
136
|
imageBackground: {
|
|
148
|
-
width:
|
|
137
|
+
width: "100%",
|
|
149
138
|
borderRadius: 12,
|
|
150
|
-
overflow:
|
|
151
|
-
height:
|
|
139
|
+
overflow: "hidden",
|
|
140
|
+
height: "100%",
|
|
141
|
+
backgroundColor: "red",
|
|
152
142
|
},
|
|
153
143
|
});
|
|
154
144
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-sg-basic-carousel",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "An npm package to display basic carousel on react native applications",
|
|
5
5
|
"main": "index.tsx",
|
|
6
6
|
"scripts": {
|
|
@@ -24,9 +24,8 @@
|
|
|
24
24
|
},
|
|
25
25
|
"homepage": "https://github.com/siddhant124/react-native-sg-basic-carousel#readme",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@types/react": "^
|
|
28
|
-
"
|
|
29
|
-
"react": "
|
|
30
|
-
"react-native": "^0.75.4"
|
|
27
|
+
"@types/react": "^19.2.0",
|
|
28
|
+
"react": "19.2.0",
|
|
29
|
+
"react-native": "0.83.0"
|
|
31
30
|
}
|
|
32
31
|
}
|