Skip to content

Commit

Permalink
[added] onClick handler to <Link />
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Boyt committed Aug 8, 2014
1 parent cecdf12 commit f3dc513
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/api/components/Link.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ name through the link's properties to the resulting url.
The className a `Link` receives when it's route is active. Defaults to
`active`.

### `onClick`

A custom handler for the click event. Works just like a handler on an `<a>`
tag - calling `e.preventDefault()` or returning `false` will prevent the
transition from firing, while `e.stopPropagation()` will prevent the event
from bubbling.

### *others*

You can also pass props you'd like to be on the `<a>` such as a title, id, or className.
Expand Down
17 changes: 14 additions & 3 deletions modules/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ var Link = React.createClass({
propTypes: {
to: React.PropTypes.string.isRequired,
activeClassName: React.PropTypes.string.isRequired,
query: React.PropTypes.object
query: React.PropTypes.object,
onClick: React.PropTypes.func
},

getDefaultProps: function () {
Expand Down Expand Up @@ -108,13 +109,23 @@ var Link = React.createClass({
});
},

handleClick: function (event) {
handleClick: function(event) {
var allowTransition = true;
var ret;

if (this.props.onClick)
ret = this.props.onClick(event);

if (isModifiedEvent(event) || !isLeftClick(event))
return;

if (ret === false || event.preventDefaulted === true)
allowTransition = false;

event.preventDefault();

transitionTo(this.props.to, this.getParams(), this.props.query);
if (allowTransition)
transitionTo(this.props.to, this.getParams(), this.props.query);
},

render: function () {
Expand Down

0 comments on commit f3dc513

Please sign in to comment.