1
1
var React = require ( 'react' ) ;
2
- var warning = require ( 'react/lib/warning ' ) ;
2
+ var merge = require ( 'react/lib/merge ' ) ;
3
3
var ActiveState = require ( '../mixins/ActiveState' ) ;
4
- var RouteLookup = require ( '../mixins/RouteLookup' ) ;
5
4
var Transitions = require ( '../mixins/Transitions' ) ;
6
- var withoutProperties = require ( '../utils/withoutProperties' ) ;
7
- var hasOwnProperty = require ( '../utils/hasOwnProperty' ) ;
8
5
9
6
function isLeftClickEvent ( event ) {
10
7
return event . button === 0 ;
@@ -14,66 +11,29 @@ function isModifiedEvent(event) {
14
11
return ! ! ( event . metaKey || event . altKey || event . ctrlKey || event . shiftKey ) ;
15
12
}
16
13
17
- /**
18
- * DEPRECATED: A map of <Link> component props that are reserved for use by the
19
- * router and/or React. All other props are used as params that are
20
- * interpolated into the link's path.
21
- */
22
- var RESERVED_PROPS = {
23
- to : true ,
24
- key : true ,
25
- className : true ,
26
- activeClassName : true ,
27
- query : true ,
28
- onClick :true ,
29
- children : true // ReactChildren
30
- } ;
31
-
32
14
/**
33
15
* <Link> components are used to create an <a> element that links to a route.
34
16
* When that route is active, the link gets an "active" class name (or the
35
17
* value of its `activeClassName` prop).
36
18
*
37
19
* For example, assuming you have the following route:
38
20
*
39
- * <Route name="showPost" path="/posts/:postId " handler={Post}/>
21
+ * <Route name="showPost" path="/posts/:postID " handler={Post}/>
40
22
*
41
23
* You could use the following component to link to that route:
42
24
*
43
- * <Link to="showPost" params={{postId : "123"}} />
25
+ * <Link to="showPost" params={{ postID : "123" }} />
44
26
*
45
27
* In addition to params, links may pass along query string parameters
46
28
* using the `query` prop.
47
29
*
48
- * <Link to="showPost" params={{postId : "123"}} query={{show:true}}/>
30
+ * <Link to="showPost" params={{ postID : "123" }} query={{ show:true }}/>
49
31
*/
50
32
var Link = React . createClass ( {
51
33
52
34
displayName : 'Link' ,
53
35
54
- mixins : [ ActiveState , RouteLookup , Transitions ] ,
55
-
56
- statics : {
57
-
58
- // TODO: Deprecate passing props as params in v1.0
59
- getUnreservedProps : function ( props ) {
60
- var props = withoutProperties ( props , RESERVED_PROPS ) ;
61
- warning (
62
- Object . keys ( props ) . length === 0 ,
63
- 'Passing props for params on <Link>s is deprecated, ' +
64
- 'please use the `params` property.'
65
- ) ;
66
- return props ;
67
- } ,
68
-
69
- /**
70
- * Returns a hash of URL parameters to use in this <Link>'s path.
71
- */
72
- getParams : function ( props ) {
73
- return props . params || Link . getUnreservedProps ( props ) ;
74
- }
75
-
76
- } ,
36
+ mixins : [ ActiveState , Transitions ] ,
77
37
78
38
propTypes : {
79
39
to : React . PropTypes . string . isRequired ,
@@ -95,71 +55,63 @@ var Link = React.createClass({
95
55
} ;
96
56
} ,
97
57
98
- /**
99
- * Returns the value of the "href" attribute to use on the DOM element.
100
- */
101
- getHref : function ( ) {
102
- return this . makeHref ( this . props . to , Link . getParams ( this . props ) , this . props . query ) ;
103
- } ,
104
-
105
- /**
106
- * Returns the value of the "class" attribute to use on the DOM element, which contains
107
- * the value of the activeClassName property when this <Link> is active.
108
- */
109
- getClassName : function ( ) {
110
- var className = this . props . className || '' ;
111
-
112
- if ( this . state . isActive )
113
- return className + ' ' + this . props . activeClassName ;
114
-
115
- return className ;
116
- } ,
117
-
118
- componentWillReceiveProps : function ( nextProps ) {
119
- var params = Link . getParams ( nextProps ) ;
120
-
58
+ updateActiveState : function ( ) {
121
59
this . setState ( {
122
- isActive : this . isActive ( nextProps . to , params , nextProps . query )
60
+ isActive : this . isActive ( this . props . to , this . props . params , this . props . query )
123
61
} ) ;
124
62
} ,
125
63
126
- updateActiveState : function ( ) {
64
+ componentWillReceiveProps : function ( nextProps ) {
127
65
this . setState ( {
128
- isActive : this . isActive ( this . props . to , Link . getParams ( this . props ) , this . props . query )
66
+ isActive : this . isActive ( nextProps . to , nextProps . params , nextProps . query )
129
67
} ) ;
130
68
} ,
131
69
132
70
handleClick : function ( event ) {
133
71
var allowTransition = true ;
134
- var ret ;
72
+ var onClickResult ;
135
73
136
74
if ( this . props . onClick )
137
- ret = this . props . onClick ( event ) ;
75
+ onClickResult = this . props . onClick ( event ) ;
138
76
139
77
if ( isModifiedEvent ( event ) || ! isLeftClickEvent ( event ) )
140
78
return ;
141
79
142
- if ( ret === false || event . defaultPrevented === true )
80
+ if ( onClickResult === false || event . defaultPrevented === true )
143
81
allowTransition = false ;
144
82
145
83
event . preventDefault ( ) ;
146
84
147
85
if ( allowTransition )
148
- this . transitionTo ( this . props . to , Link . getParams ( this . props ) , this . props . query ) ;
86
+ this . transitionTo ( this . props . to , this . props . params , this . props . query ) ;
87
+ } ,
88
+
89
+ /**
90
+ * Returns the value of the "href" attribute to use on the DOM element.
91
+ */
92
+ getHref : function ( ) {
93
+ return this . makeHref ( this . props . to , this . props . params , this . props . query ) ;
94
+ } ,
95
+
96
+ /**
97
+ * Returns the value of the "class" attribute to use on the DOM element, which contains
98
+ * the value of the activeClassName property when this <Link> is active.
99
+ */
100
+ getClassName : function ( ) {
101
+ var className = this . props . className || '' ;
102
+
103
+ if ( this . state . isActive )
104
+ className += ' ' + this . props . activeClassName ;
105
+
106
+ return className ;
149
107
} ,
150
108
151
109
render : function ( ) {
152
- var props = {
110
+ var props = merge ( this . props , {
153
111
href : this . getHref ( ) ,
154
112
className : this . getClassName ( ) ,
155
113
onClick : this . handleClick
156
- } ;
157
-
158
- // pull in props without overriding
159
- for ( var propName in this . props ) {
160
- if ( hasOwnProperty ( this . props , propName ) && hasOwnProperty ( props , propName ) === false )
161
- props [ propName ] = this . props [ propName ] ;
162
- }
114
+ } ) ;
163
115
164
116
return React . DOM . a ( props , this . props . children ) ;
165
117
}
0 commit comments