Reset form on submit and cancel

This commit is contained in:
Ushran Gouhar 2025-09-24 22:47:35 +05:30
parent 7d779df790
commit e2a19cfd10
2 changed files with 14 additions and 3 deletions

View File

@ -303,7 +303,7 @@ const AddDomainForm = ({
delete (data as CreateDomain & { domains?: unknown }).domains; delete (data as CreateDomain & { domains?: unknown }).domains;
} }
onSubmit(data).then(() => form.resetFields()); onSubmit(data);
}; };
return ( return (

View File

@ -24,6 +24,7 @@ export interface FormDrawerConfig<T = any>
form: ReactNode; form: ReactNode;
onSubmit: (data: T) => Promise<void> | void; onSubmit: (data: T) => Promise<void> | void;
onCancel?: () => void; onCancel?: () => void;
onReset?: () => void;
submitLabel?: string; submitLabel?: string;
cancelLabel?: string; cancelLabel?: string;
loading?: boolean; loading?: boolean;
@ -84,6 +85,7 @@ export const useFormDrawer = <T = any,>(config: FormDrawerConfig<T>) => {
form, form,
onSubmit, onSubmit,
onCancel, onCancel,
onReset,
submitLabel = t('label.save'), submitLabel = t('label.save'),
cancelLabel = t('label.cancel'), cancelLabel = t('label.cancel'),
loading = false, loading = false,
@ -114,7 +116,10 @@ export const useFormDrawer = <T = any,>(config: FormDrawerConfig<T>) => {
label: cancelLabel, label: cancelLabel,
variant: 'text', variant: 'text',
testId: cancelTestId, testId: cancelTestId,
onClick: () => closeRef.current(), onClick: () => {
closeRef.current();
onReset?.(); // Reset form on cancel
},
}, },
primaryButton: { primaryButton: {
label: submitLabel, label: submitLabel,
@ -131,6 +136,7 @@ export const useFormDrawer = <T = any,>(config: FormDrawerConfig<T>) => {
// Form submission error handled by caller // Form submission error handled by caller
} finally { } finally {
setIsSubmitting(false); setIsSubmitting(false);
onReset?.(); // Reset form on submit
} }
}, },
}, },
@ -173,7 +179,11 @@ export const useFormDrawer = <T = any,>(config: FormDrawerConfig<T>) => {
*/ */
export const useFormDrawerWithRef = <T = any,>( export const useFormDrawerWithRef = <T = any,>(
config: FormDrawerConfig<T> & { config: FormDrawerConfig<T> & {
formRef?: { submit: () => void; validateFields?: () => Promise<any> }; formRef?: {
submit: () => void;
validateFields?: () => Promise<any>;
resetFields: () => void;
};
} }
) => { ) => {
const { formRef, onSubmit, ...restConfig } = config; const { formRef, onSubmit, ...restConfig } = config;
@ -198,6 +208,7 @@ export const useFormDrawerWithRef = <T = any,>(
const drawer = useFormDrawer({ const drawer = useFormDrawer({
...restConfig, ...restConfig,
onSubmit: handleSubmit, onSubmit: handleSubmit,
onReset: formRef?.resetFields,
}); });
const submitForm = useCallback(() => { const submitForm = useCallback(() => {