Skip to content

Commit e78213f

Browse files
feat: add auto reset form option to use-modal-form hook (#6835)
Co-authored-by: Alican Erdurmaz <alicanerdurmaz@gmail.com>
1 parent fcb1096 commit e78213f

8 files changed

Lines changed: 160 additions & 1 deletion

File tree

.changeset/cyan-carrots-remain.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@refinedev/react-hook-form": patch
3+
"@refinedev/mantine": patch
4+
"@refinedev/antd": patch
5+
---
6+
7+
feat: add `autoResetFormWhenClose` option to `useModalForm` hook
8+
9+
Added `autoResetFormWhenClose` prop to reset form when modal closes. Default is `true`.

documentation/docs/packages/react-hook-form/use-modal-form/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,18 @@ const modalForm = useModalForm({
663663
});
664664
```
665665

666+
### autoResetFormWhenClose
667+
668+
When `true`, form will be reset when modal closes. Defaults to `true`.
669+
670+
```tsx
671+
const modalForm = useModalForm({
672+
modalProps: {
673+
autoResetFormWhenClose: false,
674+
},
675+
});
676+
```
677+
666678
### warnWhenUnsavedChanges
667679

668680
When you have unsaved changes and try to leave the current page, Refine shows a confirmation modal box. To activate this feature. By default, this feature is disabled.

documentation/docs/ui-integrations/ant-design/hooks/use-modal-form/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,16 @@ const modalForm = useModalForm({
475475
});
476476
```
477477

478+
### autoResetFormWhenClose
479+
480+
`autoResetFormWhenClose` will reset the form when modal closes. It is `true` by default.
481+
482+
```tsx
483+
const modalForm = useModalForm({
484+
autoResetFormWhenClose: false,
485+
});
486+
```
487+
478488
### warnWhenUnsavedChanges
479489

480490
When set to true, `warnWhenUnsavedChanges` shows a warning when the user tries to leave the page with unsaved changes. It is used to prevent the user from accidentally leaving the page. It is `false` by default

documentation/docs/ui-integrations/mantine/hooks/use-modal-form/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,18 @@ const modalForm = useModalForm({
793793
});
794794
```
795795

796+
### autoResetFormWhenClose
797+
798+
When `true`, form will be reset when modal closes. It is `true` by default.
799+
800+
```tsx
801+
const modalForm = useModalForm({
802+
modalProps: {
803+
autoResetFormWhenClose: false,
804+
},
805+
});
806+
```
807+
796808
### syncWithLocation
797809

798810
When `true`, the modals visibility state and the `id` of the record will be synced with the URL. It is `false` by default.

packages/antd/src/hooks/form/useModalForm/useModalForm.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export type UseModalFormProps<
8989
defaultVisible?: boolean;
9090
autoSubmitClose?: boolean;
9191
autoResetForm?: boolean;
92+
autoResetFormWhenClose?: boolean;
9293
};
9394
/**
9495
* `useModalForm` hook allows you to manage a form within a modal. It returns Ant Design {@link https://ant.design/components/form/ Form} and {@link https://ant.design/components/modal/ Modal} components props.
@@ -113,6 +114,7 @@ export const useModalForm = <
113114
defaultVisible = false,
114115
autoSubmitClose = true,
115116
autoResetForm = true,
117+
autoResetFormWhenClose = true,
116118
autoSave,
117119
invalidates,
118120
...rest
@@ -291,6 +293,10 @@ export const useModalForm = <
291293

292294
setId?.(undefined);
293295
sunflowerUseModal.close();
296+
297+
if (autoResetFormWhenClose) {
298+
form.resetFields();
299+
}
294300
}, [warnWhen, autoSaveProps.status]);
295301

296302
const handleShow = useCallback(

packages/mantine/src/hooks/form/useModalForm/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export type UseModalFormProps<
7474
defaultVisible?: boolean;
7575
autoSubmitClose?: boolean;
7676
autoResetForm?: boolean;
77+
autoResetFormWhenClose?: boolean;
7778
};
7879
} & FormWithSyncWithLocationParams;
7980

@@ -123,6 +124,7 @@ export const useModalForm = <
123124
defaultVisible = false,
124125
autoSubmitClose = true,
125126
autoResetForm = true,
127+
autoResetFormWhenClose = true,
126128
} = modalProps ?? {};
127129

128130
const {
@@ -277,6 +279,10 @@ export const useModalForm = <
277279

278280
setId?.(undefined);
279281
close();
282+
283+
if (autoResetFormWhenClose) {
284+
reset();
285+
}
280286
}, [warnWhen, autoSaveProps.status]);
281287

282288
const handleShow = useCallback(

packages/react-hook-form/src/useModalForm/index.spec.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,99 @@ describe("useModalForm Hook", () => {
271271
undefined,
272272
);
273273
});
274+
275+
it("when 'autoResetFormWhenClose' is true, 'reset' should be called when 'close' is called", async () => {
276+
const { result } = renderHook(
277+
() =>
278+
useModalForm({
279+
refineCoreProps: {
280+
resource: "posts",
281+
action: "create",
282+
},
283+
modalProps: {
284+
autoResetFormWhenClose: true,
285+
},
286+
}),
287+
{
288+
wrapper: TestWrapper({}),
289+
},
290+
);
291+
292+
await act(async () => {
293+
result.current.modal.show();
294+
// register values to form
295+
result.current.register("test");
296+
result.current.setValue("test", "test");
297+
});
298+
299+
await act(async () => {
300+
result.current.modal.close();
301+
});
302+
303+
// check if form is reset. (values object should be empty)
304+
expect(result.current.getValues()).toStrictEqual({});
305+
});
306+
307+
it("when 'autoResetFormWhenClose' is false, 'reset' should not be called when 'close' is called", async () => {
308+
const { result } = renderHook(
309+
() =>
310+
useModalForm({
311+
refineCoreProps: {
312+
resource: "posts",
313+
action: "create",
314+
},
315+
modalProps: {
316+
autoResetFormWhenClose: false,
317+
},
318+
}),
319+
{
320+
wrapper: TestWrapper({}),
321+
},
322+
);
323+
324+
await act(async () => {
325+
result.current.modal.show();
326+
// register values to form
327+
result.current.register("test");
328+
result.current.setValue("test", "test");
329+
});
330+
331+
await act(async () => {
332+
result.current.modal.close();
333+
});
334+
335+
// check if form is not reset. (values object should NOT be empty)
336+
expect(result.current.getValues()).toStrictEqual({ test: "test" });
337+
});
338+
339+
it("when 'autoResetFormWhenClose' is true for edit action, 'reset' should be called when 'close' is called", async () => {
340+
const { result } = renderHook(
341+
() =>
342+
useModalForm({
343+
refineCoreProps: {
344+
resource: "posts",
345+
action: "edit",
346+
id: "5",
347+
},
348+
modalProps: {
349+
autoResetFormWhenClose: true,
350+
},
351+
}),
352+
{
353+
wrapper: TestWrapper({}),
354+
},
355+
);
356+
357+
await act(async () => {
358+
result.current.modal.show();
359+
result.current.register("test");
360+
result.current.setValue("test", "test");
361+
});
362+
363+
await act(async () => {
364+
result.current.modal.close();
365+
});
366+
367+
expect(result.current.getValues()).toStrictEqual({});
368+
});
274369
});

packages/react-hook-form/src/useModalForm/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,21 @@ export type UseModalFormProps<
6868
* `autoSubmitClose`: Whether the form should be submitted when the modal is closed.
6969
*
7070
* `autoResetForm`: Whether the form should be reset when the form is submitted.
71+
*
72+
* `autoResetFormWhenClose`: Whether the form should be reset to defaultValues when the modal is closed.
7173
* @type `{
7274
defaultVisible?: boolean;
7375
autoSubmitClose?: boolean;
7476
autoResetForm?: boolean;
77+
autoResetFormWhenClose?: boolean;
7578
}`
76-
* @default `defaultVisible = false` `autoSubmitClose = true` `autoResetForm = true`
79+
* @default `defaultVisible = false` `autoSubmitClose = true` `autoResetForm = true` `autoResetFormWhenClose = true`
7780
*/
7881
modalProps?: {
7982
defaultVisible?: boolean;
8083
autoSubmitClose?: boolean;
8184
autoResetForm?: boolean;
85+
autoResetFormWhenClose?: boolean;
8286
};
8387
} & FormWithSyncWithLocationParams;
8488

@@ -146,6 +150,7 @@ export const useModalForm = <
146150
defaultVisible = false,
147151
autoSubmitClose = true,
148152
autoResetForm = true,
153+
autoResetFormWhenClose = true,
149154
} = modalProps ?? {};
150155

151156
const useHookFormResult = useForm<
@@ -271,6 +276,10 @@ export const useModalForm = <
271276

272277
setId?.(undefined);
273278
close();
279+
280+
if (autoResetFormWhenClose) {
281+
reset();
282+
}
274283
}, [warnWhen, autoSaveProps.status]);
275284

276285
const handleShow = useCallback(

0 commit comments

Comments
 (0)