diff --git a/packages/react-navigation/src/routers/StackRouter.js b/packages/react-navigation/src/routers/StackRouter.js index d26c6d60..cb77e7a1 100644 --- a/packages/react-navigation/src/routers/StackRouter.js +++ b/packages/react-navigation/src/routers/StackRouter.js @@ -107,7 +107,7 @@ export default (routeConfigs, stackConfig = {}) => { const { getPathAndParamsForRoute, getActionForPathAndParams, - } = createPathParser(childRouters, routeConfigs, stackConfig.paths); + } = createPathParser(childRouters, routeConfigs, stackConfig); return { childRouters, diff --git a/packages/react-navigation/src/routers/SwitchRouter.js b/packages/react-navigation/src/routers/SwitchRouter.js index e5b76dfd..1b028ebe 100644 --- a/packages/react-navigation/src/routers/SwitchRouter.js +++ b/packages/react-navigation/src/routers/SwitchRouter.js @@ -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( diff --git a/packages/react-navigation/src/routers/__tests__/PathHandling-test.js b/packages/react-navigation/src/routers/__tests__/PathHandling-test.js index 5d09237a..0863565c 100644 --- a/packages/react-navigation/src/routers/__tests__/PathHandling-test.js +++ b/packages/react-navigation/src/routers/__tests__/PathHandling-test.js @@ -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 = () =>
; + const NestedNavigator = () =>
; + 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); +}); diff --git a/packages/react-navigation/src/routers/pathUtils.js b/packages/react-navigation/src/routers/pathUtils.js index 4b0e7c75..180f5ea2 100644 --- a/packages/react-navigation/src/routers/pathUtils.js +++ b/packages/react-navigation/src/routers/pathUtils.js @@ -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(