Skip to content

Commit

Permalink
Dependency inject React.
Browse files Browse the repository at this point in the history
Allows use with React Native or other custom React version.
  • Loading branch information
skevy committed Sep 21, 2015
1 parent 8ca59b7 commit 3fc8acb
Show file tree
Hide file tree
Showing 31 changed files with 301 additions and 108 deletions.
9 changes: 7 additions & 2 deletions modules/History.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { history } from './PropTypes'
import createPropTypes from './PropTypes'

export default function createHistory(React) {
const { history } = createPropTypes(React)

const History = {

Expand All @@ -10,4 +13,6 @@ const History = {

}

export default History
return History

}
9 changes: 6 additions & 3 deletions modules/IndexLink.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react'
import Link from './Link'
import createLink from './Link'

export default function createIndexLink(React) {
const Link = createLink(React)

const IndexLink = React.createClass({

Expand All @@ -9,4 +11,5 @@ const IndexLink = React.createClass({

})

export default IndexLink
return IndexLink
}
13 changes: 9 additions & 4 deletions modules/IndexRoute.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from 'react'
import invariant from 'invariant'
import warning from 'warning'
import { createRouteFromReactElement } from './RouteUtils'
import { component, components, falsy } from './PropTypes'
import createRouteUtils from './RouteUtils'
import createPropTypes from './PropTypes'

export default function createIndexRoute(React) {
const { createRouteFromReactElement } = createRouteUtils(React)
const { component, components, falsy } = createPropTypes(React)

const { bool, func } = React.PropTypes

Expand Down Expand Up @@ -44,4 +47,6 @@ const IndexRoute = React.createClass({

})

export default IndexRoute
return IndexRoute

}
7 changes: 5 additions & 2 deletions modules/Lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import invariant from 'invariant'

export default function createLifecycle(React) {

const { object } = React.PropTypes

/**
Expand Down Expand Up @@ -66,4 +67,6 @@ const Lifecycle = {

}

export default Lifecycle
return Lifecycle

}
9 changes: 5 additions & 4 deletions modules/Link.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import React from 'react'
import warning from 'warning'

const { bool, object, string, func } = React.PropTypes

function isLeftClickEvent(event) {
return event.button === 0
}
Expand All @@ -19,6 +16,9 @@ function isEmptyObject(object) {
return true
}

export default function createLink(React) {
const { bool, object, string, func } = React.PropTypes

/**
* A <Link> is used to create an <a> element that links to a route.
* When that route is active, the link gets an "active" class name
Expand Down Expand Up @@ -115,4 +115,5 @@ const Link = React.createClass({

})

export default Link
return Link
}
25 changes: 14 additions & 11 deletions modules/PropTypes.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import { PropTypes } from 'react'
export default function createPropTypes(React) {

const { func, object, arrayOf, oneOfType, element, shape, string } = PropTypes
const { func, object, arrayOf, oneOfType, element, shape, string } = React.PropTypes

export function falsy(props, propName, componentName) {
function falsy(props, propName, componentName) {
if (props[propName])
return new Error(`<${componentName}> should not have a "${propName}" prop`)
}

export const history = shape({
const history = shape({
listen: func.isRequired,
pushState: func.isRequired,
replaceState: func.isRequired,
go: func.isRequired
})

export const location = shape({
const location = shape({
pathname: string.isRequired,
search: string.isRequired,
state: object,
action: string.isRequired,
key: string
})

export const component = oneOfType([ func, string ])
export const components = oneOfType([ component, object ])
export const route = oneOfType([ object, element ])
export const routes = oneOfType([ route, arrayOf(route) ])
const component = oneOfType([ func, string ])
const components = oneOfType([ component, object ])
const route = oneOfType([ object, element ])
const routes = oneOfType([ route, arrayOf(route) ])

export default {
return {
falsy,
history,
location,
component,
components,
route
route,
routes
}

}
14 changes: 10 additions & 4 deletions modules/Redirect.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React from 'react'
import invariant from 'invariant'
import { createRouteFromReactElement } from './RouteUtils'
import createRouteUtils from './RouteUtils'
import { formatPattern } from './PatternUtils'
import { falsy } from './PropTypes'
import createPropTypes from './PropTypes'

export default function createRedirect(React) {
const { createRouteFromReactElement } = createRouteUtils(React)

const { falsy } = createPropTypes(React)

const { string, object } = React.PropTypes

Expand Down Expand Up @@ -64,4 +68,6 @@ const Redirect = React.createClass({

})

export default Redirect
return Redirect

}
15 changes: 11 additions & 4 deletions modules/Route.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import React from 'react'
import warning from 'warning'
import invariant from 'invariant'
import { createRouteFromReactElement } from './RouteUtils'
import { component, components } from './PropTypes'

import createRouteUtils from './RouteUtils'
import createPropTypes from './PropTypes'

export default function createRoute(React) {

const { createRouteFromReactElement } = createRouteUtils(React)
const { component, components } = createPropTypes(React)

const { string, bool, func } = React.PropTypes

Expand Down Expand Up @@ -56,4 +61,6 @@ const Route = React.createClass({

})

export default Route
return Route

}
6 changes: 4 additions & 2 deletions modules/RouteContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
export default function createRouteContext(React) {

const { object } = React.PropTypes

Expand Down Expand Up @@ -26,4 +26,6 @@ const RouteContext = {

}

export default RouteContext
return RouteContext

}
20 changes: 15 additions & 5 deletions modules/RouteUtils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react'
import warning from 'warning'

export default function createRouteUtils(React) {

function isValidChild(object) {
return object == null || React.isValidElement(object)
}

export function isReactChildren(object) {
function isReactChildren(object) {
return isValidChild(object) || (Array.isArray(object) && object.every(isValidChild))
}

Expand All @@ -26,7 +27,7 @@ function createRoute(defaultProps, props) {
return { ...defaultProps, ...props }
}

export function createRouteFromReactElement(element) {
function createRouteFromReactElement(element) {
const type = element.type
const route = createRoute(type.defaultProps, element.props)

Expand Down Expand Up @@ -62,7 +63,7 @@ export function createRouteFromReactElement(element) {
* Note: This method is automatically used when you provide <Route> children
* to a <Router> component.
*/
export function createRoutesFromReactChildren(children, parentRoute) {
function createRoutesFromReactChildren(children, parentRoute) {
const routes = []

React.Children.forEach(children, function (element) {
Expand All @@ -86,7 +87,7 @@ export function createRoutesFromReactChildren(children, parentRoute) {
* Creates and returns an array of routes from the given object which
* may be a JSX route, a plain object route, or an array of either.
*/
export function createRoutes(routes) {
function createRoutes(routes) {
if (isReactChildren(routes)) {
routes = createRoutesFromReactChildren(routes)
} else if (!Array.isArray(routes)) {
Expand All @@ -95,3 +96,12 @@ export function createRoutes(routes) {

return routes
}

return {
isReactChildren,
createRouteFromReactElement,
createRoutesFromReactChildren,
createRoutes
}

}
17 changes: 11 additions & 6 deletions modules/Router.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react'
import warning from 'warning'
import createHashHistory from 'history/lib/createHashHistory'
import { createRoutes } from './RouteUtils'
import RoutingContext from './RoutingContext'
import useRoutes from './useRoutes'
import { routes } from './PropTypes'
import createRouteUtils from './RouteUtils'
import createRoutingContext from './RoutingContext'
import createUseRoutes from './useRoutes'
import createPropTypes from './PropTypes'

export default function createRouter(React) {
const { createRoutes } = createRouteUtils(React)
const RoutingContext = createRoutingContext(React)
const useRoutes = createUseRoutes(React)
const { routes } = createPropTypes(React)
const { func, object } = React.PropTypes

/**
Expand Down Expand Up @@ -94,4 +98,5 @@ const Router = React.createClass({

})

export default Router
return Router
}
7 changes: 5 additions & 2 deletions modules/RoutingContext.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react'
import invariant from 'invariant'
import getRouteParams from './getRouteParams'

export default function createRoutingContext(React) {

const { array, func, object } = React.PropTypes

/**
Expand Down Expand Up @@ -88,4 +89,6 @@ const RoutingContext = React.createClass({

})

export default RoutingContext
return RoutingContext

}
15 changes: 10 additions & 5 deletions modules/__tests__/History-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/*eslint-env mocha */
import expect from 'expect'
import React from 'react'
import History from '../History'
import Router from '../Router'
import Route from '../Route'
import createHistory from 'history/lib/createMemoryHistory'
import createHistory from '../History'
import createRouter from '../Router'
import createRoute from '../Route'
import createMemoryHistory from 'history/lib/createMemoryHistory'

const History = createHistory(React)
const Router = createRouter(React)
const Route = createRoute(React)


describe('History Mixin', function () {

Expand All @@ -18,7 +23,7 @@ describe('History Mixin', function () {
})

it('assigns the history to the component instance', function (done) {
let history = createHistory('/')
let history = createMemoryHistory('/')

function assertHistory() {
expect(this.history).toExist()
Expand Down
10 changes: 7 additions & 3 deletions modules/__tests__/IndexRoute-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import expect from 'expect'
import React from 'react'
import createHistory from 'history/lib/createMemoryHistory'
import IndexRoute from '../IndexRoute'
import Router from '../Router'
import Route from '../Route'
import createIndexRoute from '../IndexRoute'
import createRouter from '../Router'
import createRoute from '../Route'

const IndexRoute = createIndexRoute(React)
const Router = createRouter(React)
const Route = createRoute(React)

describe('an <IndexRoute/>', function () {

Expand Down
10 changes: 7 additions & 3 deletions modules/__tests__/Link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import expect from 'expect'
import React from 'react/addons'
import createHistory from 'history/lib/createMemoryHistory'
import execSteps from './execSteps'
import Router from '../Router'
import Route from '../Route'
import Link from '../Link'
import createRouter from '../Router'
import createRoute from '../Route'
import createLink from '../Link'

const Router = createRouter(React)
const Route = createRoute(React)
const Link = createLink(React)

const { click } = React.addons.TestUtils.Simulate

Expand Down
10 changes: 7 additions & 3 deletions modules/__tests__/Redirect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
import expect from 'expect'
import React from 'react'
import createHistory from 'history/lib/createMemoryHistory'
import Redirect from '../Redirect'
import Router from '../Router'
import Route from '../Route'
import createRedirect from '../Redirect'
import createRouter from '../Router'
import createRoute from '../Route'

const Redirect = createRedirect(React)
const Router = createRouter(React)
const Route = createRoute(React)

describe('A <Redirect>', function () {

Expand Down
Loading

0 comments on commit 3fc8acb

Please sign in to comment.