|
| 1 | +import React from "react"; |
| 2 | +import { StyleProp, ViewStyle, StyleSheet, View } from "react-native"; |
| 3 | +import DeckSwiperComponent from "react-native-deck-swiper"; |
| 4 | + |
| 5 | +export interface DeckSwiperProps { |
| 6 | + onIndexChanged?: (index: number) => void; |
| 7 | + onEndReached?: () => void; |
| 8 | + startCardIndex?: number; |
| 9 | + infiniteSwiping?: boolean; |
| 10 | + verticalEnabled?: boolean; |
| 11 | + horizontalEnabled?: boolean; |
| 12 | + visibleCardCount?: number; |
| 13 | + style?: StyleProp<ViewStyle>; |
| 14 | +} |
| 15 | + |
| 16 | +const DeckSwiper: React.FC<React.PropsWithChildren<DeckSwiperProps>> = ({ |
| 17 | + onIndexChanged, |
| 18 | + onEndReached, |
| 19 | + startCardIndex = 0, |
| 20 | + infiniteSwiping = false, |
| 21 | + verticalEnabled = true, |
| 22 | + horizontalEnabled = true, |
| 23 | + visibleCardCount = 1, |
| 24 | + style, |
| 25 | + children, |
| 26 | +}) => { |
| 27 | + const childrenArray = React.useMemo( |
| 28 | + () => React.Children.toArray(children), |
| 29 | + [children] |
| 30 | + ); |
| 31 | + |
| 32 | + // an array of indices based on children count |
| 33 | + const cardsFillerData = React.useMemo( |
| 34 | + () => Array.from(Array(childrenArray.length).keys()), |
| 35 | + [childrenArray] |
| 36 | + ); |
| 37 | + |
| 38 | + /** |
| 39 | + * By default react-native-deck-swiper positions everything with absolute position. |
| 40 | + * To overcome this, it is wrapped in a View to be able to add the component in any layout structure. |
| 41 | + * |
| 42 | + * |
| 43 | + * Since all children of that View are absolutley positioned, the View does not have a height and still looks and behaves weird. |
| 44 | + * To fix/mitage this without setting a static height, the first card is rendered in invisible state to take up space. |
| 45 | + * This effectivley makes the default height of the container be the height of the first card. |
| 46 | + */ |
| 47 | + |
| 48 | + return ( |
| 49 | + <View> |
| 50 | + <View style={styles.containerHeightFiller}> |
| 51 | + {childrenArray.length && childrenArray[0]} |
| 52 | + </View> |
| 53 | + <DeckSwiperComponent |
| 54 | + cards={cardsFillerData} |
| 55 | + renderCard={(_, i) => <>{childrenArray[i]}</>} |
| 56 | + keyExtractor={(card) => card?.toString()} |
| 57 | + containerStyle={ |
| 58 | + StyleSheet.flatten([styles.cardsContainer, style]) as |
| 59 | + | object |
| 60 | + | undefined |
| 61 | + } |
| 62 | + cardStyle={styles.card as object | undefined} |
| 63 | + onSwiped={onIndexChanged} |
| 64 | + onSwipedAll={onEndReached} |
| 65 | + cardIndex={startCardIndex} |
| 66 | + infinite={infiniteSwiping} |
| 67 | + verticalSwipe={verticalEnabled} |
| 68 | + horizontalSwipe={horizontalEnabled} |
| 69 | + showSecondCard={visibleCardCount > 1} |
| 70 | + stackSize={visibleCardCount} |
| 71 | + backgroundColor="transparent" |
| 72 | + cardVerticalMargin={0} |
| 73 | + cardHorizontalMargin={0} |
| 74 | + /> |
| 75 | + </View> |
| 76 | + ); |
| 77 | +}; |
| 78 | + |
| 79 | +const styles = StyleSheet.create({ |
| 80 | + cardsContainer: { |
| 81 | + width: "100%", |
| 82 | + }, |
| 83 | + card: { |
| 84 | + left: 0, |
| 85 | + right: 0, |
| 86 | + width: "auto", |
| 87 | + height: "auto", |
| 88 | + }, |
| 89 | + containerHeightFiller: { |
| 90 | + opacity: 0.0, |
| 91 | + }, |
| 92 | +}); |
| 93 | + |
| 94 | +export default DeckSwiper; |
0 commit comments