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

🐛[BUG] 当destroyOnClose为true时,刚打开弹窗setFieldsValue无效 #3500

Closed
without-yy opened this issue Aug 20, 2021 · 5 comments
Closed
Labels
⭕️ bug Something isn't working form

Comments

@without-yy
Copy link

🐛 bug 描述

当destroyOnClose为true时,openModal方法中setFieldsValue无效,请问应该怎么处理才会生效

📷 复现步骤

点用openModal方法时传入value

🏞 期望结果

表单被赋值

💻 复现代码

import React, { useState, useImperativeHandle, useRef } from 'react';
import { ModalForm, ProFormText, ProFormSelect, ProFormTextArea } from '@ant-design/pro-form';

export default React.forwardRef(function ProjectForm(props, ref) {
  const [type, setType] = useState('');
  const [visible, setVisible] = useState(false);
  const formRef = useRef();

  const onFinish = async (values) => {
    return await props?.onFinish(type, values);
  };

  useImperativeHandle(ref, () => {
    return {
      openModal: (type, values) => {
        setVisible(true);
        setType(type);
        const form = formRef.current;
        if (type === 'edit') {
          console.log(form, values);
          form?.setFieldsValue(values);
        }
        // form?.setFieldsValue({ user: [{ value: 1, label: 'xxxx' }] });
      },
    };
  });

  return (
    <ModalForm
      formRef={formRef}
      title={type === 'add' ? '新增' : '修改'}
      visible={visible}
      onVisibleChange={setVisible}
      onFinish={onFinish}
      modalProps={{ destroyOnClose: true }}
    >
      <ProFormText label="项目名称" name="name1" />
      <ProFormText label="项目简称" name="name" />
    </ModalForm>
  );
});

© 版本信息

"@ant-design/pro-form@^1.31.2": version "1.32.1"

  • ProComponents 版本: [e.g. 4.0.0]
  • umi 版本
  • 浏览器环境
  • 开发环境 [e.g. mac OS]

🚑 其他信息

@github-actions
Copy link

以下的 Issues 可能会帮助到你 / The following issues may help you

@kiner-tang
Copy link
Member

看你的示例看起来是在设置表单的初始值,为什么不尝试使用initialValues属性呢?

@without-yy
Copy link
Author

without-yy commented Sep 3, 2021

看你的示例看起来是在设置表单的初始值,为什么不尝试使用initialValues属性呢?
@kiner-tang
这个组件会被多次打开,当type为add时我需要重置表单,为edit时设置value,请问应该怎么处理呢

@chenshuai2144
Copy link
Contributor

是我的bug,因为关闭的时候form 没了。我排查下为什么

@chenshuai2144 chenshuai2144 added form ⭕️ bug Something isn't working labels Sep 22, 2021
@chenshuai2144
Copy link
Contributor

修复了。因为触发了重新set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭕️ bug Something isn't working form
Projects
None yet
Development

No branches or pull requests

3 participants