2015-10-07 02:43:31 +00:00
|
|
|
/**
|
2016-04-08 01:08:29 +00:00
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-10-07 02:43:31 +00:00
|
|
|
*
|
|
|
|
* @providesModule ViewPagerAndroid
|
2015-11-13 16:03:54 +00:00
|
|
|
* @flow
|
2015-10-07 02:43:31 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('React');
|
2016-04-08 02:43:49 +00:00
|
|
|
var ReactNative = require('ReactNative');
|
2015-10-07 02:43:31 +00:00
|
|
|
var ReactElement = require('ReactElement');
|
|
|
|
var ReactPropTypes = require('ReactPropTypes');
|
2015-11-27 13:39:00 +00:00
|
|
|
var UIManager = require('UIManager');
|
2015-11-18 16:24:26 +00:00
|
|
|
var View = require('View');
|
2015-10-07 02:43:31 +00:00
|
|
|
|
|
|
|
var dismissKeyboard = require('dismissKeyboard');
|
2015-11-18 16:24:26 +00:00
|
|
|
var requireNativeComponent = require('requireNativeComponent');
|
2015-10-07 02:43:31 +00:00
|
|
|
|
|
|
|
var VIEWPAGER_REF = 'viewPager';
|
|
|
|
|
2016-01-29 09:16:40 +00:00
|
|
|
type Event = Object;
|
|
|
|
|
|
|
|
export type ViewPagerScrollState = $Enum<{
|
|
|
|
idle: string;
|
|
|
|
dragging: string;
|
|
|
|
settling: string;
|
|
|
|
}>;
|
|
|
|
|
2015-10-07 02:43:31 +00:00
|
|
|
/**
|
|
|
|
* Container that allows to flip left and right between child views. Each
|
|
|
|
* child view of the `ViewPagerAndroid` will be treated as a separate page
|
2015-12-15 17:08:39 +00:00
|
|
|
* and will be stretched to fill the `ViewPagerAndroid`.
|
2015-10-07 02:43:31 +00:00
|
|
|
*
|
|
|
|
* It is important all children are `<View>`s and not composite components.
|
|
|
|
* You can set style properties like `padding` or `backgroundColor` for each
|
|
|
|
* child.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* render: function() {
|
|
|
|
* return (
|
|
|
|
* <ViewPagerAndroid
|
|
|
|
* style={styles.viewPager}
|
|
|
|
* initialPage={0}>
|
|
|
|
* <View style={styles.pageStyle}>
|
|
|
|
* <Text>First page</Text>
|
|
|
|
* </View>
|
|
|
|
* <View style={styles.pageStyle}>
|
|
|
|
* <Text>Second page</Text>
|
|
|
|
* </View>
|
|
|
|
* </ViewPagerAndroid>
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ...
|
|
|
|
*
|
|
|
|
* var styles = {
|
|
|
|
* ...
|
|
|
|
* pageStyle: {
|
|
|
|
* alignItems: 'center',
|
|
|
|
* padding: 20,
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
var ViewPagerAndroid = React.createClass({
|
|
|
|
|
|
|
|
propTypes: {
|
2015-11-18 16:24:26 +00:00
|
|
|
...View.propTypes,
|
2015-10-07 02:43:31 +00:00
|
|
|
/**
|
|
|
|
* Index of initial page that should be selected. Use `setPage` method to
|
|
|
|
* update the page, and `onPageSelected` to monitor page changes
|
|
|
|
*/
|
|
|
|
initialPage: ReactPropTypes.number,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executed when transitioning between pages (ether because of animation for
|
|
|
|
* the requested page change or when user is swiping/dragging between pages)
|
|
|
|
* The `event.nativeEvent` object for this callback will carry following data:
|
|
|
|
* - position - index of first page from the left that is currently visible
|
|
|
|
* - offset - value from range [0,1) describing stage between page transitions.
|
|
|
|
* Value x means that (1 - x) fraction of the page at "position" index is
|
|
|
|
* visible, and x fraction of the next page is visible.
|
|
|
|
*/
|
|
|
|
onPageScroll: ReactPropTypes.func,
|
|
|
|
|
2016-01-29 09:16:40 +00:00
|
|
|
/**
|
|
|
|
* Function called when the page scrolling state has changed.
|
|
|
|
* The page scrolling state can be in 3 states:
|
|
|
|
* - idle, meaning there is no interaction with the page scroller happening at the time
|
|
|
|
* - dragging, meaning there is currently an interaction with the page scroller
|
|
|
|
* - settling, meaning that there was an interaction with the page scroller, and the
|
|
|
|
* page scroller is now finishing it's closing or opening animation
|
|
|
|
*/
|
|
|
|
onPageScrollStateChanged: ReactPropTypes.func,
|
|
|
|
|
2015-10-07 02:43:31 +00:00
|
|
|
/**
|
2015-12-15 17:08:39 +00:00
|
|
|
* This callback will be called once ViewPager finish navigating to selected page
|
2015-10-07 02:43:31 +00:00
|
|
|
* (when user swipes between pages). The `event.nativeEvent` object passed to this
|
|
|
|
* callback will have following fields:
|
|
|
|
* - position - index of page that has been selected
|
|
|
|
*/
|
|
|
|
onPageSelected: ReactPropTypes.func,
|
|
|
|
|
2016-04-08 01:08:29 +00:00
|
|
|
/**
|
|
|
|
* Blank space to show between pages. This is only visible while scrolling, pages are still
|
|
|
|
* edge-to-edge.
|
|
|
|
*/
|
|
|
|
pageMargin: ReactPropTypes.number,
|
|
|
|
|
2015-10-07 02:43:31 +00:00
|
|
|
/**
|
|
|
|
* Determines whether the keyboard gets dismissed in response to a drag.
|
|
|
|
* - 'none' (the default), drags do not dismiss the keyboard.
|
|
|
|
* - 'on-drag', the keyboard is dismissed when a drag begins.
|
|
|
|
*/
|
|
|
|
keyboardDismissMode: ReactPropTypes.oneOf([
|
|
|
|
'none', // default
|
|
|
|
'on-drag',
|
|
|
|
]),
|
2016-04-22 02:42:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When false, the content does not scroll.
|
|
|
|
* The default value is true.
|
|
|
|
*/
|
2016-05-11 23:50:12 +00:00
|
|
|
scrollEnabled: ReactPropTypes.bool,
|
2015-10-07 02:43:31 +00:00
|
|
|
},
|
|
|
|
|
2015-11-13 16:03:54 +00:00
|
|
|
componentDidMount: function() {
|
|
|
|
if (this.props.initialPage) {
|
|
|
|
this.setPageWithoutAnimation(this.props.initialPage);
|
|
|
|
}
|
2015-10-07 02:43:31 +00:00
|
|
|
},
|
|
|
|
|
2015-11-13 16:03:54 +00:00
|
|
|
getInnerViewNode: function(): ReactComponent {
|
2015-10-07 02:43:31 +00:00
|
|
|
return this.refs[VIEWPAGER_REF].getInnerViewNode();
|
|
|
|
},
|
|
|
|
|
2015-11-13 16:03:54 +00:00
|
|
|
_childrenWithOverridenStyle: function(): Array {
|
2015-10-07 02:43:31 +00:00
|
|
|
// Override styles so that each page will fill the parent. Native component
|
|
|
|
// will handle positioning of elements, so it's not important to offset
|
|
|
|
// them correctly.
|
|
|
|
return React.Children.map(this.props.children, function(child) {
|
2016-01-18 21:56:19 +00:00
|
|
|
if (!child) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-10-07 02:43:31 +00:00
|
|
|
var newProps = {
|
|
|
|
...child.props,
|
|
|
|
style: [child.props.style, {
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
width: undefined,
|
|
|
|
height: undefined,
|
|
|
|
}],
|
|
|
|
collapsable: false,
|
|
|
|
};
|
2015-11-05 12:51:12 +00:00
|
|
|
if (child.type &&
|
|
|
|
child.type.displayName &&
|
|
|
|
(child.type.displayName !== 'RCTView') &&
|
|
|
|
(child.type.displayName !== 'View')) {
|
2015-10-07 02:43:31 +00:00
|
|
|
console.warn('Each ViewPager child must be a <View>. Was ' + child.type.displayName);
|
|
|
|
}
|
|
|
|
return ReactElement.createElement(child.type, newProps);
|
|
|
|
});
|
|
|
|
},
|
2015-11-13 16:03:54 +00:00
|
|
|
|
|
|
|
_onPageScroll: function(e: Event) {
|
2015-10-07 02:43:31 +00:00
|
|
|
if (this.props.onPageScroll) {
|
2015-11-13 16:03:54 +00:00
|
|
|
this.props.onPageScroll(e);
|
2015-10-07 02:43:31 +00:00
|
|
|
}
|
|
|
|
if (this.props.keyboardDismissMode === 'on-drag') {
|
|
|
|
dismissKeyboard();
|
|
|
|
}
|
|
|
|
},
|
2015-11-13 16:03:54 +00:00
|
|
|
|
2016-01-29 09:16:40 +00:00
|
|
|
_onPageScrollStateChanged: function(e: Event) {
|
|
|
|
if (this.props.onPageScrollStateChanged) {
|
|
|
|
this.props.onPageScrollStateChanged(e.nativeEvent.pageScrollState);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-13 16:03:54 +00:00
|
|
|
_onPageSelected: function(e: Event) {
|
2015-10-07 02:43:31 +00:00
|
|
|
if (this.props.onPageSelected) {
|
2015-11-13 16:03:54 +00:00
|
|
|
this.props.onPageSelected(e);
|
2015-10-07 02:43:31 +00:00
|
|
|
}
|
|
|
|
},
|
2015-11-13 16:03:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function to scroll to a specific page in the ViewPager.
|
|
|
|
* The transition between pages will be animated.
|
|
|
|
*/
|
|
|
|
setPage: function(selectedPage: number) {
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.dispatchViewManagerCommand(
|
2016-04-08 02:43:49 +00:00
|
|
|
ReactNative.findNodeHandle(this),
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.AndroidViewPager.Commands.setPage,
|
2015-11-13 16:03:54 +00:00
|
|
|
[selectedPage],
|
|
|
|
);
|
2015-10-07 02:43:31 +00:00
|
|
|
},
|
2015-11-13 16:03:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function to scroll to a specific page in the ViewPager.
|
|
|
|
* The transition between pages will be *not* be animated.
|
|
|
|
*/
|
|
|
|
setPageWithoutAnimation: function(selectedPage: number) {
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.dispatchViewManagerCommand(
|
2016-04-08 02:43:49 +00:00
|
|
|
ReactNative.findNodeHandle(this),
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.AndroidViewPager.Commands.setPageWithoutAnimation,
|
2015-11-13 16:03:54 +00:00
|
|
|
[selectedPage],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-10-07 02:43:31 +00:00
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<NativeAndroidViewPager
|
2016-04-08 01:08:29 +00:00
|
|
|
{...this.props}
|
2015-10-07 02:43:31 +00:00
|
|
|
ref={VIEWPAGER_REF}
|
|
|
|
style={this.props.style}
|
|
|
|
onPageScroll={this._onPageScroll}
|
2016-01-29 09:16:40 +00:00
|
|
|
onPageScrollStateChanged={this._onPageScrollStateChanged}
|
2015-10-07 02:43:31 +00:00
|
|
|
onPageSelected={this._onPageSelected}
|
|
|
|
children={this._childrenWithOverridenStyle()}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-11-18 16:24:26 +00:00
|
|
|
var NativeAndroidViewPager = requireNativeComponent('AndroidViewPager', ViewPagerAndroid);
|
2015-10-07 02:43:31 +00:00
|
|
|
|
|
|
|
module.exports = ViewPagerAndroid;
|