forked from aksonov/react-native-experimental-navigation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNavigationView.js
179 lines (151 loc) · 4.17 KB
/
NavigationView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Copyright (c) 2015-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.
*
* @providesModule NavigationView
* @flow
*/
'use strict';
const Animated = require('react-native').Animated;
const NavigationContainer = require('./NavigationContainer');
const React = require('react');
const StyleSheet = require('react-native').StyleSheet;
const View = require('react-native').View;
const NavigationScenesReducer = require('./NavigationScenesReducer');
const ReactComponentWithPureRenderMixin = require('react-addons-pure-render-mixin');
import type {
NavigationActionCaller,
NavigationAnimatedValue,
NavigationLayout,
NavigationParentState,
NavigationScene,
NavigationSceneRenderer,
NavigationSceneRendererProps,
} from 'NavigationTypeDefinition';
type Props = {
navigationState: NavigationParentState,
onNavigate: NavigationActionCaller,
renderScene: NavigationSceneRenderer,
style: any,
};
type State = {
layout: NavigationLayout,
scenes: Array<NavigationScene>,
};
const {PropTypes} = React;
/**
* A simple view that will render a scene for the currently focused sub-state.
* The most common use-case is for tabs, where no transition is needed
*/
class NavigationView extends React.Component<any, Props, any> {
_onLayout: (event: any) => void;
_position: NavigationAnimatedValue;
props: Props;
state: State;
static propTypes = {
navigationState: PropTypes.object.isRequired,
onNavigate: PropTypes.func.isRequired,
renderScene: PropTypes.func.isRequired,
};
constructor(props: Props, context: any) {
super(props, context);
const layout = {
initWidth: 0,
initHeight: 0,
isMeasured: false,
width: new Animated.Value(0),
height: new Animated.Value(0),
};
const {navigationState} = this.props;
this._position = new Animated.Value(navigationState.index);
this.state = {
layout,
scenes: NavigationScenesReducer([], navigationState),
};
}
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
return ReactComponentWithPureRenderMixin.shouldComponentUpdate.call(
this,
nextProps,
nextState
);
}
componentWillReceiveProps(nextProps: Props): void {
if (nextProps.navigationState !== this.props.navigationState) {
const {navigationState} = nextProps;
this.setState(
{
scenes: NavigationScenesReducer(
this.state.scenes,
navigationState,
null, // There will be no transtion.
),
},
() => {
this._position.setValue(navigationState.index);
},
);
}
}
componentWillMount(): void {
this._onLayout = this._onLayout.bind(this);
}
render(): ReactElement {
const {
navigationState,
onNavigate
} = this.props;
const {
layout,
scenes,
} = this.state;
const sceneProps = {
layout,
navigationState: navigationState,
onNavigate: onNavigate,
position: this._position,
scene: scenes[navigationState.index],
scenes,
};
return (
<View
onLayout={this._onLayout}
style={this.props.style}>
{this._renderScene(sceneProps)}
</View>
);
}
_renderScene(props: NavigationSceneRendererProps): ?ReactElement {
const child = this.props.renderScene(props);
if (child === null) {
return null;
}
return <View key={props.scene.key} style={styles.scene}>{child}</View>;
}
_onLayout(event: any): void {
const {height, width} = event.nativeEvent.layout;
const layout = {
...this.state.layout,
initHeight: height,
initWidth: width,
isMeasured: true,
};
layout.height.setValue(height);
layout.width.setValue(width);
this.setState({ layout });
}
}
const styles = StyleSheet.create({
scene: {
bottom: 0,
left: 0,
position: 'absolute',
right: 0,
top: 0,
},
});
module.exports = NavigationContainer.create(NavigationView);