From baadedaaf2cf79eb23d0edb41b42ac190f4eff28 Mon Sep 17 00:00:00 2001 From: Momen Zalabany Date: Mon, 3 Oct 2016 12:09:06 +0200 Subject: [PATCH 1/3] added selectedPage prop this prop is ignored on constructor, yet if it changes. it will trigger this.goToPage. this allow to trigger page change from other parent component after initialization. --- ViewPager.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ViewPager.js b/ViewPager.js index dba9b45..2e856d0 100644 --- a/ViewPager.js +++ b/ViewPager.js @@ -37,6 +37,7 @@ var ViewPager = React.createClass({ PropTypes.func, PropTypes.bool ]), + selectedPage:PropTypes.number, isLoop: PropTypes.bool, locked: PropTypes.bool, autoPlay: PropTypes.bool, @@ -141,20 +142,25 @@ var ViewPager = React.createClass({ this._autoPlayer = null; } } - + var controledPageChanged = Boolean(nextProps.selectedPage && nextProps.selectedPage !== this.state.currentPage); if (nextProps.dataSource) { var maxPage = nextProps.dataSource.getPageCount() - 1; - var constrainedPage = Math.max(0, Math.min(this.state.currentPage, maxPage)); - this.setState({ - currentPage: constrainedPage, - }); - + var constrainedPage = Math.max(0, Math.min(controledPageChanged ? nextProps.selectedPage : this.state.currentPage, maxPage)); + if (!nextProps.isLoop) { this.state.scrollValue.setValue(constrainedPage > 0 ? 1 : 0); } this.childIndex = Math.min(this.childIndex, constrainedPage); this.fling = false; + + if (controledPageChanged) { + this.goToPage( nextProps.selectedPage ); + } else { + this.setState({ + currentPage: constrainedPage, + }); + } } }, From ad17cfcf5a5da51f2edc05a4425525ed5256cd73 Mon Sep 17 00:00:00 2001 From: Momen Zalabany Date: Mon, 3 Oct 2016 12:14:32 +0200 Subject: [PATCH 2/3] selectedPage doc --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2709a53..03bcaf4 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ var ViewPager = require('react-native-viewpager'); this.setState({currentPage:page})} //sync parent state with component state; animation = {(animatedValue, toValue, gestureState) => { // Use the horizontal velocity of the swipe gesture // to affect the length of the transition so the faster you swipe From e183b5ff571fe2430d36cb8ea039a116f48ddeaa Mon Sep 17 00:00:00 2001 From: Momen Zalabany Date: Mon, 3 Oct 2016 12:15:19 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 03bcaf4..2f306cd 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ var ViewPager = require('react-native-viewpager'); * **`onChangePage`**: page change callback, * **`renderPageIndicator`**: render custom ViewPager indicator. * **`initialPage`**: show initially some other page than first page. +* **`selectedPage`**: allow parent component to change page of pager. ## Page Transition Animation Controls