Let inner nested stack reducers handle back actions first

Summary:Fix for #6963
Closes https://github.com/facebook/react-native/pull/6982

Differential Revision: D3180164

Pulled By: ericvicenti

fb-gh-sync-id: 234d3027097ae032ba0e24470adb3a5ebf07e351
fbshipit-source-id: 234d3027097ae032ba0e24470adb3a5ebf07e351
This commit is contained in:
Matthew Arbesfeld 2016-04-14 18:23:47 -07:00 committed by Facebook Github Bot 2
parent d3929c62d9
commit ef8e688b20
2 changed files with 63 additions and 9 deletions

View File

@ -73,14 +73,6 @@ function NavigationStackReducer({initialState, getReducerForState, getPushedRedu
if (!lastParentState) {
return lastState;
}
switch (action.type) {
case 'back':
case 'BackAction':
if (lastParentState.index === 0 || lastParentState.children.length === 1) {
return lastParentState;
}
return NavigationStateUtils.pop(lastParentState);
}
const activeSubState = lastParentState.children[lastParentState.index];
const activeSubReducer = getReducerForStateWithDefault(activeSubState);
@ -101,6 +93,16 @@ function NavigationStackReducer({initialState, getReducerForState, getPushedRedu
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;
};
}

View File

@ -103,4 +103,56 @@ describe('NavigationStackReducer', () => {
expect(state3).toBe(state2);
});
});
it('allows inner reducers to handle back actions', () => {
const subReducer = NavigationStackReducer({
getPushedReducerForAction: () => {},
initialState: {
children: [
{key: 'first'},
{key: 'second'},
],
index: 1,
key: 'myInnerStack'
},
});
const reducer = NavigationStackReducer({
getPushedReducerForAction: (action) => {
if (action.type === 'TestPushAction') {
return subReducer;
}
return null;
},
getReducerForState: (state) => {
if (state.key === 'myInnerStack') {
return subReducer;
}
return () => state;
},
initialState: {
children: [
{key: 'a'},
],
index: 0,
key: 'myStack'
}
});
const state1 = reducer(null, {type: 'MyDefaultAction'});
const state2 = reducer(state1, {type: 'TestPushAction'});
expect(state2.children.length).toBe(2);
expect(state2.children[0].key).toBe('a');
expect(state2.children[1].key).toBe('myInnerStack');
expect(state2.children[1].children.length).toBe(2);
expect(state2.children[1].children[0].key).toBe('first');
expect(state2.children[1].children[1].key).toBe('second');
const state3 = reducer(state2, NavigationRootContainer.getBackAction());
expect(state3.children.length).toBe(2);
expect(state3.children[0].key).toBe('a');
expect(state3.children[1].key).toBe('myInnerStack');
expect(state3.children[1].children.length).toBe(1);
expect(state3.children[1].children[0].key).toBe('first');
});
});