Skip to content

Commit

Permalink
Fix primefaces#4301: Carousel never controlled while in autoplay
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Apr 28, 2023
1 parent 6894bd4 commit bd9afcf
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions components/lib/carousel/Carousel.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as React from 'react';
import PrimeReact, { ariaLabel } from '../api/Api';
import { useMountEffect, usePrevious, useResizeListener, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { Ripple } from '../ripple/Ripple';
import { classNames, DomHandler, IconUtils, ObjectUtils, UniqueComponentId } from '../utils/Utils';
import { CarouselBase } from './CarouselBase';
import { ChevronUpIcon } from '../icons/chevronup';
import { ChevronRightIcon } from '../icons/chevronright';
import { ChevronDownIcon } from '../icons/chevrondown';
import { ChevronLeftIcon } from '../icons/chevronleft';
import { ChevronRightIcon } from '../icons/chevronright';
import { ChevronUpIcon } from '../icons/chevronup';
import { Ripple } from '../ripple/Ripple';
import { DomHandler, IconUtils, ObjectUtils, UniqueComponentId, classNames } from '../utils/Utils';
import { CarouselBase } from './CarouselBase';

const CarouselItem = React.memo((props) => {
const content = props.template(props.item);
Expand Down Expand Up @@ -46,9 +46,10 @@ export const Carousel = React.memo(
const isVertical = props.orientation === 'vertical';
const circular = props.circular || !!props.autoplayInterval;
const isCircular = circular && props.value && props.value.length >= numVisibleState;
const currentPage = props.onPageChange ? props.page : pageState;
const totalIndicators = props.value ? Math.max(Math.ceil((props.value.length - numVisibleState) / numScrollState) + 1, 0) : 0;
const isAutoplay = totalIndicators && props.autoplayInterval && allowAutoplay.current;
const isControlled = props.onPageChange && !isAutoplay;
const currentPage = isControlled ? props.page : pageState;

const [bindWindowResizeListener] = useResizeListener({
listener: () => {
Expand Down Expand Up @@ -98,15 +99,8 @@ export const Carousel = React.memo(
itemsContainerRef.current.style.transition = 'transform 500ms ease 0s';
}

if (props.onPageChange) {
setTotalShiftedItemsState(totalShiftedItems);
props.onPageChange({
page
});
} else {
setPageState(page);
setTotalShiftedItemsState(totalShiftedItems);
}
changePage(page);
setTotalShiftedItemsState(totalShiftedItems);
};

const calculatePosition = () => {
Expand Down Expand Up @@ -135,14 +129,7 @@ export const Carousel = React.memo(

setTotalShiftedItemsState(totalShiftedItems);
setNumScrollState(matchedResponsiveData.numScroll);

if (props.onPageChange) {
props.onPageChange({
page
});
} else {
setPageState(page);
}
changePage(page);
}

if (numVisibleState !== matchedResponsiveData.numVisible) {
Expand Down Expand Up @@ -289,6 +276,11 @@ export const Carousel = React.memo(
}
};

const changePage = (page) => {
!isControlled && setPageState(page);
props.onPageChange && props.onPageChange({ page });
};

React.useImperativeHandle(ref, () => ({
props,
getElement: () => elementRef.current
Expand Down Expand Up @@ -322,13 +314,7 @@ export const Carousel = React.memo(
if (totalIndicators !== 0 && page >= totalIndicators) {
page = totalIndicators - 1;

if (props.onPageChange) {
props.onPageChange({
page
});
} else {
setPageState(page);
}
changePage(page);

stateChanged = true;
}
Expand Down

0 comments on commit bd9afcf

Please sign in to comment.