File tree Expand file tree Collapse file tree 3 files changed +76
-2
lines changed Expand file tree Collapse file tree 3 files changed +76
-2
lines changed Original file line number Diff line number Diff line change @@ -36,9 +36,13 @@ your route handler with `this.getQuery()`.
36
36
37
37
### ` activeClassName `
38
38
39
- The className a ` Link ` receives when it's route is active. Defaults to
39
+ The className a ` Link ` receives when its route is active. Defaults to
40
40
` active ` .
41
41
42
+ ### ` activeStyle `
43
+
44
+ Object, the styles to apply to the link element when its route is active.
45
+
42
46
### ` onClick `
43
47
44
48
A custom handler for the click event. Works just like a handler on an ` <a> `
@@ -67,5 +71,8 @@ active -->
67
71
68
72
<!-- change the activeClassName -->
69
73
<Link activeClassName =" current" to =" user" params ={{userId: user.id}}>{user.name}</Link >
74
+
75
+ <!-- change style when link is active -->
76
+ <Link style ={{color: ' white' }} activeStyle ={{color: ' red' }} to =" user" params ={{userId: user.id}} query ={{foo: bar}}>{user.name}</Link >
70
77
```
71
78
Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ var Link = React.createClass({
42
42
to : PropTypes . string . isRequired ,
43
43
params : PropTypes . object ,
44
44
query : PropTypes . object ,
45
+ activeStyle : PropTypes . object ,
45
46
onClick : PropTypes . func
46
47
} ,
47
48
@@ -87,19 +88,26 @@ var Link = React.createClass({
87
88
if ( this . props . className )
88
89
classNames [ this . props . className ] = true ;
89
90
90
- if ( this . isActive ( this . props . to , this . props . params , this . props . query ) )
91
+ if ( this . getActiveState ( ) )
91
92
classNames [ this . props . activeClassName ] = true ;
92
93
93
94
return classSet ( classNames ) ;
94
95
} ,
95
96
97
+ getActiveState : function ( ) {
98
+ return this . isActive ( this . props . to , this . props . params , this . props . query ) ;
99
+ } ,
100
+
96
101
render : function ( ) {
97
102
var props = assign ( { } , this . props , {
98
103
href : this . getHref ( ) ,
99
104
className : this . getClassName ( ) ,
100
105
onClick : this . handleClick
101
106
} ) ;
102
107
108
+ if ( props . activeStyle && this . getActiveState ( ) )
109
+ props . style = props . activeStyle ;
110
+
103
111
return React . DOM . a ( props , this . props . children ) ;
104
112
}
105
113
Original file line number Diff line number Diff line change @@ -94,6 +94,65 @@ describe('A Link', function () {
94
94
} ) ;
95
95
} ) ;
96
96
} ) ;
97
+
98
+ it ( 'has applies activeStyle' , function ( done ) {
99
+ var LinkHandler = React . createClass ( {
100
+ render : function ( ) {
101
+ return (
102
+ < div >
103
+ < Link
104
+ to = "foo"
105
+ style = { { color : 'white' } }
106
+ activeStyle = { { color : 'red' } }
107
+ > Link</ Link >
108
+ < RouteHandler />
109
+ </ div >
110
+ ) ;
111
+ }
112
+ } ) ;
113
+
114
+ var routes = (
115
+ < Route path = "/" handler = { LinkHandler } >
116
+ < Route name = "foo" handler = { Foo } />
117
+ < Route name = "bar" handler = { Bar } />
118
+ </ Route >
119
+ ) ;
120
+
121
+ var div = document . createElement ( 'div' ) ;
122
+ TestLocation . history = [ '/foo' ] ;
123
+ var steps = [ ] ;
124
+
125
+ function assertActive ( ) {
126
+ var a = div . querySelector ( 'a' ) ;
127
+ expect ( a . style . color ) . toEqual ( 'red' ) ;
128
+ }
129
+
130
+ function assertInactive ( ) {
131
+ var a = div . querySelector ( 'a' ) ;
132
+ expect ( a . style . color ) . toEqual ( 'white' ) ;
133
+ }
134
+
135
+ steps . push ( ( ) => {
136
+ assertActive ( ) ;
137
+ TestLocation . push ( '/bar' ) ;
138
+ } ) ;
139
+
140
+ steps . push ( ( ) => {
141
+ assertInactive ( ) ;
142
+ TestLocation . push ( '/foo' ) ;
143
+ } ) ;
144
+
145
+ steps . push ( ( ) => {
146
+ assertActive ( ) ;
147
+ done ( ) ;
148
+ } ) ;
149
+
150
+ Router . run ( routes , TestLocation , function ( Handler ) {
151
+ React . render ( < Handler /> , div , ( ) => {
152
+ steps . shift ( ) ( ) ;
153
+ } ) ;
154
+ } ) ;
155
+ } ) ;
97
156
} ) ;
98
157
99
158
describe ( 'when clicked' , function ( ) {
You can’t perform that action at this time.
0 commit comments