This repository has been archived by the owner on Dec 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Dynamic fragments #189
Merged
Merged
Dynamic fragments #189
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { RouterProvider } from '../../src'; | ||
|
||
export default store => Root => | ||
<Provider store={store}> | ||
<RouterProvider store={store}> | ||
<Root /> | ||
</RouterProvider> | ||
<Root /> | ||
</Provider>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,67 @@ | ||
// @flow | ||
/* eslint-disable react/sort-comp */ | ||
import type { Location } from '../types'; | ||
|
||
import UrlPattern from 'url-pattern'; | ||
import React, { Children, Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { compose, withContext, getContext } from 'recompose'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import matchCache from '../util/match-cache'; | ||
import generateId from '../util/generate-id'; | ||
|
||
const withId = ComposedComponent => | ||
class WithId extends Component { | ||
id: string; | ||
|
||
constructor() { | ||
super(); | ||
this.id = generateId(); | ||
} | ||
|
||
render() { | ||
return <ComposedComponent {...this.props} id={ this.id } />; | ||
} | ||
}; | ||
|
||
const resolveChildRoute = (parentRoute, currentRoute) => { | ||
const parentIsRootRoute = | ||
parentRoute && | ||
parentRoute !== '/' && | ||
parentRoute !== currentRoute; | ||
|
||
return parentIsRootRoute | ||
? `${parentRoute}${currentRoute || ''}` | ||
: currentRoute; | ||
}; | ||
|
||
const resolveCurrentRoute = (parentRoute, currentRoute) => { | ||
if (!currentRoute) { return null; } | ||
|
||
// First route will always be a wildcard | ||
if (!parentRoute) { return `${currentRoute}*`; } | ||
|
||
const currentIsRootRoute = currentRoute === '/'; | ||
const parentIsRootRoute = parentRoute === '/'; | ||
|
||
// Only prefix non-root parent routes | ||
const routePrefix = !parentIsRootRoute && parentRoute || ''; | ||
|
||
// Support "index" routes: | ||
// <Fragment forRoute='/home'> | ||
// <Fragment forRoute='/'> | ||
// </Fragment> | ||
// </Fragment> | ||
const routeSuffix = currentIsRootRoute && | ||
!parentIsRootRoute ? '' : currentRoute; | ||
|
||
const wildcard = currentIsRootRoute && | ||
parentIsRootRoute ? '' : '*'; | ||
|
||
return `${routePrefix}${routeSuffix}${wildcard}`; | ||
}; | ||
|
||
type Props = { | ||
location: Location, | ||
matchRoute: Function, | ||
|
@@ -17,111 +73,78 @@ type Props = { | |
children: React.Element<*> | ||
}; | ||
|
||
const relativePaths = (ComposedComponent: ReactClass<*>) => { | ||
class RelativeFragment extends Component { | ||
constructor() { | ||
super(); | ||
this.id = generateId(); | ||
} | ||
class Fragment extends Component { | ||
matcher: ?Object; | ||
|
||
getChildContext() { | ||
const { parentRoute } = this.context; | ||
const { forRoute } = this.props; | ||
|
||
return { | ||
// Append the parent route if this isn't the first | ||
// RelativeFragment in the hierarchy. | ||
parentRoute: parentRoute && | ||
parentRoute !== '/' && | ||
parentRoute !== forRoute | ||
? `${parentRoute}${forRoute || ''}` | ||
: forRoute, | ||
parentId: this.id | ||
}; | ||
} | ||
constructor(props: Props, context: typeof Fragment.contextTypes) { | ||
super(props, context); | ||
|
||
props: Props; | ||
id: string; | ||
const currentRoute = resolveCurrentRoute( | ||
props.parentRoute, | ||
props.forRoute | ||
); | ||
|
||
render() { | ||
const { children, forRoute, ...rest } = this.props; | ||
const { router, parentRoute, parentId } = this.context; | ||
const { store } = router; | ||
|
||
const location = store.getState().router; | ||
|
||
const routePrefix = parentRoute && parentRoute !== '/' | ||
? parentRoute : ''; | ||
|
||
const routeSuffix = (forRoute === '/' && parentRoute && parentRoute !== '/') | ||
? '' : forRoute || ''; | ||
|
||
const combinedRoute = forRoute && `${routePrefix}${routeSuffix}`; | ||
|
||
return ( | ||
<ComposedComponent | ||
parentId={parentId} | ||
location={location} | ||
matchRoute={store.matchRoute} | ||
matchWildcardRoute={store.matchWildcardRoute} | ||
parentRoute={parentRoute} | ||
forRoute={combinedRoute} | ||
children={children} | ||
{...rest} | ||
/> | ||
); | ||
} | ||
this.matcher = currentRoute && | ||
new UrlPattern(currentRoute) || null; | ||
} | ||
|
||
// Consumes this context... | ||
RelativeFragment.contextTypes = { | ||
router: PropTypes.object, | ||
parentRoute: PropTypes.string, | ||
parentId: PropTypes.string | ||
}; | ||
componentWillReceiveProps(nextProps: Props) { | ||
if (this.props.forRoute !== nextProps.forRoute) { | ||
throw new Error('Updating route props is not yet supported.'); | ||
} | ||
} | ||
|
||
// ...and provides this context. | ||
RelativeFragment.childContextTypes = { | ||
parentRoute: PropTypes.string, | ||
parentId: PropTypes.string | ||
}; | ||
render() { | ||
const { matcher } = this; | ||
const { | ||
children, | ||
forRoute, | ||
withConditions, | ||
location, | ||
parentRoute, | ||
parentId | ||
} = this.props; | ||
|
||
return RelativeFragment; | ||
}; | ||
const currentRoute = resolveCurrentRoute(parentRoute, forRoute); | ||
|
||
const Fragment = (props: Props) => { | ||
const { | ||
location, | ||
matchRoute, | ||
matchWildcardRoute, | ||
forRoute, | ||
withConditions, | ||
children, | ||
parentId, | ||
parentRoute | ||
} = props; | ||
|
||
const matcher = (forRoute === parentRoute) ? matchRoute : matchWildcardRoute; | ||
const matchResult = matcher(location.pathname, forRoute); | ||
|
||
if ( | ||
!matchResult || | ||
(withConditions && !withConditions(location)) || | ||
(forRoute && matchResult.route !== forRoute) | ||
) { | ||
return null; | ||
} | ||
if (matcher && !matcher.match(location.pathname)) { | ||
return null; | ||
} | ||
|
||
if (parentId) { | ||
const previousMatch = matchCache.get(parentId); | ||
if (previousMatch && previousMatch !== forRoute) { | ||
if (withConditions && !withConditions(location)) { | ||
return null; | ||
} else { | ||
matchCache.add(parentId, forRoute); | ||
} | ||
} | ||
|
||
return Children.only(children); | ||
}; | ||
if (parentId) { | ||
const previousMatch = matchCache.get(parentId); | ||
if (previousMatch && previousMatch !== currentRoute) { | ||
return null; | ||
} else { | ||
matchCache.add(parentId, currentRoute); | ||
} | ||
} | ||
|
||
export default relativePaths(Fragment); | ||
return Children.only(children); | ||
} | ||
} | ||
|
||
export default compose( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is some next-level composition 馃敟 |
||
connect(state => ({ | ||
location: state.router | ||
})), | ||
getContext({ | ||
parentRoute: PropTypes.string, | ||
parentId: PropTypes.string | ||
}), | ||
withId, | ||
withContext({ | ||
parentRoute: PropTypes.string, | ||
parentId: PropTypes.string | ||
}, props => ({ | ||
parentRoute: resolveChildRoute( | ||
props.parentRoute, | ||
props.forRoute | ||
), | ||
parentId: props.id | ||
})) | ||
)(Fragment); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe you need
context
to be declared insuper()
if it's not being used in the constructorThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also don't need to pass
props
, unless you're accessingthis.props
in the constructor. React will populate them both on the instance for you.