You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-watch-change.ts 801B

123456789101112131415161718192021222324
  1. import { useEffect } from 'react';
  2. import { UseFormReturn, useWatch } from 'react-hook-form';
  3. import useGraphStore from '../../store';
  4. import { convertToStringArray } from '../../utils';
  5. export function useWatchFormChange(id?: string, form?: UseFormReturn) {
  6. let values = useWatch({ control: form?.control });
  7. const updateNodeForm = useGraphStore((state) => state.updateNodeForm);
  8. useEffect(() => {
  9. // Manually triggered form updates are synchronized to the canvas
  10. if (id && form?.formState.isDirty) {
  11. values = form?.getValues();
  12. let nextValues: any = values;
  13. nextValues = {
  14. ...values,
  15. content: convertToStringArray(values.content),
  16. };
  17. updateNodeForm(id, nextValues);
  18. }
  19. }, [form?.formState.isDirty, id, updateNodeForm, values]);
  20. }