Commit da56e71b by Wee

feat(livePath): livePath can accept array type param now (#2)

the array type param should be a collection of string. If one of string is matched, livePath matches.
parent 69f5c1b7
...@@ -24,7 +24,7 @@ class Route extends React.Component { ...@@ -24,7 +24,7 @@ class Route extends React.Component {
render: PropTypes.func, render: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
location: PropTypes.object, location: PropTypes.object,
livePath: PropTypes.string, livePath: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
alwaysLive: PropTypes.bool, alwaysLive: PropTypes.bool,
name: PropTypes.string // for LiveRoute debug name: PropTypes.string // for LiveRoute debug
} }
...@@ -156,9 +156,9 @@ class Route extends React.Component { ...@@ -156,9 +156,9 @@ class Route extends React.Component {
console.log(`>>> ` + this.props.name + ` <<<`) console.log(`>>> ` + this.props.name + ` <<<`)
// compute if livePath match // compute if livePath match
const livePath = nextProps.livePath const livePath = nextProps.livePath
const nextPropsWithLivePath = { ...nextProps, path: livePath } const nextPropsWithLivePath = { ...nextProps, paths: livePath }
const prevMatch = this.computeMatch(props, this.context.router) const prevMatch = this.computeMatch(props, this.context.router)
const livePathMatch = this.computeMatch(nextPropsWithLivePath, nextContext.router) const livePathMatch = this.computePathsMatch(nextPropsWithLivePath, nextContext.router)
if (match) { if (match) {
// normal matched render // normal matched render
console.log('--- NORMAL MATCH FLAG ---') console.log('--- NORMAL MATCH FLAG ---')
...@@ -184,10 +184,32 @@ class Route extends React.Component { ...@@ -184,10 +184,32 @@ class Route extends React.Component {
} }
} }
computeMatch({ computedMatch, location, path, strict, exact, sensitive }, router) { computePathsMatch({ computedMatch, location, paths, strict, exact, sensitive }, router) {
// react-live-route: ignore match from <Switch>, actually LiveRoute should not be wrapped by <Switch>. invariant(router, 'You should not use <Route> or withRouter() outside a <Router>')
const { route } = router
const pathname = (location || route.location).pathname
// livePath could accept a string or an array of string
if (Array.isArray(paths)) {
for (let path of paths) {
if (typeof path !== 'string') {
continue
}
const currPath = matchPath(pathname, { path, strict, exact, sensitive }, router.match)
// return if one of the livePaths is matched
if (currPath) {
return currPath
}
}
return null
} else {
return matchPath(pathname, { path: paths, strict, exact, sensitive }, router.match)
}
}
computeMatch({ computedMatch, location, path, strict, exact, sensitive }, router) {
// DO NOT use the computedMatch from Switch! // DO NOT use the computedMatch from Switch!
// react-live-route: ignore match from <Switch>, actually LiveRoute should not be wrapped by <Switch>.
// if (computedMatch) return computedMatch // <Switch> already computed the match for us // if (computedMatch) return computedMatch // <Switch> already computed the match for us
invariant(router, 'You should not use <Route> or withRouter() outside a <Router>') invariant(router, 'You should not use <Route> or withRouter() outside a <Router>')
......
{ {
"name": "react-live-route", "name": "react-live-route",
"version": "1.1.17", "version": "1.2.4",
"description": "A living route for react-router-v4", "description": "A living route for react-router-v4",
"repository": "fi3ework/react-live-route", "repository": "fi3ework/react-live-route",
"license": "MIT", "license": "MIT",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment