use-watch-form-change.ts
Overview
The use-watch-form-change.ts file defines a custom React hook useWatchFormChange designed to integrate React Hook Form's form state monitoring with a global state management store (likely Zustand) called useGraphStore. This hook watches for changes in a form's values and synchronizes those changes to a graphical canvas or node system by updating the corresponding node's form data in the global store.
In essence, it bridges form input changes and the application's visual graph state, ensuring that the UI representation remains consistent with the form data.
Detailed Explanation
useWatchFormChange
function useWatchFormChange(id?: string, form?: UseFormReturn<any>): void
Purpose
A custom hook that monitors form changes using React Hook Form's useWatch and synchronizes these changes to a node identified by id in a global graph store.
Parameters
id(optional:string):
The unique identifier of the node whose form data should be updated. Ifidis not provided, synchronization does not occur.form(optional:UseFormReturn<any>):
The form object returned by React Hook Form'suseFormhook. This includes the control object for watching form fields and methods to get the current form values.
Return Value
This hook returns nothing (
void). It relies on side effects to update the global store when the form changes.
Functionality & Usage
Internally, it uses
useWatchfrom React Hook Form to subscribe to changes in the form's values.It accesses
updateNodeFormfrom the globaluseGraphStoreto update the node's form data.Inside a
useEffect, it listens to:Changes in form's dirty state (
form?.formState.isDirty) indicating that the user modified the form.Changes in the
idto ensure the node target is current.Changes in watched form values (
values).
When triggered, it calls
updateNodeFormwith the current nodeidand the latest form values.The form values are explicitly fetched via
form?.getValues()to ensure the most up-to-date snapshot is used.
Example Usage
import { useForm } from "react-hook-form";
import { useWatchFormChange } from "./use-watch-form-change";
function NodeEditor({ nodeId }: { nodeId: string }) {
const form = useForm({ defaultValues: { name: "", description: "" } });
// Synchronize form changes with node in global store
useWatchFormChange(nodeId, form);
return (
<form>
<input {...form.register("name")} placeholder="Node name" />
<textarea {...form.register("description")} placeholder="Description" />
{/* other form fields */}
</form>
);
}
Implementation Details and Algorithms
React Hook Form Integration:
The hook usesuseWatchto subscribe to form value changes reactively without manually registering listeners for each field.Global Store Synchronization:
The hook obtains theupdateNodeFormaction fromuseGraphStore(likely Zustand state hook). This function updates the form data for a given node in the global graph state.Effect Dependencies and Update Triggering:
By includingform?.formState.isDirty,id,updateNodeForm, andvaluesin theuseEffectdependency array, the hook ensures that any relevant change triggers a synchronization.Manual Values Fetch:
Instead of relying solely on the watchedvalues, the hook fetches the latest values viaform?.getValues()before updating. This guards against stale values in some edge cases whereuseWatchmight lag.
Interaction with Other Parts of the System
React Hook Form:
The hook depends on React Hook Form's form instance (UseFormReturn) to track and retrieve form state and values.Global State Management (
useGraphStore):
The hook interacts with a global state store, presumably containing graph nodes and their associated data. The store provides theupdateNodeFormfunction to update node data.Graphical Canvas or Node System:
By updating the node's form data in the store, this hook indirectly causes updates in the graphical canvas or node visualization components that consume the global state.
This interaction pattern ensures that form input changes immediately reflect in the graph representation, maintaining UI-data consistency.
Mermaid Diagram
The following flowchart illustrates the key functions and data flow within use-watch-form-change.ts:
flowchart TD
A[useWatchFormChange Hook]
B[useWatch - subscribe to form values]
C[useEffect - watch form dirty state, id, values]
D[getValues() - fetch latest form values]
E[updateNodeForm(id, values) - update global store]
F[useGraphStore - global graph state]
A --> B
B --> C
C --> D
D --> E
E --> F
Summary
use-watch-form-change.ts provides a concise, efficient way to synchronize React Hook Form form changes with a global graph store's node data. Through React Hook Form's useWatch and Zustand-like global state, it maintains real-time consistency between user input and graphical representation of nodes.
This hook is essential in applications where form inputs directly manipulate graphical objects or nodes, streamlining state synchronization without manual event wiring.
Keywords
React Hook Form, useWatch, Zustand, global state, form synchronization, node update, custom hook, React, form state, graph editor.