@@ -70,14 +70,17 @@ class Carousel extends React.Component {
7070 }
7171
7272 if ( prevProps . children . length > children . length ) {
73+ const {
74+ itemsToShow : calculatedItemsToShow
75+ } = this . getDerivedPropsFromBreakPoint ( ) ;
7376 // number of items is reduced (we don't care if number of items is increased)
7477 // we need to check if our current index is not out of boundaries
7578 // we need to include itemsToShow so we can fill up the slots
7679 const lastIndex = children . length - 1 ;
77- const isOutOfRange = activeIndex + itemsToShow > lastIndex ;
80+ const isOutOfRange = activeIndex + calculatedItemsToShow > lastIndex ;
7881 if ( isOutOfRange ) {
7982 // we are out of boundaries, go "back" to last item of the list (respect itemsToShow)
80- this . goTo ( children . length - itemsToShow ) ;
83+ this . goTo ( Math . max ( 0 , children . length - calculatedItemsToShow ) ) ;
8184 }
8285 }
8386 }
@@ -204,14 +207,16 @@ class Carousel extends React.Component {
204207 } ;
205208
206209 onContainerResize = sliderContainerNode => {
207- const {
208- onResize,
209- verticalMode,
210- itemsToShow
211- } = this . getDerivedPropsFromBreakPoint ( ) ;
212210 const { width } = sliderContainerNode . contentRect ;
213211 // update slider container width
214212 this . setState ( { sliderContainerWidth : width } , ( ) => {
213+ // we must get these props inside setState (get future props because its async)
214+ const {
215+ onResize,
216+ verticalMode,
217+ itemsToShow
218+ } = this . getDerivedPropsFromBreakPoint ( ) ;
219+
215220 /* based on slider container's width, get num of items to show
216221 * and calculate child's width (and update it in state)
217222 */
@@ -521,14 +526,8 @@ class Carousel extends React.Component {
521526 return ;
522527 }
523528 if ( outOfBoundry ) {
524- if ( children . length - itemsToShow > 0 ) {
525- nextItemId = children . length - itemsToShow ;
526- } else {
527- nextItemId = Math . max (
528- children . length - 1 ,
529- children . length - itemsToShow
530- ) ;
531- }
529+ // Either go to last index (respect itemsToShow) or 0 index if we can't fill the slider
530+ nextItemId = Math . max ( 0 , children . length - itemsToShow ) ;
532531 }
533532 let direction = consts . NEXT ;
534533 let positionEndCb = this . onNextEnd ;
0 commit comments