Add disableRouteNamePaths option to router configs (#4824)

* Add routeNameAsPathDisabled prop to router config

* Rename option to disableRouteNamePaths

* Update PathHandling-test.js
This commit is contained in:
Nicolas Charpentier
2018-10-03 13:11:34 -04:00
parent abe07200e6
commit be0dc597a7
4 changed files with 43 additions and 5 deletions

View File

@@ -107,7 +107,7 @@ export default (routeConfigs, stackConfig = {}) => {
const {
getPathAndParamsForRoute,
getActionForPathAndParams,
} = createPathParser(childRouters, routeConfigs, stackConfig.paths);
} = createPathParser(childRouters, routeConfigs, stackConfig);
return {
childRouters,

View File

@@ -47,7 +47,7 @@ export default (routeConfigs, config = {}) => {
const {
getPathAndParamsForRoute,
getActionForPathAndParams,
} = createPathParser(childRouters, routeConfigs, config.paths);
} = createPathParser(childRouters, routeConfigs, config);
if (initialRouteIndex === -1) {
throw new Error(

View File

@@ -4,6 +4,8 @@ import React from 'react';
import SwitchRouter from '../SwitchRouter';
import StackRouter from '../StackRouter';
import TabRouter from '../TabRouter';
import StackActions from '../StackActions';
import NavigationActions from '../../NavigationActions';
import { _TESTING_ONLY_normalize_keys } from '../KeyGenerator';
@@ -582,3 +584,39 @@ test('Handles nested switch routers', () => {
expect(action.action.type).toEqual(NavigationActions.NAVIGATE);
expect(action.action.routeName).toEqual('B');
});
const performRouteNameAsPathDisabledTest = createTestRouter => {
const BScreen = () => <div />;
const NestedNavigator = () => <div />;
NestedNavigator.router = createTestRouter({
B: {
screen: BScreen,
path: 'baz',
},
});
const router = createTestRouter(
{
A: NestedNavigator,
},
{ disableRouteNamePaths: true }
);
test('disableRouteNamePaths option on router prevent the default path to be the routeName', () => {
const action = router.getActionForPathAndParams('baz', {});
expect(action.routeName).toBe('A');
expect(action.action.routeName).toBe('B');
});
};
describe('Stack router handles disableRouteNamePaths', () => {
performRouteNameAsPathDisabledTest(StackRouter);
});
describe('Switch router handles disableRouteNamePaths', () => {
performRouteNameAsPathDisabledTest(SwitchRouter);
});
describe('Tab router handles disableRouteNamePaths', () => {
performRouteNameAsPathDisabledTest(TabRouter);
});

View File

@@ -67,7 +67,7 @@ export const urlToPathAndParams = (url, uriPrefix) => {
export const createPathParser = (
childRouters,
routeConfigs,
pathConfigs = {}
{ paths: pathConfigs = {}, disableRouteNamePaths }
) => {
const pathsByRouteNames = {};
let paths = [];
@@ -84,8 +84,8 @@ export const createPathParser = (
}
if (pathPattern === undefined) {
// If the user hasn't specified a path at all, then we assume the routeName is an appropriate path
pathPattern = routeName;
// If the user hasn't specified a path at all nor disableRouteNamePaths, then we assume the routeName is an appropriate path
pathPattern = disableRouteNamePaths ? null : routeName;
}
invariant(