Skip to content

Commit 7d1954c

Browse files
rui.franclimsilvasofiacteixeira
authored andcommitted
fix(carousel): fix problem with direction when there are only two images
1 parent 0c19d8d commit 7d1954c

2 files changed

Lines changed: 29 additions & 16 deletions

File tree

src/context/CarouselProvider.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,26 @@ class CarouselProvider extends Component {
6161
}
6262

6363
_getDirection(newActiveItem, activeItem = this.state.activeItem) {
64-
const lastItem = this.state.itemsLength - 1;
65-
66-
if (newActiveItem === 0 && lastItem === activeItem) {
64+
const { isInfinite, direction, itemsLength } = this.state;
65+
const lastItem = itemsLength - 1;
66+
67+
// Going from last to first
68+
if (
69+
isInfinite &&
70+
newActiveItem === 0 &&
71+
lastItem === activeItem &&
72+
direction === 'next'
73+
) {
6774
return 'next';
6875
}
6976

70-
if (activeItem === 0 && newActiveItem === lastItem) {
77+
// Going from first to last
78+
if (
79+
isInfinite &&
80+
activeItem === 0 &&
81+
newActiveItem === lastItem &&
82+
direction === 'prev'
83+
) {
7184
return 'prev';
7285
}
7386

tests/context/CarouselProvider.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -671,38 +671,38 @@ describe('<CarouselProvider/>', () => {
671671
expect(result).toBe(prevDirection);
672672
});
673673

674-
test('should return prev if new active item is the last item of items', () => {
674+
test('should return next when is infinite and user moves in next direction while at the end of the carousel', () => {
675675
const wrapper = shallow(
676-
<CarouselProvider>
676+
<CarouselProvider isInfinite>
677677
<MockComponent />
678678
</CarouselProvider>
679679
);
680680

681681
const instance = wrapper.instance();
682682

683683
instance._setItemsLength(2);
684-
instance._setActiveItem(0);
685-
686-
const result = instance._getDirection(1);
684+
instance._goNext();
685+
instance._setActiveItem(1);
686+
const result = instance._getDirection(0);
687687

688-
expect(result).toBe(prevDirection);
688+
expect(result).toBe(nextDirection);
689689
});
690690

691-
test('should return next if new active item is the first item of items', () => {
691+
test('should return prev when is infinite and user moves in prev direction while at the start of the carousel', () => {
692692
const wrapper = shallow(
693-
<CarouselProvider>
693+
<CarouselProvider isInfinite>
694694
<MockComponent />
695695
</CarouselProvider>
696696
);
697697

698698
const instance = wrapper.instance();
699699

700700
instance._setItemsLength(2);
701-
instance._setActiveItem(1);
702-
703-
const result = instance._getDirection(0);
701+
instance._goPrev();
702+
instance._setActiveItem(0);
703+
const result = instance._getDirection(1);
704704

705-
expect(result).toBe(nextDirection);
705+
expect(result).toBe(prevDirection);
706706
});
707707
});
708708

0 commit comments

Comments
 (0)