Skip to content

Commit 04ba639

Browse files
committed
[added] Link activeStyle property
1 parent d03b968 commit 04ba639

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

docs/api/components/Link.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ your route handler with `this.getQuery()`.
3636

3737
### `activeClassName`
3838

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
4040
`active`.
4141

42+
### `activeStyle`
43+
44+
Object, the styles to apply to the link element when its route is active.
45+
4246
### `onClick`
4347

4448
A custom handler for the click event. Works just like a handler on an `<a>`
@@ -67,5 +71,8 @@ active -->
6771

6872
<!-- change the activeClassName -->
6973
<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>
7077
```
7178

modules/components/Link.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var Link = React.createClass({
4242
to: PropTypes.string.isRequired,
4343
params: PropTypes.object,
4444
query: PropTypes.object,
45+
activeStyle: PropTypes.object,
4546
onClick: PropTypes.func
4647
},
4748

@@ -87,19 +88,26 @@ var Link = React.createClass({
8788
if (this.props.className)
8889
classNames[this.props.className] = true;
8990

90-
if (this.isActive(this.props.to, this.props.params, this.props.query))
91+
if (this.getActiveState())
9192
classNames[this.props.activeClassName] = true;
9293

9394
return classSet(classNames);
9495
},
9596

97+
getActiveState: function () {
98+
return this.isActive(this.props.to, this.props.params, this.props.query);
99+
},
100+
96101
render: function () {
97102
var props = assign({}, this.props, {
98103
href: this.getHref(),
99104
className: this.getClassName(),
100105
onClick: this.handleClick
101106
});
102107

108+
if (props.activeStyle && this.getActiveState())
109+
props.style = props.activeStyle;
110+
103111
return React.DOM.a(props, this.props.children);
104112
}
105113

modules/components/__tests__/Link-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,65 @@ describe('A Link', function () {
9494
});
9595
});
9696
});
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+
});
97156
});
98157

99158
describe('when clicked', function () {

0 commit comments

Comments
 (0)