Skip to content

[BUG] useModalForm doesn't show after switching action from edit to create #7095

@krakowski

Description

@krakowski

Describe the bug

I tried Workaround 1 from #6451 (comment) by @aliemir and found that initially (i.e. using the initial action state) the modal does open, but opening it in edit mode and going back to create leaves modal.visible at false even though modal.show() was called.

Steps To Reproduce

import type { BaseKey, FormAction } from "@refinedev/core";
import { useModalForm } from "@refinedev/react-hook-form";
import { useCallback, useState } from "react";

{/*

    1. Click on "Open Create Modal"    State label will change to "visible" 

    2. Click on "Close Modal"    State label will change to "invisible" 

    3. Click on "Open Edit Modal"    State label will change to "visible" 

    4. Click on "Close Modal"   State label will change to "invisible" 

    5. Click on "Open Create Modal"   State label will stay at "invisible" 

/*}

export const Repro = () => {
	const [action, setAction] = useState<FormAction>();

	const form = useModalForm({
		refineCoreProps: {
			action: action,
			resource: "organization-contacts",
		},
	});

	const showModal = useCallback(
		(action: FormAction, id?: BaseKey) => {
			setAction(action);
			form.modal.show(id);
		},
		[form],
	);

	return (
		<div>
			<span>
				Current Modal State: {form.modal.visible ? "visible" : "invisible"}
			</span>
			<div
				style={{
					display: "flex",
					flexDirection: "row",
					gap: 16,
					alignItems: "center",
				}}
			>
				<button type="button" onClick={() => showModal("create")}>
					Open Create Modal
				</button>
				<button type="button" onClick={() => showModal("edit", 4)}>
					Open Edit Modal
				</button>
				<button type="button" onClick={() => form.modal.close()}>
					Close Modal
				</button>
			</div>
		</div>
	);
};

Expected behavior

Switching from action: "edit" to action: "create" and calling modal.show() should make the modal visible.

Packages

## System:
 - OS: Linux 6.16 Manjaro Linux
 - CPU: (16) x64 AMD Ryzen 7 7800X3D 8-Core Processor
## Binaries:
 - Node: 24.8.0 - /usr/bin/node
 - Yarn: 4.10.1 - /usr/bin/yarn
 - npm: 11.6.0 - /usr/bin/npm
## Browsers:
 - Chrome: Not Found
 - Firefox: 144.0
## Refine Packages:
 - @refinedev/core: 5.0.4
 - @refinedev/react-hook-form: 5.0.1
 - @refinedev/simple-rest: 6.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions