Skip to content

Commit 753051f

Browse files
committed
fix: fix comments
1 parent c20d2fd commit 753051f

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

docs/props.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@
5757
| prev | ({ count = 1, animated = false, onFinished?: () => void }) => void | Scroll to previous item, it takes one optional argument (count), which allows you to specify how many items to cross |
5858
| next | ({ count = 1, animated = false, onFinished?: () => void }) => void | Scroll to next item, it takes one optional argument (count), which allows you to specify how many items to cross |
5959
| scrollTo | ({ count = 1, animated = false, onFinished?: () => void }) => void | Use count to scroll to a position where relative to the current position, scrollTo(-2) is equivalent to prev(2), scrollTo(2) is equivalent to next(2) |
60-
| goToIndex | (index: number, animated?: boolean) => void | Go to index |
60+
| (deprecated)goToIndex | (index: number, animated?: boolean) => void | Go to index |
6161
| getCurrentIndex | ()=>number | Get current item index |

src/Carousel.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function Carousel<T>(
6969
originalLength: data.length,
7070
onScrollEnd: () => runOnJS(_onScrollEnd)(),
7171
onScrollBegin: () => !!onScrollBegin && runOnJS(onScrollBegin)(),
72-
onChange: (i) => onSnapToItem && runOnJS(onSnapToItem)(i),
72+
onChange: (i) => !!onSnapToItem && runOnJS(onSnapToItem)(i),
7373
duration: scrollAnimationDuration,
7474
});
7575

@@ -82,9 +82,9 @@ function Carousel<T>(
8282
getCurrentIndex,
8383
} = carouselController;
8484

85-
const { run, pause } = useAutoPlay({
85+
const { start, pause } = useAutoPlay({
8686
autoPlay,
87-
autoPlayInterval: Math.max(autoPlayInterval, 0),
87+
autoPlayInterval,
8888
autoPlayReverse,
8989
carouselController,
9090
});
@@ -100,9 +100,9 @@ function Carousel<T>(
100100
}, [sharedPreIndex, sharedIndex, computedIndex, onScrollEnd]);
101101

102102
const scrollViewGestureOnScrollEnd = React.useCallback(() => {
103-
run();
103+
start();
104104
_onScrollEnd();
105-
}, [_onScrollEnd, run]);
105+
}, [_onScrollEnd, start]);
106106

107107
const goToIndex = React.useCallback(
108108
(i: number, animated?: boolean) => {

src/hooks/useAutoPlay.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,39 @@ export function useAutoPlay(opts: {
1717
const timer = React.useRef<NodeJS.Timer>();
1818
const stopped = React.useRef<boolean>(!autoPlay);
1919

20-
const pause = React.useCallback(() => {
21-
timer.current && clearInterval(timer.current);
22-
stopped.current = true;
23-
}, []);
24-
25-
const run = React.useCallback(() => {
20+
const play = React.useCallback(() => {
2621
if (stopped.current) {
2722
return;
2823
}
2924

3025
timer.current = setTimeout(() => {
3126
autoPlayReverse
32-
? carouselController.prev({ onFinished: run })
33-
: carouselController.next({ onFinished: run });
27+
? carouselController.prev({ onFinished: play })
28+
: carouselController.next({ onFinished: play });
3429
}, autoPlayInterval);
3530
}, [autoPlayReverse, autoPlayInterval, carouselController]);
3631

32+
const pause = React.useCallback(() => {
33+
timer.current && clearInterval(timer.current);
34+
stopped.current = true;
35+
}, []);
36+
37+
const start = React.useCallback(() => {
38+
stopped.current = false;
39+
play();
40+
}, [play]);
41+
3742
React.useEffect(() => {
3843
if (autoPlay) {
39-
stopped.current = false;
40-
run();
44+
start();
4145
} else {
4246
pause();
4347
}
4448
return pause;
45-
}, [run, pause, autoPlay]);
49+
}, [pause, start, autoPlay]);
4650

4751
return {
48-
run,
4952
pause,
53+
start,
5054
};
5155
}

src/hooks/useInitProps.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ import React from 'react';
22
import { DATA_LENGTH } from '../constants';
33
import type { TCarouselProps } from '../types';
44

5-
export type TInitializeCarouselProps<T> = TCarouselProps<T> & {
6-
defaultIndex: Required<TCarouselProps>['defaultIndex'];
7-
loop: Required<TCarouselProps>['loop'];
8-
width: Required<TCarouselProps>['width'];
9-
height: Required<TCarouselProps>['height'];
10-
scrollAnimationDuration: Required<TCarouselProps>['scrollAnimationDuration'];
11-
autoPlayInterval: Required<TCarouselProps>['autoPlayInterval'];
12-
};
5+
type TGetRequiredProps<P extends keyof TCarouselProps> = Record<
6+
P,
7+
Required<TCarouselProps>[P]
8+
>;
9+
10+
export type TInitializeCarouselProps<T> = TCarouselProps<T> &
11+
TGetRequiredProps<
12+
| 'defaultIndex'
13+
| 'loop'
14+
| 'width'
15+
| 'height'
16+
| 'scrollAnimationDuration'
17+
| 'autoPlayInterval'
18+
>;
1319

1420
export function useInitProps<T>(
1521
props: TCarouselProps<T>
@@ -18,7 +24,7 @@ export function useInitProps<T>(
1824
defaultIndex = 0,
1925
data: _data = [],
2026
loop = true,
21-
autoPlayInterval = 1000,
27+
autoPlayInterval: _autoPlayInterval = 1000,
2228
scrollAnimationDuration = 500,
2329
style = {},
2430
panGestureHandlerProps = {},
@@ -30,6 +36,7 @@ export function useInitProps<T>(
3036

3137
const width = Math.round(_width || 0);
3238
const height = Math.round(_height || 0);
39+
const autoPlayInterval = Math.max(_autoPlayInterval, 0);
3340

3441
const data = React.useMemo<T[]>(() => {
3542
if (!loop) return _data;

0 commit comments

Comments
 (0)