Skip to content

Commit 7d1811c

Browse files
dean-added-auto-scroll-classifier (#1474)
* dean-added-auto-scroll-classifier * add typing * change the boolean isAutoScrolled arg to an object * changing isAutoScrolled to an Object Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 6e305e9 commit 7d1811c

File tree

6 files changed

+54
-61
lines changed

6 files changed

+54
-61
lines changed

demo/src/screens/componentScreens/CarouselScreen.tsx

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
33
import {StyleSheet, ScrollView} from 'react-native';
4-
import {
5-
Constants,
6-
Spacings,
7-
View,
8-
Text,
9-
Carousel,
10-
Image,
11-
Colors
12-
} from 'react-native-ui-lib';
13-
import {
14-
renderBooleanOption,
15-
renderSliderOption
16-
} from '../ExampleScreenPresenter';
4+
import {Constants, Spacings, View, Text, Carousel, Image, Colors} from 'react-native-ui-lib';
5+
import {renderBooleanOption, renderSliderOption} from '../ExampleScreenPresenter';
176

187
const INITIAL_PAGE = 2;
198
const IMAGES = [
@@ -57,7 +46,7 @@ class CarouselScreen extends Component<Props, State> {
5746
limitShownPages: false,
5847
numberOfPagesShown: 7,
5948
currentPage: INITIAL_PAGE,
60-
autoplay: false
49+
autoplay: true
6150
};
6251
}
6352

@@ -82,7 +71,7 @@ class CarouselScreen extends Component<Props, State> {
8271
return Constants.windowWidth - Spacings.s5 * 2;
8372
};
8473

85-
onChangePage = (currentPage: number) => {
74+
onChangePage = (currentPage: number, _: any) => {
8675
this.setState({currentPage});
8776
};
8877

@@ -102,23 +91,14 @@ class CarouselScreen extends Component<Props, State> {
10291
</Text>
10392

10493
<View marginH-20 marginB-20>
105-
{renderBooleanOption.call(
106-
this,
107-
'Limit number of pages shown in page control',
108-
'limitShownPages'
109-
)}
94+
{renderBooleanOption.call(this, 'Limit number of pages shown in page control', 'limitShownPages')}
11095
{renderBooleanOption.call(this, 'autoplay', 'autoplay')}
111-
{renderSliderOption.call(
112-
this,
113-
'Number of pages shown',
114-
'numberOfPagesShown',
115-
{
116-
min: 5,
117-
max: 10,
118-
step: 1,
119-
initial: 7
120-
}
121-
)}
96+
{renderSliderOption.call(this, 'Number of pages shown', 'numberOfPagesShown', {
97+
min: 5,
98+
max: 10,
99+
step: 1,
100+
initial: 7
101+
})}
122102
</View>
123103

124104
<Carousel
@@ -136,12 +116,10 @@ class CarouselScreen extends Component<Props, State> {
136116
pageControlProps={{onPagePress: this.onPagePress, limitShownPages}}
137117
// showCounter
138118
allowAccessibleLayout
119+
loop
139120
>
140121
{_.map([...Array(numberOfPagesShown)], (item, index) => (
141-
<Page
142-
style={{backgroundColor: BACKGROUND_COLORS[index]}}
143-
key={index}
144-
>
122+
<Page style={{backgroundColor: BACKGROUND_COLORS[index]}} key={index}>
145123
<Text margin-15>CARD {index}</Text>
146124
</Page>
147125
))}

generatedTypes/src/components/carousel/CarouselPresenter.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { PropsWithChildren } from 'react';
2-
import { CarouselProps, CarouselState } from './types';
1+
import {PropsWithChildren} from 'react';
2+
import {CarouselProps, CarouselState} from './types';
33
export declare function getChildrenLength(props: PropsWithChildren<CarouselProps>): number;
44
export declare function calcOffset(props: CarouselProps, state: Omit<CarouselState, 'initialOffset' | 'prevProps'>): {
55
x: number;

generatedTypes/src/components/carousel/index.d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { Component, RefObject, ReactNode, Key } from 'react';
2-
import { ScrollView, LayoutChangeEvent, NativeSyntheticEvent, NativeScrollEvent } from 'react-native';
3-
import { CarouselProps, CarouselState, PageControlPosition } from './types';
4-
export { CarouselProps, PageControlPosition };
1+
import React, {Component, RefObject, ReactNode, Key} from 'react';
2+
import {ScrollView, LayoutChangeEvent, NativeSyntheticEvent, NativeScrollEvent} from 'react-native';
3+
import {CarouselProps, CarouselState, PageControlPosition} from './types';
4+
export {CarouselProps, PageControlPosition};
55
declare type DefaultProps = Partial<CarouselProps>;
66
/**
77
* @description: Carousel for scrolling pages horizontally
@@ -19,6 +19,7 @@ declare class Carousel extends Component<CarouselProps, CarouselState> {
1919
autoplayTimer?: ReturnType<typeof setTimeout>;
2020
orientationChange?: boolean;
2121
skippedInitialScroll?: boolean;
22+
isAutoScrolled: boolean;
2223
constructor(props: CarouselProps);
2324
static getDerivedStateFromProps(nextProps: CarouselProps, prevState: CarouselState): {
2425
pageWidth: number;
@@ -48,7 +49,7 @@ declare class Carousel extends Component<CarouselProps, CarouselState> {
4849
getSnapToOffsets: () => number[] | undefined;
4950
shouldUsePageWidth(): number | false | undefined;
5051
shouldEnablePagination(): boolean | undefined;
51-
onContainerLayout: ({ nativeEvent: { layout: { width: containerWidth, height: containerHeight } } }: LayoutChangeEvent) => void;
52+
onContainerLayout: ({nativeEvent: {layout: {width: containerWidth, height: containerHeight}}}: LayoutChangeEvent) => void;
5253
shouldAllowAccessibilityLayout(): boolean | undefined;
5354
onContentSizeChange: () => void;
5455
onMomentumScrollEnd: () => void;
@@ -63,7 +64,7 @@ declare class Carousel extends Component<CarouselProps, CarouselState> {
6364
renderCarousel(): JSX.Element;
6465
render(): JSX.Element;
6566
}
66-
export { Carousel };
67+
export {Carousel};
6768
declare const _default: React.ComponentClass<CarouselProps & {
6869
useCustomTheme?: boolean | undefined;
6970
}, any> & Carousel & {

generatedTypes/src/components/carousel/types.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ScrollViewProps, StyleProp, ViewStyle, NativeSyntheticEvent, NativeScrollEvent, PointPropType, Animated } from 'react-native';
2-
import { PageControlProps } from '../pageControl';
1+
import {ScrollViewProps, StyleProp, ViewStyle, NativeSyntheticEvent, NativeScrollEvent, PointPropType, Animated} from 'react-native';
2+
import {PageControlProps} from '../pageControl';
33
export declare enum PageControlPosition {
4-
OVER = "over",
5-
UNDER = "under"
4+
OVER = 'over',
5+
UNDER = 'under'
66
}
77
export interface CarouselProps extends ScrollViewProps {
88
/**
@@ -37,7 +37,7 @@ export interface CarouselProps extends ScrollViewProps {
3737
/**
3838
* callback for when page has changed
3939
*/
40-
onChangePage?: (newPageIndex: number, oldPageIndex: number) => void;
40+
onChangePage?: (newPageIndex: number, oldPageIndex: number, info: {isAutoScrolled: boolean}) => void;
4141
/**
4242
* callback for onScroll event of the internal ScrollView
4343
*/

src/components/carousel/index.tsx

Lines changed: 25 additions & 12 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
@@ -37,13 +44,14 @@ class Carousel extends Component<CarouselProps, CarouselState> {
3744
autoplayTimer?: ReturnType<typeof setTimeout>;
3845
orientationChange?: boolean;
3946
skippedInitialScroll?: boolean;
47+
isAutoScrolled: boolean;
4048

4149
constructor(props: CarouselProps) {
4250
super(props);
4351

4452
const defaultPageWidth = props.loop || !props.pageWidth ? Constants.screenWidth : props.pageWidth;
4553
const pageHeight = props.pageHeight ?? Constants.screenHeight;
46-
54+
this.isAutoScrolled = false;
4755
this.state = {
4856
containerWidth: undefined,
4957
// @ts-ignore (defaultProps)
@@ -66,8 +74,10 @@ class Carousel extends Component<CarouselProps, CarouselState> {
6674
const {pageWidth, pageHeight} = prevProps;
6775
const {pageWidth: nextPageWidth, pageHeight: nextPageHeight} = nextProps;
6876

69-
if ((!_.isUndefined(nextPageWidth) && pageWidth !== nextPageWidth)
70-
|| (!_.isUndefined(nextPageHeight) && pageHeight !== nextPageHeight)) {
77+
if (
78+
(!_.isUndefined(nextPageWidth) && pageWidth !== nextPageWidth) ||
79+
(!_.isUndefined(nextPageHeight) && pageHeight !== nextPageHeight)
80+
) {
7181
const pageWidth = nextPageWidth as number;
7282
const pageHeight = nextPageHeight as number;
7383

@@ -167,6 +177,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
167177

168178
startAutoPlay() {
169179
this.autoplayTimer = setInterval(() => {
180+
this.isAutoScrolled = true;
170181
this.goToNextPage();
171182
}, this.props.autoplayInterval);
172183
}
@@ -218,7 +229,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
218229

219230
shouldEnablePagination() {
220231
const {pagingEnabled, horizontal} = this.props;
221-
return horizontal ? (pagingEnabled && !this.shouldUsePageWidth()) : true;
232+
return horizontal ? pagingEnabled && !this.shouldUsePageWidth() : true;
222233
}
223234

224235
onContainerLayout = ({
@@ -258,7 +269,8 @@ class Carousel extends Component<CarouselProps, CarouselState> {
258269
if (index < pagesCount) {
259270
this.setState({currentStandingPage: index});
260271
if (currentStandingPage !== index) {
261-
this.props.onChangePage?.(index, currentStandingPage);
272+
this.props.onChangePage?.(index, currentStandingPage, {isAutoScrolled: this.isAutoScrolled});
273+
this.isAutoScrolled = false;
262274
}
263275
}
264276
};
@@ -320,16 +332,17 @@ class Carousel extends Component<CarouselProps, CarouselState> {
320332
};
321333

322334
// @ts-ignore
323-
onScrollEvent = Animated.event([{nativeEvent: {contentOffset: {y: this.props?.animatedScrollOffset?.y, x: this.props?.animatedScrollOffset?.x}}}], {
324-
useNativeDriver: true,
325-
listener: this.onScroll
326-
});
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+
});
327340

328341
renderChild = (child: ReactNode, key: Key): JSX.Element | undefined => {
329342
if (child) {
330343
const {pageWidth, pageHeight} = this.state;
331344
const {horizontal} = this.props;
332-
const paddingLeft = horizontal ? this.shouldUsePageWidth() ? this.getItemSpacings(this.props) : undefined : 0;
345+
const paddingLeft = horizontal ? (this.shouldUsePageWidth() ? this.getItemSpacings(this.props) : undefined) : 0;
333346
const index = Number(key);
334347
const length = presenter.getChildrenLength(this.props);
335348
const containerMarginHorizontal = this.getContainerMarginHorizontal();

src/components/carousel/types.tsx

Lines changed: 2 additions & 1 deletion
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) => void;
43+
onChangePage?: (newPageIndex: number, oldPageIndex: number, info: {isAutoScrolled: boolean}) => void;
4444
/**
4545
* callback for onScroll event of the internal ScrollView
4646
*/
@@ -95,6 +95,7 @@ export interface CarouselProps extends ScrollViewProps {
9595
* Carousel scroll offset (pass new Animated.ValueXY())
9696
*/
9797
animatedScrollOffset?: Animated.ValueXY;
98+
9899
}
99100

100101
export interface CarouselState {

0 commit comments

Comments
 (0)