Skip to content

Commit 4691d8a

Browse files
committed
chore: some changes with review
1 parent 7645b75 commit 4691d8a

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

example/src/App.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
/* eslint-disable react-native/no-inline-styles */
22
import * as React from 'react';
3-
import {
4-
Button,
5-
Dimensions,
6-
Image,
7-
ImageSourcePropType,
8-
View,
9-
} from 'react-native';
3+
import { Button, Image, ImageSourcePropType, View } from 'react-native';
104
import Carousel from '../../src/index';
115
import type { ICarouselInstance } from '../../src/types';
126
import Animated, {
@@ -16,9 +10,6 @@ import Animated, {
1610
useSharedValue,
1711
} from 'react-native-reanimated';
1812

19-
const window = Dimensions.get('window');
20-
const PAGE_WIDTH = window.width;
21-
2213
const data: ImageSourcePropType[] = [
2314
require('../assets/carousel-0.jpg'),
2415
require('../assets/carousel-1.jpg'),
@@ -42,7 +33,8 @@ export default function App() {
4233
style={{ height: 240 }}
4334
defaultIndex={0}
4435
ref={r}
45-
width={PAGE_WIDTH}
36+
vertical
37+
height={240}
4638
parallaxScrollingScale={0.8}
4739
data={data}
4840
renderItem={(source, index) => {
@@ -83,8 +75,9 @@ export default function App() {
8375
onProgressChange={(_, absoluteProgress) => {
8476
progressValue.value = absoluteProgress;
8577
}}
78+
vertical
8679
mode="parallax"
87-
width={window.width}
80+
height={240}
8881
parallaxScrollingScale={0.8}
8982
data={data}
9083
renderItem={(source, i) => {

src/Carousel.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import { useIndexController } from './hooks/useIndexController';
1313
import { usePropsErrorBoundary } from './hooks/usePropsErrorBoundary';
1414
import { ScrollViewGesture } from './ScrollViewGesture';
1515
import { useVisibleRanges } from './hooks/useVisibleRanges';
16-
import type { ICarouselInstance, ICarouselProps } from './types';
16+
import type { ICarouselInstance, TCarouselProps } from './types';
1717
import { StyleSheet, View } from 'react-native';
1818

1919
function Carousel<T>(
20-
props: PropsWithChildren<ICarouselProps<T>>,
20+
props: PropsWithChildren<TCarouselProps<T>>,
2121
ref: React.Ref<ICarouselInstance>
2222
) {
2323
const {
@@ -41,21 +41,19 @@ function Carousel<T>(
4141

4242
usePropsErrorBoundary({
4343
...props,
44-
viewCount: _data.length,
44+
data: _data,
45+
mode,
46+
loop,
47+
style,
48+
defaultIndex,
49+
autoPlayInterval,
50+
panGestureHandlerProps,
4551
});
4652

47-
const width = React.useMemo(
48-
() => Math.round(props.width || 0),
49-
[props.width]
50-
);
51-
const height = React.useMemo(
52-
() => Math.round(props.height || 0),
53-
[props.height]
54-
);
55-
const size = React.useMemo(
56-
() => (vertical ? height : width),
57-
[width, height, vertical]
58-
);
53+
const width: number = props.vertical ? 0 : Math.round(props.width || 0);
54+
const height: number = props.vertical ? Math.round(props.height || 0) : 0;
55+
const size: number = vertical ? height : width;
56+
5957
const layoutStyle = React.useMemo(() => {
6058
return {
6159
width: !vertical ? width : '100%',
@@ -126,7 +124,7 @@ function Carousel<T>(
126124
return handlerOffsetX.value;
127125
}
128126
return isNaN(x) ? 0 : x;
129-
}, [loop, width, data]);
127+
}, [loop, size, data]);
130128

131129
useAnimatedReaction(
132130
() => offsetX.value,

src/hooks/usePropsErrorBoundary.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import React from 'react';
2-
import type { ICarouselProps } from 'src/types';
2+
import type { TCarouselProps } from 'src/types';
33

4-
export function usePropsErrorBoundary(
5-
props: ICarouselProps & { viewCount: number }
6-
) {
4+
export function usePropsErrorBoundary(props: TCarouselProps) {
75
React.useEffect(() => {
8-
const { defaultIndex, viewCount, vertical, height, width } = props;
6+
const { defaultIndex, data } = props;
97

8+
const viewCount = data.length;
109
if (typeof defaultIndex === 'number' && viewCount > 0) {
1110
if (defaultIndex < 0 || defaultIndex >= viewCount) {
1211
throw Error(
1312
'DefaultIndex must be in the range of data length.'
1413
);
1514
}
1615
}
17-
if (!vertical && !width) {
16+
if (!props.vertical && !props.width) {
1817
throw Error('`width` must be specified for vertical carousels.');
1918
}
20-
if (vertical && !height) {
19+
if (props.vertical && !props.height) {
2120
throw Error('`height` must be specified for vertical carousels.');
2221
}
2322
}, [props]);

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type { ICarouselProps, ICarouselInstance } from './types';
1+
export type { TCarouselProps, ICarouselInstance } from './types';
22
import Carousel from './Carousel';
33

44
export default Carousel;

src/types.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,30 @@ import type { ViewStyle } from 'react-native';
22
import type { PanGestureHandlerProps } from 'react-native-gesture-handler';
33
import type { TMode } from './layouts';
44

5-
export interface ICarouselProps<T = any> {
5+
interface IHorizontalModeProps {
6+
/**
7+
* Layout items vertically instead of horizontally
8+
*/
9+
vertical?: false;
10+
/**
11+
* Specified carousel container width.
12+
*/
13+
width: number;
14+
}
15+
16+
interface IVerticalModeProps {
17+
/**
18+
* Layout items vertically instead of horizontally
19+
*/
20+
vertical: true;
21+
/**
22+
* Specified carousel container height.
23+
* @default '100%'
24+
*/
25+
height: number;
26+
}
27+
28+
export type TCarouselProps<T = any> = {
629
ref?: React.Ref<ICarouselInstance>;
730
/**
831
* Carousel loop playback.
@@ -14,15 +37,6 @@ export interface ICarouselProps<T = any> {
1437
* @default 'default'
1538
*/
1639
mode?: TMode;
17-
/**
18-
* Specified carousel container width.
19-
*/
20-
width?: number;
21-
/**
22-
* Specified carousel container height.
23-
* @default '100%'
24-
*/
25-
height?: number;
2640
/**
2741
* Carousel items data set.
2842
*/
@@ -74,10 +88,6 @@ export interface ICarouselProps<T = any> {
7488
* @default 0 all items will respond to pan gesture events.
7589
*/
7690
windowSize?: number;
77-
/**
78-
* Layout items vertically instead of horizontally
79-
*/
80-
vertical?: boolean;
8191
/**
8292
* Render carousel item.
8393
*/
@@ -103,7 +113,7 @@ export interface ICarouselProps<T = any> {
103113
offsetProgress: number,
104114
absoluteProgress: number
105115
) => void;
106-
}
116+
} & (IHorizontalModeProps | IVerticalModeProps);
107117

108118
export interface ICarouselInstance {
109119
/**

0 commit comments

Comments
 (0)