Skip to content

Commit

Permalink
add POP_AND_PUSH action to emulate better 'replace' handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aksonov committed Jul 25, 2017
1 parent 54652fa commit 8343f8e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Example/Example.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const Example = () => {
<Scene key="register" back>
<Scene key="_register" component={Register} title="Register"/>
<Scene key="register2" component={Register} title="Register2"/>
<Scene key="home" component={Home} title="Replace" type={ActionConst.REPLACE}/>
<Scene key="home" component={Home} title="Replace" type={ActionConst.POP_AND_PUSH}/>
</Scene>
<Scene key="launch" component={Launch} title="Launch" initial/>
<Scene
Expand Down
3 changes: 2 additions & 1 deletion dist/ActionConst.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var REFRESH=exports.REFRESH='REACT_NATIVE_ROUTER_FLUX_REFRESH';
var RESET=exports.RESET='REACT_NATIVE_ROUTER_FLUX_RESET';
var FOCUS=exports.FOCUS='REACT_NATIVE_ROUTER_FLUX_FOCUS';
var BLUR=exports.BLUR='REACT_NATIVE_ROUTER_FLUX_BLUR';
var ANDROID_BACK=exports.ANDROID_BACK='REACT_NATIVE_ROUTER_FLUX_ANDROID_BACK';
var ANDROID_BACK=exports.ANDROID_BACK='REACT_NATIVE_ROUTER_FLUX_ANDROID_BACK';
var POP_AND_PUSH=exports.POP_AND_PUSH='REACT_NATIVE_ROUTER_FLUX_POP_AND_PUSH';
24 changes: 17 additions & 7 deletions dist/navigationStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ ActionConst.POP_AND_REPLACE,'pop'),_defineProperty(_actionMap,
ActionConst.POP_TO,'popTo'),_defineProperty(_actionMap,
ActionConst.REFRESH,'refresh'),_defineProperty(_actionMap,
ActionConst.RESET,'reset'),_defineProperty(_actionMap,
ActionConst.PUSH_OR_POP,'push'),_actionMap);
ActionConst.PUSH_OR_POP,'push'),_defineProperty(_actionMap,
ActionConst.POP_AND_PUSH,'popAndPush'),_actionMap);


var supportedActions=exports.supportedActions=(_supportedActions={},_defineProperty(_supportedActions,
ActionConst.PUSH,_reactNavigation.NavigationActions.NAVIGATE),_defineProperty(_supportedActions,
ActionConst.JUMP,_reactNavigation.NavigationActions.NAVIGATE),_defineProperty(_supportedActions,
ActionConst.BACK,_reactNavigation.NavigationActions.BACK),_defineProperty(_supportedActions,
ActionConst.REFRESH,_reactNavigation.NavigationActions.BACK),_defineProperty(_supportedActions,
ActionConst.RESET,_reactNavigation.NavigationActions.RESET),_supportedActions);
ActionConst.RESET,_reactNavigation.NavigationActions.RESET),_defineProperty(_supportedActions,
ActionConst.REPLACE,_reactNavigation.NavigationActions.RESET),_supportedActions);

function filterParam(data){
if(data.toString()!=='[object Object]'){
Expand Down Expand Up @@ -193,7 +195,7 @@ currentState=newState;
if(nextScene===routeName){
_this.setState(newState);
}
}else if(type===ActionConst.REPLACE){
}else if(type===ActionConst.POP_AND_PUSH){
_this.pop();
_this.push.apply(_this,[routeName].concat(params));
}
Expand Down Expand Up @@ -245,14 +247,22 @@ _this.refresh(res.refresh);
};this.

popTo=function(routeName){for(var _len5=arguments.length,params=Array(_len5>1?_len5-1:0),_key5=1;_key5<_len5;_key5++){params[_key5-1]=arguments[_key5];}
_this.run.apply(_this,[ActionConst.POP_TO,routeName].concat(params));
_this.run.apply(_this,[ActionConst.POP_TO,routeName,null].concat(params));
};this.

replace=function(routeName){for(var _len6=arguments.length,params=Array(_len6>1?_len6-1:0),_key6=1;_key6<_len6;_key6++){params[_key6-1]=arguments[_key6];}
_this.run.apply(_this,[ActionConst.REPLACE,routeName].concat(params));
popAndPush=function(routeName){for(var _len6=arguments.length,params=Array(_len6>1?_len6-1:0),_key6=1;_key6<_len6;_key6++){params[_key6-1]=arguments[_key6];}
_this.run.apply(_this,[ActionConst.POP_AND_PUSH,routeName,null].concat(params));
};this.

reset=function(routeName){for(var _len7=arguments.length,params=Array(_len7>1?_len7-1:0),_key7=1;_key7<_len7;_key7++){params[_key7-1]=arguments[_key7];}
replace=function(routeName){for(var _len7=arguments.length,params=Array(_len7>1?_len7-1:0),_key7=1;_key7<_len7;_key7++){params[_key7-1]=arguments[_key7];}
var res=uniteParams(routeName,params);
_this.run(ActionConst.REPLACE,routeName,{key:routeName,index:0,actions:[_reactNavigation.NavigationActions.navigate({
routeName:routeName,
params:res})]});

};this.

reset=function(routeName){for(var _len8=arguments.length,params=Array(_len8>1?_len8-1:0),_key8=1;_key8<_len8;_key8++){params[_key8-1]=arguments[_key8];}
var res=uniteParams(routeName,params);
_this.run(ActionConst.RESET,routeName,{key:null,index:0,actions:[_reactNavigation.NavigationActions.navigate({
routeName:routeName,
Expand Down
1 change: 1 addition & 0 deletions src/ActionConst.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export const RESET = 'REACT_NATIVE_ROUTER_FLUX_RESET';
export const FOCUS = 'REACT_NATIVE_ROUTER_FLUX_FOCUS';
export const BLUR = 'REACT_NATIVE_ROUTER_FLUX_BLUR';
export const ANDROID_BACK = 'REACT_NATIVE_ROUTER_FLUX_ANDROID_BACK';
export const POP_AND_PUSH = 'REACT_NATIVE_ROUTER_FLUX_POP_AND_PUSH';
16 changes: 13 additions & 3 deletions src/navigationStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const actionMap = {
[ActionConst.REFRESH]: 'refresh',
[ActionConst.RESET]: 'reset',
[ActionConst.PUSH_OR_POP]: 'push',
[ActionConst.POP_AND_PUSH]: 'popAndPush',
};

export const supportedActions = {
Expand All @@ -22,6 +23,7 @@ export const supportedActions = {
[ActionConst.BACK]: NavigationActions.BACK,
[ActionConst.REFRESH]: NavigationActions.BACK,
[ActionConst.RESET]: NavigationActions.RESET,
[ActionConst.REPLACE]: NavigationActions.RESET,
};
function filterParam(data) {
if (data.toString() !== '[object Object]') {
Expand Down Expand Up @@ -193,7 +195,7 @@ class NavigationStore {
if (nextScene === routeName) {
this.setState(newState);
}
} else if (type === ActionConst.REPLACE) {
} else if (type === ActionConst.POP_AND_PUSH) {
this.pop();
this.push(routeName, ...params);
}
Expand Down Expand Up @@ -245,11 +247,19 @@ class NavigationStore {
};

popTo = (routeName, ...params) => {
this.run(ActionConst.POP_TO, routeName, ...params);
this.run(ActionConst.POP_TO, routeName, null, ...params);
};

popAndPush = (routeName, ...params) => {
this.run(ActionConst.POP_AND_PUSH, routeName, null, ...params);
};

replace = (routeName, ...params) => {
this.run(ActionConst.REPLACE, routeName, ...params);
const res = uniteParams(routeName, params);
this.run(ActionConst.REPLACE, routeName, { key: routeName, index: 0, actions: [NavigationActions.navigate({
routeName,
params: res,
})] });
};

reset = (routeName, ...params) => {
Expand Down

0 comments on commit 8343f8e

Please sign in to comment.