-
Notifications
You must be signed in to change notification settings - Fork 548
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
Fixed : Disable Submit Button #9526
Changes from all commits
f66ab92
3438758
2557354
7d7b8f4
b46c85d
6a8092f
96903ce
2c88392
9193e17
cf2bd9e
aeb32d5
8e179fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { useEffect, useMemo, useRef, useState } from "react"; | ||
import React, { useEffect, useMemo, useRef, useState } from "react"; | ||
import { useForm } from "react-hook-form"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
|
||
import { Cancel, Submit } from "@/components/Common/ButtonV2"; | ||
import { FieldValidator } from "@/components/Form/FieldValidators"; | ||
import { | ||
FormContextValue, | ||
|
@@ -43,45 +46,61 @@ const Form = <T extends FormDetails>({ | |
hideCancelButton = false, | ||
...props | ||
}: Props<T>) => { | ||
const { t } = useTranslation(); | ||
const initial = { form: props.defaults, errors: {} }; | ||
const [isLoading, setIsLoading] = useState(!!asyncGetDefaults); | ||
const [state, dispatch] = useAutoSaveReducer<T>(formReducer, initial); | ||
const formVals = useRef(props.defaults); | ||
|
||
interface FormData extends FormDetails { | ||
[key: string]: unknown; | ||
} | ||
|
||
const methods = useForm<FormData>({ | ||
defaultValues: props.defaults, | ||
mode: "onChange", | ||
}); | ||
|
||
const { formState } = methods; | ||
|
||
useEffect(() => { | ||
if (!asyncGetDefaults) return; | ||
|
||
asyncGetDefaults().then((form) => { | ||
dispatch({ type: "set_form", form }); | ||
methods.reset(form); | ||
setIsLoading(false); | ||
}); | ||
}, [asyncGetDefaults]); | ||
}, [asyncGetDefaults, methods, dispatch]); | ||
|
||
Comment on lines
66
to
+74
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. 🛠️ Refactor suggestion Add error handling for async defaults. While the synchronization of form state is good, the async operation lacks error handling. if (!asyncGetDefaults) return;
-asyncGetDefaults().then((form) => {
- dispatch({ type: "set_form", form });
- methods.reset(form);
- setIsLoading(false);
-});
+try {
+ const form = await asyncGetDefaults();
+ dispatch({ type: "set_form", form });
+ methods.reset(form);
+} catch (error) {
+ console.error("Failed to load form defaults:", error);
+ Notification.Error({ msg: t("errors.form.loading") });
+} finally {
+ setIsLoading(false);
+}
|
||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
const handleSubmit = async (data: T) => { | ||
try { | ||
if (validate) { | ||
const errors = omitBy(validate(data), isEmpty) as FormErrors<T>; | ||
|
||
if (validate) { | ||
const errors = omitBy(validate(state.form), isEmpty) as FormErrors<T>; | ||
if (Object.keys(errors).length) { | ||
dispatch({ type: "set_errors", errors }); | ||
|
||
if (Object.keys(errors).length) { | ||
dispatch({ type: "set_errors", errors }); | ||
|
||
if (errors.$all) { | ||
Notification.Error({ msg: errors.$all }); | ||
if (errors.$all) { | ||
Notification.Error({ msg: errors.$all }); | ||
} | ||
return; | ||
} | ||
return; | ||
} | ||
} | ||
|
||
const errors = await props.onSubmit(state.form); | ||
if (errors) { | ||
dispatch({ | ||
type: "set_errors", | ||
errors: { ...state.errors, ...errors }, | ||
}); | ||
} else if (props.resetFormValsOnSubmit) { | ||
dispatch({ type: "set_form", form: formVals.current }); | ||
const errors = await props.onSubmit(data); | ||
if (errors) { | ||
dispatch({ | ||
type: "set_errors", | ||
errors: { ...state.errors, ...errors }, | ||
}); | ||
} else if (props.resetFormValsOnSubmit) { | ||
dispatch({ type: "set_form", form: formVals.current }); | ||
methods.reset(formVals.current); | ||
} | ||
} catch (error) { | ||
console.error("Form submission error:", error); | ||
Notification.Error({ msg: t("errors.form.submission") }); | ||
} | ||
}; | ||
|
||
|
@@ -97,10 +116,11 @@ const Form = <T extends FormDetails>({ | |
|
||
return ( | ||
<form | ||
onSubmit={handleSubmit} | ||
onKeyDown={(e) => { | ||
if (e.key === "Enter") { | ||
handleSubmit(e); | ||
onSubmit={methods.handleSubmit((data) => handleSubmit(data as T))} | ||
onKeyDown={(e: React.KeyboardEvent<HTMLFormElement>) => { | ||
if (e.key === "Enter" && !e.shiftKey) { | ||
e.preventDefault(); | ||
methods.handleSubmit((data) => handleSubmit(data as T))(); | ||
} | ||
}} | ||
className={classNames( | ||
|
@@ -113,6 +133,12 @@ const Form = <T extends FormDetails>({ | |
<DraftSection | ||
handleDraftSelect={(newState: FormState<T>) => { | ||
dispatch({ type: "set_state", state: newState }); | ||
Object.entries(newState.form).forEach(([key, value]) => { | ||
methods.setValue(key, value, { | ||
shouldDirty: true, | ||
shouldTouch: true, | ||
}); | ||
}); | ||
props.onDraftRestore?.(newState); | ||
}} | ||
formData={state.form} | ||
|
@@ -123,13 +149,18 @@ const Form = <T extends FormDetails>({ | |
return { | ||
name, | ||
id: name, | ||
onChange: ({ name, value }: FieldChangeEvent<T[keyof T]>) => | ||
onChange: ({ name, value }: FieldChangeEvent<T[keyof T]>) => { | ||
dispatch({ | ||
type: "set_field", | ||
name, | ||
value, | ||
error: validate?.(value), | ||
}), | ||
}); | ||
methods.setValue(name as string, value, { | ||
shouldDirty: true, | ||
shouldTouch: true, | ||
}); | ||
}, | ||
value: state.form[name], | ||
error: state.errors[name], | ||
disabled, | ||
|
@@ -141,17 +172,22 @@ const Form = <T extends FormDetails>({ | |
</div> | ||
<div className="flex flex-col-reverse justify-end gap-3 sm:flex-row"> | ||
{!hideCancelButton && ( | ||
<Cancel | ||
<Button | ||
onClick={handleCancel} | ||
label={props.cancelLabel ?? "Cancel"} | ||
/> | ||
variant="secondary" | ||
disabled={disabled} | ||
> | ||
{props.cancelLabel ?? t("Cancel")} | ||
</Button> | ||
)} | ||
<Submit | ||
<Button | ||
data-testid="submit-button" | ||
type="submit" | ||
disabled={disabled} | ||
label={props.submitLabel ?? "Submit"} | ||
/> | ||
disabled={disabled || !formState.isDirty} | ||
variant="primary" | ||
> | ||
{props.submitLabel ?? t("Submit")} | ||
</Button> | ||
</div> | ||
</Provider> | ||
</DraftSection> | ||
|
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.
lets not modify the old custom
Form
component with newForm
component from shadcn ui.Shadcn's form is built to be used with shadcn's form fields, not our form field components.