Skip to content

Commit 95d4848

Browse files
committed
Revert "Fix/carousel android rtl jumps (#2831)"
This reverts commit b997db4.
1 parent f3e00eb commit 95d4848

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

src/components/carousel/index.tsx

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
5252
const defaultPageWidth = props.loop || !props.pageWidth ? Constants.screenWidth : props.pageWidth;
5353
const pageHeight = props.pageHeight ?? Constants.screenHeight;
5454
this.isAutoScrolled = false;
55-
55+
5656
this.state = {
5757
containerWidth: undefined,
5858
// @ts-ignore (defaultProps)
@@ -195,30 +195,28 @@ class Carousel extends Component<CarouselProps, CarouselState> {
195195
}
196196

197197
goToPage(pageIndex: number, animated = true) {
198-
this.setState({currentPage: pageIndex}, () => this.updateOffset(animated));
198+
this.setState({currentPage: this.getCalcIndex(pageIndex)}, () => this.updateOffset(animated));
199199
}
200200

201201
goToNextPage() {
202202
const {currentPage} = this.state;
203203
const pagesCount = presenter.getChildrenLength(this.props);
204204
const {loop} = this.props;
205205
let nextPageIndex;
206+
206207
if (loop) {
207-
if (currentPage === pagesCount + 1) {
208-
this.goToPage(0, false);
209-
return;
210-
}
211208
nextPageIndex = currentPage + 1;
212209
} else {
213210
nextPageIndex = Math.min(pagesCount - 1, currentPage + 1);
214211
}
215212

216213
this.goToPage(nextPageIndex, true);
217-
// // in case of a loop, after we advanced right to the cloned first page,
218-
// // we return silently to the real first page
219-
// if (loop && currentPage === pagesCount) {
220-
// this.goToPage(0, false);
221-
// }
214+
215+
// in case of a loop, after we advanced right to the cloned first page,
216+
// we return silently to the real first page
217+
if (loop && currentPage === pagesCount) {
218+
this.goToPage(0, false);
219+
}
222220
}
223221

224222
getCalcIndex(index: number): number {
@@ -241,8 +239,10 @@ class Carousel extends Component<CarouselProps, CarouselState> {
241239
if (containerWidth) {
242240
const spacings = pageWidth === containerWidth ? 0 : this.getItemSpacings(this.props);
243241
const initialBreak = pageWidth - (containerWidth - pageWidth - spacings) / 2;
244-
const snapToOffsets = _.times(presenter.getChildrenLength(this.props),
245-
index => initialBreak + index * pageWidth + this.getContainerMarginHorizontal());
242+
const snapToOffsets = _.times(
243+
presenter.getChildrenLength(this.props),
244+
index => initialBreak + index * pageWidth + this.getContainerMarginHorizontal()
245+
);
246246
return snapToOffsets;
247247
}
248248
};
@@ -307,13 +307,14 @@ class Carousel extends Component<CarouselProps, CarouselState> {
307307
onMomentumScrollEnd = () => {
308308
// finished full page scroll
309309
const {currentStandingPage, currentPage} = this.state;
310+
const index = this.getCalcIndex(currentPage);
310311
const pagesCount = presenter.getChildrenLength(this.props);
311312

312-
if (currentPage < pagesCount) {
313-
this.setState({currentStandingPage: currentPage});
313+
if (index < pagesCount) {
314+
this.setState({currentStandingPage: index});
314315

315-
if (currentStandingPage !== currentPage) {
316-
this.props.onChangePage?.(currentPage, currentStandingPage, {isAutoScrolled: this.isAutoScrolled});
316+
if (currentStandingPage !== index) {
317+
this.props.onChangePage?.(index, currentStandingPage, {isAutoScrolled: this.isAutoScrolled});
317318
this.isAutoScrolled = false;
318319
}
319320
}
@@ -354,11 +355,10 @@ class Carousel extends Component<CarouselProps, CarouselState> {
354355
};
355356

356357
onScrollEvent = Animated.event([
357-
{
358-
nativeEvent: {
359-
contentOffset:
360-
// @ts-ignore
361-
{y: this.props?.animatedScrollOffset?.y, x: this.props?.animatedScrollOffset?.x}
358+
{nativeEvent:
359+
{contentOffset:
360+
// @ts-ignore
361+
{y: this.props?.animatedScrollOffset?.y, x: this.props?.animatedScrollOffset?.x}
362362
}
363363
}
364364
],
@@ -404,10 +404,9 @@ class Carousel extends Component<CarouselProps, CarouselState> {
404404
};
405405

406406
renderChildren() {
407-
const {children: propsChildren, loop} = this.props;
407+
const {children, loop} = this.props;
408408
const length = presenter.getChildrenLength(this.props);
409-
const children =
410-
Constants.isRTL && Constants.isAndroid ? React.Children.toArray(propsChildren).reverse() : propsChildren;
409+
411410
const childrenArray = React.Children.map(children, (child, index) => {
412411
return this.renderChild(child, `${index}`);
413412
});
@@ -424,7 +423,6 @@ class Carousel extends Component<CarouselProps, CarouselState> {
424423

425424
renderPageControl() {
426425
const {pageControlPosition, pageControlProps = {}} = this.props;
427-
const {currentStandingPage} = this.state;
428426

429427
if (pageControlPosition) {
430428
const {
@@ -449,7 +447,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
449447
color={color}
450448
{...others}
451449
numOfPages={pagesCount}
452-
currentPage={currentStandingPage}
450+
currentPage={this.getCalcIndex(this.state.currentPage)}
453451
/>
454452
);
455453
}
@@ -473,6 +471,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
473471

474472
renderAccessibleLayout() {
475473
const {containerStyle, children, testID} = this.props;
474+
476475
return (
477476
<View style={containerStyle} onLayout={this.onContainerLayout}>
478477
<ScrollView
@@ -493,17 +492,20 @@ class Carousel extends Component<CarouselProps, CarouselState> {
493492
}
494493

495494
renderCarousel() {
496-
const {containerStyle, animated, horizontal, animatedScrollOffset, style, ...others} = this.props;
495+
const {containerStyle, animated, horizontal, animatedScrollOffset, ...others} = this.props;
497496
const scrollContainerStyle = this.shouldUsePageWidth()
498497
? {paddingRight: this.getItemSpacings(this.props)}
499498
: undefined;
500499
const snapToOffsets = this.getSnapToOffsets();
501500
const marginBottom = Math.max(0, this.getContainerPaddingVertical() - 16);
502501
const ScrollContainer = animatedScrollOffset ? Animated.ScrollView : ScrollView;
503502
const contentOffset = this.getInitialContentOffset(snapToOffsets);
504-
const _style = Constants.isRTL && Constants.isAndroid ? [styles.invertedView, style] : style;
505503
return (
506-
<View animated={animated} style={[{marginBottom}, containerStyle]} onLayout={this.onContainerLayout}>
504+
<View
505+
animated={animated}
506+
style={[{marginBottom}, containerStyle]}
507+
onLayout={this.onContainerLayout}
508+
>
507509
<ScrollContainer
508510
showsHorizontalScrollIndicator={false}
509511
showsVerticalScrollIndicator={false}
@@ -519,7 +521,6 @@ class Carousel extends Component<CarouselProps, CarouselState> {
519521
contentOffset={contentOffset}
520522
// onContentSizeChange={this.onContentSizeChange}
521523
onMomentumScrollEnd={this.onMomentumScrollEnd}
522-
style={_style}
523524
>
524525
{this.renderChildren()}
525526
</ScrollContainer>
@@ -555,8 +556,5 @@ const styles = StyleSheet.create({
555556
hiddenText: {
556557
position: 'absolute',
557558
width: 1
558-
},
559-
invertedView: {
560-
transform: [{scaleX: -1}]
561559
}
562560
});

0 commit comments

Comments
 (0)