Skip to content

Commit 57a9116

Browse files
authored
fix: height style issue (#107)
* fix: style issue when the number of slides is less than or equal to slidesToShow * fix: only modify height & fix wrong cloned element * feat: add tests
1 parent 3343a0d commit 57a9116

File tree

5 files changed

+145
-3
lines changed

5 files changed

+145
-3
lines changed

docs/demos.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import CustomArrows from "../examples/CustomArrows";
2626
import PreviousNextMethods from "../examples/PreviousNextMethods";
2727
import DynamicSlides from "../examples/DynamicSlides";
2828
import VerticalMode from "../examples/VerticalMode";
29+
import VerticalModeWhenUnslick from "../examples/VerticalModeWhenUnslick";
2930
import SwipeToSlide from "../examples/SwipeToSlide";
3031
import VerticalSwipeToSlide from "../examples/VerticalSwipeToSlide";
3132
import CustomPaging from "../examples/CustomPaging";
@@ -62,6 +63,7 @@ export default class App extends React.Component {
6263
<PreviousNextMethods />
6364
<DynamicSlides />
6465
<VerticalMode />
66+
<VerticalModeWhenUnslick />
6567
<SwipeToSlide />
6668
<VerticalSwipeToSlide />
6769
<AsNavFor />

examples/VerticalModeWhenUnslick.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React, { Component } from "react";
2+
import Slider from "../src/slider";
3+
4+
export default class VerticalModeWhenUnslick extends Component {
5+
render() {
6+
const settings = {
7+
dots: true,
8+
infinite: true,
9+
slidesToShow: 3,
10+
slidesToScroll: 1,
11+
vertical: true,
12+
verticalSwiping: true,
13+
beforeChange: function (currentSlide, nextSlide) {
14+
console.log("before change", currentSlide, nextSlide);
15+
},
16+
afterChange: function (currentSlide) {
17+
console.log("after change", currentSlide);
18+
},
19+
};
20+
return (
21+
<div>
22+
<h2>Vertical Mode When Unslick</h2>
23+
<Slider {...settings}>
24+
<div>
25+
<h3>1</h3>
26+
</div>
27+
<div>
28+
<h3>2</h3>
29+
</div>
30+
</Slider>
31+
</div>
32+
);
33+
}
34+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import React from "react";
2+
import { mount } from "enzyme";
3+
4+
import VerticalMode from "../VerticalMode";
5+
import VerticalModeWhenUnslick from "../VerticalModeWhenUnslick";
6+
import Slider from "../../src/slider";
7+
8+
describe("Vertical Mode", function () {
9+
it("should have 15 slides (3(preclone) + 6(actual) + 6(postclone))", function () {
10+
const wrapper = mount(<VerticalMode />);
11+
expect(wrapper.find(".slick-slide").length).toEqual(15);
12+
});
13+
it("should have 9 clone slides", function () {
14+
const wrapper = mount(<VerticalMode />);
15+
expect(wrapper.find(".slick-cloned").length).toEqual(9);
16+
});
17+
it("should have 3 active slide", function () {
18+
const wrapper = mount(<VerticalMode />);
19+
expect(wrapper.find(".slick-slide.slick-active").length).toEqual(3);
20+
});
21+
it("should have 6 dots", function () {
22+
const wrapper = mount(<VerticalMode />);
23+
expect(wrapper.find(".slick-dots").children().length).toEqual(6);
24+
});
25+
it("should have 1 active dot", function () {
26+
const wrapper = mount(<VerticalMode />);
27+
expect(wrapper.find(".slick-dots .slick-active").length).toEqual(1);
28+
});
29+
it("should have a prev arrow", function () {
30+
const wrapper = mount(<VerticalMode />);
31+
expect(wrapper.find(".slick-prev").length).toEqual(1);
32+
});
33+
it("should have a next arrow", function () {
34+
const wrapper = mount(<VerticalMode />);
35+
expect(wrapper.find(".slick-next").length).toEqual(1);
36+
});
37+
});
38+
39+
describe("Vertical Mode when Unslick", function () {
40+
// ref: https://github.com/enzymejs/enzyme/issues/1940
41+
// fix the 'offsetHeight' to 100px
42+
const originalOffsetHeight = Object.getOwnPropertyDescriptor(
43+
HTMLElement.prototype,
44+
"offsetHeight"
45+
);
46+
beforeAll(() => {
47+
Object.defineProperty(HTMLElement.prototype, "offsetHeight", {
48+
configurable: true,
49+
value: 100,
50+
});
51+
});
52+
afterAll(() => {
53+
Object.defineProperty(
54+
HTMLElement.prototype,
55+
"offsetHeight",
56+
originalOffsetHeight
57+
);
58+
});
59+
60+
it("should render correct height when only child", function () {
61+
const wrapper = mount(
62+
<Slider vertical>
63+
<div>
64+
<h3>1</h3>
65+
</div>
66+
</Slider>
67+
);
68+
69+
const slides = wrapper.find(".slick-slide");
70+
const firstSlideHeight = slides.first().getDOMNode().offsetHeight;
71+
const totalHeight = parseInt(firstSlideHeight) * slides.length;
72+
const sliderTrackHeight = getComputedStyle(
73+
wrapper.find(".slick-track").getDOMNode()
74+
).height;
75+
76+
expect(totalHeight).toEqual(parseInt(sliderTrackHeight));
77+
});
78+
79+
it("should render correct height when unslick", function () {
80+
const wrapper = mount(<VerticalModeWhenUnslick />);
81+
82+
const slides = wrapper.find(".slick-slide");
83+
const firstSlideHeight = slides.first().getDOMNode().offsetHeight;
84+
const totalHeight = parseInt(firstSlideHeight) * slides.length;
85+
const sliderTrackHeight = getComputedStyle(
86+
wrapper.find(".slick-track").getDOMNode()
87+
).height;
88+
89+
expect(totalHeight).toEqual(parseInt(sliderTrackHeight));
90+
});
91+
92+
it("should have 2 slides (0(preclone) + 2(actual) + 0(postclone))", function () {
93+
const wrapper = mount(<VerticalModeWhenUnslick />);
94+
expect(wrapper.find(".slick-slide").length).toEqual(2);
95+
});
96+
it("should have 0 clone slides", function () {
97+
const wrapper = mount(<VerticalModeWhenUnslick />);
98+
expect(wrapper.find(".slick-cloned").length).toEqual(0);
99+
});
100+
it("should have 2 active slide", function () {
101+
const wrapper = mount(<VerticalModeWhenUnslick />);
102+
expect(wrapper.find(".slick-slide.slick-active").length).toEqual(2);
103+
});
104+
});

src/track.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ const renderSlides = (spec) => {
135135
);
136136

137137
// if slide needs to be precloned or postcloned
138-
if (spec.infinite && spec.fade === false) {
138+
if (spec.infinite && spec.fade === false && !spec.unslick) {
139139
let preCloneNo = childrenCount - index;
140140
if (
141141
preCloneNo <= getPreClones(spec) &&

src/utils/innerSliderUtils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function clamp(number, lowerBound, upperBound) {
66

77
export const safePreventDefault = event => {
88
const passiveEvents = ["onTouchStart", "onTouchMove", "onWheel"];
9-
if(!passiveEvents.includes(event._reactName)) {
9+
if (!passiveEvents.includes(event._reactName)) {
1010
event.preventDefault();
1111
}
1212
}
@@ -591,10 +591,12 @@ export const getTrackCSS = spec => {
591591
"slideWidth"
592592
]);
593593
let trackWidth, trackHeight;
594-
const trackChildren = spec.slideCount + 2 * spec.slidesToShow;
595594
if (!spec.vertical) {
596595
trackWidth = getTotalSlides(spec) * spec.slideWidth;
597596
} else {
597+
const trackChildren = spec.unslick
598+
? spec.slideCount
599+
: spec.slideCount + 2 * spec.slidesToShow;
598600
trackHeight = trackChildren * spec.slideHeight;
599601
}
600602
let style = {

0 commit comments

Comments
 (0)