Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧐[问题] ModalForm 打开时 onOpenChange 触发两次 #8156

Closed
Icesource opened this issue Feb 5, 2024 · 2 comments · Fixed by #8311
Closed

🧐[问题] ModalForm 打开时 onOpenChange 触发两次 #8156

Icesource opened this issue Feb 5, 2024 · 2 comments · Fixed by #8311

Comments

@Icesource
Copy link

🧐 问题描述

使用 open 和 onOpenChange 控制 ModalForm 时,打开ModalForm会触发两次onOpenChange true,关闭时触发一次 false
相似问题 #7545

💻 示例代码

  const _onOpenChange = (open: boolean) => {
    console.log(open);
    setOpen(open);
  }

<ModalForm<any> title={<BaseModalTitle {...titleProps}/>}
                      {...(showEntryDom && {trigger: realEntryDom})}
                      modalProps={{
                        destroyOnClose: true, style: style,
                        afterClose: afterClose
                      }}
                      width={width}
                      open={open}
                      onOpenChange={_onOpenChange}
                      form={form}
                      initialValues={initialValues}
                      labelAlign={"left"}
                      layout={"horizontal"}
                      loading={loading}

                      {...(onFinish && {onFinish: onFinish})}
                      {...layout}
                      submitter={{
                        render: (props, defaultDoms) => {
                          return [
                            <div key={"baseModalSubmitter"}
                                 style={{width: "100%", display: "flex", justifyContent: "space-between"}}>
                              <div>{footer?.left}</div>
                              <div><Space>{
                                FooterRight ? FooterRight : showConfirmButton? defaultDoms: defaultDoms[0]
                              }</Space></div>
                            </div>
                          ]
                        }
                      }}>
        {children}
      </ModalForm>

🚑 其他信息

image

@one-pieces
Copy link

应该是同时使用了 open 和 trigger 来控制弹窗的开关,在第一次触发 onOpenChange 的时候,去 setOpen,然后改了 open,有触发了第二次 onOpenChange

@one-pieces
Copy link

在 onOpenChange 里加个判断前后 open 是否一致,不一致就执行逻辑,这样可以暂时解决执行两次的问题。

const [open, setOpen] = useState(false);
...
const onOpenChange = (isOpen: boolean) => {
  setOpen(isOpen);
  if (isOpen && !open) {
    // 弹窗打开时需要执行的逻辑
    ....
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants