Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clicking next/prev arrows when initialSlide is not 0 #2029

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions __tests__/innerSliderUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { changeSlide } from "../src/utils/innerSliderUtils";

describe("changeSlide", () => {
it("should not pass to before 0 when not infinite", function() {
const settings = {
slidesToScroll: 1,
slidesToShow: 1,
slideCount: 10,
currentSlide: 0,
targetSlide: 0,
lazyLoad: false,
infinite: false
};

const targetSlide = changeSlide(settings, { message: "previous" });
expect(targetSlide).toEqual(0);
});

it("should not pass to after last slide when not infinite", function() {
const settings = {
slidesToScroll: 1,
slidesToShow: 1,
slideCount: 10,
currentSlide: 9,
targetSlide: 9,
lazyLoad: false,
infinite: false
};

const targetSlide = changeSlide(settings, { message: "next" });
expect(targetSlide).toEqual(9);
});
});
4 changes: 3 additions & 1 deletion src/inner-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class InnerSlider extends React.Component {
this.state = {
...initialState,
currentSlide: this.props.initialSlide,
targetSlide: this.props.initialSlide,
slideCount: React.Children.count(this.props.children)
};
this.callbackTimers = [];
Expand Down Expand Up @@ -304,7 +305,8 @@ export class InnerSlider extends React.Component {
};
checkImagesLoad = () => {
let images =
(this.list && this.list.querySelectorAll &&
(this.list &&
this.list.querySelectorAll &&
this.list.querySelectorAll(".slick-slide img")) ||
[];
let imagesCount = images.length,
Expand Down
22 changes: 12 additions & 10 deletions src/utils/innerSliderUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export function clamp(number, lowerBound, upperBound) {

export const safePreventDefault = event => {
const passiveEvents = ["onTouchStart", "onTouchMove", "onWheel"];
if(!passiveEvents.includes(event._reactName)) {
if (!passiveEvents.includes(event._reactName)) {
event.preventDefault();
}
}
};

export const getOnDemandLazySlides = spec => {
let onDemandSlides = [];
Expand Down Expand Up @@ -268,7 +268,6 @@ export const changeSlide = (spec, options) => {
slidesToShow,
slideCount,
currentSlide,
targetSlide: previousTargetSlide,
lazyLoad,
infinite
} = spec;
Expand All @@ -282,8 +281,8 @@ export const changeSlide = (spec, options) => {
previousInt = currentSlide - slideOffset;
targetSlide = previousInt === -1 ? slideCount - 1 : previousInt;
}
if (!infinite) {
targetSlide = previousTargetSlide - slidesToScroll;
if (!infinite && targetSlide < 0) {
targetSlide = 0;
}
} else if (options.message === "next") {
slideOffset = indexOffset === 0 ? slidesToScroll : indexOffset;
Expand All @@ -292,8 +291,8 @@ export const changeSlide = (spec, options) => {
targetSlide =
((currentSlide + slidesToScroll) % slideCount) + indexOffset;
}
if (!infinite) {
targetSlide = previousTargetSlide + slidesToScroll;
if (!infinite && targetSlide >= slideCount) {
targetSlide = slideCount - 1;
}
} else if (options.message === "dots") {
// Click on dots
Expand Down Expand Up @@ -386,9 +385,12 @@ export const swipeMove = (e, spec) => {
let touchSwipeLength = touchObject.swipeLength;
if (!infinite) {
if (
(currentSlide === 0 && (swipeDirection === "right" || swipeDirection === "down")) ||
(currentSlide + 1 >= dotCount && (swipeDirection === "left" || swipeDirection === "up")) ||
(!canGoNext(spec) && (swipeDirection === "left" || swipeDirection === "up"))
(currentSlide === 0 &&
(swipeDirection === "right" || swipeDirection === "down")) ||
(currentSlide + 1 >= dotCount &&
(swipeDirection === "left" || swipeDirection === "up")) ||
(!canGoNext(spec) &&
(swipeDirection === "left" || swipeDirection === "up"))
) {
touchSwipeLength = touchObject.swipeLength * edgeFriction;
if (edgeDragged === false && onEdge) {
Expand Down