Skip to content

Commit

Permalink
sdp-tech#11 feat: add carousel module
Browse files Browse the repository at this point in the history
  • Loading branch information
SJ-Kwak committed Jan 28, 2024
1 parent 2853d27 commit 60a92fd
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 225 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = {
allowUndefined: true,
},
],
'react-native-reanimated/plugin'
],
};
9 changes: 9 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,11 @@ PODS:
- React-Core
- RNKeychain (8.1.2):
- React-Core
- RNReanimated (3.6.2):
- glog
- RCT-Folly (= 2022.05.16.00)
- React-Core
- ReactCommon/turbomodule/core
- RNScreens (3.29.0):
- glog
- RCT-Folly (= 2022.05.16.00)
Expand Down Expand Up @@ -1209,6 +1214,7 @@ DEPENDENCIES:
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
- RNGestureHandler (from `../node_modules/react-native-gesture-handler`)
- RNKeychain (from `../node_modules/react-native-keychain`)
- RNReanimated (from `../node_modules/react-native-reanimated`)
- RNScreens (from `../node_modules/react-native-screens`)
- RNSVG (from `../node_modules/react-native-svg`)
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)
Expand Down Expand Up @@ -1333,6 +1339,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native-gesture-handler"
RNKeychain:
:path: "../node_modules/react-native-keychain"
RNReanimated:
:path: "../node_modules/react-native-reanimated"
RNScreens:
:path: "../node_modules/react-native-screens"
RNSVG:
Expand Down Expand Up @@ -1404,6 +1412,7 @@ SPEC CHECKSUMS:
ReactCommon: 45b5d4f784e869c44a6f5a8fad5b114ca8f78c53
RNGestureHandler: 61bfdfc05db9b79dd61f894dcd29d3dcc6db3c02
RNKeychain: a65256b6ca6ba6976132cc4124b238a5b13b3d9c
RNReanimated: 5589be82dc26b3f94738eb7c6b1f942787532b25
RNScreens: b582cb834dc4133307562e930e8fa914b8c04ef2
RNSVG: ba3e7232f45e34b7b47e74472386cf4e1a676d0a
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
"react-native-gesture-handler": "^2.14.0",
"react-native-keychain": "^8.1.2",
"react-native-pager-view": "^6.2.3",
"react-native-reanimated": "^3.6.2",
"react-native-safe-area-context": "^4.8.2",
"react-native-screens": "^3.29.0",
"react-native-snap-carousel": "git+https://github.com/SJ-Kwak/react-native-snap-carousel.git",
"react-native-svg": "^14.1.0",
"react-native-tab-view": "^3.5.2",
"styled-components": "^6.1.8",
Expand All @@ -39,6 +41,7 @@
"@react-native/typescript-config": "0.73.1",
"@types/react": "^18.2.6",
"@types/react-native-dotenv": "^0.2.2",
"@types/react-native-snap-carousel": "^3.8.10",
"@types/react-test-renderer": "^18.0.0",
"@types/styled-components": "^5.1.34",
"@types/styled-components-react-native": "^5.2.5",
Expand Down
65 changes: 65 additions & 0 deletions src/common/Carousel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { useState } from 'react';
import { View, Dimensions } from 'react-native';
import CarouselModule from 'react-native-snap-carousel';
import Slider from './Slider';
import styled from 'styled-components/native';
import { LIGHTGRAY, PURPLE } from '../styles/GlobalColor';

interface CarouselProps {
data: any[];
renderItem: any;
dot?: boolean;
slider?: boolean;
}

const Carousel = ({ data, renderItem, dot, slider }: CarouselProps) => {
const { width } = Dimensions.get('window');
const [page, setPage] = useState<number>(0);
return (
<>
<CarouselModule
data={data}
renderItem={renderItem}
sliderWidth={width}
itemWidth={width}
onSnapToItem={(index: number) => setPage(index)}
keyExtractor={(item, index) => index.toString()}
/>
{dot ? (
<DotContainer>
{Array.from({length: data.length}, (_, i) => i).map((i) => (
<Dot key={i} focused={i === page ? true : false} />
))}
</DotContainer>
):(<></>)}
{slider ? (
<SliderContainer>
<Slider total={data.length-1} page={page} />
</SliderContainer>
):(<></>)}
</>
)
}

const Dot = styled.View<{ focused: boolean }>`
width: ${(props: { focused: boolean; }) => props.focused ? 10 : 7}px;
height: ${(props: { focused: boolean; }) => props.focused ? 10 : 7}px;
margin: 0px 6px;
border-radius: 16px;
background: ${(props: { focused: boolean; }) => props.focused ? PURPLE : LIGHTGRAY};
opacity: ${(props: { focused: boolean; }) => props.focused ? 1 : 0.5};
`

const DotContainer = styled.View`
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
`

const SliderContainer = styled.View`
display: flex;
padding: 5px 20px;
`

export default Carousel;
53 changes: 53 additions & 0 deletions src/common/Slider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { useState, useEffect, useRef } from 'react';
import { View, Animated } from 'react-native';
import styled from 'styled-components/native';
import { BLACK, BLACK2 } from '../styles/GlobalColor';

interface SliderProps {
total: number;
page: number;
}

const Slider = ({ total, page }: SliderProps) => {
const loaderValue = useRef(new Animated.Value(0)).current;

const load = (count: number) => {
Animated.timing(loaderValue, {
toValue: (count / total) * 100,
duration: 500,
useNativeDriver: false,
}).start();
};

const width = loaderValue.interpolate({
inputRange: [0, 100],
outputRange: ['10%', '100%'],
extrapolate: 'clamp'
});

useEffect(() => {
load(page)
}, [page]);

return (
<>
<SliderBar>
<Animated.View
style={{
backgroundColor: BLACK,
width,
height: 3
}}
/>
</SliderBar>
</>
)
}

const SliderBar = styled.View`
width: 100%;
height: 3px;
background: ${BLACK2};
`

export default Slider;
3 changes: 2 additions & 1 deletion src/styles/GlobalColor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export const PURPLE2 = '#CEBFFA';
export const PURPLE3 = '#E7E0FD';
export const GREEN = '#DBFC72';
export const BLACK = '#222222';
export const BLACK2 = '#929292';
export const BLACK2 = '#929292';
export const LIGHTGRAY = '#DFDFDF';
Loading

0 comments on commit 60a92fd

Please sign in to comment.