Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-hook-form): correctly handle unsaved changes warning (#6691) #6702

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-dryers-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@refinedev/react-hook-form": patch
---

fix(react-hook-form): properly warn unsaved changes only when there are actual changes (#6691)
9 changes: 5 additions & 4 deletions packages/react-hook-form/src/useForm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const useForm = <
watch,
setValue,
getValues,
formState,
handleSubmit: handleSubmitReactHookForm,
setError,
} = useHookFormResult;
Expand Down Expand Up @@ -229,11 +230,11 @@ export const useForm = <
return () => subscription.unsubscribe();
}, [watch]);

const onValuesChange = (changeValues: TVariables) => {
if (warnWhenUnsavedChanges) {
setWarnWhen(true);
}
useEffect(() => {
setWarnWhen(formState.isDirty && warnWhenUnsavedChanges);
}, [formState.isDirty, warnWhenUnsavedChanges]);

const onValuesChange = (changeValues: TVariables) => {
if (refineCoreProps?.autoSave?.enabled) {
setWarnWhen(false);

Expand Down