Skip to content

Commit

Permalink
fix: fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Jan 16, 2022
1 parent c20d2fd commit 753051f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@
| 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 |
| 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 |
| 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) |
| goToIndex | (index: number, animated?: boolean) => void | Go to index |
| (deprecated)goToIndex | (index: number, animated?: boolean) => void | Go to index |
| getCurrentIndex | ()=>number | Get current item index |
10 changes: 5 additions & 5 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function Carousel<T>(
originalLength: data.length,
onScrollEnd: () => runOnJS(_onScrollEnd)(),
onScrollBegin: () => !!onScrollBegin && runOnJS(onScrollBegin)(),
onChange: (i) => onSnapToItem && runOnJS(onSnapToItem)(i),
onChange: (i) => !!onSnapToItem && runOnJS(onSnapToItem)(i),
duration: scrollAnimationDuration,
});

Expand All @@ -82,9 +82,9 @@ function Carousel<T>(
getCurrentIndex,
} = carouselController;

const { run, pause } = useAutoPlay({
const { start, pause } = useAutoPlay({
autoPlay,
autoPlayInterval: Math.max(autoPlayInterval, 0),
autoPlayInterval,
autoPlayReverse,
carouselController,
});
Expand All @@ -100,9 +100,9 @@ function Carousel<T>(
}, [sharedPreIndex, sharedIndex, computedIndex, onScrollEnd]);

const scrollViewGestureOnScrollEnd = React.useCallback(() => {
run();
start();
_onScrollEnd();
}, [_onScrollEnd, run]);
}, [_onScrollEnd, start]);

const goToIndex = React.useCallback(
(i: number, animated?: boolean) => {
Expand Down
28 changes: 16 additions & 12 deletions src/hooks/useAutoPlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,39 @@ export function useAutoPlay(opts: {
const timer = React.useRef<NodeJS.Timer>();
const stopped = React.useRef<boolean>(!autoPlay);

const pause = React.useCallback(() => {
timer.current && clearInterval(timer.current);
stopped.current = true;
}, []);

const run = React.useCallback(() => {
const play = React.useCallback(() => {
if (stopped.current) {
return;
}

timer.current = setTimeout(() => {
autoPlayReverse
? carouselController.prev({ onFinished: run })
: carouselController.next({ onFinished: run });
? carouselController.prev({ onFinished: play })
: carouselController.next({ onFinished: play });
}, autoPlayInterval);
}, [autoPlayReverse, autoPlayInterval, carouselController]);

const pause = React.useCallback(() => {
timer.current && clearInterval(timer.current);
stopped.current = true;
}, []);

const start = React.useCallback(() => {
stopped.current = false;
play();
}, [play]);

React.useEffect(() => {
if (autoPlay) {
stopped.current = false;
run();
start();
} else {
pause();
}
return pause;
}, [run, pause, autoPlay]);
}, [pause, start, autoPlay]);

return {
run,
pause,
start,
};
}
25 changes: 16 additions & 9 deletions src/hooks/useInitProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import React from 'react';
import { DATA_LENGTH } from '../constants';
import type { TCarouselProps } from '../types';

export type TInitializeCarouselProps<T> = TCarouselProps<T> & {
defaultIndex: Required<TCarouselProps>['defaultIndex'];
loop: Required<TCarouselProps>['loop'];
width: Required<TCarouselProps>['width'];
height: Required<TCarouselProps>['height'];
scrollAnimationDuration: Required<TCarouselProps>['scrollAnimationDuration'];
autoPlayInterval: Required<TCarouselProps>['autoPlayInterval'];
};
type TGetRequiredProps<P extends keyof TCarouselProps> = Record<
P,
Required<TCarouselProps>[P]
>;

export type TInitializeCarouselProps<T> = TCarouselProps<T> &
TGetRequiredProps<
| 'defaultIndex'
| 'loop'
| 'width'
| 'height'
| 'scrollAnimationDuration'
| 'autoPlayInterval'
>;

export function useInitProps<T>(
props: TCarouselProps<T>
Expand All @@ -18,7 +24,7 @@ export function useInitProps<T>(
defaultIndex = 0,
data: _data = [],
loop = true,
autoPlayInterval = 1000,
autoPlayInterval: _autoPlayInterval = 1000,
scrollAnimationDuration = 500,
style = {},
panGestureHandlerProps = {},
Expand All @@ -30,6 +36,7 @@ export function useInitProps<T>(

const width = Math.round(_width || 0);
const height = Math.round(_height || 0);
const autoPlayInterval = Math.max(_autoPlayInterval, 0);

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

0 comments on commit 753051f

Please sign in to comment.