mirror of
https://github.com/karakeep-app/karakeep.git
synced 2026-02-28 18:25:55 +01:00
* feat: preserve unsaved title changes when modifying bookmark tags Prevents loss of unsaved title edits when users interact with tag selectors or other UI elements. Adds useDialogFormReset hook to maintain form state consistency across component re-renders. Fixes #1339 * Revert unnecessary modifications --------- Co-authored-by: Mohamed Bassem <me@mbassem.com>
28 lines
851 B
TypeScript
28 lines
851 B
TypeScript
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]);
|
|
}
|