Add SetParams handling in TabRouter.js (#132)

This commit is contained in:
microwavesafe
2017-02-08 10:46:20 +00:00
committed by Satyajit Sahoo
parent 5501bcdb44
commit c4787a4bbb
2 changed files with 35 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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 });