"use client"; import * as React from "react"; import { zodResolver } from "@hookform/resolvers/zod"; import { type DefaultValues, type FieldValues, FormProvider, type SubmitHandler, type UseFormProps, type UseFormReturn, useForm, } from "react-hook-form"; import type { z } from "zod"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; export interface UseZodDialogFormOptions extends Omit, "resolver" | "defaultValues"> { schema: z.ZodType; defaultValues: DefaultValues; } export function useZodDialogForm({ schema, defaultValues, ...options }: UseZodDialogFormOptions) { return useForm({ ...options, defaultValues, resolver: zodResolver(schema), }); } export interface DialogFormProps { form: UseFormReturn; open: boolean; onOpenChange: (open: boolean) => void; onSubmit: SubmitHandler; title: string; description?: string; children: React.ReactNode; trigger?: React.ReactNode; formId?: string; submitLabel?: string; cancelLabel?: string; submitting?: boolean; } export function DialogForm({ form, open, onOpenChange, onSubmit, title, description, children, trigger, formId, submitLabel = "Save", cancelLabel = "Cancel", submitting = false, }: DialogFormProps) { const reactId = React.useId(); const resolvedFormId = formId ?? `dialog-form-${reactId}`; return ( {trigger ? {trigger} : null} {title} {description ? {description} : null}
{children}
); } export interface DestructiveConfirmDialogProps { open: boolean; onOpenChange: (open: boolean) => void; title: string; description: string; confirmLabel?: string; cancelLabel?: string; confirming?: boolean; onConfirm: () => void | Promise; } export function DestructiveConfirmDialog({ open, onOpenChange, title, description, confirmLabel = "Delete", cancelLabel = "Cancel", confirming = false, onConfirm, }: DestructiveConfirmDialogProps) { return ( {title} {description} {cancelLabel} void onConfirm()}> {confirmLabel} ); }