Skip to content

Commit f3dc513

Browse files
author
Thomas Boyt
committed
[added] onClick handler to <Link />
1 parent cecdf12 commit f3dc513

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

docs/api/components/Link.md

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ name through the link's properties to the resulting url.
2727
The className a `Link` receives when it's route is active. Defaults to
2828
`active`.
2929

30+
### `onClick`
31+
32+
A custom handler for the click event. Works just like a handler on an `<a>`
33+
tag - calling `e.preventDefault()` or returning `false` will prevent the
34+
transition from firing, while `e.stopPropagation()` will prevent the event
35+
from bubbling.
36+
3037
### *others*
3138

3239
You can also pass props you'd like to be on the `<a>` such as a title, id, or className.

modules/components/Link.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ var Link = React.createClass({
5252
propTypes: {
5353
to: React.PropTypes.string.isRequired,
5454
activeClassName: React.PropTypes.string.isRequired,
55-
query: React.PropTypes.object
55+
query: React.PropTypes.object,
56+
onClick: React.PropTypes.func
5657
},
5758

5859
getDefaultProps: function () {
@@ -108,13 +109,23 @@ var Link = React.createClass({
108109
});
109110
},
110111

111-
handleClick: function (event) {
112+
handleClick: function(event) {
113+
var allowTransition = true;
114+
var ret;
115+
116+
if (this.props.onClick)
117+
ret = this.props.onClick(event);
118+
112119
if (isModifiedEvent(event) || !isLeftClick(event))
113120
return;
114121

122+
if (ret === false || event.preventDefaulted === true)
123+
allowTransition = false;
124+
115125
event.preventDefault();
116126

117-
transitionTo(this.props.to, this.getParams(), this.props.query);
127+
if (allowTransition)
128+
transitionTo(this.props.to, this.getParams(), this.props.query);
118129
},
119130

120131
render: function () {

0 commit comments

Comments
 (0)