Skip to content

Commit 14a6f90

Browse files
committed
extract canGoNext to arrow and helper
1 parent 87f43ca commit 14a6f90

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

src/arrows.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import React from 'react';
44
import classnames from 'classnames';
5+
import Helpers from './mixins/helpers';
56

67
export var PrevArrow = React.createClass({
78

@@ -47,24 +48,11 @@ export var NextArrow = React.createClass({
4748
var nextClasses = {'slick-arrow': true, 'slick-next': true};
4849
var nextHandler = this.clickHandler.bind(this, {message: 'next'});
4950

50-
if (!this.props.infinite) {
51-
if (this.props.centerMode) {
52-
// check if current slide is last slide
53-
if (this.props.currentSlide >= (this.props.slideCount - 1)) {
54-
nextClasses['slick-disabled'] = true;
55-
nextHandler = null;
56-
}
57-
} else {
58-
// check if all slides are shown in slider
59-
if (this.props.slideCount <= this.props.slidesToShow ||
60-
this.props.currentSlide >= (this.props.slideCount - this.props.slidesToShow)) {
61-
nextClasses['slick-disabled'] = true;
62-
nextHandler = null;
63-
}
64-
}
51+
if (!Helpers.canGoNext(this.props)) {
52+
nextClasses['slick-disabled'] = true;
53+
nextHandler = null;
6554
}
6655

67-
6856
var nextArrowProps = {
6957
key: '1',
7058
'data-role': 'none',

src/mixins/helpers.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ var helpers = {
103103
}
104104
}
105105
},
106+
canGoNext: function (opts){
107+
var canGo = true;
108+
if (!opts.infinite) {
109+
if (opts.centerMode) {
110+
// check if current slide is last slide
111+
if (opts.currentSlide >= (opts.slideCount - 1)) {
112+
canGo = false;
113+
}
114+
} else {
115+
// check if all slides are shown in slider
116+
if (opts.slideCount <= opts.slidesToShow ||
117+
opts.currentSlide >= (opts.slideCount - opts.slidesToShow)) {
118+
canGo = false;
119+
}
120+
}
121+
}
122+
return canGo;
123+
},
106124
slideHandler: function (index) {
107125
// Functionality of animateSlide and postSlide is merged into this function
108126
// console.log('slideHandler', index);
@@ -121,7 +139,7 @@ var helpers = {
121139
if(this.props.infinite === false &&
122140
(index < 0 || index >= this.state.slideCount)) {
123141
return;
124-
}
142+
}
125143

126144
// Shifting targetSlide back into the range
127145
if (index < 0) {
@@ -290,21 +308,32 @@ var helpers = {
290308

291309
return 'vertical';
292310
},
311+
play: function(){
312+
var nextIndex;
313+
314+
if (!this.state.mounted) {
315+
return false
316+
}
317+
318+
if (this.props.rtl) {
319+
nextIndex = this.state.currentSlide - this.props.slidesToScroll;
320+
} else {
321+
if (this.canGoNext(Object.assign({}, this.props,this.state))) {
322+
nextIndex = this.state.currentSlide + this.props.slidesToScroll;
323+
} else {
324+
return false;
325+
}
326+
}
327+
328+
this.slideHandler(nextIndex);
329+
},
293330
autoPlay: function () {
294331
if (this.state.autoPlayTimer) {
295332
return;
296333
}
297-
var play = () => {
298-
if (this.state.mounted) {
299-
var nextIndex = this.props.rtl ?
300-
this.state.currentSlide - this.props.slidesToScroll:
301-
this.state.currentSlide + this.props.slidesToScroll;
302-
this.slideHandler(nextIndex);
303-
}
304-
};
305334
if (this.props.autoplay) {
306335
this.setState({
307-
autoPlayTimer: setInterval(play, this.props.autoplaySpeed)
336+
autoPlayTimer: setInterval(this.play, this.props.autoplaySpeed)
308337
});
309338
}
310339
},

0 commit comments

Comments
 (0)