且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-17 18:39:26

您可以尝试使用

组件的 submission 属性.

根据文档..

提交数据以填写表格.您可以加载以前的提交或使用一些预填充数据创建提交.如果你这样做不提供提交表单将初始化一个空提交使用表单中的默认值.

因此在这种情况下,您可以在父组件中控制组件.

这里的 submissionData 是一个父组件状态,组件用父组件的状态初始化(最初为空值).

现在,在 onSubmit 处理程序中,您可以尝试重置状态并强制重新渲染.

 onSubmit={() =>{//重置提交数据setSubmissionData({});};}

完整的代码如下所示.

export default () =>{const [submissionData, setSubmissionData] = useState({});返回 (<表格//所有表单道具提交={{数据:提交数据}}onSubmit={() =>{var promise1 = new Promise(function(resolve, reject) {设置超时(函数(){解决(富");}, 300);});promise1.then(函数(值){警报(值);//重置提交数据setSubmissionData({});});}}/>}

附加代码和框链接作为评论.

I am using the react-formio package to generate a form dynamically.

I have generated a simple login-form using this link: 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?

How can we enable button again ?? when my promise is resolved and how to reset the form after submitting it?

here is my code, codesandbox link

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

{
   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.

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).

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

Now, inside the onSubmit handler you can try to reset the state and force a re-render.

    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.