Skip to content

Commit 87bf01a

Browse files
committed
feat(NavLink)!: migrate to restart/ui
BREAKING CHANGE: remove `onSelect` in NavItem
1 parent 2a7a0e2 commit 87bf01a

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/NavLink.tsx

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import classNames from 'classnames';
22
import PropTypes from 'prop-types';
33

44
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';
811
import { useBootstrapPrefix } from './ThemeProvider';
912
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
1013

1114
export interface NavLinkProps
1215
extends BsPrefixProps,
13-
Omit<AbstractNavItemProps, 'as'> {}
16+
Omit<BaseNavItemProps, 'as'> {}
1417

1518
const propTypes = {
1619
/**
@@ -37,14 +40,6 @@ const propTypes = {
3740
/** The HTML href attribute for the `NavLink` */
3841
href: PropTypes.string,
3942

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-
4843
/**
4944
* Uniquely identifies the `NavItem` amongst its siblings,
5045
* used to determine and control the active state of the parent `Nav`
@@ -57,20 +52,39 @@ const propTypes = {
5752

5853
const defaultProps = {
5954
disabled: false,
60-
as: SafeAnchor,
6155
};
6256

6357
const NavLink: BsPrefixRefForwardingComponent<'a', NavLinkProps> =
6458
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+
) => {
6670
bsPrefix = useBootstrapPrefix(bsPrefix, 'nav-link');
71+
const [navItemProps, meta] = useNavItem({
72+
key: makeEventKey(eventKey, props.href),
73+
active,
74+
...props,
75+
});
76+
6777
return (
68-
<AbstractNavItem
78+
<Component
6979
{...props}
80+
{...navItemProps}
7081
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+
)}
7488
/>
7589
);
7690
},

0 commit comments

Comments
 (0)