Skip to content

Commit

Permalink
fix: fixed pagingEnabled prop error
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Jun 20, 2022
1 parent 681695f commit 3c17836
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 48 deletions.
1 change: 1 addition & 0 deletions exampleExpo/src/circular/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function Index() {
}}
>
<SBImageItem
showIndex={false}
style={{
position: 'absolute',
width: '100%',
Expand Down
9 changes: 7 additions & 2 deletions exampleExpo/src/components/SBImageItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ import {
interface Props {
style?: StyleProp<ViewStyle>;
index?: number;
showIndex?: boolean;
}

export const SBImageItem: React.FC<Props> = ({ style, index: _index }) => {
export const SBImageItem: React.FC<Props> = ({
style,
index: _index,
showIndex = true,
}) => {
const index = (_index || 0) + 1;
const source = React.useRef<ImageURISource>({
uri: `https://picsum.photos/id/${index}/400/300`,
Expand All @@ -37,7 +42,7 @@ export const SBImageItem: React.FC<Props> = ({ style, index: _index }) => {
paddingTop: 2,
}}
>
{index}
{showIndex ? index : ''}
</Text>
</View>
);
Expand Down
41 changes: 8 additions & 33 deletions exampleExpo/src/fold/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import Carousel from 'react-native-reanimated-carousel';
import SButton from '../components/SButton';
import type { TAnimationStyle } from '../../../src/layouts/BaseLayout';
import { ElementsText, window } from '../constants';
import Animated, {
Extrapolate,
interpolate,
useAnimatedStyle,
} from 'react-native-reanimated';
import Animated, { Extrapolate, interpolate } from 'react-native-reanimated';
import { SBImageItem } from '../components/SBImageItem';
import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
import { StyleSheet, Text, View } from 'react-native';
Expand Down Expand Up @@ -121,19 +117,7 @@ function Index() {
const Item: React.FC<{
index: number;
animationValue: Animated.SharedValue<number>;
}> = ({ index, animationValue }) => {
const maskStyle = useAnimatedStyle(() => {
// const backgroundColor = interpolateColor(
// animationValue.value,
// [-1, 0, 1],
// ['#000000dd', 'transparent', '#000000dd']
// );

return {
// backgroundColor,
};
}, [animationValue]);

}> = ({ index }) => {
return (
<TouchableWithoutFeedback
onPress={() => {
Expand All @@ -151,7 +135,12 @@ const Item: React.FC<{
alignItems: 'center',
}}
>
<SBImageItem key={index} style={styles.image} index={index} />
<SBImageItem
showIndex={false}
key={index}
style={styles.image}
index={index}
/>
<Text
style={{
color: 'white',
Expand All @@ -163,20 +152,6 @@ const Item: React.FC<{
>
{faker.name.findName().slice(0, 2).toUpperCase()}
</Text>

<Animated.View
pointerEvents="none"
style={[
{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
},
maskStyle,
]}
/>
</View>
</TouchableWithoutFeedback>
);
Expand Down
9 changes: 9 additions & 0 deletions exampleExpo/src/normal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function Index() {
const [isVertical, setIsVertical] = React.useState(false);
const [isFast, setIsFast] = React.useState(false);
const [isAutoPlay, setIsAutoPlay] = React.useState(false);
const [isPagingEnabled, setIsPagingEnabled] = React.useState(true);
const ref = React.useRef<ICarouselInstance>(null);

const baseOptions = isVertical
Expand All @@ -35,6 +36,7 @@ function Index() {
autoPlay={isAutoPlay}
autoPlayInterval={isFast ? 100 : 2000}
data={data}
pagingEnabled={isPagingEnabled}
onSnapToItem={(index) => console.log('current index:', index)}
renderItem={({ index }) => <SBItem key={index} index={index} />}
/>
Expand All @@ -52,6 +54,13 @@ function Index() {
>
{isFast ? 'NORMAL' : 'FAST'}
</SButton>
<SButton
onPress={() => {
setIsPagingEnabled(!isPagingEnabled);

This comment has been minimized.

Copy link
@ddikodroid

ddikodroid Jun 20, 2022

wouldn't it be better to write like this?

setIsPagingEnabled( state => !state);

}}
>
PagingEnabled:{isPagingEnabled.toString()}
</SButton>
<SButton
onPress={() => {
setIsAutoPlay(!isAutoPlay);
Expand Down
32 changes: 19 additions & 13 deletions src/ScrollViewGesture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
const origin = translation.value;
const velocity = scrollEndVelocity.value;
if (!pagingEnabled) {
/**
* If enabled, releasing the touch will scroll to the nearest item.
* valid when pagingEnabled=false
*/
if (snapEnabled) {
const nextPage =
Math.round((origin + velocity * 0.4) / size) * size;

translation.value = _withSpring(nextPage, onFinished);
return;
}
Expand All @@ -107,29 +112,30 @@ const IScrollViewGesture: React.FC<Props> = (props) => {
});
return;
}
const page = Math.round(-translation.value / size);
const velocityPage = Math.round(
-(translation.value + scrollEndVelocity.value) / size
);
let finalPage = Math.min(
page + 1,
Math.max(page - 1, velocityPage)
);

const direction =
-scrollEndTranslation.value /
Math.abs(scrollEndTranslation.value);
const computed = direction < 0 ? Math.ceil : Math.floor;
const page = computed(-translation.value / size);
let finalPage = page + direction;

if (!infinite) {
finalPage = Math.min(maxPage - 1, Math.max(0, finalPage));
}

translation.value = _withSpring(-finalPage * size, onFinished);
},
[
infinite,
_withSpring,
translation,
scrollEndVelocity,
size,
maxPage,
scrollEndVelocity.value,
pagingEnabled,
size,
scrollEndTranslation.value,
infinite,
_withSpring,
snapEnabled,
maxPage,
]
);

Expand Down

0 comments on commit 3c17836

Please sign in to comment.