-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.js
More file actions
51 lines (40 loc) · 1.08 KB
/
Link.js
File metadata and controls
51 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { PropTypes } from 'react';
import history from '../../core/history';
class Link extends React.Component {
static propTypes = {
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
onClick: PropTypes.func,
children: PropTypes.node,
};
handleClick = (event) => {
if (this.props.onClick) {
this.props.onClick(event);
}
if (event.button !== 0 /* left click */) {
return;
}
if (event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) {
return;
}
if (event.defaultPrevented === true) {
return;
}
event.preventDefault();
if (this.props.to) {
history.push(this.props.to);
}
else {
history.push({
pathname: event.currentTarget.pathname,
search: event.currentTarget.search,
});
}
};
render() {
const { to, ...props } = this.props; // eslint-disable-line no-use-before-define
return (<a href={history.createHref(to)} {...props} onClick={this.handleClick}>
{this.props.children}
</a>);
}
}
export default Link;