Skip to content

Fix/carousel android rtl jumps #2831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions src/components/carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
const defaultPageWidth = props.loop || !props.pageWidth ? Constants.screenWidth : props.pageWidth;
const pageHeight = props.pageHeight ?? Constants.screenHeight;
this.isAutoScrolled = false;

this.state = {
containerWidth: undefined,
// @ts-ignore (defaultProps)
Expand Down Expand Up @@ -195,28 +195,30 @@ class Carousel extends Component<CarouselProps, CarouselState> {
}

goToPage(pageIndex: number, animated = true) {
this.setState({currentPage: this.getCalcIndex(pageIndex)}, () => this.updateOffset(animated));
this.setState({currentPage: pageIndex}, () => this.updateOffset(animated));
}

goToNextPage() {
const {currentPage} = this.state;
const pagesCount = presenter.getChildrenLength(this.props);
const {loop} = this.props;
let nextPageIndex;

if (loop) {
if (currentPage === pagesCount + 1) {
this.goToPage(0, false);
return;
}
nextPageIndex = currentPage + 1;
} else {
nextPageIndex = Math.min(pagesCount - 1, currentPage + 1);
}

this.goToPage(nextPageIndex, true);

// in case of a loop, after we advanced right to the cloned first page,
// we return silently to the real first page
if (loop && currentPage === pagesCount) {
this.goToPage(0, false);
}
// // in case of a loop, after we advanced right to the cloned first page,
// // we return silently to the real first page
// if (loop && currentPage === pagesCount) {
// this.goToPage(0, false);
// }
}

getCalcIndex(index: number): number {
Expand All @@ -239,10 +241,8 @@ class Carousel extends Component<CarouselProps, CarouselState> {
if (containerWidth) {
const spacings = pageWidth === containerWidth ? 0 : this.getItemSpacings(this.props);
const initialBreak = pageWidth - (containerWidth - pageWidth - spacings) / 2;
const snapToOffsets = _.times(
presenter.getChildrenLength(this.props),
index => initialBreak + index * pageWidth + this.getContainerMarginHorizontal()
);
const snapToOffsets = _.times(presenter.getChildrenLength(this.props),
index => initialBreak + index * pageWidth + this.getContainerMarginHorizontal());
return snapToOffsets;
}
};
Expand Down Expand Up @@ -307,14 +307,13 @@ class Carousel extends Component<CarouselProps, CarouselState> {
onMomentumScrollEnd = () => {
// finished full page scroll
const {currentStandingPage, currentPage} = this.state;
const index = this.getCalcIndex(currentPage);
const pagesCount = presenter.getChildrenLength(this.props);

if (index < pagesCount) {
this.setState({currentStandingPage: index});
if (currentPage < pagesCount) {
this.setState({currentStandingPage: currentPage});

if (currentStandingPage !== index) {
this.props.onChangePage?.(index, currentStandingPage, {isAutoScrolled: this.isAutoScrolled});
if (currentStandingPage !== currentPage) {
this.props.onChangePage?.(currentPage, currentStandingPage, {isAutoScrolled: this.isAutoScrolled});
this.isAutoScrolled = false;
}
}
Expand Down Expand Up @@ -355,10 +354,11 @@ class Carousel extends Component<CarouselProps, CarouselState> {
};

onScrollEvent = Animated.event([
{nativeEvent:
{contentOffset:
// @ts-ignore
{y: this.props?.animatedScrollOffset?.y, x: this.props?.animatedScrollOffset?.x}
{
nativeEvent: {
contentOffset:
// @ts-ignore
{y: this.props?.animatedScrollOffset?.y, x: this.props?.animatedScrollOffset?.x}
}
}
],
Expand Down Expand Up @@ -404,9 +404,10 @@ class Carousel extends Component<CarouselProps, CarouselState> {
};

renderChildren() {
const {children, loop} = this.props;
const {children: propsChildren, loop} = this.props;
const length = presenter.getChildrenLength(this.props);

const children =
Constants.isRTL && Constants.isAndroid ? React.Children.toArray(propsChildren).reverse() : propsChildren;
const childrenArray = React.Children.map(children, (child, index) => {
return this.renderChild(child, `${index}`);
});
Expand All @@ -423,6 +424,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {

renderPageControl() {
const {pageControlPosition, pageControlProps = {}} = this.props;
const {currentStandingPage} = this.state;

if (pageControlPosition) {
const {
Expand All @@ -447,7 +449,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
color={color}
{...others}
numOfPages={pagesCount}
currentPage={this.getCalcIndex(this.state.currentPage)}
currentPage={currentStandingPage}
/>
);
}
Expand All @@ -471,7 +473,6 @@ class Carousel extends Component<CarouselProps, CarouselState> {

renderAccessibleLayout() {
const {containerStyle, children, testID} = this.props;

return (
<View style={containerStyle} onLayout={this.onContainerLayout}>
<ScrollView
Expand All @@ -492,20 +493,17 @@ class Carousel extends Component<CarouselProps, CarouselState> {
}

renderCarousel() {
const {containerStyle, animated, horizontal, animatedScrollOffset, ...others} = this.props;
const {containerStyle, animated, horizontal, animatedScrollOffset, style, ...others} = this.props;
const scrollContainerStyle = this.shouldUsePageWidth()
? {paddingRight: this.getItemSpacings(this.props)}
: undefined;
const snapToOffsets = this.getSnapToOffsets();
const marginBottom = Math.max(0, this.getContainerPaddingVertical() - 16);
const ScrollContainer = animatedScrollOffset ? Animated.ScrollView : ScrollView;
const contentOffset = this.getInitialContentOffset(snapToOffsets);
const _style = Constants.isRTL && Constants.isAndroid ? [styles.invertedView, style] : style;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I inverted the view on android rtl so it would behave the same as on ios.

return (
<View
animated={animated}
style={[{marginBottom}, containerStyle]}
onLayout={this.onContainerLayout}
>
<View animated={animated} style={[{marginBottom}, containerStyle]} onLayout={this.onContainerLayout}>
<ScrollContainer
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
Expand All @@ -521,6 +519,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
contentOffset={contentOffset}
// onContentSizeChange={this.onContentSizeChange}
onMomentumScrollEnd={this.onMomentumScrollEnd}
style={_style}
>
{this.renderChildren()}
</ScrollContainer>
Expand Down Expand Up @@ -556,5 +555,8 @@ const styles = StyleSheet.create({
hiddenText: {
position: 'absolute',
width: 1
},
invertedView: {
transform: [{scaleX: -1}]
}
});