diff --git a/src/Actions.js b/src/Actions.js index 0ece353fe..050e74b19 100644 --- a/src/Actions.js +++ b/src/Actions.js @@ -44,14 +44,14 @@ class Actions { this.init = this.init.bind(this); this.pop = this.pop.bind(this); this.refresh = this.refresh.bind(this); - + this.focus = this.focus.bind(this); } iterate(root: Scene, parentProps = {}, refs = {}) { 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, RESET_ACTION, "create", + 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); @@ -106,6 +106,11 @@ class Actions { this.callback && this.callback({...props, type: REFRESH_ACTION}); } + focus(props = {}){ + props = filterParam(props); + this.callback && this.callback({...props, type: FOCUS_ACTION}); + } + create(scene:Scene){ assert(scene, "root scene should be defined"); let refs = {}; diff --git a/src/DefaultRenderer.js b/src/DefaultRenderer.js index c52467364..5b29bae8a 100644 --- a/src/DefaultRenderer.js +++ b/src/DefaultRenderer.js @@ -15,6 +15,7 @@ const { } = NavigationExperimental; import TabBar from "./TabBar"; import NavBar from "./NavBar"; +import Actions from './Actions'; export default class DefaultRenderer extends Component { constructor(props) { @@ -28,6 +29,24 @@ export default class DefaultRenderer extends Component { navigationState: PropTypes.any, }; + componentDidMount() { + this.dispatchFocusAction(this.props); + } + + componentWillReceiveProps(nextProps) { + if (nextProps.navigationState !== this.props.navigationState) { + this.dispatchFocusAction(nextProps); + } + } + + dispatchFocusAction({navigationState}) { + if (!navigationState || navigationState.component || navigationState.tabs) { + return; + } + const scene = navigationState.children[navigationState.index]; + Actions.focus({scene}); + } + getChildContext() { return { navigationState: this.props.navigationState, diff --git a/src/Reducer.js b/src/Reducer.js index eca90a8c1..b90d1d664 100644 --- a/src/Reducer.js +++ b/src/Reducer.js @@ -7,7 +7,7 @@ * */ -import {PUSH_ACTION, POP_ACTION2, FOCUS_ACTION, JUMP_ACTION, INIT_ACTION, REPLACE_ACTION, RESET_ACTION, POP_ACTION, REFRESH_ACTION} from "./Actions"; +import {PUSH_ACTION, POP_ACTION2, JUMP_ACTION, INIT_ACTION, REPLACE_ACTION, RESET_ACTION, POP_ACTION, REFRESH_ACTION} from "./Actions"; import assert from "assert"; import {getInitialState} from "./State";