final-form / final-form

🏁 Framework agnostic, high performance, subscription-based form state management

Home Page:https://final-form.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support returned Observable from submit handler

belev opened this issue · comments

Are you submitting a bug report or a feature request?

Feature request

What is the current behavior?

Returning observable from submit handler behaves as a synchronous operation and form state is not changing (submitting, .etc).

What is the expected behavior?

Support returned observable from submit like it is handling returned Promise.

Sandbox Link

https://stackblitz.com/edit/react-final-form-reset-submit-sktcvb

What's your environment?

Final form version - 4.18.5
React final form version - 6.3.0
OS - MacOS
Browser - Chrome version 80.0.3987.163

Note: not environment specific.

Other information

Observables and RxJS are being used more and more in the UI world. It would be nice to have Observables support from submit handlers if at all this is something that can go into final-form.

If this is something that can be considered as a feature request, I would be happy to help and try to implement with eventual help/guidance.

commented

Curious, isn't this solvable by just calling toPromise() on the observable in your own submit handler?

Yep, it will be solved. However, this way is just like using Promises. This is the thing that we would like to avoid in order to use only Observables.

commented

The crux is that you want to do something async in your onSubmit handler.

Final-Form already provides 2 ways of doing something async during onSubmit

onSubmit = (formData,api,done) => observableRequest(formData)
  .pipe(....your pipe)
  .subscribe(
    () => done()
    errs => done(errs)
  );

or

onSubmit = formData => observableRequest(formData).pipe(...your pipe).toPromise();

A proposed PR would simply check if result from the onSubmit handler has a subscribe property that is a function and then call toPromise() on the result before processing it as a promise like it does on this line.

Thank you for the links, we are using callback in a couple of places already. But still, I was thinking that it would be nice to have Observable support because it will save some additional lines of code. Unfortunately, the PR won't be so simple - just to check for subscribe and call toPromise() because types support should be added which will be the harder part, I believe.

I will play around with it a little bit more to see if I will get it working.

After trying to add support into final-form and play around more with the callback that is provided for asynchronous submit handlers, the advantage of having Observable support from submit doesn't seem to make much sense. It is not going to save writing more/much code and callback is working perfectly.

Thanks @akmjenkins for the help.

This being said, I am closing this as it is not relevant anymore.