Skip to content

Commit b90be84

Browse files
authored
Merge pull request #104 from sag1v/children-bug
resolve #101 (fixed bug of 1 item broke functionality)
2 parents d8beca6 + dd57707 commit b90be84

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { Children } from "react";
22
import PropTypes from "prop-types";
33
import ResizeObserver from "resize-observer-polyfill";
44
import Only from "react-only-when";
@@ -49,10 +49,11 @@ class Carousel extends React.Component {
4949
} = this.props;
5050
const { activeIndex, sliderContainerWidth } = this.state;
5151
const nextItem = this.getNextItemIndex(activeIndex, false);
52-
52+
const currentChildrenLength = Children.toArray(children).length;
53+
const prevChildrenLength = Children.toArray(prevProps.children).length;
5354
// update pages (for pagination)
5455
if (
55-
prevProps.children !== children ||
56+
prevChildrenLength !== currentChildrenLength ||
5657
prevProps.itemsToShow !== itemsToShow ||
5758
prevProps.itemsToScroll !== itemsToScroll ||
5859
prevProps.breakPoints !== breakPoints ||
@@ -70,18 +71,18 @@ class Carousel extends React.Component {
7071
this.removeAutoPlay();
7172
}
7273

73-
if (prevProps.children.length > children.length) {
74+
if (prevChildrenLength !== currentChildrenLength) {
7475
const {
7576
itemsToShow: calculatedItemsToShow
7677
} = this.getDerivedPropsFromBreakPoint();
7778
// number of items is reduced (we don't care if number of items is increased)
7879
// we need to check if our current index is not out of boundaries
7980
// we need to include itemsToShow so we can fill up the slots
80-
const lastIndex = children.length - 1;
81+
const lastIndex = currentChildrenLength - 1;
8182
const isOutOfRange = activeIndex + calculatedItemsToShow > lastIndex;
8283
if (isOutOfRange) {
8384
// we are out of boundaries, go "back" to last item of the list (respect itemsToShow)
84-
this.goTo(Math.max(0, children.length - calculatedItemsToShow));
85+
this.goTo(Math.max(0, currentChildrenLength - calculatedItemsToShow));
8586
}
8687
}
8788
}
@@ -173,7 +174,7 @@ class Carousel extends React.Component {
173174
transitionMs
174175
} = this.getDerivedPropsFromBreakPoint();
175176
const { childWidth, childHeight, activeIndex } = state;
176-
const totalItems = children.length;
177+
const totalItems = Children.toArray(children).length;
177178
const hiddenSlots = totalItems - itemsToShow;
178179
let moveBy = activeIndex * -1;
179180
const emptySlots = itemsToShow - (totalItems - activeIndex);
@@ -203,7 +204,7 @@ class Carousel extends React.Component {
203204
const { height } = sliderNode.contentRect;
204205
const nextState = {};
205206
if (verticalMode) {
206-
const childHeight = height / children.length;
207+
const childHeight = height / Children.toArray(children).length;
207208
nextState.rootHeight = childHeight * itemsToShow;
208209
nextState.childHeight = childHeight;
209210
} else {
@@ -263,7 +264,7 @@ class Carousel extends React.Component {
263264
const { children } = this.props;
264265
// support decimal itemsToShow
265266
const roundedIdx = Math.round(index);
266-
const child = children[roundedIdx];
267+
const child = Children.toArray(children)[roundedIdx];
267268
return { item: child.props, index: roundedIdx };
268269
};
269270

@@ -273,8 +274,9 @@ class Carousel extends React.Component {
273274
itemsToShow,
274275
itemsToScroll
275276
} = this.getDerivedPropsFromBreakPoint();
276-
const notEnoughItemsToShow = itemsToShow > children.length;
277-
let limit = getPrev ? 0 : children.length - itemsToShow;
277+
const childrenLength = Children.toArray(children).length;
278+
const notEnoughItemsToShow = itemsToShow > childrenLength;
279+
let limit = getPrev ? 0 : childrenLength - itemsToShow;
278280

279281
if (notEnoughItemsToShow) {
280282
limit = 0; // basically don't move
@@ -292,7 +294,7 @@ class Carousel extends React.Component {
292294
const nextItemIndex = this.getNextItemIndex(activeIndex, getPrev);
293295
// support decimal itemsToShow
294296
const roundedIdx = Math.round(nextItemIndex);
295-
const asElement = children[roundedIdx];
297+
const asElement = Children.toArray(children)[roundedIdx];
296298
const asObj = { item: asElement.props, index: roundedIdx };
297299
return asObj;
298300
};
@@ -323,8 +325,9 @@ class Carousel extends React.Component {
323325
} = this.getDerivedPropsFromBreakPoint();
324326

325327
// determine how far can user swipe
328+
const childrenLength = Children.toArray(children).length;
326329
const isOnStart = activeIndex === 0;
327-
const isOnEnd = activeIndex === children.length - itemsToShow;
330+
const isOnEnd = activeIndex === childrenLength - itemsToShow;
328331
const defaultDivider = 1.5;
329332
const largeDivider = itemsToShow * 2;
330333
let divider = defaultDivider;
@@ -535,16 +538,17 @@ class Carousel extends React.Component {
535538
itemsToShow
536539
} = this.getDerivedPropsFromBreakPoint();
537540
const { activeIndex } = this.state;
541+
const childrenLength = Children.toArray(children).length;
538542
const isPrev = activeIndex > nextItemId;
539543
const nextAvailbaleItem = this.getNextItemIndex(activeIndex, isPrev);
540544
const noChange = nextAvailbaleItem === activeIndex;
541-
const outOfBoundry = nextItemId + itemsToShow >= children.length;
545+
const outOfBoundry = nextItemId + itemsToShow >= childrenLength;
542546
if (noChange) {
543547
return;
544548
}
545549
if (outOfBoundry) {
546550
// Either go to last index (respect itemsToShow) or 0 index if we can't fill the slider
547-
nextItemId = Math.max(0, children.length - itemsToShow);
551+
nextItemId = Math.max(0, childrenLength - itemsToShow);
548552
}
549553
let direction = consts.NEXT;
550554
let positionEndCb = this.onNextEnd;
@@ -571,7 +575,8 @@ class Carousel extends React.Component {
571575

572576
getNumOfPages = () => {
573577
const { children, itemsToShow } = this.getDerivedPropsFromBreakPoint();
574-
const numOfPages = Math.ceil(children.length / itemsToShow);
578+
const childrenLength = Children.toArray(children).length;
579+
const numOfPages = Math.ceil(childrenLength / itemsToShow);
575580
return numOfPages || 1;
576581
};
577582

@@ -681,7 +686,7 @@ class Carousel extends React.Component {
681686
>
682687
<Track
683688
verticalMode={verticalMode}
684-
children={children}
689+
children={Children.toArray(children)}
685690
childWidth={childWidth}
686691
currentItem={activeIndex}
687692
autoTabIndexVisibleItems={autoTabIndexVisibleItems}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ describe("Carousel - public API (props)", () => {
2222
expect(children.length).toEqual(Items.length);
2323
});
2424

25+
it("one child wont break on next", () => {
26+
const wrapper = mount(<Carousel>{Items[0]}</Carousel>);
27+
const nextButton = wrapper.find(Carousel).find("button.rec-arrow-right");
28+
nextButton.simulate("click");
29+
});
30+
2531
it("renders with className in root", () => {
2632
const testClassName = "test-root";
2733
const wrapper = mount(

0 commit comments

Comments
 (0)