Get type inference without using defaultValues
Hello, I am trying not to use
defaultValues in my form and resetting the form when the deliveryNote prop changes because I want my form to be synchronized with my data sources, both the deliveryNote and nextAvailableDeliveryNoteIdData are updated in realtime.10 Replies
ambitious-aquaOPβ’4mo ago
The issue is that now, the
onSubmit handler is not able to infer the value data type with my form fields:
Previously, I was using defaultValues like this:
In the later case, the onSubmit value has the type correctly inferredfair-roseβ’4mo ago
this seems overcomplicated. I think a
key could be the better approach for thisfair-roseβ’4mo ago
LeCarbonator
StackBlitz
Resetting with key - StackBlitz
Next generation frontend tooling. It's fast!
fair-roseβ’4mo ago
see this example
you're telling React that the component has a dependency on the value, so it will force a reload if it changes
essentially resetting the form back to the default values it has - in this case defaultValues that are derived from the passed prop
ambitious-aquaOPβ’4mo ago
Oh looks promising, thanks! I guess that using key is as efficient/unefficient as any other approach because react will rerender the whole form anyways, right?
fair-roseβ’4mo ago
I'm not sure how performance could be hit by this, actually. But considering the workaround you listed above that's needed to get it working without remounting, a
key with a comment explaining its purpose is way more readableambitious-aquaOPβ’4mo ago
Perfect, thankss β€οΈ
fair-roseβ’4mo ago
cool use of the read-only bit by the way. At my workplace, weβre still trying to figure out the best way to manage read-only permissions for forms and the like. This could be helpful
ambitious-aquaOPβ’4mo ago
I am just experimenting with that, the main issue now is that I have a field which should be readonly and "display only", it should not be submitted with the other form values. I have to manually delete it (I am trying to use
delete keyword or object unpacking, both suck) from the onSubmit value π΅βπ« which is cumbersome.
Also the validation schemas are a issue, I have had to put the field with a zod's .optional() in the update schema but it should not be part of the schema...
Hmmm is there a way to make that the form validators validate only some fields?
I otherwise I guess I could switch to validating each field instead........ π΅βπ«
oh wait I think I got it
The goal:
If form mode is readonly: value should only be shown
If form mode is edit: value should be shown, but it should be readonly. The field should not be submitted
If form mode is create: value should be editable by the user and submitted
Component receives a prop with the field value for the readonly and edit modes:
In the form, we define it in defaultValues only in the FormMode.Create case, using a default value. Type casting is required in order to avoid having null as the only inferred type
The form field should use the form state.values variable in the create FormMode.Create case and the prop variable value in the other cases
The form validators work perfectly!
My zod schemas:
What do you think @Luca | LeCarbonator π₯°fair-roseβ’4mo ago
well, as far as the avoiding submission goes, you can check for the mode in
onSubmit and not perform it in that case
as for the update vs create, this will do for small cases. In case you have a big form where there's lots of differences between create and update (for example, a version controlled form), then field groups can help out with that