Skip to content

Commit c698b5c

Browse files
authored
Merge pull request #78 from dohooo/fix/issue_74
fix: onProgressChange/onSnapToItem props wrong calculation
2 parents c89afa8 + 85de2f4 commit c698b5c

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/Carousel.tsx

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useVisibleRanges } from './hooks/useVisibleRanges';
1515
import type { ICarouselInstance, TCarouselProps } from './types';
1616
import { StyleSheet, View } from 'react-native';
1717
import type { StackAnimationConfig } from './layouts/StackLayout';
18+
import { DATA_LENGTH } from './constants';
1819

1920
function Carousel<T>(
2021
props: PropsWithChildren<TCarouselProps<T>>,
@@ -78,11 +79,11 @@ function Carousel<T>(
7879
const data = React.useMemo<T[]>(() => {
7980
if (!loop) return _data;
8081

81-
if (_data.length === 1) {
82+
if (_data.length === DATA_LENGTH.SINGLE_ITEM) {
8283
return [_data[0], _data[0], _data[0]];
8384
}
8485

85-
if (_data.length === 2) {
86+
if (_data.length === DATA_LENGTH.DOUBLE_ITEM) {
8687
return [_data[0], _data[1], _data[0], _data[1]];
8788
}
8889

@@ -149,15 +150,27 @@ function Carousel<T>(
149150

150151
useAnimatedReaction(
151152
() => offsetX.value,
152-
(value) => {
153+
(_value) => {
154+
let value = _value;
155+
156+
if (_data.length === DATA_LENGTH.SINGLE_ITEM) {
157+
value = value % size;
158+
}
159+
160+
if (_data.length === DATA_LENGTH.DOUBLE_ITEM) {
161+
value = value % (size * 2);
162+
}
163+
153164
let absoluteProgress = Math.abs(value / size);
165+
154166
if (value > 0) {
155167
absoluteProgress = data.length - absoluteProgress;
156168
}
169+
157170
!!onProgressChange &&
158171
runOnJS(onProgressChange)(value, absoluteProgress);
159172
},
160-
[onProgressChange, props.children]
173+
[onProgressChange, _data]
161174
);
162175

163176
const next = React.useCallback(() => {
@@ -199,6 +212,15 @@ function Carousel<T>(
199212

200213
const renderLayout = React.useCallback(
201214
(item: T, i: number) => {
215+
let realIndex = i;
216+
if (_data.length === DATA_LENGTH.SINGLE_ITEM) {
217+
realIndex = i % 1;
218+
}
219+
220+
if (_data.length === DATA_LENGTH.DOUBLE_ITEM) {
221+
realIndex = i % 2;
222+
}
223+
202224
switch (mode) {
203225
case 'parallax':
204226
return (
@@ -215,7 +237,7 @@ function Carousel<T>(
215237
loop={loop}
216238
visibleRanges={visibleRanges}
217239
>
218-
{renderItem(item, i)}
240+
{renderItem(item, realIndex)}
219241
</ParallaxLayout>
220242
);
221243
case 'stack':
@@ -233,7 +255,7 @@ function Carousel<T>(
233255
loop={loop}
234256
visibleRanges={visibleRanges}
235257
>
236-
{renderItem(item, i)}
258+
{renderItem(item, realIndex)}
237259
</StackLayout>
238260
);
239261
case 'default':
@@ -250,25 +272,26 @@ function Carousel<T>(
250272
loop={loop}
251273
visibleRanges={visibleRanges}
252274
>
253-
{renderItem(item, i)}
275+
{renderItem(item, realIndex)}
254276
</NormalLayout>
255277
);
256278
}
257279
},
258280
[
259281
loop,
260-
mode,
261282
data,
283+
mode,
284+
width,
285+
_data,
286+
height,
262287
offsetX,
263-
parallaxScrollingOffset,
264-
parallaxScrollingScale,
288+
vertical,
289+
showLength,
265290
renderItem,
266291
visibleRanges,
267-
vertical,
268292
animationConfig,
269-
width,
270-
height,
271-
showLength,
293+
parallaxScrollingScale,
294+
parallaxScrollingOffset,
272295
]
273296
);
274297

src/constants/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum DATA_LENGTH {
2+
SINGLE_ITEM = 1,
3+
DOUBLE_ITEM = 2,
4+
}

0 commit comments

Comments
 (0)