aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/lib/hooks/useDialogFormReset.ts
blob: de087ca9e1c4106daab89f6fd3f624b2f79d82dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import type { FieldValues, UseFormReturn } from "react-hook-form";
import { useEffect, useRef } from "react";

/**
 * Custom hook to handle form reset behavior in dialogs
 * Only resets the form when the dialog transitions from closed to open,
 * preventing loss of unsaved changes when external data updates occur
 *
 * @param open - Dialog open state
 * @param form - React Hook Form instance
 * @param resetData - Data to reset the form with
 */
export function useDialogFormReset<T extends FieldValues>(
  open: boolean,
  form: UseFormReturn<T>,
  resetData: T,
) {
  const prevOpenRef = useRef(open);

  useEffect(() => {
    // Only reset form when transitioning from closed to open, not on data updates
    if (open && !prevOpenRef.current) {
      form.reset(resetData);
    }
    prevOpenRef.current = open;
  }, [open, form, resetData]);
}