Skip to content

Commit

Permalink
Disable drag and and hide arrows as well as set clones when children …
Browse files Browse the repository at this point in the history
…is not enough
  • Loading branch information
YIZHUANG committed Jun 5, 2019
1 parent e9c2a4d commit 62377b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
36 changes: 23 additions & 13 deletions src/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
populatePreviousSlides,
populateSlidesOnMouseTouchMove,
isInLeftEnd,
isInRightEnd
isInRightEnd,
notEnoughChildren
} from "./utils";
import { CarouselInternalState, CarouselProps, stateCallBack } from "./types";
import Dots from "./Dots";
Expand Down Expand Up @@ -129,16 +130,18 @@ class Carousel extends React.Component<CarouselProps, CarouselInternalState> {
this.state.slidesToShow,
childrenArr
);
this.setState(
{
clones,
totalItems: clones.length,
currentSlide: forResizing ? this.state.currentSlide : initialSlide
},
() => {
this.correctItemsPosition(itemWidth || this.state.itemWidth);
}
);
if(!notEnoughChildren(this.state, this.props, slidesToShow)) {
this.setState(
{
clones,
totalItems: clones.length,
currentSlide: forResizing ? this.state.currentSlide : initialSlide
},
() => {
this.correctItemsPosition(itemWidth || this.state.itemWidth);
}
);
}
}
public setItemsToShow(shouldCorrectItemPosition?: boolean): void {
const { responsive, infinite } = this.props;
Expand Down Expand Up @@ -277,6 +280,9 @@ class Carousel extends React.Component<CarouselProps, CarouselInternalState> {
}
public next(slidesHavePassed = 0): void {
const { afterChange, beforeChange } = this.props;
if(notEnoughChildren(this.state, this.props)) {
return;
}
/*
two cases:
1. We are not over-sliding.
Expand Down Expand Up @@ -314,6 +320,9 @@ class Carousel extends React.Component<CarouselProps, CarouselInternalState> {
}
public previous(slidesHavePassed = 0): void {
const { afterChange, beforeChange } = this.props;
if(notEnoughChildren(this.state, this.props)) {
return;
}
const { nextSlides, nextPosition } = populatePreviousSlides(
this.state,
this.props,
Expand Down Expand Up @@ -379,7 +388,8 @@ class Carousel extends React.Component<CarouselProps, CarouselInternalState> {
public handleMove(e: any): void {
if (
(e.touches && !this.props.swipeable) ||
(e && !e.touches && !this.props.draggable)
(e && !e.touches && !this.props.draggable) ||
notEnoughChildren(this.state, this.props)
) {
return;
}
Expand Down Expand Up @@ -586,7 +596,7 @@ class Carousel extends React.Component<CarouselProps, CarouselInternalState> {
((deviceType && removeArrowOnDeviceType.indexOf(deviceType) > -1) ||
(this.state.deviceType &&
removeArrowOnDeviceType.indexOf(this.state.deviceType) > -1))
);
) && !notEnoughChildren(this.state, this.props);
const disableLeftArrow = !infinite && isLeftEndReach;
const disableRightArrow = !infinite && isRightEndReach;
// this lib supports showing next set of items paritially as well as center mode which shows both.
Expand Down
14 changes: 13 additions & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as React from "react";
import {
getParitialVisibilityGutter,
getWidthFromDeviceType
Expand Down Expand Up @@ -86,11 +87,22 @@ function isInRightEnd({
return !(currentSlide + slidesToShow < totalItems);
}

function notEnoughChildren(
state: CarouselInternalState,
props: CarouselProps,
items?: number | undefined
): boolean {
const childrenArr = React.Children.toArray(props.children);
const { slidesToShow } = state;
return items ? childrenArr.length < items : childrenArr.length < slidesToShow;
}

export {
isInLeftEnd,
isInRightEnd,
getInitialState,
getIfSlideIsVisbile,
getTransformForCenterMode,
getTransformForPartialVsibile
getTransformForPartialVsibile,
notEnoughChildren
};
6 changes: 4 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
getTransformForCenterMode,
getTransformForPartialVsibile,
isInLeftEnd,
isInRightEnd
isInRightEnd,
notEnoughChildren
} from "./common";
import throttle from "./throttle";
import throwError from "./throwError";
Expand All @@ -41,5 +42,6 @@ export {
throwError,
populateNextSlides,
populatePreviousSlides,
populateSlidesOnMouseTouchMove
populateSlidesOnMouseTouchMove,
notEnoughChildren
};

0 comments on commit 62377b0

Please sign in to comment.