1
1
import React from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import {
4
- Route , useHref , useLocation , useMatch , useNavigate ,
4
+ useHref ,
5
+ useLocation ,
6
+ useMatch ,
7
+ useNavigate ,
5
8
} from 'react-router-dom' ;
6
9
import { isFunction } from 'lodash' ;
7
10
8
- const isModifiedEvent = ( event ) => ! ! ( event . metaKey || event . altKey || event . ctrlKey || event . shiftKey ) ;
11
+ const isModifiedEvent = ( event ) =>
12
+ ! ! ( event . metaKey || event . altKey || event . ctrlKey || event . shiftKey ) ;
9
13
10
14
const LinkContainer = ( {
11
15
children,
@@ -20,8 +24,19 @@ const LinkContainer = ({
20
24
// eslint-disable-next-line comma-dangle
21
25
...props
22
26
} ) => {
27
+ const path = typeof to === 'object' ? to . pathname : to ;
23
28
const navigate = useNavigate ( ) ;
24
29
const href = useHref ( typeof to === 'string' ? { pathname : to } : to ) ;
30
+ const match = useMatch ( path ) ;
31
+ const location = useLocation ( ) ;
32
+ const child = React . Children . only ( children ) ;
33
+
34
+ const isActive = ! ! ( getIsActive
35
+ ? isFunction ( getIsActive )
36
+ ? getIsActive ( match , location )
37
+ : getIsActive
38
+ : match ) ;
39
+
25
40
const handleClick = ( event ) => {
26
41
if ( children . props . onClick ) {
27
42
children . props . onClick ( event ) ;
@@ -32,9 +47,9 @@ const LinkContainer = ({
32
47
}
33
48
34
49
if (
35
- ! event . defaultPrevented // onClick prevented default
36
- && event . button === 0 // ignore right clicks
37
- && ! isModifiedEvent ( event ) // ignore clicks with modifier keys
50
+ ! event . defaultPrevented && // onClick prevented default
51
+ event . button === 0 && // ignore right clicks
52
+ ! isModifiedEvent ( event ) // ignore clicks with modifier keys
38
53
) {
39
54
event . preventDefault ( ) ;
40
55
@@ -44,56 +59,35 @@ const LinkContainer = ({
44
59
}
45
60
} ;
46
61
47
- const child = React . Children . only ( children ) ;
48
- const path = typeof to === 'object' ? to . pathname : to ;
49
- const InnerRouteElement = ( ) => {
50
- const match = useMatch ( path ) ;
51
- const location = useLocation ( ) ;
52
- const isActive = ! ! ( getIsActive ? ( isFunction ( getIsActive ) ? getIsActive ( match , location ) : getIsActive ) : match ) ;
53
-
54
- return React . cloneElement (
55
- child ,
56
- {
57
- ...props ,
58
- className : [ className , child . props . className , isActive ? activeClassName : null ]
59
- . join ( ' ' ) . trim ( ) ,
60
- style : isActive ? { ...style , ...activeStyle } : style ,
61
- href,
62
- onClick : handleClick ,
63
- } ,
64
- ) ;
65
- } ;
66
-
67
- return (
68
- < Route
69
- path = { path }
70
- element = { < InnerRouteElement /> }
71
- />
72
- ) ;
62
+ return React . cloneElement ( child , {
63
+ ...props ,
64
+ className : [
65
+ className ,
66
+ child . props . className ,
67
+ isActive ? activeClassName : null ,
68
+ ]
69
+ . join ( ' ' )
70
+ . trim ( ) ,
71
+ style : isActive ? { ...style , ...activeStyle } : style ,
72
+ href,
73
+ onClick : handleClick ,
74
+ } ) ;
73
75
} ;
74
76
75
77
LinkContainer . propTypes = {
76
78
children : PropTypes . element . isRequired ,
77
79
onClick : PropTypes . func ,
78
80
replace : PropTypes . bool ,
79
- to : PropTypes . oneOfType ( [
80
- PropTypes . string ,
81
- PropTypes . object ,
82
- ] ) . isRequired ,
81
+ to : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . object ] ) . isRequired ,
83
82
className : PropTypes . string ,
84
83
activeClassName : PropTypes . string ,
85
- style : PropTypes . objectOf ( PropTypes . oneOfType ( [
86
- PropTypes . string ,
87
- PropTypes . number ,
88
- ] ) ) ,
89
- activeStyle : PropTypes . objectOf ( PropTypes . oneOfType ( [
90
- PropTypes . string ,
91
- PropTypes . number ,
92
- ] ) ) ,
93
- isActive : PropTypes . oneOfType ( [
94
- PropTypes . func ,
95
- PropTypes . bool ,
96
- ] ) ,
84
+ style : PropTypes . objectOf (
85
+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
86
+ ) ,
87
+ activeStyle : PropTypes . objectOf (
88
+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ,
89
+ ) ,
90
+ isActive : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . bool ] ) ,
97
91
} ;
98
92
99
93
LinkContainer . defaultProps = {
0 commit comments