airjp73 / rvf

Easy form validation and state management for React and Remix
https://rvf-js.io
MIT License
827 stars 66 forks source link

[Bug]: defaultValues are not re-read on form reset #392

Open Menduist opened 1 day ago

Menduist commented 1 day ago

Which packages are impacted?

What version of these packages are you using?

Please provide a link to a minimal reproduction of the issue.

https://stackblitz.com/edit/stackblitz-starters-xtvc7x?file=app%2Froutes%2F_index.tsx

Steps to Reproduce the Bug or Issue

The repro contains 3 examples: Broky1, Broky2 & Worky. Switch the default between them to see the different behaviors.

Expected behavior

Since resetAfterSubmit is set, and the loader is returning random data, I would expect the input to update its value on each validation Instead, the initial default value is kept

Screenshots or Videos

No response

Platform

Additional context

No response

airjp73 commented 20 hours ago

I think this is the correct behavior. If reset always looked at looked at the latest defaultValues, there would no longer be a guarantee that it would actually reset to the expected values. And if resetAfterSubmit had different behavior than onSubmitSuccess={() => form.resetForm()} that would break expectations as well.

But I think this is worth clarifying in the docs.

We could potentially add first-calls support for this case by letting you specify something like resetAfterFormSubmit="reinitialize" or by adding a key to useForm that would reset & reinitialize.

Menduist commented 7 hours ago

I've added a Worky2 to the example that uses a remix Form, you'll see that it behaves as I would expect (updates the values)

IMO, the defaultValue of each field should always be up to date with the defaultValues of useForm, and thus the reset should always put back the latest defaultValues. This stays consistent with "vanilla react"