Skip to content

Commit dff812e

Browse files
author
sagiv.bengiat
committed
resolve #58
1 parent 96d835f commit dff812e

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/react-elastic-carousel/components/Carousel.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,38 @@ class Carousel extends React.Component {
2727
isSwiping: false,
2828
transitioning: false,
2929
firstItem: this.props.initialFirstItem,
30+
pages: [],
3031
activePage: 0,
3132
sliderContainerWidth: 0
3233
};
3334

3435
componentDidMount() {
3536
this.initResizeObserver();
3637
this.updateActivePage();
38+
this.setPages();
3739
}
3840

3941
componentDidUpdate(prevProps, prevState) {
40-
const { enableAutoPlay, children, itemsToShow } = this.props;
42+
const {
43+
enableAutoPlay,
44+
children,
45+
itemsToShow,
46+
itemsToScroll,
47+
breakPoints
48+
} = this.props;
4149
const { firstItem } = this.state;
4250
const nextItem = this.getNextItemIndex(firstItem, false);
4351

52+
// update pages (for pagination)
53+
if (
54+
prevProps.children !== children ||
55+
prevProps.itemsToShow !== itemsToShow ||
56+
prevProps.itemsToScroll !== itemsToScroll ||
57+
prevProps.breakPoints !== breakPoints
58+
) {
59+
this.setPages();
60+
}
61+
4462
// autoplay update
4563
if (firstItem === nextItem) {
4664
this.removeAutoPlay();
@@ -104,6 +122,12 @@ class Carousel extends React.Component {
104122
}
105123
};
106124

125+
setPages = () => {
126+
const numOfPages = this.getNumOfPages();
127+
const pages = numberToArray(numOfPages);
128+
this.setState({ pages });
129+
};
130+
107131
onSliderTransitionEnd = fn => {
108132
this.slider.addEventListener("transitionend", fn);
109133
};
@@ -426,6 +450,7 @@ class Carousel extends React.Component {
426450
sliderPosition,
427451
swipedSliderPosition,
428452
rootHeight,
453+
pages,
429454
firstItem
430455
} = this.state;
431456
const {
@@ -548,7 +573,7 @@ class Carousel extends React.Component {
548573
<Only when={pagination}>
549574
{renderPagination ? (
550575
renderPagination({
551-
pages: numberToArray(numOfPages),
576+
pages: pages,
552577
activePage,
553578
onClick: this.onIndicatorClick
554579
})

0 commit comments

Comments
 (0)