Skip to content

Commit b61ae2c

Browse files
committed
Add <History basename>
Fixes #2
1 parent 892226d commit b61ae2c

7 files changed

+99
-12
lines changed

modules/BrowserHistory.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const getHistoryState = () => {
2929
*/
3030
class BrowserHistory extends React.Component {
3131
static propTypes = {
32+
basename: PropTypes.string,
3233
children: PropTypes.func.isRequired,
3334
keyLength: PropTypes.number
3435
}
@@ -278,11 +279,12 @@ class BrowserHistory extends React.Component {
278279
}
279280

280281
render() {
281-
const { children } = this.props
282+
const { basename, children } = this.props
282283
const { action, location } = this.state
283284

284285
return (
285286
<HistoryContext
287+
basename={basename}
286288
children={children}
287289
action={action}
288290
location={location}

modules/HashHistory.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const replaceHashPath = (path) => {
5757
*/
5858
class HashHistory extends React.Component {
5959
static propTypes = {
60+
basename: PropTypes.string,
6061
children: PropTypes.func.isRequired,
6162
hashType: PropTypes.oneOf(Object.keys(HashPathCoders))
6263
}
@@ -309,11 +310,12 @@ class HashHistory extends React.Component {
309310
}
310311

311312
render() {
312-
const { children } = this.props
313+
const { basename, children } = this.props
313314
const { action, location } = this.state
314315

315316
return (
316317
<HistoryContext
318+
basename={basename}
317319
children={children}
318320
action={action}
319321
location={location}

modules/HistoryContext.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ import {
66
location as locationType
77
} from './PropTypes'
88

9+
const stripPrefix = (prefix, string) =>
10+
string.indexOf(prefix) === 0 ? string.substring(prefix.length) : string
11+
912
/**
1013
* The common public API for all *History components.
1114
*/
1215
class HistoryContext extends React.Component {
1316
static propTypes = {
17+
basename: PropTypes.string,
1418
children: PropTypes.func.isRequired,
1519
action: actionType.isRequired,
1620
location: locationType.isRequired,
@@ -20,34 +24,42 @@ class HistoryContext extends React.Component {
2024
go: PropTypes.func.isRequired
2125
}
2226

27+
static defaultProps = {
28+
basename: ''
29+
}
30+
2331
static childContextTypes = {
2432
history: historyContextType.isRequired
2533
}
2634

2735
getChildContext() {
28-
const { prompt, push, replace, go } = this.props
29-
3036
return {
3137
history: {
32-
prompt,
33-
push,
34-
replace,
35-
go
38+
prompt: this.props.prompt,
39+
push: this.push,
40+
replace: this.replace,
41+
go: this.props.go
3642
}
3743
}
3844
}
3945

46+
push = (path, state) =>
47+
this.props.push(this.props.basename + path, state)
48+
49+
replace = (path, state) =>
50+
this.props.replace(this.props.basename + path, state)
51+
4052
render() {
41-
const { action, location } = this.props
53+
const { basename, children, action, location } = this.props
4254

4355
const { path, ...everythingElse } = location
4456
const { pathname, search, hash } = parsePath(path)
4557

46-
return this.props.children({
58+
return children({
4759
action,
4860
location: {
4961
...everythingElse,
50-
pathname,
62+
pathname: basename ? stripPrefix(basename, pathname) : pathname,
5163
search,
5264
hash
5365
}

modules/MemoryHistory.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const clamp = (n, lowerBound, upperBound) =>
1212
*/
1313
class MemoryHistory extends React.Component {
1414
static propTypes = {
15+
basename: PropTypes.string,
1516
children: PropTypes.func.isRequired,
1617
initialEntries: PropTypes.array,
1718
initialIndex: PropTypes.number,
@@ -159,12 +160,13 @@ class MemoryHistory extends React.Component {
159160
}
160161

161162
render() {
162-
const { children } = this.props
163+
const { basename, children } = this.props
163164
const { action, index, entries } = this.state
164165
const location = entries[index]
165166

166167
return (
167168
<HistoryContext
169+
basename={basename}
168170
children={children}
169171
action={action}
170172
location={location}

modules/__tests__/BrowserHistory-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ describe('BrowserHistory', () => {
6969
})
7070
})
7171

72+
describe('with a basename', () => {
73+
describe('push', () => {
74+
it('emits a new location', (done) => {
75+
const children = RenderTestSequences.PushEmitsANewLocation(done)
76+
render(<BrowserHistory basename="/base" children={children}/>, node)
77+
})
78+
})
79+
80+
describe('replace', () => {
81+
it('emits a new location', (done) => {
82+
const children = RenderTestSequences.ReplaceEmitsANewLocation(done)
83+
render(<BrowserHistory basename="/base" children={children}/>, node)
84+
})
85+
})
86+
87+
describe('pop', () => {
88+
it('emits a new location', (done) => {
89+
const children = RenderTestSequences.PopEmitsANewLocation(done)
90+
render(<BrowserHistory basename="/base" children={children}/>, node)
91+
})
92+
})
93+
})
94+
7295
describe('prompt', () => {
7396
it('blocks a push', (done) => {
7497
const children = RenderTestSequences.PromptBlocksAPush(done)

modules/__tests__/HashHistory-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ describe('HashHistory', () => {
5959
})
6060
})
6161

62+
describe('with a basename', () => {
63+
describe('push', () => {
64+
it('emits a new location', (done) => {
65+
const children = RenderTestSequences.PushEmitsANewLocation(done)
66+
render(<HashHistory basename="/base" children={children}/>, node)
67+
})
68+
})
69+
70+
describe('replace', () => {
71+
it('emits a new location', (done) => {
72+
const children = RenderTestSequences.ReplaceEmitsANewLocation(done)
73+
render(<HashHistory basename="/base" children={children}/>, node)
74+
})
75+
})
76+
77+
describe('pop', () => {
78+
it('emits a new location', (done) => {
79+
const children = RenderTestSequences.PopEmitsANewLocation(done)
80+
render(<HashHistory basename="/base" children={children}/>, node)
81+
})
82+
})
83+
})
84+
6285
describe('prompt', () => {
6386
it('blocks a push', (done) => {
6487
const children = RenderTestSequences.PromptBlocksAPush(done)

modules/__tests__/MemoryHistory-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ describe('MemoryHistory', () => {
5353
})
5454
})
5555

56+
describe('with a basename', () => {
57+
describe('push', () => {
58+
it('emits a new location', (done) => {
59+
const children = RenderTestSequences.PushEmitsANewLocation(done)
60+
render(<MemoryHistory basename="/base" children={children}/>, node)
61+
})
62+
})
63+
64+
describe('replace', () => {
65+
it('emits a new location', (done) => {
66+
const children = RenderTestSequences.ReplaceEmitsANewLocation(done)
67+
render(<MemoryHistory basename="/base" children={children}/>, node)
68+
})
69+
})
70+
71+
describe('pop', () => {
72+
it('emits a new location', (done) => {
73+
const children = RenderTestSequences.PopEmitsANewLocation(done)
74+
render(<MemoryHistory basename="/base" children={children}/>, node)
75+
})
76+
})
77+
})
78+
5679
describe('prompt', () => {
5780
it('blocks a push', (done) => {
5881
const children = RenderTestSequences.PromptBlocksAPush(done)

0 commit comments

Comments
 (0)