-
The |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 9 replies
-
That's a really interesting idea. I think we should definitely consider this at v7. |
Beta Was this translation helpful? Give feedback.
-
One solution is to create a provider that includes the type AllFormMethods<TFieldValues extends FieldValues = FieldValues> = UseFormMethods<TFieldValues> &
UseFieldArrayMethods;
const FieldArrayFormContext = React.createContext<AllFormMethods | null>(null);
FieldArrayFormContext.displayName = "RHFArrayContext";
export const useFieldArrayFormContext = <TFieldValues extends FieldValues>(): AllFormMethods<
TFieldValues
> => {
return useContext(FieldArrayFormContext) as AllFormMethods<TFieldValues>;
};
export declare type FieldArrayFormProviderProps<TFieldValues extends FieldValues = FieldValues> = {
children: React.ReactNode;
} & AllFormMethods<TFieldValues>;
export const FieldArrayFormProvider = <TFieldValues extends FieldValues>({
children,
...props
}: FieldArrayFormProviderProps<TFieldValues>) => {
return (
<FieldArrayFormContext.Provider value={{ ...props } as AllFormMethods}>
{children}
</FieldArrayFormContext.Provider>
);
}; usage: const formMethods = useForm({
defaultValues: { "test": {name: "myName" } },
});
const { control, handleSubmit } = formMethods;
const fieldArrayMethods = useFieldArray({
control,
name: "test",
});
return (
<FieldArrayFormProvider {...formMethods} {...fieldArrayMethods}>
<form
onSubmit={handleSubmit((data) => {
// TODO: handle this.
console.log(data);
})}
>
...
</form>
</FieldArrayFormProvider>
);
}; |
Beta Was this translation helpful? Give feedback.
-
I see that this was not included in v7. Is it still being considered as a feature or should we accept the workaround as a final solution to this problem? |
Beta Was this translation helpful? Give feedback.
-
This answer helped me a lot! In RHF ChangeLog ## [v7.0.0-alpha.2] - 2021-02-04 there are name changes:
Not greate for TS, but this workin for me now:
It’s a pity that your decision was not accepted by the RHF @bluebill1049 |
Beta Was this translation helpful? Give feedback.
-
I struggled real hard with this one as well and thank you @Prince-Gizard for your inspiration. I devised a more TypeScript friendly solution inspired in the code you posted. My approach still has the disadvantage of only being able to pass 1 single array field, but it's good enough for me for now. Some kind of distinction according the field name would probably be necessary. Disclaimer: In my scenario (since I use ShadeCN Form Provider) I thought it to be cleaner in a first instance to not touch their provider, but have my own const createUseFieldArrayContext = <
TFieldValues extends FieldValues = FieldValues,
FieldArrayName extends
FieldArrayPath<TFieldValues> = FieldArrayPath<TFieldValues>,
TKeyName extends string = 'id'
>() => {
type FormFieldContextValue = UseFieldArrayReturn<
TFieldValues,
FieldArrayName,
TKeyName
>
const FieldArrayFormContext = React.createContext<FormFieldContextValue>(
{} as FormFieldContextValue
)
const useFieldArrayFormContext = (): FormFieldContextValue => {
return React.useContext(FieldArrayFormContext)
}
type FieldArrayFormProviderProps = {
children: React.ReactNode
} & FormFieldContextValue
const FieldArrayFormProvider = ({
children,
...props
}: FieldArrayFormProviderProps) => {
return (
<FieldArrayFormContext.Provider value={{ ...props }}>
{children}
</FieldArrayFormContext.Provider>
)
}
return {
FieldArrayFormContext,
useFieldArrayFormContext,
FieldArrayFormProvider
}
} And then when you need to use it: const {
FieldArrayFormProvider: FooFieldArrayProvider,
useFieldArrayFormContext: useFooFieldArrayContext
} = createUseFieldArrayContext<FooFormValues>() Hope this helps someone down the line 🙏 |
Beta Was this translation helpful? Give feedback.
-
It will be cool to have separate decoupled logic for field array |
Beta Was this translation helpful? Give feedback.
One solution is to create a provider that includes the
useForm
methods anduseFieldArray
methods. This adapts the currentFormContext
code and addsUseFieldArrayMethods
to it.