mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
Support initialRouteParams for TabRouters (#3352)
This commit is contained in:
committed by
Satyajit Sahoo
parent
088e9e82ad
commit
a1fc566679
@@ -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'
|
||||
|
||||
@@ -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),
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user