diff --git a/index.js b/index.js index 637814562..f8836c6df 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,13 @@ -import Actions from './src/Actions'; -import DefaultRenderer from './src/DefaultRenderer'; -import Modal from './src/Modal'; -import NavBar from './src/NavBar'; -import Reducer from './src/Reducer'; import Router from './src/Router'; import Scene from './src/Scene'; -import Switch from './src/Switch'; -import TabBar from './src/TabBar'; +import Actions from './src/Actions'; import getInitialState from './src/State'; +import Reducer from './src/Reducer'; +import TabBar from './src/TabBar'; +import Modal from './src/Modal'; +import DefaultRenderer from './src/DefaultRenderer'; +import Switch from './src/Switch'; +import NavBar from './src/NavBar'; -module.exports = { - Actions, - DefaultRenderer, - Modal, - NavBar, - Reducer, - Router, - Scene, - Switch, - TabBar, - getInitialState, -}; +module.exports = { DefaultRenderer, NavBar, Switch, Modal, Router, TabBar, + Scene, Actions, Reducer, getInitialState }; diff --git a/src/Actions.js b/src/Actions.js index e43f38a5d..2dd4d5c81 100644 --- a/src/Actions.js +++ b/src/Actions.js @@ -23,15 +23,14 @@ function isNumeric(n) { } function filterParam(data) { - if (data.toString() !== '[object Object]') { - return { data }; - } + if (data.toString() != '[object Object]') + return { data: data }; if (!data) { return {}; } - const proto = (data || {}).constructor.name; + var proto = (data || {}).constructor.name; // avoid passing React Native parameters - if (proto !== 'Object') { + if (proto != 'Object') { data = {}; } return data; @@ -52,25 +51,23 @@ class Actions { assert(root.props, 'props should be defined for stack'); const key = root.key || 'root'; assert(key, 'unique key should be defined ', root); - assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION, - FOCUS_ACTION, RESET_ACTION, 'create', 'init', 'callback', 'iterate', 'current', - ].indexOf(key) === -1, `${key} is not allowed as key name`); - const { ...staticProps } = root.props; + assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION, FOCUS_ACTION, RESET_ACTION, 'create', + 'init', 'callback', 'iterate', 'current'].indexOf(key) == -1, key + ' is not allowed as key name'); + const { children, ...staticProps } = root.props; let type = root.props.type || (parentProps.tabs ? JUMP_ACTION : PUSH_ACTION); if (type === 'switch') { type = JUMP_ACTION; } - const res = { name: key, ...staticProps, key, sceneKey: key, type, parent: parentProps.key }; + let res = { name:key, ...staticProps, key, sceneKey:key, type, parent:parentProps.key }; if (root.props.children) { - const list = root.props.children instanceof Array ? - root.props.children : [root.props.children]; + const list = root.props.children instanceof Array ? root.props.children : [root.props.children]; res.children = list.map(c => this.iterate(c, res, refs).key); } else { - assert(staticProps.component, `component property is not set for key=${key}`); + assert(staticProps.component, 'component property is not set for key=' + key); // wrap scene if parent is "tabs" if (parentProps.tabs) { - const innerKey = `${res.key}_`; - const inner = { ...res, name: key, key: innerKey, type: PUSH_ACTION, parent: res.key }; + const innerKey = res.key + '_'; + const inner = { ...res, name:key, key: innerKey, type: PUSH_ACTION, parent:res.key }; refs[innerKey] = inner; res.children = [innerKey]; delete res.component; @@ -78,50 +75,45 @@ class Actions { res.index = 0; } if (this[key]) { - console.log(`Key ${root.key} is already defined!`); + console.log('Key ' + root.key + ' is already defined!'); } - this[key] = (props = {}) => { - assert(this.callback, 'Actions.callback is not defined!'); - this.callback({ key: root.key, type, ...filterParam(props) }); - }; + this[key] = + (props = {}) => { assert(this.callback, 'Actions.callback is not defined!'); + this.callback({ key: root.key, type, ...filterParam(props) }); }; refs[res.key] = res; return res; } pop(props = {}) { - if (this.callback) { - this.callback({ ...filterParam(props), type: POP_ACTION }); - } + props = filterParam(props); + const data = isNumeric(props) ? { num: props } : props; + this.callback && this.callback({ ...props, type: POP_ACTION }); } jump(props = {}) { - if (this.callback) { - this.callback({ ...filterParam(props), type: JUMP_ACTION }); - } + props = filterParam(props); + this.callback && this.callback({ ...props, type: JUMP_ACTION }); } init(props = {}) { - if (this.callback) { - this.callback({ ...filterParam(props), type: INIT_ACTION }); - } + props = filterParam(props); + this.callback && this.callback({ ...props, type: INIT_ACTION }); } refresh(props = {}) { - if (this.callback) { - this.callback({ ...filterParam(props), type: REFRESH_ACTION }); - } + props = filterParam(props); + this.callback && this.callback({ ...props, type: REFRESH_ACTION }); } focus(props = {}) { - if (this.callback) { - this.callback({ ...filterParam(props), type: FOCUS_ACTION }); - } + props = filterParam(props); + this.callback && this.callback({ ...props, type: FOCUS_ACTION }); } create(scene:Scene) { assert(scene, 'root scene should be defined'); - const refs = {}; + let refs = {}; this.iterate(scene, {}, refs); return refs; } diff --git a/src/DefaultRenderer.js b/src/DefaultRenderer.js index 2415e1c14..a3cd404d6 100644 --- a/src/DefaultRenderer.js +++ b/src/DefaultRenderer.js @@ -6,51 +6,28 @@ * LICENSE file in the root directory of this source tree. * */ -import React, { - Animated, - NavigationExperimental, - PropTypes, - StyleSheet, - View, -} from 'react-native'; +import React, { Component, Animated, PropTypes, StyleSheet, View, NavigationExperimental } from 'react-native'; const { - AnimatedView: NavigationAnimatedView, - Card: NavigationCard, - RootContainer: NavigationRootContainer, - Header: NavigationHeader, -} = NavigationExperimental; -import Actions from './Actions'; -import NavBar from './NavBar'; + AnimatedView: NavigationAnimatedView, + Card: NavigationCard, + RootContainer: NavigationRootContainer, + Header: NavigationHeader, + } = NavigationExperimental; import TabBar from './TabBar'; +import NavBar from './NavBar'; +import Actions from './Actions'; -const propTypes = { - navigationState: PropTypes.object, -}; - -const styles = StyleSheet.create({ - animatedView: { - flex: 1, - backgroundColor: 'transparent', - }, -}); - -class DefaultRenderer extends React.Component { - static childContextTypes = { - navigationState: PropTypes.any, - }; - +export default class DefaultRenderer extends Component { constructor(props) { super(props); - this.renderCard = this.renderCard.bind(this); - this.renderScene = this.renderScene.bind(this); - this.renderHeader = this.renderHeader.bind(this); + this._renderCard = this._renderCard.bind(this); + this._renderScene = this._renderScene.bind(this); + this._renderHeader = this._renderHeader.bind(this); } - getChildContext() { - return { - navigationState: this.props.navigationState, - }; - } + static childContextTypes = { + navigationState: PropTypes.any, + }; componentDidMount() { this.dispatchFocusAction(this.props); @@ -70,41 +47,10 @@ class DefaultRenderer extends React.Component { Actions.focus({ scene }); } - renderHeader(/* NavigationSceneRendererProps*/ props) { - const state = props.navigationState; - let selected = state.children[state.index]; - while (selected.hasOwnProperty('children')) { - selected = selected.children[selected.index]; - } - const Component = state.navBar || selected.navBar || NavBar; - return titleState.title} />; - } - - renderCard(/* NavigationSceneRendererProps*/ props) { - const { key, direction, panHandlers, getSceneStyle } = props.scene.navigationState; - - const optionals = {}; - if (getSceneStyle) optionals.style = getSceneStyle(props); - - return ( - - ); - } - - renderScene(/* NavigationSceneRendererProps*/ props) { - return ( - - ); + getChildContext() { + return { + navigationState: this.props.navigationState, + }; } render() { @@ -118,20 +64,20 @@ class DefaultRenderer extends React.Component { } if (Component) { return ( - - - - ); + + + + ); } const selected = navigationState.children[navigationState.index]; // return - const applyAnimation = selected.applyAnimation || navigationState.applyAnimation; - const style = selected.style || navigationState.style; + let applyAnimation = selected.applyAnimation || navigationState.applyAnimation; + let style = selected.style || navigationState.style; let direction = selected.direction || navigationState.direction || 'horizontal'; - const optionals = {}; + let optionals = {}; if (applyAnimation) { optionals.applyAnimation = applyAnimation; } else { @@ -145,18 +91,55 @@ class DefaultRenderer extends React.Component { } return ( - - ); + + ); } -} -DefaultRenderer.propTypes = propTypes; + _renderHeader(/* NavigationSceneRendererProps*/ props) { + const state = props.navigationState; + const child = state.children[state.index]; + let selected = state.children[state.index]; + while (selected.hasOwnProperty('children')) { + selected = selected.children[selected.index]; + } + const Component = state.navBar || selected.navBar || NavBar; + return state.title} />; + } -export default DefaultRenderer; + _renderCard(/* NavigationSceneRendererProps*/ props) { + const { key, direction, panHandlers, getSceneStyle } = props.scene.navigationState; + + const optionals = {}; + if (getSceneStyle) optionals.style = getSceneStyle(props); + + return ( + + ); + } + + _renderScene(/* NavigationSceneRendererProps*/ props) { + return ; + } + +} + +const styles = StyleSheet.create({ + animatedView: { + flex: 1, + backgroundColor:'transparent' + }, +}); diff --git a/src/Modal.js b/src/Modal.js index b8ab53343..47f06a865 100644 --- a/src/Modal.js +++ b/src/Modal.js @@ -1,22 +1,18 @@ -import React, { View } from 'react-native'; +import React, { Component, View } from 'react-native'; import DefaultRenderer from './DefaultRenderer'; -export default function Modal() { - const children = this.props.navigationState.children; - const state = children[0]; - return ( - - - {children.length > 1 && children.map((el, i) => { - if (i > 0 && el.component) { - const Component = el.component; - return ; - } - })} - - ); +export default class extends Component { + render() { + const children = this.props.navigationState.children; + const state = children[0]; + return ( + + {children.length > 1 && children.map((el, i) => { + if (i > 0 && el.component) { + const Component = el.component; + return ; + } + })} + ); + } }