Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: change initialValue behaviour when field wasn't touched yet #3845

Closed
FRSgit opened this issue Jul 15, 2022 · 2 comments
Closed

Feature: change initialValue behaviour when field wasn't touched yet #3845

FRSgit opened this issue Jul 15, 2022 · 2 comments
Labels
✨ enhancement a "nice to have" feature

Comments

@FRSgit
Copy link

FRSgit commented Jul 15, 2022

Is your feature request related to a problem? Please describe.

Current behaviour of initialValue is a bit problematic for the async use cases. I've tried to show it on this example:
https://codesandbox.io/s/basic-example-composition-api-forked-u2pc93?file=/src/App.vue:303-572

In short - even though initialValue supports being passed as a ref it doesn't seem to bring any value. After doing an API call I'm changing the the value of initialValue ref and... nothing happens. Not only the value of the field stays the same (even though I - as a user - didn't touched it yet), but also the dirty state stays unchanged.

Describe the solution you'd like

I would propose to change the way initialValue changes are being handled.

  1. When there was no interaction with the field whatsoever (meta.touched or meta.dirty are false) the new value of initialValue should be propagated directly to the value of the field - so there is no need for resetting the field after every API call.
  2. resetField should use current value of initialValue when value field is not provided.
  3. meta.dirty should be reevaluated every time initialValue changes.

Describe alternatives you've considered

Currently I need to run resetField every time I fetch data from the API (which becomes pretty tedious when having multiple fields and multiple API calls in the same file). Also I need to pass value of initialValue every time I run resetField - it seems that resetField ignores initialValue.
https://codesandbox.io/s/basic-example-composition-api-forked-ubxk88?file=/src/App.vue:405-701

I know that I've described 3 things here, so if you believe only some of them should be introduced - please write about that.
Also - I'm willing to write a PR changing this.

@logaretm
Copy link
Owner

logaretm commented Jul 16, 2022

The behavior you are asking for is actually implemented but only works if the field is part of a form context and the initial values are controlled by the form.

https://codesandbox.io/s/basic-example-composition-api-forked-fzmz83?file=/src/App.vue

Whether or not it should be implemented on the single field level, I think it makes sense to be consistent with the form behavior and make it work similarly. I will see what I can do, if you would like to try PR then go for it! Just remember you need to do it for the useField only as useForm already has this implemented.

@logaretm logaretm added the ✨ enhancement a "nice to have" feature label Jul 16, 2022
@FRSgit
Copy link
Author

FRSgit commented Jul 17, 2022

@logaretm Hey, thanks for the response! Ofc I'll give it a try - if you have any additional suggestions just shoot them my way. If everything goes smoothly I should file a PR this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement a "nice to have" feature
Projects
None yet
Development

No branches or pull requests

2 participants