-
Notifications
You must be signed in to change notification settings - Fork 7
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(ui-commons): prompt form Form displayed on dialog validation #2089
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { FormEvent, useEffect, useMemo, useRef } from "react"; | ||
import { FormEvent, useEffect, useMemo, useRef, useState } from "react"; | ||
import { | ||
DeepPartial, | ||
FieldPath, | ||
|
@@ -116,6 +116,7 @@ function Form<TFieldValues extends FieldValues, TContext>( | |
const { t } = useTranslation(); | ||
const autoSubmitConfig = toAutoSubmitConfig(autoSubmit); | ||
|
||
const [isInProgress, setIsInProgress] = useState(false); | ||
const [showAutoSubmitLoader, setShowAutoSubmitLoader] = useDebouncedState( | ||
false, | ||
750, | ||
|
@@ -130,7 +131,6 @@ function Form<TFieldValues extends FieldValues, TContext>( | |
const lastSubmittedData = useRef<TFieldValues>(); | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
const submitSuccessfulCb = useRef(() => {}); | ||
const preventClose = useRef(false); | ||
|
||
const contextValue = useMemo( | ||
() => ({ isAutoSubmitEnabled: autoSubmitConfig.enable }), | ||
|
@@ -224,7 +224,7 @@ function Form<TFieldValues extends FieldValues, TContext>( | |
// Prevent browser close if a submit is pending | ||
useEffect(() => { | ||
const listener = (event: BeforeUnloadEvent) => { | ||
if (preventClose.current) { | ||
if (isInProgress) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// eslint-disable-next-line no-param-reassign | ||
event.returnValue = t("form.submit.inProgress"); | ||
} else if (isDirty) { | ||
|
@@ -238,14 +238,14 @@ function Form<TFieldValues extends FieldValues, TContext>( | |
return () => { | ||
window.removeEventListener("beforeunload", listener); | ||
}; | ||
}, [t, isDirty]); | ||
}, [t, isInProgress, isDirty]); | ||
|
||
useUpdateEffect(() => onStateChange?.(formState), [formState]); | ||
|
||
useEffect(() => setRef(apiRef, formApiPlus)); | ||
|
||
usePrompt(t("form.submit.inProgress"), preventClose.current); | ||
usePrompt(t("form.changeNotSaved"), isDirty); | ||
usePrompt(t("form.submit.inProgress"), isInProgress); | ||
usePrompt(t("form.changeNotSaved"), isDirty && !isInProgress); | ||
Comment on lines
+247
to
+248
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional: maybe you can wrap those in a useEffect, use strict conditions, and add a cleanup for the prompt useEffect(() => {
if (isSubmitting) {
usePrompt(t("form.submit.inProgress"), true);
} else if (isDirty) {
usePrompt(t("form.changeNotSaved"), true);
} else {
usePrompt(null, false); // Clear the prompt when no conditions are met
}
// Cleanup function to clear the prompt when the component unmounts
return () => usePrompt(null, false);
}, [isSubmitting, isDirty, t]); |
||
|
||
//////////////////////////////////////////////////////////////// | ||
// Submit | ||
|
@@ -299,7 +299,7 @@ function Form<TFieldValues extends FieldValues, TContext>( | |
}); | ||
}) | ||
.finally(() => { | ||
preventClose.current = false; | ||
setIsInProgress(false); | ||
}); | ||
}, onInvalid); | ||
|
||
|
@@ -309,7 +309,8 @@ function Form<TFieldValues extends FieldValues, TContext>( | |
const submitDebounced = useDebounce(submit, autoSubmitConfig.wait); | ||
|
||
const requestSubmit = () => { | ||
preventClose.current = true; | ||
setIsInProgress(true); | ||
|
||
if (autoSubmitConfig.enable) { | ||
submitDebounced(); | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider this instead:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isSubmitting
already exist...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the same component? Ok mybad didnt see that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe you can just add the 'submit' to indicate it refers to a submitting state like
isSubmitInProgress
(it's a detail tho feel free to skip)