2016-02-05 22:25:17 +00:00
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
|
2016-03-29 22:56:53 +00:00
|
|
|
const Animated = require('Animated');
|
|
|
|
const NavigationContainer = require('NavigationContainer');
|
2016-04-09 03:36:40 +00:00
|
|
|
const React = require('React');
|
2016-03-29 22:56:53 +00:00
|
|
|
const StyleSheet = require('StyleSheet');
|
|
|
|
const View = require('View');
|
|
|
|
const NavigationScenesReducer = require('NavigationScenesReducer');
|
|
|
|
const ReactComponentWithPureRenderMixin = require('ReactComponentWithPureRenderMixin');
|
|
|
|
|
|
|
|
import type {
|
|
|
|
NavigationActionCaller,
|
|
|
|
NavigationAnimatedValue,
|
|
|
|
NavigationLayout,
|
|
|
|
NavigationParentState,
|
|
|
|
NavigationScene,
|
|
|
|
NavigationSceneRenderer,
|
|
|
|
NavigationSceneRendererProps,
|
|
|
|
} from 'NavigationTypeDefinition';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
navigationState: NavigationParentState,
|
|
|
|
onNavigate: NavigationActionCaller,
|
|
|
|
renderScene: NavigationSceneRenderer,
|
|
|
|
style: any,
|
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
2016-04-20 02:55:31 +00:00
|
|
|
layout: NavigationLayout,
|
2016-03-29 22:56:53 +00:00
|
|
|
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);
|
|
|
|
|
2016-04-20 02:55:31 +00:00
|
|
|
const layout = {
|
2016-03-29 22:56:53 +00:00
|
|
|
initWidth: 0,
|
|
|
|
initHeight: 0,
|
2016-04-20 02:55:31 +00:00
|
|
|
isMeasured: false,
|
2016-03-29 22:56:53 +00:00
|
|
|
width: new Animated.Value(0),
|
|
|
|
height: new Animated.Value(0),
|
|
|
|
};
|
|
|
|
|
|
|
|
const {navigationState} = this.props;
|
|
|
|
|
|
|
|
this._position = new Animated.Value(navigationState.index);
|
|
|
|
|
|
|
|
this.state = {
|
2016-04-20 02:55:31 +00:00
|
|
|
layout,
|
2016-03-29 22:56:53 +00:00
|
|
|
scenes: NavigationScenesReducer([], navigationState),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
|
|
|
|
return ReactComponentWithPureRenderMixin.shouldComponentUpdate.call(
|
|
|
|
this,
|
|
|
|
nextProps,
|
|
|
|
nextState
|
2016-02-05 22:25:17 +00:00
|
|
|
);
|
2016-03-29 22:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-04-20 02:55:31 +00:00
|
|
|
layout,
|
2016-03-29 22:56:53 +00:00
|
|
|
scenes,
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
const sceneProps = {
|
2016-04-20 02:55:31 +00:00
|
|
|
layout,
|
2016-03-29 22:56:53 +00:00
|
|
|
navigationState: navigationState,
|
|
|
|
onNavigate: onNavigate,
|
|
|
|
position: this._position,
|
|
|
|
scene: scenes[navigationState.index],
|
|
|
|
scenes,
|
|
|
|
};
|
|
|
|
|
2016-02-05 22:25:17 +00:00
|
|
|
return (
|
|
|
|
<View
|
2016-03-29 22:56:53 +00:00
|
|
|
onLayout={this._onLayout}
|
|
|
|
style={this.props.style}>
|
|
|
|
{this._renderScene(sceneProps)}
|
2016-02-05 22:25:17 +00:00
|
|
|
</View>
|
|
|
|
);
|
2016-03-29 22:56:53 +00:00
|
|
|
}
|
2016-02-05 22:25:17 +00:00
|
|
|
|
2016-03-29 22:56:53 +00:00
|
|
|
_renderScene(props: NavigationSceneRendererProps): ?ReactElement {
|
2016-02-05 22:25:17 +00:00
|
|
|
|
2016-03-29 22:56:53 +00:00
|
|
|
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 = {
|
2016-04-20 02:55:31 +00:00
|
|
|
...this.state.layout,
|
2016-03-29 22:56:53 +00:00
|
|
|
initHeight: height,
|
|
|
|
initWidth: width,
|
2016-04-20 02:55:31 +00:00
|
|
|
isMeasured: true,
|
2016-03-29 22:56:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
layout.height.setValue(height);
|
|
|
|
layout.width.setValue(width);
|
2016-04-20 02:55:31 +00:00
|
|
|
|
|
|
|
this.setState({ layout });
|
2016-03-29 22:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
scene: {
|
|
|
|
bottom: 0,
|
2016-02-05 22:25:17 +00:00
|
|
|
left: 0,
|
2016-03-29 22:56:53 +00:00
|
|
|
position: 'absolute',
|
2016-02-05 22:25:17 +00:00
|
|
|
right: 0,
|
|
|
|
top: 0,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-03-29 22:56:53 +00:00
|
|
|
module.exports = NavigationContainer.create(NavigationView);
|