Skip to content

Commit 3c17836

Browse files
committed
fix: fixed pagingEnabled prop error
1 parent 681695f commit 3c17836

File tree

5 files changed

+44
-48
lines changed

5 files changed

+44
-48
lines changed

exampleExpo/src/circular/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function Index() {
9393
}}
9494
>
9595
<SBImageItem
96+
showIndex={false}
9697
style={{
9798
position: 'absolute',
9899
width: '100%',

exampleExpo/src/components/SBImageItem.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ import {
1313
interface Props {
1414
style?: StyleProp<ViewStyle>;
1515
index?: number;
16+
showIndex?: boolean;
1617
}
1718

18-
export const SBImageItem: React.FC<Props> = ({ style, index: _index }) => {
19+
export const SBImageItem: React.FC<Props> = ({
20+
style,
21+
index: _index,
22+
showIndex = true,
23+
}) => {
1924
const index = (_index || 0) + 1;
2025
const source = React.useRef<ImageURISource>({
2126
uri: `https://picsum.photos/id/${index}/400/300`,
@@ -37,7 +42,7 @@ export const SBImageItem: React.FC<Props> = ({ style, index: _index }) => {
3742
paddingTop: 2,
3843
}}
3944
>
40-
{index}
45+
{showIndex ? index : ''}
4146
</Text>
4247
</View>
4348
);

exampleExpo/src/fold/index.tsx

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import Carousel from 'react-native-reanimated-carousel';
33
import SButton from '../components/SButton';
44
import type { TAnimationStyle } from '../../../src/layouts/BaseLayout';
55
import { ElementsText, window } from '../constants';
6-
import Animated, {
7-
Extrapolate,
8-
interpolate,
9-
useAnimatedStyle,
10-
} from 'react-native-reanimated';
6+
import Animated, { Extrapolate, interpolate } from 'react-native-reanimated';
117
import { SBImageItem } from '../components/SBImageItem';
128
import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
139
import { StyleSheet, Text, View } from 'react-native';
@@ -121,19 +117,7 @@ function Index() {
121117
const Item: React.FC<{
122118
index: number;
123119
animationValue: Animated.SharedValue<number>;
124-
}> = ({ index, animationValue }) => {
125-
const maskStyle = useAnimatedStyle(() => {
126-
// const backgroundColor = interpolateColor(
127-
// animationValue.value,
128-
// [-1, 0, 1],
129-
// ['#000000dd', 'transparent', '#000000dd']
130-
// );
131-
132-
return {
133-
// backgroundColor,
134-
};
135-
}, [animationValue]);
136-
120+
}> = ({ index }) => {
137121
return (
138122
<TouchableWithoutFeedback
139123
onPress={() => {
@@ -151,7 +135,12 @@ const Item: React.FC<{
151135
alignItems: 'center',
152136
}}
153137
>
154-
<SBImageItem key={index} style={styles.image} index={index} />
138+
<SBImageItem
139+
showIndex={false}
140+
key={index}
141+
style={styles.image}
142+
index={index}
143+
/>
155144
<Text
156145
style={{
157146
color: 'white',
@@ -163,20 +152,6 @@ const Item: React.FC<{
163152
>
164153
{faker.name.findName().slice(0, 2).toUpperCase()}
165154
</Text>
166-
167-
<Animated.View
168-
pointerEvents="none"
169-
style={[
170-
{
171-
position: 'absolute',
172-
top: 0,
173-
left: 0,
174-
right: 0,
175-
bottom: 0,
176-
},
177-
maskStyle,
178-
]}
179-
/>
180155
</View>
181156
</TouchableWithoutFeedback>
182157
);

exampleExpo/src/normal/index.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function Index() {
1212
const [isVertical, setIsVertical] = React.useState(false);
1313
const [isFast, setIsFast] = React.useState(false);
1414
const [isAutoPlay, setIsAutoPlay] = React.useState(false);
15+
const [isPagingEnabled, setIsPagingEnabled] = React.useState(true);
1516
const ref = React.useRef<ICarouselInstance>(null);
1617

1718
const baseOptions = isVertical
@@ -35,6 +36,7 @@ function Index() {
3536
autoPlay={isAutoPlay}
3637
autoPlayInterval={isFast ? 100 : 2000}
3738
data={data}
39+
pagingEnabled={isPagingEnabled}
3840
onSnapToItem={(index) => console.log('current index:', index)}
3941
renderItem={({ index }) => <SBItem key={index} index={index} />}
4042
/>
@@ -52,6 +54,13 @@ function Index() {
5254
>
5355
{isFast ? 'NORMAL' : 'FAST'}
5456
</SButton>
57+
<SButton
58+
onPress={() => {
59+
setIsPagingEnabled(!isPagingEnabled);
60+
}}
61+
>
62+
PagingEnabled:{isPagingEnabled.toString()}
63+
</SButton>
5564
<SButton
5665
onPress={() => {
5766
setIsAutoPlay(!isAutoPlay);

src/ScrollViewGesture.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,14 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
9595
const origin = translation.value;
9696
const velocity = scrollEndVelocity.value;
9797
if (!pagingEnabled) {
98+
/**
99+
* If enabled, releasing the touch will scroll to the nearest item.
100+
* valid when pagingEnabled=false
101+
*/
98102
if (snapEnabled) {
99103
const nextPage =
100104
Math.round((origin + velocity * 0.4) / size) * size;
105+
101106
translation.value = _withSpring(nextPage, onFinished);
102107
return;
103108
}
@@ -107,29 +112,30 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
107112
});
108113
return;
109114
}
110-
const page = Math.round(-translation.value / size);
111-
const velocityPage = Math.round(
112-
-(translation.value + scrollEndVelocity.value) / size
113-
);
114-
let finalPage = Math.min(
115-
page + 1,
116-
Math.max(page - 1, velocityPage)
117-
);
115+
116+
const direction =
117+
-scrollEndTranslation.value /
118+
Math.abs(scrollEndTranslation.value);
119+
const computed = direction < 0 ? Math.ceil : Math.floor;
120+
const page = computed(-translation.value / size);
121+
let finalPage = page + direction;
122+
118123
if (!infinite) {
119124
finalPage = Math.min(maxPage - 1, Math.max(0, finalPage));
120125
}
121126

122127
translation.value = _withSpring(-finalPage * size, onFinished);
123128
},
124129
[
125-
infinite,
126-
_withSpring,
127130
translation,
128-
scrollEndVelocity,
129-
size,
130-
maxPage,
131+
scrollEndVelocity.value,
131132
pagingEnabled,
133+
size,
134+
scrollEndTranslation.value,
135+
infinite,
136+
_withSpring,
132137
snapEnabled,
138+
maxPage,
133139
]
134140
);
135141

0 commit comments

Comments
 (0)