diff --git a/examples/nextjs-with-typescript/src/Link.tsx b/examples/nextjs-with-typescript/src/Link.tsx index a3d1912195c309..18d0af0b620997 100644 --- a/examples/nextjs-with-typescript/src/Link.tsx +++ b/examples/nextjs-with-typescript/src/Link.tsx @@ -5,7 +5,8 @@ import { useRouter } from 'next/router'; import NextLink, { LinkProps as NextLinkProps } from 'next/link'; import MuiLink, { LinkProps as MuiLinkProps } from '@material-ui/core/Link'; -type NextComposedProps = React.AnchorHTMLAttributes & NextLinkProps; +type NextComposedProps = Omit, 'href'> & + NextLinkProps; const NextComposed = React.forwardRef((props, ref) => { const { as, href, replace, scroll, passHref, shallow, prefetch, ...other } = props; @@ -31,29 +32,39 @@ interface LinkPropsBase { naked?: boolean; } -type LinkProps = LinkPropsBase & NextComposedProps & Omit; +export type LinkProps = LinkPropsBase & NextComposedProps & Omit; // A styled version of the Next.js Link component: // https://nextjs.org/docs/#with-link function Link(props: LinkProps) { const { + href, activeClassName = 'active', className: classNameProps, innerRef, naked, ...other } = props; - const router = useRouter(); + const router = useRouter(); + const pathname = typeof href === 'string' ? href : href.pathname; const className = clsx(classNameProps, { - [activeClassName]: router.pathname === props.href && activeClassName, + [activeClassName]: router.pathname === pathname && activeClassName, }); if (naked) { - return ; + return ; } - return ; + return ( + + ); } export default React.forwardRef((props, ref) => ( diff --git a/examples/nextjs/src/Link.js b/examples/nextjs/src/Link.js index 7d481dba54a55a..d6d608fc0ee089 100644 --- a/examples/nextjs/src/Link.js +++ b/examples/nextjs/src/Link.js @@ -17,8 +17,8 @@ const NextComposed = React.forwardRef(function NextComposed(props, ref) { }); NextComposed.propTypes = { - as: PropTypes.string, - href: PropTypes.string, + as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), + href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), prefetch: PropTypes.bool, }; @@ -26,30 +26,34 @@ NextComposed.propTypes = { // https://nextjs.org/docs/#with-link function Link(props) { const { + href, activeClassName = 'active', className: classNameProps, innerRef, naked, ...other } = props; - const router = useRouter(); + const router = useRouter(); + const pathname = typeof href === 'string' ? href : href.pathname; const className = clsx(classNameProps, { - [activeClassName]: router.pathname === props.href && activeClassName, + [activeClassName]: router.pathname === pathname && activeClassName, }); if (naked) { - return ; + return ; } - return ; + return ( + + ); } Link.propTypes = { activeClassName: PropTypes.string, - as: PropTypes.string, + as: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), className: PropTypes.string, - href: PropTypes.string, + href: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]), naked: PropTypes.bool, onClick: PropTypes.func,