Skip to content

Commit 8c4fa66

Browse files
committed
change the boolean isAutoScrolled arg to an object
1 parent 6949b61 commit 8c4fa66

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/components/carousel/index.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import _ from 'lodash';
22
import React, {Component, RefObject, ReactNode, Key} from 'react';
3-
import {Animated, ScrollView, StyleSheet, LayoutChangeEvent, NativeSyntheticEvent, NativeScrollEvent} from 'react-native';
3+
import {
4+
Animated,
5+
ScrollView,
6+
StyleSheet,
7+
LayoutChangeEvent,
8+
NativeSyntheticEvent,
9+
NativeScrollEvent
10+
} from 'react-native';
411
import {Constants} from '../../helpers';
512
import {Colors} from '../../style';
613
import {asBaseComponent} from '../../commons/new';
@@ -11,7 +18,7 @@ import * as presenter from './CarouselPresenter';
1118
import {CarouselProps, CarouselState, PageControlPosition} from './types';
1219
export {CarouselProps, PageControlPosition};
1320

14-
type DefaultProps = Partial<CarouselProps>
21+
type DefaultProps = Partial<CarouselProps>;
1522

1623
/**
1724
* @description: Carousel for scrolling pages horizontally
@@ -67,8 +74,10 @@ class Carousel extends Component<CarouselProps, CarouselState> {
6774
const {pageWidth, pageHeight} = prevProps;
6875
const {pageWidth: nextPageWidth, pageHeight: nextPageHeight} = nextProps;
6976

70-
if ((!_.isUndefined(nextPageWidth) && pageWidth !== nextPageWidth)
71-
|| (!_.isUndefined(nextPageHeight) && pageHeight !== nextPageHeight)) {
77+
if (
78+
(!_.isUndefined(nextPageWidth) && pageWidth !== nextPageWidth) ||
79+
(!_.isUndefined(nextPageHeight) && pageHeight !== nextPageHeight)
80+
) {
7281
const pageWidth = nextPageWidth as number;
7382
const pageHeight = nextPageHeight as number;
7483

@@ -171,7 +180,6 @@ class Carousel extends Component<CarouselProps, CarouselState> {
171180
this.isAutoScrolled = true;
172181
this.goToNextPage();
173182
}, this.props.autoplayInterval);
174-
175183
}
176184

177185
stopAutoPlay() {
@@ -221,7 +229,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
221229

222230
shouldEnablePagination() {
223231
const {pagingEnabled, horizontal} = this.props;
224-
return horizontal ? (pagingEnabled && !this.shouldUsePageWidth()) : true;
232+
return horizontal ? pagingEnabled && !this.shouldUsePageWidth() : true;
225233
}
226234

227235
onContainerLayout = ({
@@ -261,7 +269,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
261269
if (index < pagesCount) {
262270
this.setState({currentStandingPage: index});
263271
if (currentStandingPage !== index) {
264-
this.props.onChangePage?.(index, currentStandingPage, this.isAutoScrolled);
272+
this.props.onChangePage?.(index, currentStandingPage, {isAutoScrolled: this.isAutoScrolled});
265273
this.isAutoScrolled = false;
266274
}
267275
}
@@ -280,13 +288,12 @@ class Carousel extends Component<CarouselProps, CarouselState> {
280288
}
281289

282290
this.goToPage(nextPageIndex, true);
283-
291+
284292
// in case of a loop, after we advanced right to the cloned first page,
285293
// we return silently to the real first page
286294
if (loop && currentPage === pagesCount) {
287295
this.goToPage(0, false);
288296
}
289-
290297
}
291298

292299
onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
@@ -325,16 +332,17 @@ class Carousel extends Component<CarouselProps, CarouselState> {
325332
};
326333

327334
// @ts-ignore
328-
onScrollEvent = Animated.event([{nativeEvent: {contentOffset: {y: this.props?.animatedScrollOffset?.y, x: this.props?.animatedScrollOffset?.x}}}], {
329-
useNativeDriver: true,
330-
listener: this.onScroll
331-
});
335+
onScrollEvent = Animated.event([{nativeEvent: {contentOffset: {y: this.props?.animatedScrollOffset?.y, x: this.props?.animatedScrollOffset?.x}}}],
336+
{
337+
useNativeDriver: true,
338+
listener: this.onScroll
339+
});
332340

333341
renderChild = (child: ReactNode, key: Key): JSX.Element | undefined => {
334342
if (child) {
335343
const {pageWidth, pageHeight} = this.state;
336344
const {horizontal} = this.props;
337-
const paddingLeft = horizontal ? this.shouldUsePageWidth() ? this.getItemSpacings(this.props) : undefined : 0;
345+
const paddingLeft = horizontal ? (this.shouldUsePageWidth() ? this.getItemSpacings(this.props) : undefined) : 0;
338346
const index = Number(key);
339347
const length = presenter.getChildrenLength(this.props);
340348
const containerMarginHorizontal = this.getContainerMarginHorizontal();
@@ -430,7 +438,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
430438
const {containerStyle, children} = this.props;
431439

432440
return (
433-
<View style={containerStyle} onLayout={this.onContainerLayout}>
441+
<View style={containerStyle} onLayout={this.onContainerLayout}>
434442
<ScrollView
435443
ref={this.carousel}
436444
showsVerticalScrollIndicator={false}

src/components/carousel/types.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface CarouselProps extends ScrollViewProps {
4040
/**
4141
* callback for when page has changed
4242
*/
43-
onChangePage?: (newPageIndex: number, oldPageIndex: number, isAutoScrolled: boolean) => void;
43+
onChangePage?: (newPageIndex: number, oldPageIndex: number, info: {isAutoScrolled: boolean}) => void;
4444
/**
4545
* callback for onScroll event of the internal ScrollView
4646
*/
@@ -106,7 +106,6 @@ export interface CarouselState {
106106
pageHeight: number;
107107
initialOffset: PointPropType;
108108
prevProps: CarouselProps;
109-
// isAutoScrolled:boolean //dean
110109
}
111110

112111
// @ts-ignore

0 commit comments

Comments
 (0)