From 42191df3f80aba66f2abaa80e3f4433653f806d5 Mon Sep 17 00:00:00 2001 From: Joseph White Date: Wed, 8 Jul 2020 17:33:03 -0600 Subject: [PATCH] Forward refs properly --- src/index.js | 70 +++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/src/index.js b/src/index.js index 52ec51d..388eaab 100644 --- a/src/index.js +++ b/src/index.js @@ -46,46 +46,44 @@ function hashLinkScroll() { }, 0); } -export function genericHashLink(props, As) { - function handleClick(e) { - reset(); - if (props.onClick) props.onClick(e); - if (typeof props.to === 'string') { - hashFragment = props.to - .split('#') - .slice(1) - .join('#'); - } else if ( - typeof props.to === 'object' && - typeof props.to.hash === 'string' - ) { - hashFragment = props.to.hash.replace('#', ''); - } - if (hashFragment !== '') { - scrollFunction = - props.scroll || - (el => - props.smooth - ? el.scrollIntoView({ behavior: "smooth" }) - : el.scrollIntoView()); - hashLinkScroll(); +export function genericHashLink(As) { + return React.forwardRef((props, ref) => { + function handleClick(e) { + reset(); + if (props.onClick) props.onClick(e); + if (typeof props.to === 'string') { + hashFragment = props.to + .split('#') + .slice(1) + .join('#'); + } else if ( + typeof props.to === 'object' && + typeof props.to.hash === 'string' + ) { + hashFragment = props.to.hash.replace('#', ''); + } + if (hashFragment !== '') { + scrollFunction = + props.scroll || + (el => + props.smooth + ? el.scrollIntoView({ behavior: "smooth" }) + : el.scrollIntoView()); + hashLinkScroll(); + } } - } - const { scroll, smooth, ...filteredProps } = props; - return ( - - {props.children} - - ); + const { scroll, smooth, ...filteredProps } = props; + return ( + + {props.children} + + ); + }); } -export function HashLink(props) { - return genericHashLink(props, Link); -} +export const HashLink = genericHashLink(Link); -export function NavHashLink(props) { - return genericHashLink(props, NavLink); -} +export const NavHashLink = genericHashLink(NavLink); const propTypes = { onClick: PropTypes.func,