Skip to content

Commit

Permalink
Merge pull request #537 from lynndylanhurley/focus-action
Browse files Browse the repository at this point in the history
Implement FOCUS_ACTION using createReducer approach
  • Loading branch information
aksonov committed Apr 18, 2016
2 parents 1deab3e + a74f7fd commit c005592
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = {};
Expand Down
19 changes: 19 additions & 0 deletions src/DefaultRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down

0 comments on commit c005592

Please sign in to comment.