feat(ST-1114): add DirtyCloseGuard reusable component for dirty-form close confirmation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Janpeter Visser 2026-04-30 00:36:03 +02:00
parent bf271d899b
commit 73c27ac43e

View file

@ -0,0 +1,58 @@
'use client'
import { useState } from 'react'
import {
AlertDialog,
AlertDialogContent,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogAction,
AlertDialogCancel,
} from '@/components/ui/alert-dialog'
interface DirtyCloseGuardProps {
isDirty: boolean
onConfirm: () => void
children: (attemptClose: () => void) => React.ReactNode
}
export function DirtyCloseGuard({ isDirty, onConfirm, children }: DirtyCloseGuardProps) {
const [open, setOpen] = useState(false)
function attemptClose() {
if (isDirty) {
setOpen(true)
} else {
onConfirm()
}
}
return (
<>
{children(attemptClose)}
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogContent size="sm">
<AlertDialogHeader>
<AlertDialogTitle>Wijzigingen niet opgeslagen</AlertDialogTitle>
<AlertDialogDescription>
Wil je de wijzigingen weggooien?
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={() => setOpen(false)}>
Blijven
</AlertDialogCancel>
<AlertDialogAction
variant="destructive"
onClick={() => { setOpen(false); onConfirm() }}
>
Weggooien
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
)
}