Skip to content

Commit 1f7bbb1

Browse files
author
Vijay Sapkota
authored
Merge pull request #437 from brendan-rius/master
It is now possible to listen to index changes when user swipes
2 parents dfb9118 + 9dc6017 commit 1f7bbb1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ AppRegistry.registerComponent('myproject', () => swiper);
185185
| index | 0 | `number` | Index number of initial slide. |
186186
| showsButtons | false | `bool` | Set to `true` make control buttons visible. |
187187
| autoplay | false | `bool` | Set to `true` enable auto play mode. |
188+
| onIndexChanged | (index) => null | `func` | Called with the new index when the user swiped |
188189

189190
#### Custom basic style & content
190191

src/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ export default class extends Component {
125125
dotStyle: PropTypes.object,
126126
activeDotStyle: PropTypes.object,
127127
dotColor: PropTypes.string,
128-
activeDotColor: PropTypes.string
128+
activeDotColor: PropTypes.string,
129+
/**
130+
* Called when the index has changed because the user swiped.
131+
*/
132+
onIndexChanged: PropTypes.func
129133
}
130134

131135
/**
@@ -150,7 +154,8 @@ export default class extends Component {
150154
autoplay: false,
151155
autoplayTimeout: 2.5,
152156
autoplayDirection: true,
153-
index: 0
157+
index: 0,
158+
onIndexChanged: () => null
154159
}
155160

156161
/**
@@ -180,6 +185,11 @@ export default class extends Component {
180185
this.loopJumpTimer && clearTimeout(this.loopJumpTimer)
181186
}
182187

188+
componentWillUpdate (nextProps, nextState) {
189+
// If the index has changed, we notify the parent via the onIndexChanged callback
190+
if (this.state.index !== nextState.index) this.props.onIndexChanged(nextState.index)
191+
}
192+
183193
initState (props) {
184194
// set the current state
185195
const state = this.state || {}

0 commit comments

Comments
 (0)