UNPKG

@etsoo/toolpad

Version:

Dashboard framework extention based on Toolpad Core

20 lines (19 loc) 810 B
import { jsx as _jsx } from "react/jsx-runtime"; import * as React from "react"; import { RouterContext } from "./context"; export const Link = React.forwardRef(function Link(props, ref) { const { children, href, onClick, history, ...rest } = props; const routerContext = React.useContext(RouterContext); const handleLinkClick = React.useMemo(() => { if (!routerContext) { return onClick; } return (event) => { event.preventDefault(); const url = new URL(event.currentTarget.href); routerContext.navigate(url.pathname, { history }); onClick?.(event); }; }, [routerContext, onClick, history]); return (_jsx("a", { ref: ref, href: href, ...rest, onClick: handleLinkClick, children: children })); });