Skip to content

Commit 732645c

Browse files
authored
Merge pull request #179 from react-bootstrap/support-action
Support router action type
2 parents 91db73d + 68a636c commit 732645c

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
},
3333
"homepage": "https://github.com/react-bootstrap/react-router-bootstrap",
3434
"peerDependencies": {
35-
"react": ">=0.14.0",
36-
"react-router": ">=2.0.0"
35+
"react": ">=0.14.0"
3736
},
3837
"devDependencies": {
3938
"babel-cli": "^6.5.2",

src/LinkContainer.js

+54-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
// This is largely taken from react-router/lib/Link.
22

33
import React from 'react';
4-
import Link from 'react-router/lib/Link';
4+
5+
function isLeftClickEvent(event) {
6+
return event.button === 0;
7+
}
8+
9+
function isModifiedEvent(event) {
10+
return !!(
11+
event.metaKey ||
12+
event.altKey ||
13+
event.ctrlKey ||
14+
event.shiftKey
15+
);
16+
}
17+
18+
function createLocationDescriptor(to, query, hash, state) {
19+
if (query || hash || state) {
20+
return { pathname: to, query, hash, state };
21+
}
22+
23+
return to;
24+
}
525

626
const propTypes = {
727
onlyActiveOnIndex: React.PropTypes.bool.isRequired,
828
to: React.PropTypes.oneOfType([
929
React.PropTypes.string,
1030
React.PropTypes.object,
1131
]).isRequired,
32+
query: React.PropTypes.string,
33+
hash: React.PropTypes.string,
34+
state: React.PropTypes.object,
35+
action: React.PropTypes.oneOf([
36+
'push',
37+
'replace',
38+
]).isRequired,
1239
onClick: React.PropTypes.func,
1340
active: React.PropTypes.bool,
41+
target: React.PropTypes.string,
1442
children: React.PropTypes.node.isRequired,
1543
};
1644

@@ -20,15 +48,37 @@ const contextTypes = {
2048

2149
const defaultProps = {
2250
onlyActiveOnIndex: false,
51+
action: 'push',
2352
};
2453

2554
class LinkContainer extends React.Component {
2655
onClick = (event) => {
27-
if (this.props.children.props.onClick) {
28-
this.props.children.props.onClick(event);
56+
const {
57+
to, query, hash, state, children, onClick, target, action,
58+
} = this.props;
59+
60+
if (children.props.onClick) {
61+
children.props.onClick(event);
62+
}
63+
64+
if (onClick) {
65+
onClick(event);
2966
}
3067

31-
Link.prototype.handleClick.call(this, event);
68+
if (
69+
target ||
70+
event.defaultPrevented ||
71+
isModifiedEvent(event) ||
72+
!isLeftClickEvent(event)
73+
) {
74+
return;
75+
}
76+
77+
event.preventDefault();
78+
79+
this.context.router[action](
80+
createLocationDescriptor(to, query, hash, state)
81+
);
3282
};
3383

3484
render() {

0 commit comments

Comments
 (0)