Skip to content

Commit 42c389b

Browse files
authored
Merge pull request #113 from dohooo/fix/rawDataRawCalculationError
fix: onProgressChange & onSnapToItem bug when only 2 image
2 parents 9b44591 + bdb2d74 commit 42c389b

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/Carousel.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function Carousel<T>(
2525

2626
const {
2727
data,
28+
rawData,
2829
loop,
2930
mode,
3031
style,
@@ -58,7 +59,7 @@ function Carousel<T>(
5859
}, [loop, size, data]);
5960

6061
usePropsErrorBoundary(props);
61-
useOnProgressChange({ size, offsetX, data, onProgressChange });
62+
useOnProgressChange({ size, offsetX, rawData, onProgressChange });
6263

6364
const carouselController = useCarouselController({
6465
loop,
@@ -135,11 +136,11 @@ function Carousel<T>(
135136
const renderLayout = React.useCallback(
136137
(item: T, i: number) => {
137138
let realIndex = i;
138-
if (data.length === DATA_LENGTH.SINGLE_ITEM) {
139+
if (rawData.length === DATA_LENGTH.SINGLE_ITEM) {
139140
realIndex = i % 1;
140141
}
141142

142-
if (data.length === DATA_LENGTH.DOUBLE_ITEM) {
143+
if (rawData.length === DATA_LENGTH.DOUBLE_ITEM) {
143144
realIndex = i % 2;
144145
}
145146

@@ -162,7 +163,7 @@ function Carousel<T>(
162163
);
163164
},
164165
[
165-
data,
166+
rawData,
166167
offsetX,
167168
visibleRanges,
168169
renderItem,

src/hooks/useInitProps.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ export type TInitializeCarouselProps<T> = TCarouselProps<T> &
1515
| 'height'
1616
| 'scrollAnimationDuration'
1717
| 'autoPlayInterval'
18-
>;
18+
> & {
19+
// Raw data that has not been processed
20+
rawData: T[];
21+
};
1922

2023
export function useInitProps<T>(
2124
props: TCarouselProps<T>
2225
): TInitializeCarouselProps<T> {
2326
const {
2427
defaultIndex = 0,
25-
data: _data = [],
28+
data: rawData = [],
2629
loop = true,
2730
autoPlayInterval: _autoPlayInterval = 1000,
2831
scrollAnimationDuration = 500,
@@ -39,18 +42,18 @@ export function useInitProps<T>(
3942
const autoPlayInterval = Math.max(_autoPlayInterval, 0);
4043

4144
const data = React.useMemo<T[]>(() => {
42-
if (!loop) return _data;
45+
if (!loop) return rawData;
4346

44-
if (_data.length === DATA_LENGTH.SINGLE_ITEM) {
45-
return [_data[0], _data[0], _data[0]];
47+
if (rawData.length === DATA_LENGTH.SINGLE_ITEM) {
48+
return [rawData[0], rawData[0], rawData[0]];
4649
}
4750

48-
if (_data.length === DATA_LENGTH.DOUBLE_ITEM) {
49-
return [_data[0], _data[1], _data[0], _data[1]];
51+
if (rawData.length === DATA_LENGTH.DOUBLE_ITEM) {
52+
return [rawData[0], rawData[1], rawData[0], rawData[1]];
5053
}
5154

52-
return _data;
53-
}, [_data, loop]);
55+
return rawData;
56+
}, [rawData, loop]);
5457

5558
if (props.mode === 'vertical-stack' || props.mode === 'horizontal-stack') {
5659
if (!props.modeConfig) {
@@ -63,6 +66,7 @@ export function useInitProps<T>(
6366
...props,
6467
defaultIndex,
6568
data,
69+
rawData,
6670
loop,
6771
autoPlayInterval,
6872
scrollAnimationDuration,

src/hooks/useOnProgressChange.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ export function useOnProgressChange(
99
opts: {
1010
size: number;
1111
offsetX: Animated.SharedValue<number>;
12-
} & Pick<TCarouselProps, 'data' | 'onProgressChange'>
12+
rawData: TCarouselProps['data'];
13+
} & Pick<TCarouselProps, 'onProgressChange'>
1314
) {
14-
const { offsetX, data, size, onProgressChange } = opts;
15+
const { offsetX, rawData, size, onProgressChange } = opts;
1516
useAnimatedReaction(
1617
() => offsetX.value,
1718
(_value) => {
1819
let value = _value;
1920

20-
if (data.length === DATA_LENGTH.SINGLE_ITEM) {
21+
if (rawData.length === DATA_LENGTH.SINGLE_ITEM) {
2122
value = value % size;
2223
}
2324

24-
if (data.length === DATA_LENGTH.DOUBLE_ITEM) {
25+
if (rawData.length === DATA_LENGTH.DOUBLE_ITEM) {
2526
value = value % (size * 2);
2627
}
2728

2829
let absoluteProgress = Math.abs(value / size);
2930

3031
if (value > 0) {
31-
absoluteProgress = data.length - absoluteProgress;
32+
absoluteProgress = rawData.length - absoluteProgress;
3233
}
3334

3435
!!onProgressChange &&
3536
runOnJS(onProgressChange)(value, absoluteProgress);
3637
},
37-
[onProgressChange, data]
38+
[onProgressChange, rawData]
3839
);
3940
}

0 commit comments

Comments
 (0)