Skip to content

Commit

Permalink
fix: handles boundary cases for raw data
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Sep 8, 2021
1 parent e9e0c47 commit 4386bfd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
8 changes: 4 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export default function App() {
mode="parallax"
width={width}
data={[
{ color: 'red' },
{ color: 'purple' },
{ color: 'blue' },
{ color: 'yellow' },
// { color: 'red' },
// { color: 'purple' },
// { color: 'blue' },
// { color: 'yellow' },
]}
parallaxScrollingScale={0.8}
onSnapToItem={(index) => {
Expand Down
28 changes: 24 additions & 4 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,25 @@ function Carousel<T extends unknown = any>(
} = props;
const handlerOffsetX = useSharedValue<number>(0);
const data = React.useMemo<T[]>(() => {
if (!loop) return _data;

if (_data.length === 1) {
return [_data[0], _data[0], _data[0]];
}

if (_data.length === 2) {
return [_data[0], _data[1], _data[0], _data[1]];
}

return _data;
}, [_data]);
}, [_data, loop]);

const computedAnimResult = useComputedAnim(width, data.length);
const carouselController = useCarouselController({ width, handlerOffsetX });
const carouselController = useCarouselController({
width,
handlerOffsetX,
disable: !data.length,
});
useLoop({
autoPlay,
autoPlayInterval,
Expand All @@ -166,8 +174,20 @@ function Carousel<T extends unknown = any>(

useAnimatedReaction(
() => index.value,
(i) => onSnapToItem && runOnJS(onSnapToItem)(i),
[onSnapToItem]
(i) => {
if (loop) {
switch (_data.length) {
case 1:
i = 0;
break;
case 2:
i = i % 2;
break;
}
onSnapToItem && runOnJS(onSnapToItem)(i);
}
},
[onSnapToItem, loop, _data]
);

const callComputedIndex = React.useCallback(
Expand Down
9 changes: 6 additions & 3 deletions src/useCarouselController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useSharedValue } from 'react-native-reanimated';
interface IOpts {
width: number;
handlerOffsetX: Animated.SharedValue<number>;
disable?: boolean;
}

export interface ICarouselController {
Expand All @@ -15,7 +16,7 @@ export interface ICarouselController {

export function useCarouselController(opts: IOpts): ICarouselController {
const lock = useSharedValue<boolean>(false);
const { width, handlerOffsetX } = opts;
const { width, handlerOffsetX, disable = false } = opts;

const closeLock = React.useCallback(
(isFinished: boolean) => {
Expand All @@ -31,6 +32,7 @@ export function useCarouselController(opts: IOpts): ICarouselController {

const next = React.useCallback(
(callback?: (isFinished: boolean) => void) => {
if (disable) return;
if (lock.value) return;
openLock();
handlerOffsetX.value = _withTiming(
Expand All @@ -41,11 +43,12 @@ export function useCarouselController(opts: IOpts): ICarouselController {
}
);
},
[width, openLock, closeLock, lock, handlerOffsetX]
[width, openLock, closeLock, lock, handlerOffsetX, disable]
);

const prev = React.useCallback(
(callback?: (isFinished: boolean) => void) => {
if (disable) return;
if (lock.value) return;
openLock();
handlerOffsetX.value = _withTiming(
Expand All @@ -56,7 +59,7 @@ export function useCarouselController(opts: IOpts): ICarouselController {
}
);
},
[width, openLock, closeLock, lock, handlerOffsetX]
[width, openLock, closeLock, lock, handlerOffsetX, disable]
);

return {
Expand Down

0 comments on commit 4386bfd

Please sign in to comment.