@@ -6,11 +6,15 @@ import {
6
6
location as locationType
7
7
} from './PropTypes'
8
8
9
+ const stripPrefix = ( prefix , string ) =>
10
+ string . indexOf ( prefix ) === 0 ? string . substring ( prefix . length ) : string
11
+
9
12
/**
10
13
* The common public API for all *History components.
11
14
*/
12
15
class HistoryContext extends React . Component {
13
16
static propTypes = {
17
+ basename : PropTypes . string ,
14
18
children : PropTypes . func . isRequired ,
15
19
action : actionType . isRequired ,
16
20
location : locationType . isRequired ,
@@ -20,34 +24,42 @@ class HistoryContext extends React.Component {
20
24
go : PropTypes . func . isRequired
21
25
}
22
26
27
+ static defaultProps = {
28
+ basename : ''
29
+ }
30
+
23
31
static childContextTypes = {
24
32
history : historyContextType . isRequired
25
33
}
26
34
27
35
getChildContext ( ) {
28
- const { prompt, push, replace, go } = this . props
29
-
30
36
return {
31
37
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
36
42
}
37
43
}
38
44
}
39
45
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
+
40
52
render ( ) {
41
- const { action, location } = this . props
53
+ const { basename , children , action, location } = this . props
42
54
43
55
const { path, ...everythingElse } = location
44
56
const { pathname, search, hash } = parsePath ( path )
45
57
46
- return this . props . children ( {
58
+ return children ( {
47
59
action,
48
60
location : {
49
61
...everythingElse ,
50
- pathname,
62
+ pathname : basename ? stripPrefix ( basename , pathname ) : pathname ,
51
63
search,
52
64
hash
53
65
}
0 commit comments