-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLink.js
57 lines (45 loc) · 1.3 KB
/
Link.js
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
52
53
54
55
56
57
/**
* React Static Boilerplate
* https://github.com/kriasoft/react-static-boilerplate
*
* Copyright © 2015-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
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,
};
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} />;
}
}
export default Link;