it('identifies parents correctly with getParent', () => { for (var i = 0; i <= VALID_PARENT_STATES.length; i++) { var navState = VALID_PARENT_STATES[0]; expect(NavigationStateUtils.getParent(navState)).toBe(navState); } for (var i = 0; i <= INVALID_PARENT_STATES.length; i++) { var navState = INVALID_PARENT_STATES[0]; expect(NavigationStateUtils.getParent(navState)).toBe(null); } });
return function (lastState: ?NavigationState, action: any): NavigationState { if (key == null) { key = DEFAULT_KEY; } if (initialIndex == null) { initialIndex = initialStates.length - 1; } if (!lastState) { lastState = { index: initialIndex, children: initialStates, key, }; } const lastParentState = NavigationStateUtils.getParent(lastState); if (!action || !lastParentState) { return lastState; } switch (action.type) { case ActionTypes.PUSH: return NavigationStateUtils.push( lastParentState, action.state ); case ActionTypes.POP: case 'BackAction': if (lastParentState.index === 0 || lastParentState.children.length === 1) { return lastParentState; } return NavigationStateUtils.pop(lastParentState); case ActionTypes.JUMP_TO: return NavigationStateUtils.jumpTo( lastParentState, action.key ); case ActionTypes.JUMP_TO_INDEX: return NavigationStateUtils.jumpToIndex( lastParentState, action.index ); case ActionTypes.RESET: return { ...lastParentState, index: action.index, children: action.children, }; } if (matchAction(action)) { return NavigationStateUtils.push( lastParentState, actionStateMap(action) ); } return lastParentState; };
return function(lastNavState: ?NavigationRoute, action: ?any): NavigationRoute { if (!lastNavState) { lastNavState = { routes: tabReducers.map(reducer => reducer(null, null)), index: initialIndex || 0, key, }; } const lastParentNavState = NavigationStateUtils.getParent(lastNavState); if (!action || !lastParentNavState) { return lastNavState; } if ( action.type === ActionTypes.JUMP_TO && action.index !== lastParentNavState.index ) { return NavigationStateUtils.jumpToIndex( lastParentNavState, action.index, ); } const subReducers = tabReducers.map((tabReducer, tabIndex) => { return function(navState: ?NavigationRoute, tabAction: any): NavigationRoute { if (!navState) { return lastParentNavState; } const parentState = NavigationStateUtils.getParent(navState); const tabState = parentState && parentState.routes[tabIndex]; const nextTabState = tabReducer(tabState, tabAction); if (nextTabState && tabState !== nextTabState) { const tabs = parentState && parentState.routes || []; tabs[tabIndex] = nextTabState; return { ...lastParentNavState, tabs, index: tabIndex, }; } return lastParentNavState; }; }); let selectedTabReducer = subReducers.splice(lastParentNavState.index, 1)[0]; subReducers.unshift(function(navState: ?NavigationRoute, action: any): NavigationRoute { if (navState && action.type === 'BackAction') { return NavigationStateUtils.jumpToIndex( lastParentNavState, initialIndex || 0 ); } return lastParentNavState; }); subReducers.unshift(selectedTabReducer); const findReducer = NavigationFindReducer(subReducers, lastParentNavState); return findReducer(lastParentNavState, action); };
constructor(props: Props) { super(props); let navState = null; if (!this.props.persistenceKey) { navState = NavigationStateUtils.getParent( this.props.reducer(null, props.initialAction) ); } this.state = { navState }; }
return function(navState: ?NavigationRoute, tabAction: any): NavigationRoute { if (!navState) { return lastParentNavState; } const parentState = NavigationStateUtils.getParent(navState); const tabState = parentState && parentState.routes[tabIndex]; const nextTabState = tabReducer(tabState, tabAction); if (nextTabState && tabState !== nextTabState) { const tabs = parentState && parentState.routes || []; tabs[tabIndex] = nextTabState; return { ...lastParentNavState, tabs, index: tabIndex, }; } return lastParentNavState; };
return function (lastState: ?NavigationRoute, action: any): NavigationRoute { if (!lastState) { return initialState; } const lastParentState = NavigationStateUtils.getParent(lastState); if (!lastParentState) { return lastState; } const activeSubState = lastParentState.routes[lastParentState.index]; const activeSubReducer = getReducerForStateWithDefault(activeSubState); const nextActiveState = activeSubReducer(activeSubState, action); if (nextActiveState !== activeSubState) { const nextChildren = [...lastParentState.routes]; nextChildren[lastParentState.index] = nextActiveState; return { ...lastParentState, routes: 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.routes.length === 1) { return lastParentState; } return NavigationStateUtils.pop(lastParentState); } return lastParentState; };