mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 12:15:37 +08:00
Navigation Back support and examples for Android
Summary: public - Intro new back action - Add support in the two main reducers - Use it in examples to support Android back button - Disable NavigationCard gestures on Android Reviewed By: hedgerwang Differential Revision: D2914154 fb-gh-sync-id: d4dce6538e19613a2ffca21e2e3b2ecaded3d5dc shipit-source-id: d4dce6538e19613a2ffca21e2e3b2ecaded3d5dc
This commit is contained in:
committed by
facebook-github-bot-5
parent
7b57b5c84a
commit
7b2b0c3c1c
@@ -22,7 +22,7 @@ import type {
|
||||
NavigationReducer
|
||||
} from 'NavigationState';
|
||||
|
||||
function NavigationFindReducer(reducers: Array<NavigationReducer>): ?NavigationReducer {
|
||||
function NavigationFindReducer(reducers: Array<NavigationReducer>): NavigationReducer {
|
||||
return function(lastState: ?NavigationState, action: ?any): ?NavigationState {
|
||||
for (let i = 0; i < reducers.length; i++) {
|
||||
let reducer = reducers[i];
|
||||
|
||||
@@ -18,7 +18,11 @@ import type {
|
||||
NavigationReducer,
|
||||
} from 'NavigationState';
|
||||
|
||||
export type NavigationStackReducerAction = {
|
||||
import type {
|
||||
BackAction,
|
||||
} from 'NavigationRootContainer';
|
||||
|
||||
export type NavigationStackReducerAction = BackAction | {
|
||||
type: string,
|
||||
};
|
||||
|
||||
@@ -101,6 +105,7 @@ function NavigationStackReducer({initialStates, initialIndex, key, matchAction,
|
||||
action.state
|
||||
);
|
||||
case ActionTypes.POP:
|
||||
case 'BackAction':
|
||||
if (lastParentState.index === 0 || lastParentState.children.length === 1) {
|
||||
return lastParentState;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ const NavigationStateUtils = require('NavigationState');
|
||||
import type {
|
||||
NavigationReducer,
|
||||
NavigationReducerWithDefault,
|
||||
NavigationState
|
||||
NavigationState,
|
||||
NavigationParentState
|
||||
} from 'NavigationState';
|
||||
|
||||
const ActionTypes = {
|
||||
@@ -106,17 +107,15 @@ function NavigationTabsReducer({key, initialIndex, tabReducers}: TabsReducerConf
|
||||
}
|
||||
}
|
||||
const subReducers = tabReducers.map((tabReducer, tabIndex) => {
|
||||
return function reduceTab(lastTabState: ?NavigationState, tabAction: ?any): ?NavigationState {
|
||||
if (!lastTabState) {
|
||||
return tabReducer(lastTabState, tabAction);
|
||||
return function reduceTab(lastNavState: ?NavigationState, tabAction: ?any): ?NavigationState {
|
||||
if (!tabReducer || !lastNavState) {
|
||||
return lastNavState;
|
||||
}
|
||||
if (!lastParentNavState) {
|
||||
return lastTabState;
|
||||
}
|
||||
const lastSubTabState = lastParentNavState.children[tabIndex];
|
||||
const lastParentNavState = NavigationStateUtils.getParent(lastNavState);
|
||||
const lastSubTabState = lastParentNavState && lastParentNavState.children[tabIndex];
|
||||
const nextSubTabState = tabReducer(lastSubTabState, tabAction);
|
||||
if (nextSubTabState && lastSubTabState !== nextSubTabState) {
|
||||
const tabs = lastParentNavState.children;
|
||||
const tabs = lastParentNavState && lastParentNavState.children || [];
|
||||
tabs[tabIndex] = nextSubTabState;
|
||||
return {
|
||||
...lastParentNavState,
|
||||
@@ -129,11 +128,17 @@ function NavigationTabsReducer({key, initialIndex, tabReducers}: TabsReducerConf
|
||||
});
|
||||
let selectedTabReducer = subReducers.splice(lastParentNavState.index, 1)[0];
|
||||
subReducers.unshift(selectedTabReducer);
|
||||
subReducers.push((lastParentNavState: ?NavigationState, action: ?any) => {
|
||||
if (lastParentNavState && action && action.type === 'BackAction') {
|
||||
return NavigationStateUtils.jumpToIndex(
|
||||
lastParentNavState,
|
||||
0
|
||||
);
|
||||
}
|
||||
return lastParentNavState;
|
||||
});
|
||||
const findReducer = NavigationFindReducer(subReducers);
|
||||
if (findReducer) {
|
||||
return findReducer(lastParentNavState, action);
|
||||
}
|
||||
return lastParentNavState;
|
||||
return findReducer(lastParentNavState, action);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user