-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Extend setValue functionality #1513
Extend setValue functionality #1513
Conversation
@jaredpalmer If you had any ideas on why |
@samstarling it looks like the specific version of TypeScript that is used in the build requires an explicit
I'm a bit confused as to why we need a new function instead of modifying the behavior of setValues to accept a partial state, and an optional shouldValidate parameter. It seems to do the exact same thing as setValues. |
@johnrom Thanks for pointing that out – really useful, and I'll copy across that 3.3.x upgrade. Regarding the difference with |
Oh duh! Hope the TS fix helps. |
@johnrom It worked: thanks! |
How about this signature? setValues(values: Values | (values: Values) => Values) Basicaly, you pass setter funcion and you decide whether to merge or replace values. No breaking change! Cc @jaredpalmer |
@Andreyco agreed this is a wart. |
Oh I’m sorry. Why do we need this? We already have setValues? |
@jaredpalmer As far as I understand it, |
I like the Edit: Also with @Andreyco 's method, no longer need to update typescript as all the object destructuring will happen in your project, and you can update typescript yourself. |
I am against introducing new API for this. To explain my previous comment with code... // Now, Formik's setValues replaces values altogether. Merging is "hard", you need to pass all values
formik.setValues({ anotherField: 'value' }) // -> bad, original values are lost
formik.setValues({ ...allValues, anotherField: 'value' }) // -> good, but... passing values is required
// With setter callback, values could be passed by Formik
formik.setValues(values => ({ ...values, anotherField: 'value' })) // -> good, same as React's setState, values are passed. You decide whether to merge or replace No new API, backward compatible. The other thing is I cannot imagine situation when you would want to replace original fields with other ones (can we actually type this?!), thus merge would be what I expect, default behaviour. But this would be breaking change. |
@johnrom @Andreyco @jaredpalmer Thanks to you all for your feedback – I've just pushed a commit which implements your suggestion. Could you take a look and let me know what you think? |
With the last revisions, I'm liking this API. You should merge Finally, it might be useful to make this callback async friendly in the future, similar to what was proposed in #909. But the process of using Promises for callbacks like this hasn't been established in v1 that I know of. |
@johnrom Thanks – I might leave the async-friendliness until later then, if the process hasn't been established yet. For the |
Rebase, please |
ecb1e27
to
e0b2458
Compare
@Andreyco I've just rebased |
@samstarling would you be so kind and go thru change requests? TY! |
@Andreyco Absolutely - thanks for all the reviewing, I’ll address the feedback tomorrow! |
Hey everyone: the only outstanding change now is the documentation, but please let me know if I missed anything. I asked a question above about how the types are documented – once I know how to proceed, I'll update the docs and we should be good to go. |
This PR should be against v2. |
@jaredpalmer Ah, I hadn't realised that, sorry – how can I fix that? |
e81da44
to
d09cd64
Compare
@jaredpalmer I just rebased from master and fixed the conflicts, anything else needed? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiny nitpick; overall I think the code looks good (I haven't tested)
|
||
Set `touched` imperatively. | ||
|
||
#### `setValues: (fields: { [field: string]: any }) => void` | ||
#### `setValues: (valuesOrValuesFactory: ValuesOrValuesFactory<Values>) => void` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#### `setValues: (valuesOrValuesFactory: ValuesOrValuesFactory<Values>) => void` | |
#### `setValues: (valuesOrValuesUpdater: Values | (v: Values) => void) => void` |
I think this is clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: We could just use React.SetStateAction<S>
I can't push up my changes? |
@samstarling can you give me the ability to edit this PR? https://help.github.com/en/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork |
Hi @jaredpalmer – sorry for the delay, I was away on holiday. You should be able to push now. |
Hi everyone, is there any progress on this PR? I think the ability to use updater functions as the I'm interested in working on this PR (or another if preferred) to provide such functionality. |
This PR looks like it was really close, any chance of it being picked up again, or would it be better to start a new one? |
I had assumed this was closed because the changes were integrated into another branch. If that's not the case, we should reopen. |
I can't find these changes on any other branch. It seems to have been closed shortly after being labelled stale. I assume the PR needs to be rebased against a current branch (master or canary?) and, reading through the comments, I only saw some minor type fixes being suggested by @jaredpalmer; otherwise the PR could be good to go. Is there anyone with edit permissions willing to take it over the finish line? @samstarling or @PatoBeltran maybe? I could also do it, if needed Thanks for all your work, I think this PR will be a good addition to formik! |
I would also really like this, this would be so much better for us. Avoid stale values and prevent the need for passing around the values object when we only need to update a few fields. We could really simplify our code with this change. What do we need to do? I can help? |
Wishing to see this PR get merged, it is neccessary to prevent stale values. At the moment I am using continuous setFieldValue calls to update multiple values, the enhanced API do help make the code more readable. |
Hey @jaredpalmer I updated the branch for this PR with latest master and refactored to use |
@jaredpalmer should I try to open a new PR to get traction on this functional updates with setValues feature or can we just reopen this one since the comments have already been addressed? |
This is a continuation of #1513 which was closed for inactivity. This PR has addressed all the comments and reused React.SetStateAction types. With this, formik consumers could pass a value or a function to setValues just like you can with React's setState dispatchers.
This is a continuation of jaredpalmer/formik#1513 which was closed for inactivity. This PR has addressed all the comments and reused React.SetStateAction types. With this, formik consumers could pass a value or a function to setValues just like you can with React's setState dispatchers.
This is a continuation of jaredpalmer/formik#1513 which was closed for inactivity. This PR has addressed all the comments and reused React.SetStateAction types. With this, formik consumers could pass a value or a function to setValues just like you can with React's setState dispatchers.
This is a continuation of jaredpalmer/formik#1513 which was closed for inactivity. This PR has addressed all the comments and reused React.SetStateAction types. With this, formik consumers could pass a value or a function to setValues just like you can with React's setState dispatchers.
This is a continuation of jaredpalmer/formik#1513 which was closed for inactivity. This PR has addressed all the comments and reused React.SetStateAction types. With this, formik consumers could pass a value or a function to setValues just like you can with React's setState dispatchers.
Formik is great, but there are one or two parts where the methods could be a little more TypeScript friendly, and in this PR I'm introducing a new method which I hope takes a step in this direction.
For context: I have a form where a user can enter an ID into a field to perform a lookup. Data is fetched over HTTP, and I use data from the response to populate other form fields by using multiple
setFieldValue
calls. The signature ofsetFieldValue
isn't type-safe – thevalue
isany
, and thefield
may not necessarily exist. (There was some discussion of this in #1388)This PR introduces a
setFieldValues
method that takes aPartial<Values>
and merges that with the existing form values. I would find this really useful.Things that I'm not too sure about, and that I'd welcome feedback on:
people.0.first_name
)?setFieldValues
? It's basically likesetValues
, but it allows subsets and merges rather than overwriting.