From 9e99bfdad3bfb89631ff53db949f171a8b36adcd Mon Sep 17 00:00:00 2001 From: Shervin Aflatooni Date: Tue, 1 Aug 2017 16:41:54 +1000 Subject: [PATCH 1/2] Add support for array of strings in route path --- packages/react-router/modules/Route.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/react-router/modules/Route.js b/packages/react-router/modules/Route.js index 9d2a14dee7..61c9203c11 100644 --- a/packages/react-router/modules/Route.js +++ b/packages/react-router/modules/Route.js @@ -13,7 +13,10 @@ const isEmptyChildren = (children) => class Route extends React.Component { static propTypes = { computedMatch: PropTypes.object, // private, from - path: PropTypes.string, + path: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.arrayOf(PropTypes.string) + ]), exact: PropTypes.bool, strict: PropTypes.bool, component: PropTypes.func, From 60339728fa0252408b3b7de4212c62b066126600 Mon Sep 17 00:00:00 2001 From: Shervin Aflatooni Date: Wed, 2 Aug 2017 11:48:06 +1000 Subject: [PATCH 2/2] add tests to handle path as an array and fix code --- .../modules/__tests__/matchPath-test.js | 30 +++++++++++++++++++ packages/react-router/modules/matchPath.js | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/react-router/modules/__tests__/matchPath-test.js b/packages/react-router/modules/__tests__/matchPath-test.js index 765b4001cf..ec512cab76 100644 --- a/packages/react-router/modules/__tests__/matchPath-test.js +++ b/packages/react-router/modules/__tests__/matchPath-test.js @@ -33,6 +33,36 @@ describe('matchPath', () => { }) }) + describe('with an array of paths', () => { + it('return the correct url at "/elsewhere"', () => { + const path = ['/somewhere', '/elsewhere'] + const pathname = '/elsewhere' + const match = matchPath(pathname, { path }) + expect(match.url).toBe('/elsewhere') + }) + + it('returns correct url at "/elsewhere/else"', () => { + const path = ['/somewhere', '/elsewhere'] + const pathname = '/elsewhere/else' + const match = matchPath(pathname, { path }) + expect(match.url).toBe('/elsewhere') + }) + + it('returns correct url at "/elsewhere/else" with path "/" in array', () => { + const path = ['/somewhere', '/'] + const pathname = '/elsewhere/else' + const match = matchPath(pathname, { path }) + expect(match.url).toBe('/') + }) + + it('returns correct url at "/somewhere" with path "/" in array', () => { + const path = ['/somewhere', '/'] + const pathname = '/somewhere' + const match = matchPath(pathname, { path }) + expect(match.url).toBe('/somewhere') + }) + }) + describe('with no path', () => { it('matches the root URL', () => { const match = matchPath('/test-location/7', {}) diff --git a/packages/react-router/modules/matchPath.js b/packages/react-router/modules/matchPath.js index 91b1656c16..c5b52f802b 100644 --- a/packages/react-router/modules/matchPath.js +++ b/packages/react-router/modules/matchPath.js @@ -45,7 +45,7 @@ const matchPath = (pathname, options = {}) => { return { path, // the path pattern used to match - url: path === '/' && url === '' ? '/' : url, // the matched portion of the URL + url: url === '' ? '/' : url, // the matched portion of the URL isExact, // whether or not we matched exactly params: keys.reduce((memo, key, index) => { memo[key.name] = values[index]