forked from reactstrap/reactstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NavLink.js
85 lines (73 loc) · 1.7 KB
/
NavLink.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
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules, tagPropType } from './utils';
const propTypes = {
/** Add active class to NavLink */
active: PropTypes.bool,
/** Add custom class */
className: PropTypes.string,
/** Change underlying component's CSS base class name */
cssModule: PropTypes.object,
/** Disable the link */
disabled: PropTypes.bool,
href: PropTypes.any,
innerRef: PropTypes.oneOfType([
PropTypes.object,
PropTypes.func,
PropTypes.string,
]),
/** Function to be triggered on click */
onClick: PropTypes.func,
/** Set a custom element for this component */
tag: tagPropType,
};
const defaultProps = {
tag: 'a',
};
class NavLink extends React.Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
}
onClick(e) {
if (this.props.disabled) {
e.preventDefault();
return;
}
if (this.props.href === '#') {
e.preventDefault();
}
if (this.props.onClick) {
this.props.onClick(e);
}
}
render() {
let {
className,
cssModule,
active,
tag: Tag,
innerRef,
...attributes
} = this.props;
const classes = mapToCssModules(
classNames(className, 'nav-link', {
disabled: attributes.disabled,
active: active,
}),
cssModule,
);
return (
<Tag
{...attributes}
ref={innerRef}
onClick={this.onClick}
className={classes}
/>
);
}
}
NavLink.propTypes = propTypes;
NavLink.defaultProps = defaultProps;
export default React.forwardRef((props, ref) => <NavLink innerRef={ref} {...props} />);