@@ -201,34 +201,51 @@ class Carousel extends React.Component {
201201 children,
202202 itemsToShow
203203 } = this . getDerivedPropsFromBreakPoint ( ) ;
204- const { height } = sliderNode . contentRect ;
204+ const { height : sliderHeight } = sliderNode . contentRect ;
205205 const nextState = { } ;
206+ const childrenLength = Children . toArray ( children ) . length ;
206207 if ( verticalMode ) {
207- const childHeight = height / Children . toArray ( children ) . length ;
208- nextState . rootHeight = childHeight * itemsToShow ;
208+ const childHeight = sliderHeight / childrenLength ;
209+ // We use Math.min because don't want to make the child smaller
210+ // if number of children is smaller than itemsToShow.
211+ // Because we will have "empty slots"
212+ nextState . rootHeight =
213+ childHeight * Math . min ( childrenLength , itemsToShow ) ;
209214 nextState . childHeight = childHeight ;
210215 } else {
211- nextState . rootHeight = height ;
216+ nextState . rootHeight = sliderHeight ;
212217 }
213218 this . setState ( nextState ) ;
214219 } ;
215220
216221 onContainerResize = sliderContainerNode => {
217- const { width } = sliderContainerNode . contentRect ;
222+ const { width : sliderContainerWidth } = sliderContainerNode . contentRect ;
218223 // update slider container width
219224 // disable animation on resize see https://github.com/sag1v/react-elastic-carousel/issues/94
220- this . setState ( { sliderContainerWidth : width , transitionMs : 0 } , ( ) => {
225+ this . setState ( { sliderContainerWidth, transitionMs : 0 } , ( ) => {
221226 // we must get these props inside setState (get future props because its async)
222227 const {
223228 onResize,
224229 verticalMode,
225- itemsToShow
230+ itemsToShow,
231+ children
226232 } = this . getDerivedPropsFromBreakPoint ( ) ;
227233
228234 /* based on slider container's width, get num of items to show
229235 * and calculate child's width (and update it in state)
230236 */
231- const childWidth = verticalMode ? width : width / itemsToShow ;
237+ const childrenLength = Children . toArray ( children ) . length ;
238+ let childWidth = 0 ;
239+ if ( verticalMode ) {
240+ childWidth = sliderContainerWidth ;
241+ } else {
242+ // We use Math.min because don't want to make the child smaller
243+ // if number of children is smaller than itemsToShow.
244+ // Because we will have "empty slots"
245+ childWidth =
246+ sliderContainerWidth / Math . min ( childrenLength , itemsToShow ) ;
247+ }
248+
232249 this . setState (
233250 state => ( { childWidth } ) ,
234251 ( ) => {
0 commit comments