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 changes with an external store (likely a global state or canvas state). Its main purpose is to watch form changes and synchronize the current form values with a graph or node-based data store, enabling real-time updates of a node's form data within a graphical interface or canvas.
This hook is particularly useful in applications that combine form inputs with graphical representations or nodes, such as flow editors, diagram tools, or node-based UI builders.
Detailed Explanation
useWatchFormChange Hook
export function useWatchFormChange(id?: string, form?: UseFormReturn<any>)
Description
useWatchFormChange is a React custom hook that listens for changes in a form's state and updates a node's data in a centralized graph store accordingly.
Parameters
id?: stringOptional unique identifier for the node whose form data needs to be updated.
If not provided, the hook does not perform any synchronization.
form?: UseFormReturn<any>Optional form instance returned by React Hook Form's
useFormhook.Expected to contain the form control and methods to access form state and values.
Behavior
Internally, the hook uses
useWatchfrom React Hook Form to subscribe to all form field changes (values).It also retrieves the
updateNodeFormmethod from the graph store, which is responsible for updating node data in the global state.Using a
useEffecthook, it listens to changes in form dirtiness (form?.formState.isDirty), the node identifierid, theupdateNodeFormfunction, and the watchedvalues.When any of these dependencies change and
idis provided:The hook retrieves the latest form values using
form?.getValues().Calls
updateNodeForm(id, nextValues)to synchronize the form data with the graph store.
Return Value
This hook does not return anything (
void).It exists purely for its side effect of updating the graph store based on form changes.
Usage Example
import { useForm } from 'react-hook-form';
import { useWatchFormChange } from './use-watch-form-change';
function NodeEditor({ nodeId }) {
const form = useForm({
defaultValues: {
label: '',
color: '#fff',
},
});
// Synchronize form changes with the node store
useWatchFormChange(nodeId, form);
return (
<form>
<input {...form.register('label')} placeholder="Node label" />
<input type="color" {...form.register('color')} />
</form>
);
}
Important Implementation Details
Reactive form value watching:
The hook usesuseWatchto efficiently subscribe to form field changes without manually wiring event handlers.Synchronization on "dirty" state:
The effect runs when the form'sisDirtystate changes, ensuring that updates are only propagated when the user has modified the form, avoiding unnecessary updates.Dependency on external store:
The hook depends onuseGraphStore(likely a Zustand or similar global store) to update the node's form data. This suggests tight coupling with a graphical node-based system.Safe access to form values:
The form values are retrieved usingform?.getValues()to ensure the latest snapshot of the form state is sent to the store.
Interaction with Other System Components
React Hook Form:
The hook leverages React Hook Form'suseWatchanduseFormreturn object to track changes in form inputs.Graph Store (
useGraphStore):
This is a custom global state store (possibly Zustand or Redux) that manages node data in a graph or canvas. The hook callsupdateNodeFormto reflect form changes into this store.Node-based UI Components:
Components representing nodes in a graphical interface likely use this hook to keep their form UI and underlying data model in sync.
Visual Diagram
flowchart TD
A[React Component with Form] -->|provides| B[useForm instance]
B --> C[useWatchFormChange Hook]
C --> D[useWatch (react-hook-form)]
C --> E[useGraphStore]
E --> F[updateNodeForm(id, values)]
D --> C
B --> C
C -->|on form change| F
Diagram Explanation
The React component initializes a form using React Hook Form (
useForm).The form instance is passed to the
useWatchFormChangehook.Inside the hook,
useWatchlistens for form value changes.When changes are detected, the hook retrieves
updateNodeFormfromuseGraphStore.The form values are passed to
updateNodeFormalong with the nodeid.This updates the node's data in the global graph store, syncing UI and data.
Summary
The use-watch-form-change.ts file provides a focused integration hook that binds React Hook Form state changes to a graph or node data store. It abstracts away the complexity of synchronizing form input changes with a global node-based state, enabling seamless updates within graphical or node-oriented applications.
This hook is a key utility for maintaining consistency between user input forms and their corresponding nodes on a canvas or graph structure. It leverages React Hook Form's efficient form state watching and a custom graph store update function to deliver real-time synchronization.