diff --git a/src/carousel/Carousel.js b/src/carousel/Carousel.js index aa95174e..ef0a2286 100644 --- a/src/carousel/Carousel.js +++ b/src/carousel/Carousel.js @@ -1,4 +1,4 @@ -import React, { Fragment, useState } from 'react' +import React, { Fragment, useCallback, useState } from 'react' import clsx from 'clsx' import { makeStyles } from '@material-ui/core/styles' import SwipeableViews from 'react-swipeable-views' @@ -108,6 +108,23 @@ const Carousel = React.forwardRef((props, ref) => { return {slide} } + const onChangeIndex = useCallback((index) => { + if (!infinite) { + setSelected(index) + return + } + + // carousel loop-around calculations + let nextSelectedIndex = index; + if (nextSelectedIndex + 1 > count) { + nextSelectedIndex = 0; + } else if (nextSelectedIndex < 0) { + nextSelectedIndex = count - 1; + } + + setSelected(nextSelectedIndex) + }, [infinite, count, selected, setSelected]) + return (
{
evt => { evt.preventDefault() - setSelected(selected + idxChange) + + if (!infinite) { + setSelected(selected + idxChange); + return; + } + + // carousel loop-around calculations + let nextSelectedIndex = selected + idxChange; + if (nextSelectedIndex + 1 > count) { + nextSelectedIndex = 0; + } else if (nextSelectedIndex < 0) { + nextSelectedIndex = count - 1; + } + + setSelected(nextSelectedIndex); }, - [selected, setSelected], + [selected, setSelected, count, infinite], ) return ( diff --git a/src/carousel/CarouselDots.js b/src/carousel/CarouselDots.js index d9ddb1b2..58fb27c3 100644 --- a/src/carousel/CarouselDots.js +++ b/src/carousel/CarouselDots.js @@ -15,13 +15,6 @@ const styles = theme => ({ width: '100%', }, - /** - * Styles applied to the dot representing the selected slide. - */ - dotSelected: { - backgroundColor: theme.palette.text.primary, - }, - /** * Styles applied to each dot element. */ @@ -38,6 +31,13 @@ const styles = theme => ({ // Same duration as SwipeableViews animation transitionDuration: '0.35s', }, + + /** + * Styles applied to the dot representing the selected slide. + */ + dotSelected: { + backgroundColor: theme.palette.text.primary, + } }) const useStyles = makeStyles(styles, { name: 'RSFCarouselDots' }) diff --git a/src/carousel/CarouselThumbnails.js b/src/carousel/CarouselThumbnails.js index e161dc7d..003baba6 100644 --- a/src/carousel/CarouselThumbnails.js +++ b/src/carousel/CarouselThumbnails.js @@ -6,7 +6,6 @@ import Tab from '@material-ui/core/Tab' import { makeStyles, useTheme } from '@material-ui/core/styles' import useMediaQuery from '@material-ui/core/useMediaQuery' import Image from '../Image' -import mod from '../utils/mod' export const styles = theme => ({ /** @@ -139,7 +138,7 @@ function CarouselThumbnails({ return (
setSelected(index)} orientation={isVertical ? 'vertical' : 'horizontal'}