mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-02-11 09:20:54 +08:00
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:
@@ -107,7 +107,7 @@ export default (routeConfigs, stackConfig = {}) => {
|
||||
const {
|
||||
getPathAndParamsForRoute,
|
||||
getActionForPathAndParams,
|
||||
} = createPathParser(childRouters, routeConfigs, stackConfig.paths);
|
||||
} = createPathParser(childRouters, routeConfigs, stackConfig);
|
||||
|
||||
return {
|
||||
childRouters,
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user