11// This is largely taken from react-router/lib/Link.
22
33import 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
626const 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
2149const defaultProps = {
2250 onlyActiveOnIndex : false ,
51+ action : 'push' ,
2352} ;
2453
2554class 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