简体   繁体   English

如何在提交表单后重置表单并启用提交按钮(react-formio)?

[英]how to reset the form and enable submit button after form submision (react-formio)?

I am using the react-formio package to generate a form dynamically.我正在使用react-formio package 动态生成表单。

I have generated a simple login-form using this link: https://codesandbox.io/s/cra-react-formio-iy8lz我使用此链接生成了一个简单的登录表单: https://codesandbox.io/s/cra-react-formio-iy8lz

After building, it creates a JSON. Then, I generate a form using that JSON, but when I submit by form after fulfill all validation of form it always is shown in disable mode why?构建后,它会创建一个 JSON。然后,我使用该 JSON 生成一个表单,但是当我在fulfill all validation of form后通过表单提交时,它总是以disable mode显示,为什么?

How can we enable button again??我们怎样才能再次启用按钮?? when my promise is resolved and how to reset the form after submitting it?当我的promise is resolved时,提交后如何重置表单?

here is my code, codesandbox link这是我的代码, codesandbox 链接

onSubmit={i => {
  alert(JSON.stringify(i.data));
  var promise1 = new Promise(function(resolve, reject) {
     setTimeout(function() {
        resolve("foo");
      }, 300);
   });
 }

在此处输入图像描述 one more thing还有一件事

I also added one more button我还添加了one more button

{
   label: "Click",
   action: "event",
   showValidations: false,
   block: true,
   key: "click",
   type: "button",
   input: true,
   event: "clickEvent"
},

I also added click handler but it is not working我还添加了点击处理程序,但它不起作用

clickEvent={() => {
  alert("--ss");
}}

You can try to use the submission property of the <Form /> component.您可以尝试使用<Form />组件的提交属性。

As per docs..根据文档..

Submission data to fill the form.提交数据以填写表格。 You can either load a previous submission or create a submission with some pre-filled data.您可以加载以前的提交或使用一些预填充的数据创建提交。 If you do not provide a submissions the form will initialize an empty submission using default values from the form.如果您不提供提交,表单将使用表单中的默认值初始化一个空提交。

So in this case you can control the component in a parent component.所以在这种情况下,您可以在父组件中控制该组件。

Here submissionData is a parent component state and the component initializes with the state from parent (Empty values initially).这里的submissionData是一个父组件 state 并且该组件使用父组件的 state 进行初始化(最初为空值)。

<Form submission={{ data: submissionData }} />

Now, inside the onSubmit handler you can try to reset the state and force a re-render.现在,在onSubmit处理程序中,您可以尝试重置 state 并强制重新渲染。

    onSubmit={() => {
         // Reset submission data
          setSubmissionData({});
        };
      }

Complete code would look like below.完整的代码如下所示。

export default () => {
  const [submissionData, setSubmissionData] = useState({});
  return (
    <Form
      //all form props
      submission={{ data: submissionData }}
      onSubmit={() => {
        var promise1 = new Promise(function(resolve, reject) {
          setTimeout(function() {
            resolve("foo");
          }, 300);
        });

        promise1.then(function(value) {
          alert(value);
         // Reset submission data
          setSubmissionData({});
        });
      }}
    />
  }

Attached codesandbox link as comment.附加代码和框链接作为评论。

I have used a reference to the FormIO form and then used it to get to the underlying formio object to emit a resetForm event.我使用了对 FormIO 表单的引用,然后使用它到达底层formio object 以emit resetForm事件。

This is the code for the reference:这是参考代码:

// The reference
const formComponent = useRef()
// The submit handler
const onSubmit = (e) => {
  formIOContainer.current.innerHTML = '';
  // This is the code which resets the form
  const current = formComponent.current
  current.formio.emit('resetForm')
}

// The Form IO form with the on submit event handler
return (
  <>
    <div id="formIOContainerScroller"/>
    <div id="formIOContainer" ref={formIOContainer}/>
    <Form src={targetUrl}
          onFormLoad={onFormLoad}
          options={formOptions}
          onSubmitDone={onSubmitDone}
          onSubmit={onSubmit}
          ref={formComponent}
    />
  </>
)

This code resets the form after submission and the button is enabled after submission.此代码在提交后重置表单,并在提交后启用按钮。 So you can submit multiple times.所以可以多次提交。 Every time you submit the form gets reset.每次提交表单都会重置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM