forked from react-bootstrap/react-router-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkContainer.js
64 lines (50 loc) · 1.47 KB
/
LinkContainer.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
58
59
60
61
62
63
64
// This is largely taken from rrtr/lib/Link.
import React from 'react';
import Link from 'rrtr/lib/Link';
export default class LinkContainer extends React.Component {
constructor(props, context) {
super(props, context);
this.onClick = this.onClick.bind(this);
}
onClick(event) {
if (this.props.disabled) {
event.preventDefault();
return;
}
if (this.props.children.props.onClick) {
this.props.children.props.onClick(event);
}
Link.prototype.handleClick.call(this, event);
}
render() {
const { router } = this.context;
const { onlyActiveOnIndex, to, children, ...props } = this.props;
props.onClick = this.onClick;
// Ignore if rendered outside Router context; simplifies unit testing.
if (router) {
props.href = router.createHref(to);
if (props.active == null) {
props.active = router.isActive(to, onlyActiveOnIndex);
}
}
return React.cloneElement(React.Children.only(children), props);
}
}
LinkContainer.propTypes = {
onlyActiveOnIndex: React.PropTypes.bool.isRequired,
to: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object,
]).isRequired,
onClick: React.PropTypes.func,
active: React.PropTypes.bool,
disabled: React.PropTypes.bool.isRequired,
children: React.PropTypes.node.isRequired
};
LinkContainer.contextTypes = {
router: React.PropTypes.object
};
LinkContainer.defaultProps = {
onlyActiveOnIndex: false,
disabled: false
};