Skip to content

Commit 64625bb

Browse files
fix(react-hook-form): resolve modal visibility issue when switching between actions (#7100)
Co-authored-by: Alican Erdurmaz <[email protected]>
1 parent 7050c8a commit 64625bb

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@refinedev/react-hook-form": patch
3+
---
4+
5+
Fix modal visibility issue when switching between actions in useModalForm hook. The modal now properly shows when switching from edit to create action.

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,58 @@ describe("useModalForm Hook", () => {
387387
title: "default-title",
388388
});
389389
});
390+
391+
it("should handle action switching correctly - modal should show after switching from edit to create", async () => {
392+
// This test reproduces the bug described in the issue
393+
const { result, rerender } = renderHook(
394+
({ action }: { action: "create" | "edit" }) =>
395+
useModalForm({
396+
refineCoreProps: {
397+
resource: "posts",
398+
action,
399+
},
400+
}),
401+
{
402+
wrapper: TestWrapper({}),
403+
initialProps: { action: "create" as const },
404+
},
405+
);
406+
407+
// Step 1: Open create modal - should work
408+
await act(async () => {
409+
result.current.modal.show();
410+
});
411+
expect(result.current.modal.visible).toBe(true);
412+
413+
// Step 2: Close modal
414+
await act(async () => {
415+
result.current.modal.close();
416+
});
417+
expect(result.current.modal.visible).toBe(false);
418+
419+
// Step 3: Switch to edit action
420+
rerender({ action: "edit" });
421+
422+
// Step 4: Open edit modal with ID - should work
423+
await act(async () => {
424+
result.current.modal.show("5");
425+
});
426+
expect(result.current.modal.visible).toBe(true);
427+
428+
// Step 5: Close modal
429+
await act(async () => {
430+
result.current.modal.close();
431+
});
432+
expect(result.current.modal.visible).toBe(false);
433+
434+
// Step 6: Switch back to create action
435+
rerender({ action: "create" });
436+
437+
// Step 7: Try to open create modal again
438+
await act(async () => {
439+
result.current.modal.show();
440+
});
441+
442+
expect(result.current.modal.visible).toBe(true);
443+
});
390444
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ export const useModalForm = <
309309
show();
310310
}
311311
},
312-
[id],
312+
[id, action, setId, show],
313313
);
314314

315315
const title = translate(

0 commit comments

Comments
 (0)