-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pass close fn/forwardref to promptlink (#698)
Co-authored-by: Niall Maher <[email protected]>
- Loading branch information
1 parent
7db81a6
commit 433357c
Showing
3 changed files
with
96 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,90 @@ | ||
import { useRouter } from "next/navigation"; | ||
import { usePrompt } from "@/components/PromptService/PromptContext"; | ||
import type { ReactNode } from "react"; | ||
import type { MutableRefObject, ReactNode } from "react"; | ||
import { useEffect, useState } from "react"; | ||
import Link from "next/link"; | ||
import { PromptDialog } from "./PromptDialog"; | ||
import React from "react"; | ||
|
||
interface LinkProps { | ||
to: string; | ||
children: ReactNode; | ||
className?: string; | ||
close?: ( | ||
focusableElement?: | ||
| HTMLElement | ||
| MutableRefObject<HTMLElement | null> | ||
| undefined, | ||
) => void; | ||
} | ||
|
||
export const PromptLink = ({ to, children, className }: LinkProps) => { | ||
const router = useRouter(); | ||
const { unsavedChanges } = usePrompt(); | ||
const [hasPrompt, setHasPrompt] = useState(false); | ||
const [showModal, setShowModal] = useState(false); | ||
const PromptLink = React.forwardRef( | ||
( | ||
{ to, children, className, close }: LinkProps, | ||
ref: React.Ref<HTMLAnchorElement>, | ||
) => { | ||
const router = useRouter(); | ||
const { unsavedChanges } = usePrompt(); | ||
const [hasPrompt, setHasPrompt] = useState(false); | ||
const [showModal, setShowModal] = useState(false); | ||
|
||
const handleNavigation = (event) => { | ||
event.preventDefault(); | ||
const handleNavigation = ( | ||
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>, | ||
) => { | ||
event.preventDefault(); | ||
|
||
if (unsavedChanges) { | ||
setHasPrompt(true); | ||
setShowModal(true); | ||
return false; | ||
} else { | ||
if (unsavedChanges) { | ||
setHasPrompt(true); | ||
setShowModal(true); | ||
return false; | ||
} else { | ||
if (close !== undefined) close(); | ||
router.push(to); | ||
} | ||
}; | ||
|
||
const confirmNavigation = () => { | ||
setShowModal(false); | ||
setHasPrompt(false); | ||
if (close !== undefined) close(); | ||
router.push(to); | ||
} | ||
}; | ||
}; | ||
|
||
const confirmNavigation = () => { | ||
setShowModal(false); | ||
setHasPrompt(false); | ||
router.push(to); | ||
}; | ||
const cancelNavigation = () => { | ||
setShowModal(false); | ||
return false; | ||
}; | ||
|
||
const cancelNavigation = () => { | ||
setShowModal(false); | ||
return false; | ||
}; | ||
useEffect(() => { | ||
if (hasPrompt === true) { | ||
setHasPrompt(false); | ||
} | ||
}, [hasPrompt]); | ||
|
||
useEffect(() => { | ||
if (hasPrompt === true) { | ||
setHasPrompt(false); | ||
} | ||
}, [hasPrompt]); | ||
return ( | ||
<> | ||
<Link | ||
href={to} | ||
onClick={handleNavigation} | ||
className={className} | ||
ref={ref} | ||
> | ||
{children} | ||
</Link> | ||
{showModal && ( | ||
<PromptDialog | ||
title="Unsaved Changes" | ||
subTitle="You have unsaved changes. Are you sure you want to leave?" | ||
confirm={confirmNavigation} | ||
cancel={cancelNavigation} | ||
confirmText="Continue without saving" | ||
cancelText="Keep editing" | ||
/> | ||
)} | ||
</> | ||
); | ||
}, | ||
); | ||
|
||
return ( | ||
<> | ||
<Link href={to} onClick={handleNavigation} className={className}> | ||
{children} | ||
</Link> | ||
{showModal && ( | ||
<PromptDialog | ||
title="Unsaved Changes" | ||
subTitle="You have unsaved changes. Are you sure you want to leave?" | ||
confirm={confirmNavigation} | ||
cancel={cancelNavigation} | ||
confirmText="Continue without saving" | ||
cancelText="Keep editing" | ||
/> | ||
)} | ||
</> | ||
); | ||
}; | ||
PromptLink.displayName = "PromptLink"; | ||
export { PromptLink }; |