Skip to content

Carousel - fix counter flickering between numbers #1591

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 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PropsWithChildren } from 'react';
import { CarouselProps, CarouselState } from './types';
export declare function getChildrenLength(props: PropsWithChildren<CarouselProps>): number;
export declare function calcOffset(props: CarouselProps, state: Omit<CarouselState, 'initialOffset' | 'prevProps'>): {
export declare function calcOffset(props: CarouselProps, state: Omit<CarouselState, 'initialOffset' | 'prevProps' | 'currentStandingPage'>): {
x: number;
y: number;
};
Expand Down
2 changes: 1 addition & 1 deletion generatedTypes/src/components/carousel/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface CarouselProps extends ScrollViewProps {
export interface CarouselState {
containerWidth?: number;
currentPage: number;
currentStandingPage?: number;
currentStandingPage: number;
pageWidth: number;
pageHeight: number;
initialOffset: PointPropType;
Expand Down
2 changes: 1 addition & 1 deletion src/components/carousel/CarouselPresenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function getChildrenLength(props: PropsWithChildren<CarouselProps>): numb
return React.Children.count(props.children);
}

export function calcOffset(props: CarouselProps, state: Omit<CarouselState, 'initialOffset' | 'prevProps'>) {
export function calcOffset(props: CarouselProps, state: Omit<CarouselState, 'initialOffset' | 'prevProps' | 'currentStandingPage'>) {
const {currentPage, pageWidth, pageHeight} = state;
const {loop, containerMarginHorizontal = 0} = props;
const actualCurrentPage = loop ? currentPage + 1 : currentPage;
Expand Down
8 changes: 4 additions & 4 deletions src/components/carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
containerWidth: undefined,
// @ts-ignore (defaultProps)
currentPage: this.shouldUsePageWidth() ? this.getCalcIndex(props.initialPage) : props.initialPage,
currentStandingPage: props.initialPage,
currentStandingPage: props.initialPage || 0,
Copy link
Collaborator

Choose a reason for hiding this comment

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

'initialPage' prop's default value is already 0

pageWidth: defaultPageWidth,
pageHeight,
initialOffset: presenter.calcOffset(props, {
Expand Down Expand Up @@ -262,7 +262,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {

onMomentumScrollEnd = () => {
// finished full page scroll
const {currentStandingPage = 0, currentPage} = this.state;
const {currentStandingPage, currentPage} = this.state;
const index = this.getCalcIndex(currentPage);

const pagesCount = presenter.getChildrenLength(this.props);
Expand Down Expand Up @@ -420,14 +420,14 @@ class Carousel extends Component<CarouselProps, CarouselState> {

renderCounter() {
const {pageWidth, showCounter, counterTextStyle} = this.props;
const {currentPage} = this.state;
const {currentStandingPage} = this.state;
const pagesCount = presenter.getChildrenLength(this.props);

if (showCounter && !pageWidth) {
return (
<View center style={styles.counter}>
<Text grey80 text90 style={[{fontWeight: 'bold'}, counterTextStyle]}>
{currentPage + 1}/{pagesCount}
{currentStandingPage + 1}/{pagesCount}
</Text>
</View>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/carousel/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export interface CarouselProps extends ScrollViewProps {
export interface CarouselState {
containerWidth?: number;
currentPage: number;
currentStandingPage?: number;
currentStandingPage: number;
pageWidth: number;
pageHeight: number;
initialOffset: PointPropType;
Expand Down