1
1
// This is largely taken from react-router/lib/Link.
2
2
3
3
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
+ }
5
25
6
26
const propTypes = {
7
27
onlyActiveOnIndex : React . PropTypes . bool . isRequired ,
8
28
to : React . PropTypes . oneOfType ( [
9
29
React . PropTypes . string ,
10
30
React . PropTypes . object ,
11
31
] ) . 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 ,
12
39
onClick : React . PropTypes . func ,
13
40
active : React . PropTypes . bool ,
41
+ target : React . PropTypes . string ,
14
42
children : React . PropTypes . node . isRequired ,
15
43
} ;
16
44
@@ -20,15 +48,37 @@ const contextTypes = {
20
48
21
49
const defaultProps = {
22
50
onlyActiveOnIndex : false ,
51
+ action : 'push' ,
23
52
} ;
24
53
25
54
class LinkContainer extends React . Component {
26
55
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 ) ;
29
66
}
30
67
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
+ ) ;
32
82
} ;
33
83
34
84
render ( ) {
0 commit comments