Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept array of strings in route path #5393

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/react-router/modules/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const isEmptyChildren = (children) =>
class Route extends React.Component {
static propTypes = {
computedMatch: PropTypes.object, // private, from <Switch>
path: PropTypes.string,
path: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string)
]),
exact: PropTypes.bool,
strict: PropTypes.bool,
component: PropTypes.func,
Expand Down
30 changes: 30 additions & 0 deletions packages/react-router/modules/__tests__/matchPath-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {})
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/modules/matchPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down