mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-30 23:22:41 +08:00
Summary: This is the first step to clarify and simplify the type definations about navigation state. For now, `NavigationParentState` is actually used as the real navigation state and `NavigationState` is used as a route in navigation, which has been confusion among the APIs. To be clear, our APIs has no intention and interest in dealing with nested or hierarchical navigation states, and we should avoid have the name like `ParentState` or `children`. To fully migrate the types, theer will be a lot of code changes and this is just the first step to rename. = What's Next? 1. rename `navigationState.children` to `navigationState.routes` (breaking change!) 2. remove `navigationState.key` from its type defination. Reviewed By: ericvicenti Differential Revision: D3321403 fbshipit-source-id: 3e39b60f736c1135bc85d8bf2b89027d665e28d4
103 lines
3.1 KiB
JavaScript
103 lines
3.1 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule NavigationStackReducer
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
const NavigationStateUtils = require('NavigationStateUtils');
|
|
|
|
import type {
|
|
NavigationRoute,
|
|
NavigationState,
|
|
NavigationReducer,
|
|
} from 'NavigationTypeDefinition';
|
|
|
|
export type ReducerForStateHandler = (state: NavigationRoute) => NavigationReducer;
|
|
|
|
export type PushedReducerForActionHandler = (action: any, lastState: NavigationState) => ?NavigationReducer;
|
|
|
|
export type StackReducerConfig = {
|
|
/*
|
|
* The initialState is that the reducer will use when there is no previous state.
|
|
* Must be a NavigationState:
|
|
*
|
|
* {
|
|
* children: [
|
|
* {key: 'subState0'},
|
|
* {key: 'subState1'},
|
|
* ],
|
|
* index: 0,
|
|
* key: 'navStackKey'
|
|
* }
|
|
*/
|
|
initialState: NavigationState;
|
|
|
|
/*
|
|
* Returns the sub-reducer for a particular state to handle. This will be called
|
|
* when we need to handle an action on a sub-state. If no reducer is returned,
|
|
* no action will be taken
|
|
*/
|
|
getReducerForState?: ReducerForStateHandler;
|
|
|
|
/*
|
|
* Returns a sub-reducer that will be used when pushing a new route. If a reducer
|
|
* is returned, it be called to get the new state that will be pushed
|
|
*/
|
|
getPushedReducerForAction: PushedReducerForActionHandler;
|
|
};
|
|
|
|
const defaultGetReducerForState = (initialState) => (state) => state || initialState;
|
|
|
|
function NavigationStackReducer({initialState, getReducerForState, getPushedReducerForAction}: StackReducerConfig): NavigationReducer {
|
|
const getReducerForStateWithDefault = getReducerForState || defaultGetReducerForState;
|
|
return function (lastState: ?NavigationRoute, action: any): NavigationRoute {
|
|
if (!lastState) {
|
|
return initialState;
|
|
}
|
|
const lastParentState = NavigationStateUtils.getParent(lastState);
|
|
if (!lastParentState) {
|
|
return lastState;
|
|
}
|
|
|
|
const activeSubState = lastParentState.children[lastParentState.index];
|
|
const activeSubReducer = getReducerForStateWithDefault(activeSubState);
|
|
const nextActiveState = activeSubReducer(activeSubState, action);
|
|
if (nextActiveState !== activeSubState) {
|
|
const nextChildren = [...lastParentState.children];
|
|
nextChildren[lastParentState.index] = nextActiveState;
|
|
return {
|
|
...lastParentState,
|
|
children: nextChildren,
|
|
};
|
|
}
|
|
|
|
const subReducerToPush = getPushedReducerForAction(action, lastParentState);
|
|
if (subReducerToPush) {
|
|
return NavigationStateUtils.push(
|
|
lastParentState,
|
|
subReducerToPush(null, action)
|
|
);
|
|
}
|
|
|
|
switch (action.type) {
|
|
case 'back':
|
|
case 'BackAction':
|
|
if (lastParentState.index === 0 || lastParentState.children.length === 1) {
|
|
return lastParentState;
|
|
}
|
|
return NavigationStateUtils.pop(lastParentState);
|
|
}
|
|
|
|
return lastParentState;
|
|
};
|
|
}
|
|
|
|
module.exports = NavigationStackReducer;
|