-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
NavLink.tsx
95 lines (82 loc) · 2.17 KB
/
NavLink.tsx
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
94
95
import classNames from 'classnames';
import PropTypes from 'prop-types';
import * as React from 'react';
import Anchor from '@restart/ui/Anchor';
import {
useNavItem,
NavItemProps as BaseNavItemProps,
} from '@restart/ui/NavItem';
import { makeEventKey } from '@restart/ui/SelectableContext';
import { useBootstrapPrefix } from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
export interface NavLinkProps
extends BsPrefixProps,
Omit<BaseNavItemProps, 'as'> {}
const propTypes = {
/**
* @default 'nav-link'
*/
bsPrefix: PropTypes.string,
/**
* The active state of the NavItem item.
*/
active: PropTypes.bool,
/**
* The disabled state of the NavItem item.
*/
disabled: PropTypes.bool,
/**
* The ARIA role for the `NavLink`, In the context of a 'tablist' parent Nav,
* the role defaults to 'tab'
* */
role: PropTypes.string,
/** The HTML href attribute for the `NavLink` */
href: PropTypes.string,
/**
* Uniquely identifies the `NavItem` amongst its siblings,
* used to determine and control the active state of the parent `Nav`
*/
eventKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/** @default 'a' */
as: PropTypes.elementType,
};
const NavLink: BsPrefixRefForwardingComponent<'a', NavLinkProps> =
React.forwardRef<HTMLElement, NavLinkProps>(
(
{
bsPrefix,
className,
as: Component = Anchor,
active,
eventKey,
disabled = false,
...props
},
ref,
) => {
bsPrefix = useBootstrapPrefix(bsPrefix, 'nav-link');
const [navItemProps, meta] = useNavItem({
key: makeEventKey(eventKey, props.href),
active,
disabled,
...props,
});
return (
<Component
{...props}
{...navItemProps}
ref={ref}
disabled={disabled}
className={classNames(
className,
bsPrefix,
disabled && 'disabled',
meta.isActive && 'active',
)}
/>
);
},
);
NavLink.displayName = 'NavLink';
NavLink.propTypes = propTypes;
export default NavLink;