Skip to content

Commit 30e3110

Browse files
authored
fix: prevent currentSlide from going out of bounds of totalSlides (#240)
* prevent currentSlide from going out of bounds * Update CarouselProvider.test.jsx
1 parent 56ca553 commit 30e3110

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/CarouselProvider/CarouselProvider.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ const CarouselProvider = class CarouselProvider extends React.Component {
133133
newStoreState.slideTraySize = slideTraySize(this.props.totalSlides, this.props.visibleSlides);
134134
}
135135

136+
if (this.carouselStore.state.currentSlide >= this.props.totalSlides) {
137+
newStoreState.currentSlide = this.props.totalSlides - 1;
138+
}
139+
136140
if (Object.keys(newStoreState).length > 0) {
137141
this.carouselStore.setStoreState(newStoreState);
138142
}

src/CarouselProvider/__tests__/CarouselProvider.test.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ describe('<CarouselProvider />', () => {
4949
expect(instance.carouselStore.state.slideSize).toBe(25);
5050
expect(instance.carouselStore.state.slideTraySize).toBe(200);
5151
});
52+
it('should keep currentSlide within bounds of totalSlides', () => {
53+
const wrapper = shallow(<CarouselProvider {...props} currentSlide={3} totalSlides={4} />);
54+
const instance = wrapper.instance();
55+
wrapper.setProps({ totalSlides: 3 });
56+
expect(instance.carouselStore.state.currentSlide).toEqual(2);
57+
});
5258
it('should not update the carouselStore if some prop we do not track changes', () => {
5359
const wrapper = shallow(<CarouselProvider {...props} data-foo={1} />);
5460
const instance = wrapper.instance();
@@ -58,7 +64,7 @@ describe('<CarouselProvider />', () => {
5864
expect(start).toEqual(end);
5965
});
6066
it('should not reset the currentSlide or disableAnimation values when unrelated props change', () => {
61-
const wrapper = shallow(<CarouselProvider {...props} data-foo={1} />);
67+
const wrapper = shallow(<CarouselProvider {...props} totalSlides={4} data-foo={1} />);
6268
const instance = wrapper.instance();
6369
instance.carouselStore.setStoreState({ currentSlide: 2 });
6470
const start = clone(instance.carouselStore.state);

0 commit comments

Comments
 (0)