Skip to content

Commit

Permalink
[added] Link activeStyle property
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyboy committed Feb 13, 2015
1 parent d03b968 commit 04ba639
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
9 changes: 8 additions & 1 deletion docs/api/components/Link.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ your route handler with `this.getQuery()`.

### `activeClassName`

The className a `Link` receives when it's route is active. Defaults to
The className a `Link` receives when its route is active. Defaults to
`active`.

### `activeStyle`

Object, the styles to apply to the link element when its route is active.

### `onClick`

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

<!-- change the activeClassName -->
<Link activeClassName="current" to="user" params={{userId: user.id}}>{user.name}</Link>

<!-- change style when link is active -->
<Link style={{color: 'white'}} activeStyle={{color: 'red'}} to="user" params={{userId: user.id}} query={{foo: bar}}>{user.name}</Link>
```

10 changes: 9 additions & 1 deletion modules/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var Link = React.createClass({
to: PropTypes.string.isRequired,
params: PropTypes.object,
query: PropTypes.object,
activeStyle: PropTypes.object,
onClick: PropTypes.func
},

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

if (this.isActive(this.props.to, this.props.params, this.props.query))
if (this.getActiveState())
classNames[this.props.activeClassName] = true;

return classSet(classNames);
},

getActiveState: function () {
return this.isActive(this.props.to, this.props.params, this.props.query);
},

render: function () {
var props = assign({}, this.props, {
href: this.getHref(),
className: this.getClassName(),
onClick: this.handleClick
});

if (props.activeStyle && this.getActiveState())
props.style = props.activeStyle;

return React.DOM.a(props, this.props.children);
}

Expand Down
59 changes: 59 additions & 0 deletions modules/components/__tests__/Link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,65 @@ describe('A Link', function () {
});
});
});

it('has applies activeStyle', function (done) {
var LinkHandler = React.createClass({
render: function () {
return (
<div>
<Link
to="foo"
style={{color: 'white'}}
activeStyle={{color: 'red'}}
>Link</Link>
<RouteHandler/>
</div>
);
}
});

var routes = (
<Route path="/" handler={LinkHandler}>
<Route name="foo" handler={Foo} />
<Route name="bar" handler={Bar} />
</Route>
);

var div = document.createElement('div');
TestLocation.history = ['/foo'];
var steps = [];

function assertActive () {
var a = div.querySelector('a');
expect(a.style.color).toEqual('red');
}

function assertInactive () {
var a = div.querySelector('a');
expect(a.style.color).toEqual('white');
}

steps.push(() => {
assertActive();
TestLocation.push('/bar');
});

steps.push(() => {
assertInactive();
TestLocation.push('/foo');
});

steps.push(() => {
assertActive();
done();
});

Router.run(routes, TestLocation, function (Handler) {
React.render(<Handler/>, div, () => {
steps.shift()();
});
});
});
});

describe('when clicked', function () {
Expand Down

0 comments on commit 04ba639

Please sign in to comment.