Skip to content

Solution to Multiple Post Clone Issue #2245

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

Open
wants to merge 2 commits 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
6 changes: 4 additions & 2 deletions __test__/jQSlickUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ function fetchDetails(keys) {
const slideObj = {
index: $(slide).attr("data-slick-index"),
value: $(slide)
.find("div")
.find("div")
.find("h3")
.text()
//.find("div")
// .find("div")
// .find("h3")
// .text()
};
allSlides.push(slideObj);
if ($(slide).hasClass("slick-current")) {
Expand Down
2 changes: 1 addition & 1 deletion __test__/mount/simple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const testsUtil = (settings, actions, keys) => {
expect(reactDetails.activeSlides).toEqual(jqDetails.activeSlides);
});

// Following two tests fail
// // Following two tests fail
test("checking cloned slides jQuery vs react", () => {
expect(reactDetails.clonedSlides.map(slide => slide.index)).toEqual(
jqDetails.clonedSlides.map(slide => slide.index)
Expand Down
4 changes: 2 additions & 2 deletions examples/__tests__/CentreMode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe("CenterMode Tests", () => {
let totalSlides = getSlidesCount(container);
let clonedSlides = getClonesCount(container);
let activeSlides = getActiveSlidesCount(container);
expect(totalSlides).toEqual(16);
expect(clonedSlides).toEqual(10);
expect(totalSlides).toEqual(14);
expect(clonedSlides).toEqual(8);
expect(activeSlides).toEqual(3);
//expect(beautify_html(toString(container))).toMatchSnapshot();
});
Expand Down
6 changes: 3 additions & 3 deletions examples/__tests__/MultipleItems.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import {
import MultipleItems from "../MultipleItems";

describe("Multiple Items", function() {
it("should have 9 actual slides and (3(pre) + 9(post)) clone slides", function() {
it("should have 9 actual slides and (3(pre) + 3(post)) clone slides", function() {
const { container } = render(<MultipleItems />);
expect(getSlidesCount(container)).toEqual(21);
expect(getClonesCount(container)).toEqual(12);
expect(getSlidesCount(container)).toEqual(15);
expect(getClonesCount(container)).toEqual(6);
//expect(beautify_html(toString(container))).toMatchSnapshot();
});
it("should have 3 active slides", function() {
Expand Down
8 changes: 4 additions & 4 deletions examples/__tests__/SimpleSlider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import {
} from "../../test-utils";

describe("SimpleSlider example", () => {
it("should have 13 slides (1(preclone) + 6(actual) + 6(postclone))", function() {
it("should have 13 slides (1(preclone) + 6(actual) + 1(postclone))", function() {
const { container } = render(<SimpleSlider a={5} />);
expect(container.getElementsByClassName("slick-slide").length).toBe(13);
expect(container.getElementsByClassName("slick-slide").length).toBe(8);
});
it("should have 7 clone slides", function() {
it("should have 3 clone slides", function() {
const { container } = render(<SimpleSlider />);
expect(container.getElementsByClassName("slick-cloned").length).toBe(7);
expect(container.getElementsByClassName("slick-cloned").length).toBe(2);
});
it("should have 1 current slide", function() {
const { container } = render(<SimpleSlider />);
Expand Down
4 changes: 2 additions & 2 deletions examples/__tests__/UnevenSets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ describe("UnevenSets Infinite", () => {
let clonedSlides = getClonesCount(container);
let activeSlides = getActiveSlidesCount(container);
let dots = getButtonsLength(container);
expect(totalSlides).toEqual(16);
expect(clonedSlides).toEqual(10);
expect(totalSlides).toEqual(14);
expect(clonedSlides).toEqual(8);
expect(activeSlides).toEqual(4);
expect(dots).toEqual(2);
// expect(beautify_html(toString(container))).toMatchSnapshot();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"react": "^18.0.0",
"react-dom": "^18.0.0",
"sinon": "^2.1.0",
"slick-carousel": "^1.8.1",
"slick-carousel": "^1.6.0",
"style-loader": "^0.16.1",
"uglifyjs-webpack-plugin": "^2.0.1",
"webpack": "^4.21.0",
Expand All @@ -84,6 +84,7 @@
"@testing-library/user-event": "^14.3.0",
"classnames": "^2.2.5",
"enquire.js": "^2.1.6",
"jquery": "^3.7.0",
"json2mq": "^0.2.0",
"lodash.debounce": "^4.0.8",
"resize-observer-polyfill": "^1.5.0"
Expand Down
15 changes: 10 additions & 5 deletions src/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import classnames from "classnames";
import {
lazyStartIndex,
lazyEndIndex,
getPreClones
getPreClones,
getPostClones
} from "./utils/innerSliderUtils";

// given specifications/props for a slide, fetch all the classes that need to be applied to the slide
Expand All @@ -18,7 +19,8 @@ const getSlideClasses = spec => {
} else {
index = spec.index;
}
slickCloned = index < 0 || index >= spec.slideCount;
slickCloned =
(index < spec.slidesToShow && index < 0) || index >= spec.slideCount;
if (spec.centerMode) {
centerOffset = Math.floor(spec.slidesToShow / 2);
slickCenter = (index - spec.currentSlide) % spec.slideCount === 0;
Expand Down Expand Up @@ -163,9 +165,12 @@ const renderSlides = spec => {
})
);
}

if (childrenCount !== spec.slidesToShow) {
key = childrenCount + index;
let postCloneNo = index;
if (
postCloneNo < getPostClones(spec) &&
childrenCount !== spec.slidesToShow
) {
key = postCloneNo + childrenCount;
if (key < endIndex) {
child = elem;
}
Expand Down
18 changes: 12 additions & 6 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 @@ -386,9 +386,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 Expand Up @@ -784,7 +787,10 @@ export const getPostClones = spec => {
if (spec.unslick || !spec.infinite) {
return 0;
}
return spec.slideCount;
if (spec.variableWidth) {
return spec.slideCount;
}
return spec.slidesToShow + (spec.centerMode ? 1 : 0);
};

export const getTotalSlides = spec =>
Expand Down