Skip to content

Commit

Permalink
Merge pull request #2331 from akiran/fix-2315
Browse files Browse the repository at this point in the history
Fixed #2315
  • Loading branch information
akiran authored Jan 26, 2024
2 parents 2420174 + 02629d5 commit 99adf6f
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 10 deletions.
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
trailingComma: none
35 changes: 35 additions & 0 deletions __tests__/regression/fix-2315.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Test fix of #2315: Slider crashing after a state change in parent component
import React from "react";
import { render, fireEvent } from "@testing-library/react";

import { getCurrentSlideContent, getSlidesCount } from "../../test-utils";
import { GenericSliderComponent } from "../TestComponents";

function TestSlider() {
const [count, setCount] = React.useState();

return (
<div>
<button className="increment-button" onClick={() => setCount(count + 1)}>
Increment {count}
</button>
<GenericSliderComponent slidesCount={6} settings={{ count }} />
</div>
);
}

describe("State change in parent component of slider", () => {
it("Slider shoud work afer clicking on Increment button", function() {
const { container } = render(<TestSlider />);
fireEvent(
container.getElementsByClassName("increment-button")[0],
new MouseEvent("click", {
bubbles: true,
cancelable: true
})
);
// Throws an error "Maximum update depth exceeded." if the bug exists
expect(getCurrentSlideContent(container)).toEqual("1");
expect(getSlidesCount(container)).toEqual(13);
});
});
18 changes: 18 additions & 0 deletions __tests__/utils/filterSettings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { filterSettings } from "../../src/utils/innerSliderUtils";

describe("filterSettings", () => {
it("returns empty object if there are no valid settings", () => {
expect(filterSettings({})).toEqual({});
expect(filterSettings({ age: 10 })).toEqual({});
});
it("return an object with valid settings and omits extra properties", () => {
expect(filterSettings({ arrows: true, dots: true })).toEqual({
arrows: true,
dots: true
});
expect(filterSettings({ arrows: true, dots: true, age: 10 })).toEqual({
arrows: true,
dots: true
});
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"gen": "node examples/scripts/generateExampleConfigs.js && node examples/scripts/generateExamples.js && xdg-open docs/jquery.html",
"precommit": "lint-staged",
"test": "jest",
"test-watch": "jest --watch"
"test-watch": "jest --watch",
"clear-jest": "jest --clearCache"
},
"author": "Kiran Abburi",
"license": "MIT",
Expand Down
3 changes: 2 additions & 1 deletion src/inner-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export class InnerSlider extends React.Component {
}
if (
typeof prevProps[key] === "object" ||
typeof prevProps[key] === "function"
typeof prevProps[key] === "function" ||
isNaN(prevProps[key])
) {
continue;
}
Expand Down
9 changes: 6 additions & 3 deletions src/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from "react";
import { InnerSlider } from "./inner-slider";
import json2mq from "json2mq";
import defaultProps from "./default-props";
import { canUseDOM } from "./utils/innerSliderUtils";
import { canUseDOM, filterSettings } from "./utils/innerSliderUtils";
const enquire = canUseDOM() && require("enquire.js");

export default class Slider extends React.Component {
Expand Down Expand Up @@ -198,14 +198,17 @@ export default class Slider extends React.Component {
if (settings === "unslick") {
const className = "regular slider " + (this.props.className || "");
return <div className={className}>{children}</div>;
} else if (newChildren.length <= settings.slidesToShow && !settings.infinite) {
} else if (
newChildren.length <= settings.slidesToShow &&
!settings.infinite
) {
settings.unslick = true;
}
return (
<InnerSlider
style={this.props.style}
ref={this.innerSliderRefHandler}
{...settings}
{...filterSettings(settings)}
>
{newChildren}
</InnerSlider>
Expand Down
25 changes: 20 additions & 5 deletions src/utils/innerSliderUtils.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React from "react";
import defaultProps from "../default-props";

export function clamp(number, lowerBound, upperBound) {
return Math.max(lowerBound, Math.min(number, 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 +387,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 @@ -849,3 +853,14 @@ export const canUseDOM = () =>
window.document &&
window.document.createElement
);

export const validSettings = Object.keys(defaultProps);

export function filterSettings(settings) {
return validSettings.reduce((acc, settingName) => {
if (settings.hasOwnProperty(settingName)) {
acc[settingName] = settings[settingName];
}
return acc;
}, {});
}

0 comments on commit 99adf6f

Please sign in to comment.