andrewhathaway / Winterfell

Generate complex, validated and extendable JSON-based forms in React.
http://winterfell.andrewhathaway.net
MIT License
785 stars 116 forks source link

Reset form on submit #67

Closed elaine0304 closed 8 years ago

elaine0304 commented 8 years ago

Is there a way to reset the form once it is submitted?

After I submit a form, I'm redirecting to another page. But, once I go to the form again, all the values previously typed in/selected(in case of radio buttons) are already present. I need the values to be cleared from the form.

Edit : I believe what I basically need is to refresh the questionAnswers object.

andrewhathaway commented 8 years ago

Hey @elaine0304, at the moment there is no direct reset call, however yes you can pass in the questionAnswers object, and keep track using the onUpdate handler. Another option is to ensure that Winterfell gets unmounted and remounted as a new component. This might be wise as the former option could cause issues when using multiple panels etc. Hope that clears it up!

elaine0304 commented 8 years ago

Thank you so much for your response @andrewhathaway .

Is there a specific way to ensure that Winterfell gets unmounted and remounted as a new component?

andrewhathaway commented 8 years ago

Hi @elaine0304, no worries!

One way you could do this is have a wrapper component that renders Winterfell. This component will have some boolean prop on it, which you can use to choose to render Winterfell or not, named visible or something similar.

In the higher components render function, you can do something like:

render()
{
  return (
    <div>
      {this.props.visible
        ? <Winterfell {...props} />
        : undefined}
    </div>
  );
}

Then when the Winterfell form has been submitted, you can set visible to false, and when you set it back to true the component will remount, and will be reset completely.

Hope this makes sense!

elaine0304 commented 8 years ago

Hi @andrewhathaway,

I tried this method and sure the Winterfell form gets remounted, but unfortunately it is not reset. The questionAnswers object still has the previous values which show up in the form.

One thing that I forgot to mention earlier is that I am writing a custom function to submit the form by setting the disableSubmit option to true and calling my function using the onSubmit event. Can this be a reason for the questionAnswers object to still have the previous values? If so, is there a way to reset the object in my custom submit function?