Support initialRouteParams for TabRouters (#3352)

This commit is contained in:
Yao Hui Chua
2018-01-29 19:06:32 +08:00
committed by Satyajit Sahoo
parent 088e9e82ad
commit a1fc566679
3 changed files with 25 additions and 0 deletions

View File

@@ -353,6 +353,7 @@ export type StackNavigatorConfig = {
export type NavigationTabRouterConfig = {
initialRouteName?: string,
initialRouteParams?: NavigationParams,
paths?: NavigationPathsConfig,
navigationOptions?: NavigationScreenConfig<*>,
order?: Array<string>, // todo: type these as the real route names rather than 'string'

View File

@@ -12,6 +12,7 @@ export default (routeConfigs, config = {}) => {
const order = config.order || Object.keys(routeConfigs);
const paths = config.paths || {};
const initialRouteParams = config.initialRouteParams;
const initialRouteName = config.initialRouteName || order[0];
const initialRouteIndex = order.indexOf(initialRouteName);
const backBehavior = config.backBehavior || 'initialRoute';
@@ -38,6 +39,8 @@ export default (routeConfigs, config = {}) => {
let state = inputState;
if (!state) {
const routes = order.map(routeName => {
const params =
routeName === initialRouteName ? initialRouteParams : undefined;
const tabRouter = tabRouters[routeName];
if (tabRouter) {
const childAction = NavigationActions.init();
@@ -45,11 +48,13 @@ export default (routeConfigs, config = {}) => {
...tabRouter.getStateForAction(childAction),
key: routeName,
routeName,
params,
};
}
return {
key: routeName,
routeName,
params,
};
});
state = {
@@ -69,6 +74,9 @@ export default (routeConfigs, config = {}) => {
params: {
...route.params,
...params,
...(route.routeName === initialRouteName
? initialRouteParams
: null),
},
}));
}

View File

@@ -107,6 +107,22 @@ describe('TabRouter', () => {
});
});
test('Can set the initial params', () => {
const router = TabRouter(
{ Foo: BareLeafRouteConfig, Bar: BareLeafRouteConfig },
{ initialRouteName: 'Bar', initialRouteParams: { name: 'Qux' } }
);
const state = router.getStateForAction({ type: NavigationActions.INIT });
expect(state).toEqual({
index: 1,
routes: [
{ key: 'Foo', routeName: 'Foo' },
{ key: 'Bar', routeName: 'Bar', params: { name: 'Qux' } },
],
isTransitioning: false,
});
});
test('Handles the SetParams action', () => {
const router = TabRouter({
Foo: {