Skip to content

Commit

Permalink
Merge pull request #101 from rumeshudash/main
Browse files Browse the repository at this point in the history
Optimize AutoForm value change handling
  • Loading branch information
vantezzen authored Oct 4, 2024
2 parents 6957675 + 64e5b63 commit dea897c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/components/ui/auto-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ function AutoForm<SchemaType extends ZodObjectOrWrapped>({
}
}

const values = form.watch();
// valuesString is needed because form.watch() returns a new object every time
const valuesString = JSON.stringify(values);

React.useEffect(() => {
onValuesChangeProp?.(values);
const parsedValues = formSchema.safeParse(values);
if (parsedValues.success) {
onParsedValuesChange?.(parsedValues.data);
}
}, [valuesString]);
const subscription = form.watch((values) => {
onValuesChangeProp?.(values);
const parsedValues = formSchema.safeParse(values);
if (parsedValues.success) {
onParsedValuesChange?.(parsedValues.data);
}
});

return () => subscription.unsubscribe();
}, [form, formSchema, onValuesChangeProp, onParsedValuesChange]);

const renderChildren =
typeof children === "function"
Expand Down

0 comments on commit dea897c

Please sign in to comment.