final-form icon indicating copy to clipboard operation
final-form copied to clipboard

Feature: Reuse field states from already created instance

Open Dragomir-Ivanov opened this issue 3 years ago • 5 comments

Greetings, Good to see that this project is still active.

Currently one can use createForm instance for the form, and then supply it to React with form={formDate}. The issue is when fields unsibscribe, the form erases their FieldState. E.g. touched, valid, etc. However not always that is the right solution, especially with big forms, trying to hide parts of the form as optimization.

My wish is to add and option to each field, so FF can persist their FieldState on unsubscribe, and use it again when the same field subscribe.

I am willing to provide PRs, if my request makes sense to project owners.

Best, Dragomir

Dragomir-Ivanov avatar Mar 03 '23 19:03 Dragomir-Ivanov

The use case does make sense, I have a feeling it is achievable already at this stage - have a look at wizards? If you can create a mock-up sandbox of a case where conditional rendering fails and discards the data we can have a look at it.

gertdreyer avatar Apr 20 '23 18:04 gertdreyer

Thank you and sorry for the late response. I will make the mock-up and come back again.

Dragomir-Ivanov avatar May 11 '23 11:05 Dragomir-Ivanov

Greetings @gertdreyer, Sorry for the delay. I have prepared the needed sandbox to demonstrate the issue. Please note, how field losses its state, when switching tabs.

https://stackblitz.com/edit/stackblitz-starters-vm3r9h?file=src%2FApp.tsx

Thanks again!

Dragomir-Ivanov avatar Jun 03 '23 18:06 Dragomir-Ivanov

I have not debugged this behaviour properly but I feel it may be related to the code below when the field is not rendered anymore and touched becomes undefined.

https://github.com/final-form/final-form/blob/d20c44be8766b93424e7754fe2629820fd93d21a/src/FinalForm.js#LL616C1-L619C19

I would be willing to accept a PR for a new prop that enables the retaining of the meta state as long as it does not change the current default behaviour.

gertdreyer avatar Jun 12 '23 16:06 gertdreyer

Hey @gertdreyer, any advice on this feature?

Dragomir-Ivanov avatar Aug 25 '23 12:08 Dragomir-Ivanov