Add sub-reducer support to NavigationStackReducer

Summary: Revise APIs of reducers, and ensure the stack reducer can support sub-reducers

Reviewed By: javache

Differential Revision: D2959915

fb-gh-sync-id: 20b28b9ead7ace3373489a806486999048d32aef
shipit-source-id: 20b28b9ead7ace3373489a806486999048d32aef
This commit is contained in:
Eric Vicenti
2016-02-22 16:15:42 -08:00
committed by facebook-github-bot-6
parent 876ecb291f
commit dcb68db758
10 changed files with 253 additions and 369 deletions

View File

@@ -24,8 +24,6 @@ const ActionTypes = {
JUMP_TO: 'react-native/NavigationExperimental/tabs-jumpTo',
};
const DEFAULT_KEY = 'TABS_STATE_DEFAULT_KEY';
export type JumpToAction = {
type: typeof ActionTypes.JUMP_TO,
index: number,
@@ -44,9 +42,6 @@ type TabsReducerConfig = {
};
function NavigationTabsReducer({key, initialIndex, tabReducers}: TabsReducerConfig): NavigationReducer {
if (key == null) {
key = DEFAULT_KEY;
}
return function(lastNavState: ?NavigationState, action: ?any): NavigationState {
if (!lastNavState) {
lastNavState = {
@@ -89,16 +84,16 @@ function NavigationTabsReducer({key, initialIndex, tabReducers}: TabsReducerConf
};
});
let selectedTabReducer = subReducers.splice(lastParentNavState.index, 1)[0];
subReducers.unshift(selectedTabReducer);
subReducers.push(function(navState: ?NavigationState, action: any): NavigationState {
subReducers.unshift(function(navState: ?NavigationState, action: any): NavigationState {
if (navState && action.type === 'BackAction') {
return NavigationStateUtils.jumpToIndex(
lastParentNavState,
0
initialIndex || 0
);
}
return lastParentNavState;
});
subReducers.unshift(selectedTabReducer);
const findReducer = NavigationFindReducer(subReducers, lastParentNavState);
return findReducer(lastParentNavState, action);
};