Skip to content

Commit

Permalink
fix(form): onOpenChange fires twice when opening ModalForm (#8311)
Browse files Browse the repository at this point in the history
* fix(ModalForm): onOpenChange fires twice when opening ModalForm

* test(ModalForm): onOpenChange fires twice when opening ModalForm
  • Loading branch information
zhuba-Ahhh committed Apr 17, 2024
1 parent 9daed82 commit f12ef2b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
23 changes: 13 additions & 10 deletions packages/form/src/layouts/ModalForm/index.tsx
Expand Up @@ -7,7 +7,7 @@ import { noteOnce } from 'rc-util/lib/warning';
import React, {
useCallback,
useContext,
useEffect,
// useEffect,
useImperativeHandle,
useMemo,
useRef,
Expand Down Expand Up @@ -55,7 +55,7 @@ export type ModalFormProps<
visible?: boolean;

/** @name 打开关闭的事件 */
onOpenChange?: (visible: boolean) => void;
onOpenChange?: (open: boolean) => void;
/**
* 不支持 'visible',请使用全局的 visible
*
Expand Down Expand Up @@ -130,13 +130,14 @@ function ModalForm<T = Record<string, any>, U = Record<string, any>>({
[formRef.current],
);

useEffect(() => {
if (open && (propsOpen || propVisible)) {
onOpenChange?.(true);
onVisibleChange?.(true);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [propVisible, propsOpen, open]);
// useMergedState监听了open的变化
// useEffect(() => {
// if (open && (propsOpen || propVisible)) {
// onOpenChange?.(true);
// onVisibleChange?.(true);
// }
// // eslint-disable-next-line react-hooks/exhaustive-deps
// }, [propVisible, propsOpen, open]);

const triggerDom = useMemo(() => {
if (!trigger) {
Expand Down Expand Up @@ -246,7 +247,9 @@ function ModalForm<T = Record<string, any>, U = Record<string, any>>({
}}
afterClose={() => {
resetFields();
setOpen(false);
if (open) {
setOpen(false);
}
modalProps?.afterClose?.();
}}
footer={
Expand Down
37 changes: 19 additions & 18 deletions tests/form/modalForm.test.tsx
Expand Up @@ -267,25 +267,26 @@ describe('ModalForm', () => {
await waitForWaitTime(100);
});
expect(fn).toBeCalledWith(false);
expect(fn).toBeCalledTimes(2);
expect(fn).toBeCalledTimes(1); // 关闭只触发一次 onOpenChange
});

it('📦 modal open=true simulate onOpenChange', async () => {
const fn = vi.fn();
render(
<ModalForm
open
trigger={<Button id="new">新建</Button>}
onOpenChange={(visible) => fn(visible)}
>
<ProFormText name="name" />
</ModalForm>,
);

await waitFor(() => {
expect(fn).toBeCalledWith(true);
});
});
// 如果默认就是 open 的 onOpenChange 预期应当不触发
// it('📦 modal open=true simulate onOpenChange', async () => {
// const fn = vi.fn();
// render(
// <ModalForm
// open
// trigger={<Button id="new">新建</Button>}
// onOpenChange={(visible) => fn(visible)}
// >
// <ProFormText name="name" />
// </ModalForm>,
// );

// await waitFor(() => {
// expect(fn).toBeCalledWith(true);
// });
// });

it('📦 reset button will simulate onOpenChange', async () => {
const fn = vi.fn();
Expand Down Expand Up @@ -385,7 +386,7 @@ describe('ModalForm', () => {
await waitFor(async () => {
await waitForWaitTime(100);
});
expect(fn).toBeCalledTimes(1);
expect(fn).toBeCalledTimes(0);
});

it('📦 ModalForm support submitter is false', async () => {
Expand Down

0 comments on commit f12ef2b

Please sign in to comment.