1- import React , { View , PropTypes , Navigator } from 'react-native'
1+ import React , {
2+ View ,
3+ PropTypes ,
4+ Navigator ,
5+ BackAndroid ,
6+ Platform
7+ } from 'react-native'
28import NavBar from './NavBar'
39import { defaultRouteMapper } from './RouteMapper'
4- import StyleSheetPropType from 'react-native/Libraries/StyleSheet/StyleSheetPropType'
5- import ViewStylePropTypes from 'react-native/Libraries/Components/View/ViewStylePropTypes'
610
711class NavigatorWrapper extends React . Component {
12+ static isAndroid = Platform . OS !== 'ios' ;
13+
14+ _handleAndroidBackButton ( ) {
15+ if ( this . navigator && ! this . firstComponentInStack ) {
16+ this . navigator . pop ( )
17+ return true
18+ }
19+ return false
20+ }
21+
22+ constructor ( props ) {
23+ super ( props )
24+ this . navigator = undefined
25+ this . firstComponentInStack = true
26+ }
27+
28+ componentDidMount ( ) {
29+ // Automatically handle back button under Android platform
30+ if ( NavigatorWrapper . isAndroid && this . props . initialRoute . handleBackAndroid ) {
31+ this . bindedBackFunction = this . _handleAndroidBackButton . bind ( this )
32+ BackAndroid . addEventListener ( 'hardwareBackPress' , this . bindedBackFunction )
33+ }
34+ }
35+
36+ componentWillUnmount ( ) {
37+ if ( NavigatorWrapper . isAndroid ) {
38+ BackAndroid . removeEventListener ( 'hardwareBackPress' , this . bindedBackFunction )
39+ }
40+ }
41+
842 renderScene ( route , navigator ) {
43+ let marginTop = 64
44+ this . firstComponentInStack = route . handleBackAndroid
45+ if ( NavigatorWrapper . isAndroid ) {
46+ // Save navigator to handle back button under Android
47+ if ( ! this . navigator ) {
48+ this . navigator = navigator
49+ }
50+ marginTop = 56
51+ }
952 const RenderComponent = route . component
1053 return (
11- < View style = { { flex : 1 , marginTop : 64 } } >
54+ < View style = { { flex : 1 , marginTop : marginTop } } >
1255 < RenderComponent
1356 navigator = { navigator }
1457 topNavigator = { this . props . topNavigator }
1558 route = { route }
1659 { ...route . passProps }
17- { ...this . props . passProps }
60+ { ...this . props . initialRoute . passProps }
1861 />
1962 </ View >
2063 )
2164 }
2265
2366 render ( ) {
67+ const navAnimation = ( NavigatorWrapper . isAndroid ) ? Navigator . SceneConfigs . FadeAndroid : Navigator . SceneConfigs . PushFromRight
2468 return (
2569 < Navigator
26- initialRoute = { {
27- component : this . props . initialComponent ,
28- title : this . props . title || ''
29- } }
70+ configureScene = { ( route , routeStack ) => navAnimation }
71+ initialRoute = { this . props . initialRoute }
72+ initialRouteStack = { this . props . initialRouteStack }
3073 navigationBar = {
3174 < NavBar
3275 routeMapper = { this . props . routeMapper || defaultRouteMapper ( ) }
@@ -40,11 +83,38 @@ class NavigatorWrapper extends React.Component {
4083}
4184
4285NavigatorWrapper . propTypes = {
43- initialComponent : PropTypes . func . isRequired ,
44- title : PropTypes . string ,
86+ /**
87+ * Provide the initial route or the initial route stack.
88+ *
89+ * ``leftElement``, ``textElement`` and ``rightElement``` are optional
90+ * elements to overwrite route mapper defaults.
91+ */
92+ initialRoute : PropTypes . shape ( {
93+ component : PropTypes . func . isRequired ,
94+ title : PropTypes . string . isRequired ,
95+ passProps : PropTypes . object ,
96+ leftElement : PropTypes . node ,
97+ textElement : PropTypes . node ,
98+ rightElement : PropTypes . node ,
99+ handleBackAndroid : PropTypes . bool ,
100+ } ) ,
101+ initialRouteStack : PropTypes . arrayOf ( PropTypes . object ) ,
102+
103+ /**
104+ * Optional ``topNavigator`` object to use as a parent navigator for modal
105+ * transitions.
106+ */
45107 topNavigator : PropTypes . object ,
46- passProps : PropTypes . object ,
47- navBarStyle : StyleSheetPropType ( ViewStylePropTypes ) ,
108+
109+ /**
110+ * Optional style for the default navigation bar.
111+ */
112+ navBarStyle : View . propTypes . style ,
113+
114+ /**
115+ * A ``routeMapper`` object to customize Left, Title and Right components for
116+ * the ``NavigationBar``.
117+ */
48118 routeMapper : PropTypes . object ,
49119}
50120
0 commit comments