-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathLinkContainer.js
93 lines (82 loc) · 2.5 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React from 'react';
import PropTypes from 'prop-types';
import {
useHref,
useLocation,
useMatch,
useNavigate,
} from 'react-router-dom';
const isModifiedEvent = (event) =>
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
const LinkContainer = ({
children,
onClick,
replace, // eslint-disable-line no-unused-vars
to,
className,
style,
isActive: getIsActive,
// eslint-disable-next-line comma-dangle
...props
}) => {
const path = typeof to === 'object' ? to.pathname : to;
const navigate = useNavigate();
const href = useHref(typeof to === 'string' ? {pathname: to} : to);
const match = useMatch(path);
const location = useLocation();
const child = React.Children.only(children);
const isActive = !!(getIsActive
? typeof getIsActive === 'function'
? getIsActive(match, location)
: getIsActive
: match);
const handleClick = (event) => {
if (children.props.onClick) {
children.props.onClick(event);
}
if (onClick) {
onClick(event);
}
if (
!event.defaultPrevented && // onClick prevented default
event.button === 0 && // ignore right clicks
!isModifiedEvent(event) // ignore clicks with modifier keys
) {
event.preventDefault();
navigate(to, {
replace,
});
}
};
return React.cloneElement(child, {
...props,
className: [
typeof className === 'function' ? className({isActive}) : className,
child.props.className,
]
.join(' ')
.trim(),
style: typeof style === 'function' ? style({isActive}) : style,
href,
onClick: handleClick,
});
};
LinkContainer.propTypes = {
children: PropTypes.element.isRequired,
onClick: PropTypes.func,
replace: PropTypes.bool,
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
className: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
style: PropTypes.oneOfType([PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
), PropTypes.func]),
isActive: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
};
LinkContainer.defaultProps = {
replace: false,
onClick: null,
className: ({isActive}) => isActive ? 'active' : null,
style: null,
isActive: null,
};
export default LinkContainer;