refactor(forms): update form submission methods

This commit is contained in:
Thomas Camlong committed 2026-05-29 11:46:03 +02:00
1 parent fec0eb51a3
commit ecbed76a65
3 files changed
+69 -26

No files matched your search

@@ -18,7 +18,7 @@ import { findBestIconMatch } from "./icon-matcher";
type FormType = z.infer<typeof appManageSchema>;
export interface AppFormHandle {
submit: () => void;
submit: () => Promise<boolean>;
isDirty: () => boolean;
}
@@ -29,7 +29,7 @@ interface AppFormProps {
submitAndCreateAnother?: string;
};
initialValues?: FormType;
handleSubmit: (values: FormType, redirect: boolean, afterSuccess?: () => void) => void;
handleSubmit: (values: FormType, redirect: boolean, afterSuccess?: () => void) => void | Promise<void>;
isPending: boolean;
hideButtons?: boolean;
formRef?: React.Ref<AppFormHandle>;
@@ -74,7 +74,20 @@ export const AppForm = ({
useImperativeHandle(
formRef,
() => ({
submit: () => form.onSubmit(handleSubmit)(),
submit: () =>
new Promise<boolean>((resolve) => {
form.onSubmit(
async (values) => {
try {
await Promise.resolve(handleSubmit(values));
resolve(true);
} catch {
resolve(false);
}
},
() => resolve(false),
)();
}),
isDirty: () => form.isDirty(),
}),
[form, handleSubmit],