1
1
"use strict" ;
2
2
3
3
var pattern = require ( 'url-pattern' ) ;
4
- var mergeInto = require ( 'react/lib/mergeInto ' ) ;
4
+ var assign = Object . assign || require ( 'object.assign ' ) ;
5
5
var invariant = require ( 'react/lib/invariant' ) ;
6
+ var React = require ( 'react' ) ;
6
7
7
8
/**
8
9
* Match routes against a path
@@ -22,17 +23,23 @@ function matchRoutes(routes, path) {
22
23
// Simply skip null or undefined to allow ternaries in route definitions
23
24
if ( ! current ) continue ;
24
25
26
+ // We expect to be passed an Element. If we weren't, and were just passed props,
27
+ // mock an Element's structure.
28
+ if ( ! React . isValidElement ( current ) ) {
29
+ current = { props : current , ref : current . ref } ;
30
+ }
31
+
25
32
if ( process . env . NODE_ENV !== "production" ) {
26
33
invariant (
27
- current . handler !== undefined && current . path !== undefined ,
34
+ current . props . handler !== undefined && current . props . path !== undefined ,
28
35
"Router should contain either Route or NotFound components " +
29
36
"as routes" )
30
37
}
31
38
32
- if ( current . path ) {
33
- current . pattern = current . pattern || pattern . newPattern ( current . path ) ;
39
+ if ( current . props . path ) {
40
+ current . props . pattern = current . props . pattern || pattern . newPattern ( current . props . path ) ;
34
41
if ( ! page ) {
35
- match = current . pattern . match ( path ) ;
42
+ match = current . props . pattern . match ( path ) ;
36
43
if ( match ) {
37
44
page = current ;
38
45
}
@@ -42,7 +49,7 @@ function matchRoutes(routes, path) {
42
49
}
43
50
}
44
51
}
45
- if ( ! notFound && current . path === null ) {
52
+ if ( ! notFound && current . props . path === null ) {
46
53
notFound = current ;
47
54
}
48
55
}
@@ -91,16 +98,12 @@ function Match(path, route, match) {
91
98
}
92
99
93
100
Match . prototype . getHandler = function ( ) {
94
- var props = { } ;
95
- if ( this . match ) {
96
- mergeInto ( props , this . match ) ;
97
- }
98
- if ( this . route && this . route . props ) {
99
- mergeInto ( props , this . route . props ) ;
100
- }
101
- // we will set ref later during a render call
102
- delete props . ref ;
103
- return this . route ? this . route . handler ( props ) : undefined ;
101
+ if ( ! this . route ) return undefined ;
102
+ var props = assign ( { } , this . route . props , this . match ) ;
103
+ delete props . pattern ;
104
+ delete props . path ;
105
+ delete props . handler ;
106
+ return this . route . props . handler ( props ) ;
104
107
}
105
108
106
109
module . exports = matchRoutes ;
0 commit comments