Skip to content

Fix carousel selected state and dots #179

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 4 commits into from
May 3, 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
21 changes: 19 additions & 2 deletions src/carousel/Carousel.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -108,6 +108,23 @@ const Carousel = React.forwardRef((props, ref) => {
return <Fragment key={key}>{slide}</Fragment>
}

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 (
<div
ref={ref}
Expand All @@ -122,7 +139,7 @@ const Carousel = React.forwardRef((props, ref) => {
<div className={classes.swipeWrap}>
<Tag
index={selected}
onChangeIndex={setSelected}
onChangeIndex={onChangeIndex}
className={classes.autoPlaySwipeableViews}
style={swipeStyle}
slideStyle={slideStyle}
Expand Down
18 changes: 16 additions & 2 deletions src/carousel/CarouselArrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,23 @@ export default function CarouselArrows({
const createOnClickArrow = useCallback(
idxChange => 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 (
Expand Down
14 changes: 7 additions & 7 deletions src/carousel/CarouselDots.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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' })
Expand Down
3 changes: 1 addition & 2 deletions src/carousel/CarouselThumbnails.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ({
/**
Expand Down Expand Up @@ -139,7 +138,7 @@ function CarouselThumbnails({
return (
<div className={clsx(className, styles.thumbs)}>
<Tabs
value={selected ? mod(selected, count) : false}
value={selected}
variant="scrollable"
onChange={(_, index) => setSelected(index)}
orientation={isVertical ? 'vertical' : 'horizontal'}
Expand Down