mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-04-24 04:25:34 +08:00
Add SetParams handling in TabRouter.js (#132)
This commit is contained in:
committed by
Satyajit Sahoo
parent
5501bcdb44
commit
c4787a4bbb
@@ -128,6 +128,24 @@ export default (
|
||||
}
|
||||
// console.log({navId: order.join('-'), action, order, lastIndex: state.index, index: activeTabIndex});
|
||||
}
|
||||
if (action.type === NavigationActions.SET_PARAMS) {
|
||||
const lastRoute = state.routes.find(route => route.key === action.key);
|
||||
if (lastRoute) {
|
||||
const params = {
|
||||
...lastRoute.params,
|
||||
...action.params,
|
||||
};
|
||||
const routes = [...state.routes];
|
||||
routes[state.routes.indexOf(lastRoute)] = {
|
||||
...lastRoute,
|
||||
params,
|
||||
};
|
||||
return {
|
||||
...state,
|
||||
routes,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (activeTabIndex !== state.index) {
|
||||
// console.log(`${order.join('-')}: Normal navigation`, {lastIndex: state.index, newIndex: activeTabIndex});
|
||||
return {
|
||||
|
||||
@@ -73,6 +73,23 @@ describe('TabRouter', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('Handles the SetParams action', () => {
|
||||
const router = TabRouter({
|
||||
Foo: {
|
||||
screen: () => <div />,
|
||||
},
|
||||
Bar: {
|
||||
screen: () => <div />,
|
||||
},
|
||||
});
|
||||
const state2 = router.getStateForAction({
|
||||
type: NavigationActions.SET_PARAMS,
|
||||
params: { name: 'Qux' },
|
||||
key: 'Foo',
|
||||
});
|
||||
expect(state2 && state2.routes[0].params).toEqual({ name: 'Qux' });
|
||||
});
|
||||
|
||||
test('getStateForAction returns null when navigating to same tab', () => {
|
||||
const router = TabRouter({ Foo: BareLeafRouteConfig, Bar: BareLeafRouteConfig }, { initialRouteName: 'Bar' });
|
||||
const state = router.getStateForAction({ type: NavigationActions.INIT });
|
||||
|
||||
Reference in New Issue
Block a user