refinedev / refine

A React Framework for building internal tools, admin panels, dashboards & B2B apps with unmatched flexibility.
https://refine.dev
MIT License
26.88k stars 2.06k forks source link

[FEAT] register("myFormDataField") useForm<MyFormData>() #6111

Closed jozef-slezak closed 1 month ago

jozef-slezak commented 1 month ago

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

Typescript type checks for refine useForm() similar to what React Hook Form has

Describe alternatives you've considered

No response

Additional context

React Hook Forms support typescript checking even for register("fieldName") https://codesandbox.io/s/react-hook-form-typescript-qwk7b?file=/src/index.tsx

Describe the thing to improve

Please inspire by React Hook Form Types

alicanerdurmaz commented 1 month ago

Hello @jozef-slezak, can you give specific examples, please? Which part of useForm is missing type support?

For example, if a field name is not specified, register, getFieldState and, resetField throw type errors.

example code is here:

import { type HttpError } from "@refinedev/core";

export interface IPost {
  id: number;
  title: string;
  content: string;
  status: "published" | "draft" | "rejected";
  category: { id: number };
}

const form = useForm<IPost, HttpError, IPost>();

Also, we want to implement type support for resource field. The issue is here.

aliemir commented 1 month ago

Hey @jozef-slezak. As @alicanerdurmaz explained in the comment above we already have the type support for methods from useForm.

I think there's a misunderstanding here about the generics; while react-hook-form's useForm uses the first generic for the form values, we use the third generic as the form values. @refinedev/react-hook-form's useForm will fetch the data first and try to populate the fields. This is why we have the first two generics TData and Terror.

If you try to pass the third generic for your form values, type support for the fields will be provided.

Closing this for now. If the issue continues, please do not hesitate to reopen with more info about the issue and a minimal repro 🙏