@@ -2,15 +2,18 @@ import classNames from 'classnames';
2
2
import PropTypes from 'prop-types' ;
3
3
4
4
import * as React from 'react' ;
5
-
6
- import SafeAnchor from './SafeAnchor' ;
7
- import AbstractNavItem , { AbstractNavItemProps } from './AbstractNavItem' ;
5
+ import Anchor from '@restart/ui/Anchor' ;
6
+ import {
7
+ useNavItem ,
8
+ NavItemProps as BaseNavItemProps ,
9
+ } from '@restart/ui/NavItem' ;
10
+ import { makeEventKey } from '@restart/ui/SelectableContext' ;
8
11
import { useBootstrapPrefix } from './ThemeProvider' ;
9
12
import { BsPrefixProps , BsPrefixRefForwardingComponent } from './helpers' ;
10
13
11
14
export interface NavLinkProps
12
15
extends BsPrefixProps ,
13
- Omit < AbstractNavItemProps , 'as' > { }
16
+ Omit < BaseNavItemProps , 'as' > { }
14
17
15
18
const propTypes = {
16
19
/**
@@ -37,14 +40,6 @@ const propTypes = {
37
40
/** The HTML href attribute for the `NavLink` */
38
41
href : PropTypes . string ,
39
42
40
- /** A callback fired when the `NavLink` is selected.
41
- *
42
- * ```js
43
- * function (eventKey: any, event: SyntheticEvent) {}
44
- * ```
45
- */
46
- onSelect : PropTypes . func ,
47
-
48
43
/**
49
44
* Uniquely identifies the `NavItem` amongst its siblings,
50
45
* used to determine and control the active state of the parent `Nav`
@@ -57,20 +52,39 @@ const propTypes = {
57
52
58
53
const defaultProps = {
59
54
disabled : false ,
60
- as : SafeAnchor ,
61
55
} ;
62
56
63
57
const NavLink : BsPrefixRefForwardingComponent < 'a' , NavLinkProps > =
64
58
React . forwardRef < HTMLElement , NavLinkProps > (
65
- ( { bsPrefix, disabled, className, as, ...props } , ref ) => {
59
+ (
60
+ {
61
+ bsPrefix,
62
+ className,
63
+ as : Component = Anchor ,
64
+ active,
65
+ eventKey,
66
+ ...props
67
+ } ,
68
+ ref ,
69
+ ) => {
66
70
bsPrefix = useBootstrapPrefix ( bsPrefix , 'nav-link' ) ;
71
+ const [ navItemProps , meta ] = useNavItem ( {
72
+ key : makeEventKey ( eventKey , props . href ) ,
73
+ active,
74
+ ...props ,
75
+ } ) ;
76
+
67
77
return (
68
- < AbstractNavItem
78
+ < Component
69
79
{ ...props }
80
+ { ...navItemProps }
70
81
ref = { ref }
71
- as = { as as any }
72
- disabled = { disabled }
73
- className = { classNames ( className , bsPrefix , disabled && 'disabled' ) }
82
+ className = { classNames (
83
+ className ,
84
+ bsPrefix ,
85
+ props . disabled && 'disabled' ,
86
+ meta . isActive && 'active' ,
87
+ ) }
74
88
/>
75
89
) ;
76
90
} ,
0 commit comments