Skip to content

Commit 166b9a2

Browse files
committed
Merge pull request #834 from bradleyboy/link-activestyle-property
[added] Link activeStyle property
2 parents 193222e + 0bc47e6 commit 166b9a2

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
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: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,82 @@ describe('A Link', function () {
7373
expect(a.className).toEqual('dontKillMe');
7474
}
7575

76-
steps.push(() => {
76+
steps.push(function () {
77+
assertActive();
78+
TestLocation.push('/bar');
79+
});
80+
81+
steps.push(function () {
82+
assertInactive();
83+
TestLocation.push('/foo');
84+
});
85+
86+
steps.push(function () {
87+
assertActive();
88+
done();
89+
});
90+
91+
Router.run(routes, TestLocation, function (Handler) {
92+
React.render(<Handler/>, div, function () {
93+
steps.shift()();
94+
});
95+
});
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(function () {
77136
assertActive();
78137
TestLocation.push('/bar');
79138
});
80139

81-
steps.push(() => {
140+
steps.push(function () {
82141
assertInactive();
83142
TestLocation.push('/foo');
84143
});
85144

86-
steps.push(() => {
145+
steps.push(function () {
87146
assertActive();
88147
done();
89148
});
90149

91150
Router.run(routes, TestLocation, function (Handler) {
92-
React.render(<Handler/>, div, () => {
151+
React.render(<Handler/>, div, function () {
93152
steps.shift()();
94153
});
95154
});

0 commit comments

Comments
 (0)