Skip to content

Commit

Permalink
Merge pull request #167 from sag1v/fix-empty-children
Browse files Browse the repository at this point in the history
handle empty array as children
  • Loading branch information
sag1v authored Mar 27, 2021
2 parents d1e343d + 452cdf2 commit 5e483ec
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"scripts": {
"start": "concurrently -r -k -s all \"docz dev\" \"yarn run lint:watch\"",
"demo": "concurrently -r -k -s all \"rollup --config rollup.config.demo.js --watch\" \"yarn run lint:watch\"",
"lint:fix": "eslint src/. --fix",
"lint:watch": "esw --watch --fix src/.",
"lint:fix": "eslint src/* --fix",
"lint:watch": "esw --watch --fix src/*",
"test": "cross-env CI=1 react-scripts test --env=jsdom",
"test:watch": "react-scripts test --env=jsdom",
"prebuild": "yarn run lint:fix",
Expand Down
9 changes: 5 additions & 4 deletions src/react-elastic-carousel/components/Carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ class Carousel extends React.Component {
/* based on slider container's width, get num of items to show
* and calculate child's width (and update it in state)
*/
const childrenLength = Children.toArray(children).length;
const childrenLength = Children.toArray(children).length || 1;

let childWidth = 0;
if (verticalMode) {
childWidth = sliderContainerWidth;
Expand Down Expand Up @@ -284,7 +285,7 @@ class Carousel extends React.Component {
children
} = this.getDerivedPropsFromBreakPoint();

const childrenLength = Children.toArray(children).length;
const childrenLength = Children.toArray(children).length || 1;

this.setState(
currentState => {
Expand Down Expand Up @@ -345,7 +346,7 @@ class Carousel extends React.Component {
itemsToShow,
itemsToScroll
} = this.getDerivedPropsFromBreakPoint();
const childrenLength = Children.toArray(children).length;
const childrenLength = Children.toArray(children).length || 1;
const notEnoughItemsToShow = itemsToShow > childrenLength;
let limit = getPrev ? 0 : childrenLength - itemsToShow;

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

// determine how far can user swipe
const childrenLength = Children.toArray(children).length;
const childrenLength = Children.toArray(children).length || 1;
const goingNext =
(!verticalMode && dir === "Left" && !isRTL) ||
(!verticalMode && dir === "Right" && isRTL) ||
Expand Down
6 changes: 4 additions & 2 deletions src/react-elastic-carousel/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint no-unused-vars: 0 */ // --> OFF
/* eslint no-undef: 0 */ // --> OFF
import * as React from "react";

export type RenderArrowProps = {
Expand Down Expand Up @@ -32,7 +34,7 @@ export interface ReactElasticCarouselProps {
// Defaults to false
verticalMode?: boolean;
// Defaults to false
isRTL: boolean,
isRTL: boolean;
// Defaults to true
pagination?: boolean;
// Defaults to 500
Expand Down Expand Up @@ -108,6 +110,6 @@ export interface ReactElasticCarouselProps {

declare class ReactElasticCarousel extends React.Component<
ReactElasticCarouselProps
> { }
> {}

export default ReactElasticCarousel;

0 comments on commit 5e483ec

Please sign in to comment.