Skip to content

Commit f8ae414

Browse files
authored
Merge pull request #1 from phattranky/feature/add_custom_slide_left_callback
Feature/add custom slide left callback
2 parents beba32f + 136c9b7 commit f8ae414

File tree

6 files changed

+92
-6
lines changed

6 files changed

+92
-6
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55

66
Carousel component built with React. It is a react port of [slick carousel](http://kenwheeler.github.io/slick/)
77

8+
Extend from https://github.com/akiran/react-slick
9+
### Additionals:
10+
* Prop: customTargetSlideLeft for control target slide left offset
11+
```
12+
handleCustomTagetSlideLeft(targetLeft, currentSlide, targetSlide) {
13+
return (targetLeft + 100);
14+
}
15+
```
16+
* customCurrentSlideLeft: same as target
17+
* onInitTargetLeft for control target slide left on init
18+
```
19+
onInitTargetLeft(targetLeft, slideCount, slideWidth, trackWidth) {
20+
return (targetLeft + 100);
21+
}
22+
```
23+
824
### Important
925
### Breaking changes in [email protected]
1026
* slickGoTo prop is deprecated in favor of slickGoTo method. Check this [slickGoTo usage example](https://github.com/akiran/react-slick/blob/master/examples/SlickGoTo.js).

docs/demos.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Responsive from '../examples/Responsive'
1010
import UnevenSetsInfinite from '../examples/UnevenSetsInfinite'
1111
import UnevenSetsFinite from '../examples/UnevenSetsFinite'
1212
import CenterMode from '../examples/CenterMode'
13+
import CenterModeWithFiniteCustomLeft from '../examples/CenterModeWithFiniteCustomLeft'
1314
import FocusOnSelect from '../examples/FocusOnSelect'
1415
import AutoPlay from '../examples/AutoPlay'
1516
import PauseOnHover from '../examples/PauseOnHover'
@@ -37,6 +38,7 @@ export default class App extends React.Component {
3738
<UnevenSetsInfinite />
3839
<UnevenSetsFinite />
3940
<CenterMode />
41+
<CenterModeWithFiniteCustomLeft />
4042
<FocusOnSelect />
4143
<AutoPlay />
4244
<PauseOnHover />
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, { Component } from 'react'
2+
import Slider from '../src/slider'
3+
4+
export default class CenterModeWithFiniteCustomLeft extends Component {
5+
handleCustomTagetSlideLeft(targetLeft, currentSlide, targetSlide) {
6+
return (targetLeft + 100);
7+
}
8+
9+
handleCustomCurrentSlideLeft(currentLeft) {
10+
return (currentLeft + 100);
11+
}
12+
13+
onInitTargetLeft(targetLeft, slideCount, slideWidth, trackWidth) {
14+
return 0;
15+
}
16+
17+
render() {
18+
const settings = {
19+
className: 'center',
20+
centerMode: true,
21+
infinite: false,
22+
centerPadding: 0,
23+
variableWidth: true,
24+
onInitTargetLeft: this.onInitTargetLeft,
25+
customTargetSlideLeft: this.handleCustomTagetSlideLeft,
26+
customCurrentSlideLeft: this.handleCustomCurrentSlideLeft,
27+
speed: 500
28+
};
29+
return (
30+
<div>
31+
<h2>Center Mode (finite) - Custom Slide Left - Move left 100 each step</h2>
32+
<div style={{width: 520}}>
33+
34+
<Slider {...settings}>
35+
<div style={{width: 240}}><h3>1</h3></div>
36+
<div style={{width: 240}}><h3>2</h3></div>
37+
<div style={{width: 240}}><h3>3</h3></div>
38+
<div style={{width: 240}}><h3>4</h3></div>
39+
<div style={{width: 240}}><h3>5</h3></div>
40+
<div style={{width: 240}}><h3>6</h3></div>
41+
<div style={{width: 240}}><h3>7</h3></div>
42+
<div style={{width: 240}}><h3>8</h3></div>
43+
<div style={{width: 240}}><h3>9</h3></div>
44+
</Slider>
45+
</div>
46+
</div>
47+
);
48+
}
49+
}

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "react-slick",
3-
"version": "0.14.3",
2+
"name": "react-slick-carousel",
3+
"version": "0.14.4",
44
"description": " React port of slick carousel",
55
"main": "./lib",
66
"scripts": {
@@ -9,11 +9,11 @@
99
"test": "karma start --single-run",
1010
"dev-test": "karma start"
1111
},
12-
"author": "Kiran Abburi",
12+
"author": "Phat Tran Ky",
1313
"license": "MIT",
1414
"repository": {
1515
"type": "git",
16-
"url": "https://github.com/akiran/react-slick"
16+
"url": "https://github.com/phattranky/react-slick-carousel"
1717
},
1818
"keywords": [
1919
"slick",
@@ -89,7 +89,7 @@
8989
}
9090
],
9191
"bugs": {
92-
"url": "https://github.com/akiran/react-slick/issues"
92+
"url": "https://github.com/phattranky/react-slick-carousel/issues"
9393
},
94-
"homepage": "https://github.com/akiran/react-slick"
94+
"homepage": "https://github.com/phattranky/react-slick-carousel"
9595
}

src/default-props.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ var defaultProps = {
4040
waitForAnimate: true,
4141
afterChange: null,
4242
beforeChange: null,
43+
customTargetSlideLeft: null,
44+
customCurrentSlideLeft: null,
45+
onInitTargetLeft: null,
4346
edgeEvent: null,
4447
init: null,
4548
swipeEvent: null,

src/mixins/helpers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ var helpers = {
4040
trackRef: this.track
4141
}, props, this.state));
4242
// getCSS function needs previously set state
43+
44+
if (this.props.onInitTargetLeft) {
45+
targetLeft = this.props.onInitTargetLeft(targetLeft, slideCount, slideWidth, trackWidth);
46+
}
4347
var trackStyle = getTrackCSS(assign({left: targetLeft}, props, this.state));
4448

4549
this.setState({trackStyle: trackStyle});
@@ -225,6 +229,10 @@ var helpers = {
225229

226230
if (this.props.useCSS === false) {
227231

232+
if(this.props.customCurrentSlideLeft) {
233+
currentLeft = this.props.customCurrentSlideLeft(currentLeft, currentSlide, targetSlide);
234+
}
235+
228236
this.setState({
229237
currentSlide: currentSlide,
230238
trackStyle: getTrackCSS(assign({left: currentLeft}, this.props, this.state))
@@ -236,6 +244,10 @@ var helpers = {
236244

237245
} else {
238246

247+
if(this.props.customCurrentSlideLeft) {
248+
currentLeft = this.props.customCurrentSlideLeft(currentLeft, currentSlide, targetSlide);
249+
}
250+
239251
var nextStateChanges = {
240252
animating: false,
241253
currentSlide: currentSlide,
@@ -251,6 +263,10 @@ var helpers = {
251263
delete this.animationEndCallback;
252264
};
253265

266+
if(this.props.customTargetSlideLeft) {
267+
targetLeft = this.props.customTargetSlideLeft(targetLeft, currentSlide, targetSlide);
268+
}
269+
254270
this.setState({
255271
animating: true,
256272
currentSlide: currentSlide,

0 commit comments

Comments
 (0)