From 2627cedf0368d726f563387af26d819881d53beb Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Tue, 1 Apr 2025 14:06:47 -0600 Subject: [PATCH] prevent default on link click --- web/src/components/ui/dropdown/DropdownBase.tsx | 16 ++++++++++++++-- .../components/ui/layouts/PreventNavigation.tsx | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/web/src/components/ui/dropdown/DropdownBase.tsx b/web/src/components/ui/dropdown/DropdownBase.tsx index 8f418b403..a7a03fe60 100644 --- a/web/src/components/ui/dropdown/DropdownBase.tsx +++ b/web/src/components/ui/dropdown/DropdownBase.tsx @@ -256,10 +256,22 @@ const DropdownMenuLink: React.FC<{ /> ); - if (!link) return
{content}
; + if (!link) + return ( +
e.stopPropagation()}> + {content} +
+ ); return ( - + { + e.stopPropagation(); + e.preventDefault(); + }}> {content} ); diff --git a/web/src/components/ui/layouts/PreventNavigation.tsx b/web/src/components/ui/layouts/PreventNavigation.tsx index 07b980dfc..980690673 100644 --- a/web/src/components/ui/layouts/PreventNavigation.tsx +++ b/web/src/components/ui/layouts/PreventNavigation.tsx @@ -44,14 +44,26 @@ export const PreventNavigation: React.FC = React.memo( * @param e The triggered event. */ const handleClick = useMemoizedFn((event: MouseEvent) => { - const target = event.target as HTMLAnchorElement; + let target = event.target as HTMLElement; + let href: string | null = null; + + // Traverse up the DOM tree looking for an anchor tag with href + while (target && !href) { + if (target instanceof HTMLAnchorElement && target.href) { + href = target.href; + break; + } + target = target.parentElement as HTMLElement; + } if (isDirty) { event.preventDefault(); event.stopPropagation(); + console.log(href); + confirmationFn.current = () => { - router.push(target.href); + if (href) router.push(href); }; setLeavingPage(true);