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

Support react-router-relay via applyRouterMiddleware. #23

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
64 changes: 48 additions & 16 deletions modules/RouteUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,71 @@ export function createRouteFromReactElement(
return _createRouteFromReactElement(element);
}

function getPropsFromRoute(route: RouteDef): Object {
const {
childRoutes,
component,
overlayComponent,
path,
routeType,
transition,
reducer,
indexRoute,
...rest,
} = route;
return rest;
}

function createNavigationTreeAtIndex(
createElement: ElementProvider,
routes: Array<RouteDef>,
route: RouteDef,
positionInParent: number
): ?PseudoElement {
const props = {};
const {
childRoutes,
component,
overlayComponent,
path,
routeType,
indexRoute,
} = route;

const props = {
...getPropsFromRoute(route),
route,
routes,
};

props.createElement = createElement;
props.path = route.path || `[visual]${positionInParent}`;
props.type = route.routeType;
props.component = route.component;
props.path = path || `[visual]${positionInParent}`;
props.type = routeType;
props.component = component;

if (route.overlayComponent) {
props.overlayComponent = route.overlayComponent;
if (overlayComponent) {
props.overlayComponent = overlayComponent;
}

if (route.childRoutes) {
props.navigationSubtree = route.childRoutes.map(
if (childRoutes) {
props.navigationSubtree = childRoutes.map(
(r, index) => createNavigationTreeAtIndex(createElement, routes, r, index)
);

// index route is given in `routes` but not in `childRoutes`
if (route.indexRoute) {
const indexRouteProps = {};
if (indexRoute) {
const indexRouteProps = {
...getPropsFromRoute(indexRoute),
route: indexRoute,
routes,
};

indexRouteProps.path = '[index]';
indexRouteProps.type = 'index';
indexRouteProps.component = route.indexRoute.component;
indexRouteProps.component = indexRoute.component;
indexRouteProps.createElement = createElement;

if (route.indexRoute.overlayComponent) {
indexRouteProps.overlayComponent = route.indexRoute.overlayComponent;
if (indexRoute.overlayComponent) {
indexRouteProps.overlayComponent = indexRoute.overlayComponent;
}

const indexRoutePseudoElement = {
Expand All @@ -94,13 +126,14 @@ function createNavigationTreeAtIndex(
}

let pseudoElement;
if (route.routeType === STACK_ROUTE) {
if (routeType === STACK_ROUTE) {
pseudoElement = { routeViewComponent: StackRouteView, props };
} else if (route.routeType === TABS_ROUTE) {
} else if (routeType === TABS_ROUTE) {
pseudoElement = { routeViewComponent: TabsRouteView, props };
} else {
pseudoElement = { routeViewComponent: RouteView, props };
}

return pseudoElement;
}

Expand All @@ -109,7 +142,6 @@ export function createNavigationTree(
routes: Array<RouteDef>
): ?PseudoElement {
const rootRoute = routes && routes.length && routes[0];

if (!rootRoute) {
return null;
}
Expand Down
16 changes: 16 additions & 0 deletions modules/RouteView.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ class RouteView extends Component<any, Props, any> {
);
}

getComponentProps(props: Object): Object {
const {
path,
type,
component,
overlayComponent,
navigationSubtree,
navigationState,
createElement,
onNavigate,
...rest,
} = props;
return rest;
}

renderTransition(props: NavigationTransitionProps): ReactElement<any> {
const scenes = props.scenes.map(
scene => this.renderScene({
Expand Down Expand Up @@ -119,6 +134,7 @@ class RouteView extends Component<any, Props, any> {
}

const componentProps = {
...this.getComponentProps(this.props),
params,
routeParams,
location,
Expand Down
2 changes: 2 additions & 0 deletions modules/RouterContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class RouterContext extends Component<any, any, any> {
navigationTree,
navigationState,
location,
route: {}, // TODO
routes: this.props.routes,
};

element = React.createElement(RootContainer, passProps);
Expand Down
17 changes: 17 additions & 0 deletions modules/StackRouteView.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class StackRouteView extends Component<any, Props, any> {
const { location, params, routeParams } = scene.route;

const overlayComponentProps = {
...this.getComponentProps(navigationalElement.props),
...props,
location,
params,
Expand Down Expand Up @@ -140,6 +141,21 @@ class StackRouteView extends Component<any, Props, any> {
);
}

getComponentProps(props: Object): Object {
const {
path,
type,
component,
overlayComponent,
navigationSubtree,
navigationState,
createElement,
onNavigate,
...rest,
} = props;
return rest;
}

renderTransition(props: NavigationTransitionProps): ReactElement<any> {
const overlay = this.renderOverlay({
...props,
Expand Down Expand Up @@ -203,6 +219,7 @@ class StackRouteView extends Component<any, Props, any> {
}

const componentProps = {
...this.getComponentProps(this.props),
params,
routeParams,
location,
Expand Down
18 changes: 17 additions & 1 deletion modules/TabsRouteView.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class TabsRouteView extends Component<any, Props, any> {
const overlayComponent = navigationalElement.props.overlayComponent;
if (overlayComponent) {
const { location, params, routeParams } = scene.route;

const overlayComponentProps = {
...this.getComponentProps(navigationalElement.props),
...props,
location,
params,
Expand Down Expand Up @@ -140,6 +140,21 @@ class TabsRouteView extends Component<any, Props, any> {
);
}

getComponentProps(props: Object): Object {
const {
path,
type,
component,
overlayComponent,
navigationSubtree,
navigationState,
createElement,
onNavigate,
...rest,
} = props;
return rest;
}

renderTransition(props: NavigationTransitionProps): ReactElement<any> {
const overlay = this.renderOverlay({
...props,
Expand Down Expand Up @@ -203,6 +218,7 @@ class TabsRouteView extends Component<any, Props, any> {
}

const componentProps = {
...this.getComponentProps(this.props),
params,
routeParams,
location,
Expand Down
1 change: 1 addition & 0 deletions modules/TypeDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type RouteDef = {
routeType: ?RouteType,
transition: ?string,
reducer: Function,
indexRoute: ?Object,
};

export type IndexRouteDef = {
Expand Down
37 changes: 37 additions & 0 deletions modules/applyRouterMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* This file is subject to the terms and conditions defined in the LICENSE file
* which is found in the in the root directory of React Router source tree.
*
* https://github.com/reactjs/react-router/blob/master/LICENSE.md
*/

import React, { createElement } from 'react';
import RouterContext from './RouterContext';

export default (...middlewares) => {
const withContext = middlewares.map(m => m.renderRouterContext).filter(f => f);
const withComponent = middlewares.map(m => m.renderRouteComponent).filter(f => f);
const makeCreateElement = (baseCreateElement = createElement) => (
(Component, props) => (
withComponent.reduceRight(
(previous, renderRouteComponent) => (
renderRouteComponent(previous, props)
), baseCreateElement(Component, props)
)
)
);

return (renderProps) => (
withContext.reduceRight(
(previous, renderRouterContext) => (
renderRouterContext(previous, renderProps)
), (
<RouterContext
{...renderProps}
createElement={makeCreateElement(renderProps.createElement)}
/>
)
)
);
};

3 changes: 2 additions & 1 deletion modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ export { IndexRoute, Route, StackRoute, TabsRoute };
import RouterContext from './RouterContext';
import Reducer from './Reducer';
import { render } from './render';
import applyRouterMiddleware from './applyRouterMiddleware';

export { RouterContext, Reducer, render };
export { RouterContext, Reducer, render, applyRouterMiddleware };

/* histories */
import nativeHistory from './nativeHistory';
Expand Down