Skip to content

Commit 5e483ec

Browse files
authored
Merge pull request #167 from sag1v/fix-empty-children
handle empty array as children
2 parents d1e343d + 452cdf2 commit 5e483ec

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"scripts": {
2020
"start": "concurrently -r -k -s all \"docz dev\" \"yarn run lint:watch\"",
2121
"demo": "concurrently -r -k -s all \"rollup --config rollup.config.demo.js --watch\" \"yarn run lint:watch\"",
22-
"lint:fix": "eslint src/. --fix",
23-
"lint:watch": "esw --watch --fix src/.",
22+
"lint:fix": "eslint src/* --fix",
23+
"lint:watch": "esw --watch --fix src/*",
2424
"test": "cross-env CI=1 react-scripts test --env=jsdom",
2525
"test:watch": "react-scripts test --env=jsdom",
2626
"prebuild": "yarn run lint:fix",

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ class Carousel extends React.Component {
243243
/* based on slider container's width, get num of items to show
244244
* and calculate child's width (and update it in state)
245245
*/
246-
const childrenLength = Children.toArray(children).length;
246+
const childrenLength = Children.toArray(children).length || 1;
247+
247248
let childWidth = 0;
248249
if (verticalMode) {
249250
childWidth = sliderContainerWidth;
@@ -284,7 +285,7 @@ class Carousel extends React.Component {
284285
children
285286
} = this.getDerivedPropsFromBreakPoint();
286287

287-
const childrenLength = Children.toArray(children).length;
288+
const childrenLength = Children.toArray(children).length || 1;
288289

289290
this.setState(
290291
currentState => {
@@ -345,7 +346,7 @@ class Carousel extends React.Component {
345346
itemsToShow,
346347
itemsToScroll
347348
} = this.getDerivedPropsFromBreakPoint();
348-
const childrenLength = Children.toArray(children).length;
349+
const childrenLength = Children.toArray(children).length || 1;
349350
const notEnoughItemsToShow = itemsToShow > childrenLength;
350351
let limit = getPrev ? 0 : childrenLength - itemsToShow;
351352

@@ -393,7 +394,7 @@ class Carousel extends React.Component {
393394
const childWidth = this.calculateChildWidth();
394395

395396
// determine how far can user swipe
396-
const childrenLength = Children.toArray(children).length;
397+
const childrenLength = Children.toArray(children).length || 1;
397398
const goingNext =
398399
(!verticalMode && dir === "Left" && !isRTL) ||
399400
(!verticalMode && dir === "Right" && isRTL) ||

src/react-elastic-carousel/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint no-unused-vars: 0 */ // --> OFF
2+
/* eslint no-undef: 0 */ // --> OFF
13
import * as React from "react";
24

35
export type RenderArrowProps = {
@@ -32,7 +34,7 @@ export interface ReactElasticCarouselProps {
3234
// Defaults to false
3335
verticalMode?: boolean;
3436
// Defaults to false
35-
isRTL: boolean,
37+
isRTL: boolean;
3638
// Defaults to true
3739
pagination?: boolean;
3840
// Defaults to 500
@@ -108,6 +110,6 @@ export interface ReactElasticCarouselProps {
108110

109111
declare class ReactElasticCarousel extends React.Component<
110112
ReactElasticCarouselProps
111-
> { }
113+
> {}
112114

113115
export default ReactElasticCarousel;

0 commit comments

Comments
 (0)