Should "useFieldArray append" types behave the same as the defaultValues type? #10211
Replies: 17 comments 7 replies
-
react-hook-form allows to input partial default values because there is always a reasonable case when providing full default values object is not needed, therefore on all fields passed to
If you want to make it consistent, I would recommend to use empty string as default value for |
Beta Was this translation helpful? Give feedback.
-
Having same issue but mine is with dates, how can i create a empty Date object? With default value I used undefined but append doesn't accept it. |
Beta Was this translation helpful? Give feedback.
-
Having same issue here. I want to append form by same type of defaultValues. (empty state) |
Beta Was this translation helpful? Give feedback.
-
I don't understand the design choice of having to know exactly what should be filled in when adding another form component... |
Beta Was this translation helpful? Give feedback.
-
i'm having the same issues with dates and numbers. i am trying to work around it by tweaking some type definitions and Yup schema, but it isn't pretty and breaks our existing form patterns. |
Beta Was this translation helpful? Give feedback.
-
Same issue here. Allowing for undefined values in append would be extremely helpful. |
Beta Was this translation helpful? Give feedback.
-
Same, it doesn't seem right that |
Beta Was this translation helpful? Give feedback.
-
seems this hasn't been resolved? same issue with @thiago-silva-ac, appending both date and numbers. Having a number input and when user appends we default it to 0 doesn't look great, same with dates |
Beta Was this translation helpful? Give feedback.
-
Hey @bluebill1049, as you're the official maintainer of react-hook-form I wanted to tag you in this discussion so you can give your thoughts. Quite a few people are having the same issue that I brought up initially, is there any major drawbacks on making the |
Beta Was this translation helpful? Give feedback.
-
+1 on this one |
Beta Was this translation helpful? Give feedback.
-
+1, it makes no sense to append fields to a form that are meant to be filled by the user (e.g. a user defined number of entries to fill) but expect to have the data already to include in the append call |
Beta Was this translation helpful? Give feedback.
-
This is really a questionable design choice. This is how I worked around it in typescript though. It's ugly, but it works append({
name: '', // string, give it empty string
type: '' as SomeEnum, // enum, use empty string and type cast to enum
amount: '' as unknown as number, // number, lie to TS basically,
date: '' as unknown as Date, // date object, just keep lying to TS... :(
}) |
Beta Was this translation helpful? Give feedback.
-
Here's a patch compatible with version diff --git a/node_modules/react-hook-form/dist/types/fieldArray.d.ts b/node_modules/react-hook-form/dist/types/fieldArray.d.ts
index 5fa9572..db63797 100644
--- a/node_modules/react-hook-form/dist/types/fieldArray.d.ts
+++ b/node_modules/react-hook-form/dist/types/fieldArray.d.ts
@@ -2,6 +2,7 @@ import { FieldValues } from './fields';
import { Control } from './form';
import { FieldArrayPath, FieldArrayPathValue } from './path';
import { RegisterOptions, Validate } from './validator';
+import { DeepPartial } from './utils';
export type UseFieldArrayProps<TFieldValues extends FieldValues = FieldValues, TFieldArrayName extends FieldArrayPath<TFieldValues> = FieldArrayPath<TFieldValues>, TKeyName extends string = 'id'> = {
name: TFieldArrayName;
keyName?: TKeyName;
@@ -97,7 +98,7 @@ export type UseFieldArrayPrepend<TFieldValues extends FieldValues, TFieldArrayNa
* </button>
* ```
*/
-export type UseFieldArrayAppend<TFieldValues extends FieldValues, TFieldArrayName extends FieldArrayPath<TFieldValues> = FieldArrayPath<TFieldValues>> = (value: FieldArray<TFieldValues, TFieldArrayName> | FieldArray<TFieldValues, TFieldArrayName>[], options?: FieldArrayMethodProps) => void;
+export type UseFieldArrayAppend<TFieldValues extends FieldValues, TFieldArrayName extends FieldArrayPath<TFieldValues> = FieldArrayPath<TFieldValues>> = (value: FieldArray<DeepPartial<TFieldValues>, TFieldArrayName> | FieldArray<TFieldValues, TFieldArrayName>[], options?: FieldArrayMethodProps) => void;
/**
* Remove field/fields at particular position.
* |
Beta Was this translation helpful? Give feedback.
-
I had the same problem. I resorted to adding .nullable() in zod to the field and use null in append() for the field. This caused the field not to be required anymore, so I added a superRefine to check for the field and make it required. Super hacky... would be solved if append would work the same as defaultValues and can accept Partial values. |
Beta Was this translation helpful? Give feedback.
-
Did someone created an issue for this? |
Beta Was this translation helpful? Give feedback.
-
Yep, this issue seems to persist. I’m currently trying to display placeholder text in a select input, but it’s basically impossible because I need the value to be defined, and apparently, the only way to display the placeholder is setting the value as null. So, @bluebill1049 , can you help us here? Thanks! |
Beta Was this translation helpful? Give feedback.
-
I'm having the same problem, will there soon be a fix for it? @iffa will this patch be deployed? |
Beta Was this translation helpful? Give feedback.
-
When using an array of objects with react-hook-form, the default values accepts that the keys from the object can be undefined, even though that the schema says that these are mandatory. And that's great since I can initialize my form with an empty state:
However, when using
append
from theuseFieldArray
method, I'm forced to set a value for the keys of the object I'm appending into the form. For me that's a problem because I want to append a value that's completely empty, the same way as the default value, so the user can add the initial data on itSo my question is, should the append behave the same as the default values? Allowing us to add an empty value for the initial appended data?
Here's a code sandbox with an example:
https://codesandbox.io/s/youthful-faraday-feem73?file=/src/index.tsx
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions