Skip to content

Commit d041434

Browse files
committed
merge in react-history
1 parent 094fcda commit d041434

File tree

10 files changed

+187
-41
lines changed

10 files changed

+187
-41
lines changed

modules/BrowserRouter.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import React, { PropTypes } from 'react'
2-
import BrowserHistory from 'react-history/BrowserHistory'
2+
import createBrowserHistory from 'history/createBrowserHistory'
33
import StaticRouter from './StaticRouter'
4+
import History from './History'
45

5-
/**
6-
* A router that uses the HTML5 history API.
7-
*/
8-
const BrowserRouter = ({ basename, forceRefresh, getUserConfirmation, keyLength, ...props }) => (
9-
<BrowserHistory
10-
basename={basename}
11-
forceRefresh={forceRefresh}
12-
getUserConfirmation={getUserConfirmation}
13-
keyLength={keyLength}
6+
const BrowserRouter = ({
7+
basename,
8+
forceRefresh,
9+
getUserConfirmation,
10+
keyLength,
11+
...routerProps
12+
}) => (
13+
<History
14+
createHistory={createBrowserHistory}
15+
historyOptions={{
16+
basename,
17+
forceRefresh,
18+
getUserConfirmation,
19+
keyLength
20+
}}
1421
>
1522
{({ history, action, location }) => (
1623
<StaticRouter
@@ -20,10 +27,10 @@ const BrowserRouter = ({ basename, forceRefresh, getUserConfirmation, keyLength,
2027
onPush={history.push}
2128
onReplace={history.replace}
2229
blockTransitions={history.block}
23-
{...props}
30+
{...routerProps}
2431
/>
2532
)}
26-
</BrowserHistory>
33+
</History>
2734
)
2835

2936
if (__DEV__) {

modules/HashRouter.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { PropTypes } from 'react'
2-
import HashHistory from 'react-history/HashHistory'
2+
import createHashHistory from 'history/createHashHistory'
3+
import History from './History'
34
import { addLeadingSlash, stripLeadingSlash } from 'history/PathUtils'
45
import StaticRouter from './StaticRouter'
56

@@ -25,11 +26,19 @@ const createHref = hashType => path => {
2526
/**
2627
* A router that uses the URL hash.
2728
*/
28-
const HashRouter = ({ basename, getUserConfirmation, hashType, ...props }) => (
29-
<HashHistory
30-
basename={basename}
31-
getUserConfirmation={getUserConfirmation}
32-
hashType={hashType}
29+
const HashRouter = ({
30+
basename,
31+
getUserConfirmation,
32+
hashType,
33+
...routerProps
34+
}) => (
35+
<History
36+
createHistory={createHashHistory}
37+
historyOptions={{
38+
basename,
39+
getUserConfirmation,
40+
hashType
41+
}}
3342
>
3443
{({ history, action, location }) => (
3544
<StaticRouter
@@ -40,10 +49,10 @@ const HashRouter = ({ basename, getUserConfirmation, hashType, ...props }) => (
4049
onReplace={history.replace}
4150
blockTransitions={history.block}
4251
createHref={createHref(hashType)}
43-
{...props}
52+
{...routerProps}
4453
/>
4554
)}
46-
</HashHistory>
55+
</History>
4756
)
4857

4958
if (__DEV__) {

modules/History.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React, { PropTypes } from 'react'
2+
import { historyContext as historyContextType } from './PropTypes'
3+
4+
/**
5+
* The common public API for all *History components.
6+
*/
7+
class History extends React.Component {
8+
static childContextTypes = {
9+
history: historyContextType.isRequired
10+
}
11+
12+
getChildContext() {
13+
return {
14+
history: this.history
15+
}
16+
}
17+
18+
componentWillMount() {
19+
const { createHistory, historyOptions } = this.props
20+
this.history = createHistory(historyOptions)
21+
this.unlisten = this.history.listen(() => this.forceUpdate())
22+
}
23+
24+
componentWillUnmount() {
25+
this.unlisten()
26+
}
27+
28+
render() {
29+
const history = this.history
30+
const { location, action } = history
31+
32+
return this.props.children({
33+
history,
34+
location,
35+
action
36+
})
37+
}
38+
}
39+
40+
if (__DEV__) {
41+
History.propTypes = {
42+
children: PropTypes.func.isRequired,
43+
createHistory: PropTypes.func.isRequired,
44+
historyOptions: PropTypes.object
45+
}
46+
}
47+
48+
export default History

modules/LocationUtils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { parsePath, createPath } from 'react-history/PathUtils'
2-
3-
export { locationsAreEqual } from 'react-history/LocationUtils'
1+
import { parsePath, createPath } from 'history/PathUtils'
2+
export { locationsAreEqual } from 'history/LocationUtils'
43

54
export const createRouterLocation = (input, parseQueryString, stringifyQuery) => {
65
if (typeof input === 'string') {

modules/MemoryRouter.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
import React, { PropTypes } from 'react'
2-
import MemoryHistory from 'react-history/MemoryHistory'
2+
import createMemoryHistory from 'history/createMemoryHistory'
33
import StaticRouter from './StaticRouter'
4+
import History from './History'
45

5-
/**
6-
* A router that stores all locations in memory.
7-
*/
8-
const MemoryRouter = ({ getUserConfirmation, initialEntries, initialIndex, keyLength, ...props }) => (
9-
<MemoryHistory
10-
getUserConfirmation={getUserConfirmation}
11-
initialEntries={initialEntries}
12-
initialIndex={initialIndex}
13-
keyLength={keyLength}
6+
const MemoryRouter = ({
7+
getUserConfirmation,
8+
initialEntries,
9+
initialIndex,
10+
keyLength,
11+
...routerProps
12+
}) => (
13+
<History
14+
createHistory={createMemoryHistory}
15+
historyOptions={{
16+
getUserConfirmation,
17+
initialEntries,
18+
initialIndex,
19+
keyLength
20+
}}
1421
>
1522
{({ history, action, location }) => (
1623
<StaticRouter
@@ -19,10 +26,10 @@ const MemoryRouter = ({ getUserConfirmation, initialEntries, initialIndex, keyLe
1926
onPush={history.push}
2027
onReplace={history.replace}
2128
blockTransitions={history.block}
22-
{...props}
29+
{...routerProps}
2330
/>
2431
)}
25-
</MemoryHistory>
32+
</History>
2633
)
2734

2835
if (__DEV__) {

modules/NavigationPrompt.js

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
1-
import NavigationPrompt from 'react-history/Prompt'
2-
export default NavigationPrompt
1+
import React, { PropTypes } from 'react'
2+
import { historyContext as historyContextType } from './PropTypes'
3+
4+
class NavigationPrompt extends React.Component {
5+
static contextTypes = {
6+
history: historyContextType.isRequired
7+
}
8+
9+
static defaultProps = {
10+
when: true
11+
}
12+
13+
block() {
14+
if (!this.teardownPrompt)
15+
this.teardownPrompt = this.context.history.block(this.props.message)
16+
}
17+
18+
unblock() {
19+
if (this.teardownPrompt) {
20+
this.teardownPrompt()
21+
this.teardownPrompt = null
22+
}
23+
}
24+
25+
componentWillMount() {
26+
if (this.props.when)
27+
this.block()
28+
}
329

30+
componentWillReceiveProps(nextProps) {
31+
if (nextProps.when) {
32+
this.block()
33+
} else {
34+
this.unblock()
35+
}
36+
}
37+
38+
componentWillUnmount() {
39+
this.unblock()
40+
}
41+
42+
render() {
43+
return null
44+
}
45+
}
46+
47+
if (__DEV__) {
48+
NavigationPrompt.propTypes = {
49+
when: PropTypes.bool,
50+
message: PropTypes.oneOfType([
51+
PropTypes.func,
52+
PropTypes.string
53+
]).isRequired
54+
}
55+
}
56+
57+
export default NavigationPrompt

modules/PropTypes.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,22 @@ export const location = PropTypes.shape({
2727
key: PropTypes.string
2828
})
2929

30+
export const historyContext = PropTypes.shape({
31+
action: action.isRequired,
32+
location: location.isRequired,
33+
push: PropTypes.func.isRequired,
34+
replace: PropTypes.func.isRequired,
35+
go: PropTypes.func.isRequired,
36+
goBack: PropTypes.func.isRequired,
37+
goForward: PropTypes.func.isRequired,
38+
canGo: PropTypes.func,
39+
block: PropTypes.func.isRequired
40+
})
41+
3042
export const routerContext = PropTypes.shape({
3143
transitionTo: PropTypes.func.isRequired,
3244
replaceWith: PropTypes.func.isRequired,
3345
blockTransitions: PropTypes.func.isRequired,
3446
createHref: PropTypes.func.isRequired
3547
})
48+

modules/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export Link from './Link'
22
export Match from './Match'
33
export Miss from './Miss'
4-
export NavigationPrompt from 'react-history/Prompt'
4+
export NavigationPrompt from './NavigationPrompt'
55
export Redirect from './Redirect'
66

77
// High-level wrappers

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"path-to-regexp": "^1.5.3",
3131
"query-string": "4.2.3",
3232
"react-broadcast": "^0.1.1",
33-
"react-history": "^0.14.0"
33+
"history": "^4.3.0"
3434
},
3535
"peerDependencies": {
3636
"react": "15.x"

website/components/FakeBrowser/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import React, { PropTypes } from 'react'
2-
import StaticRouter from '../../../modules/StaticRouter'
3-
import MemoryHistory from 'react-history/MemoryHistory'
2+
import History from '../../../modules/History'
43
import { B, V, H, PAD, LIGHT_GRAY, GRAY } from '../bricks'
54
import { button } from './style.css' // eslint-disable-line
65
import { stringify as stringifyQuery } from 'query-string'
76
import { createPath } from 'history/PathUtils'
7+
import createMemoryHistory from 'history/createMemoryHistory'
8+
9+
const MemoryHistory = ({ children, ...historyOptions }) => (
10+
<History
11+
children={children}
12+
createHistory={createMemoryHistory}
13+
historyOptions={historyOptions}
14+
/>
15+
)
16+
817

918
// have to recreate what StaticRouter does, there should be a way to
1019
// compose?...

0 commit comments

Comments
 (0)