Skip to content

Commit

Permalink
Merge pull request phil-r#20 from avindra/master
Browse files Browse the repository at this point in the history
bump to es6
  • Loading branch information
phil-r committed Jan 4, 2016
2 parents 4d350da + 1a14964 commit 943ff20
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
40 changes: 19 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict';

var React = require('react-native');
var TimerMixin = require('react-timer-mixin');
const TimerMixin = require('react-timer-mixin');

var {
import React, {
StyleSheet,
Text,
ScrollView,
View
} = React;
} from 'react-native';


var PAGE_CHANGE_DELAY = 4000;
const PAGE_CHANGE_DELAY = 4000;

/**
* Animates pages in cycle
Expand All @@ -28,13 +27,13 @@ var Carousel = React.createClass({
onAnimateNextPage: React.PropTypes.func
},
mixins: [TimerMixin],
getDefaultProps: function() {
getDefaultProps() {
return {
delay: PAGE_CHANGE_DELAY,
autoplay: true
};
},
getInitialState: function() {
getInitialState() {
if (!!this.props.children) {
var childrenCount = this.props.children.length;
return {
Expand All @@ -50,15 +49,15 @@ var Carousel = React.createClass({
}
}
},
componentDidMount:function(){
componentDidMount(){
if (this.state.hasChildren) {
this._setUpTimer();
}
},
_onScrollBegin: function(event) {
_onScrollBegin(event) {
this.clearTimeout(this.timer);
},
_onScrollEnd: function(event) {
_onScrollEnd(event) {
this._setUpTimer();

var offset = Object.assign({}, event.nativeEvent.contentOffset);
Expand All @@ -74,7 +73,7 @@ var Carousel = React.createClass({
this._calculateCurrentPage(offset.x);
this.setState({contentOffset: offset});
},
_onLayout: function() {
_onLayout() {
let self = this;
this.refs.container.measure(function(x, y, w, h, px, py) {
self.setState({
Expand All @@ -83,14 +82,14 @@ var Carousel = React.createClass({
});
});
},
_setUpTimer: function() {
_setUpTimer() {
//only for cycling
if (this.props.autoplay && this.props.children.length > 1) {
this.clearTimeout(this.timer);
this.timer = this.setTimeout(this._animateNextPage, this.props.delay);
}
},
_animateNextPage: function() {
_animateNextPage() {
var k = this.state.currentPage;
var size = this.state.size;
k++;
Expand All @@ -103,7 +102,7 @@ var Carousel = React.createClass({
this.refs.scrollView.scrollTo(0, k*size.width);
this._setUpTimer();
},
_calculateCurrentPage: function(offset) {
_calculateCurrentPage(offset) {
var size = this.state.size;
var page = Math.floor((offset - size.width/2) / size.width) + 1;
this.setState({currentPage: page}, () => {
Expand All @@ -113,7 +112,7 @@ var Carousel = React.createClass({
});
},
//TODO: add optional `dots` for displaying current page (like pageControl)
render: function() {
render() {
var pages = [],
contents,
containerProps;
Expand Down Expand Up @@ -152,13 +151,12 @@ var Carousel = React.createClass({
pages.push(children[0]);
}

pages = pages.map((page, i) => {
return (
<View
pages = pages.map((page, i) =>
(<View
style={[{width: size.width, height: size.height}, this.props.pageStyle]}
key={"page"+i}>{page}</View>
);
});
key={"page"+i}>{page}
</View>);
);

contents = (
<ScrollView
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"license": "MIT",
"peerDependencies": {
"react-native": ">=0.13.0 <=0.17.0"
"react-native": ">=0.16.0 <=0.17.0"
},
"dependencies": {
"react-timer-mixin": "^0.13.3"
Expand Down

0 comments on commit 943ff20

Please sign in to comment.