Skip to content
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

feat(useForm): improve type parameter specification to useForm #6568

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/nine-chefs-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@refinedev/react-hook-form": minor
"@refinedev/core": minor
---

feat: improve type parameter specification to useForm
This PR addresses the type parameter issue in the useForm hook. It ensures that the correct types are applied to the form data, resolving type conflicts when handling form submission and validation.

Resolves [#6137] (https://github.com/refinedev/refine/issues/6137)
6 changes: 6 additions & 0 deletions packages/core/src/hooks/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export type {
* @typeParam TResponseError - Custom error object that extends {@link https://refine.dev/docs/core/interface-references/#httperror `HttpError`}. Defaults to `TError`
*
*/

export {
CreateFormVariables,
ExtractFormVariables,
ExtractSubmissionVariables,
} from "./types";
export const useForm = <
TQueryFnData extends BaseRecord = BaseRecord,
TError extends HttpError = HttpError,
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/hooks/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import type { LiveModeProps } from "../../contexts/live/types";
import type { SuccessErrorNotification } from "../../contexts/notification/types";
import type { Action } from "../../contexts/router/types";

const FormVariablesSymbol = Symbol("FormVariables");
const SubmissionVariablesSymbol = Symbol("SubmissionVariables");
Comment on lines +32 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we use Symbol for FormVariables and SubmissionVariables to ensure uniqueness and avoid conflicting with the basic TVariables shape. as Ali mentioned here .


export type FormAction = Extract<Action, "create" | "edit" | "clone">;

export type RedirectAction =
Expand All @@ -50,6 +53,22 @@ export type AutoSaveProps<TVariables> = {
};
};

export type CreateFormVariables<TFormVariables, TSubmissionVariables> = {
[FormVariablesSymbol]: TFormVariables;
[SubmissionVariablesSymbol]: TSubmissionVariables;
};

export type ExtractFormVariables<T> = T extends {
[FormVariablesSymbol]: infer F;
}
? F
: T;
export type ExtractSubmissionVariables<T> = T extends {
[SubmissionVariablesSymbol]: infer S;
}
? S
: T;

export type AutoSaveReturnType<
TData extends BaseRecord = BaseRecord,
TError extends HttpError = HttpError,
Expand Down
21 changes: 17 additions & 4 deletions packages/react-hook-form/src/useForm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
type UseFormHandleSubmit,
type Path,
} from "react-hook-form";
import {
type BaseRecord,
type HttpError,
useForm as useFormCore,
useWarnAboutChange,
type UseFormProps as UseFormCoreProps,
type UseFormReturnType as UseFormReturnTypeCore,
useTranslate,
useRefineContext,
flattenObjectKeys,
CreateFormVariables,
ExtractFormVariables,
ExtractSubmissionVariables,
} from "@refinedev/core";

Check failure on line 26 in packages/react-hook-form/src/useForm/index.ts

View workflow job for this annotation

GitHub Actions / Lint

Some named imports are only used as types.

export type UseFormReturnType<
TQueryFnData extends BaseRecord = BaseRecord,
Expand Down Expand Up @@ -82,7 +85,7 @@
export const useForm = <
TQueryFnData extends BaseRecord = BaseRecord,
TError extends HttpError = HttpError,
TVariables extends FieldValues = FieldValues,
TVariables extends FieldValues | CreateFormVariables<any, any> = FieldValues,
TContext extends object = {},
TData extends BaseRecord = TQueryFnData,
TResponse extends BaseRecord = TData,
Expand Down Expand Up @@ -195,6 +198,9 @@

const { query, onFinish, formLoading, onFinishAutoSave } = useFormCoreResult;

type FormVariablesType = ExtractFormVariables<TVariables>; // Added
type SubmissionVariablesType = ExtractSubmissionVariables<TVariables>;

useEffect(() => {
const data = query?.data?.data;
if (!data) return;
Expand Down Expand Up @@ -248,17 +254,24 @@
return changeValues;
};

const handleSubmit: UseFormHandleSubmit<TVariables> =
const handleSubmit: UseFormHandleSubmit<FormVariablesType> =
(onValid, onInvalid) => async (e) => {
setWarnWhen(false);
return handleSubmitReactHookForm(onValid, onInvalid)(e);

const onValidWrapper = (variables: TVariables) => {
const formVariables =
variables as unknown as ExtractFormVariables<TVariables>;
return onValid(formVariables);
};

return handleSubmitReactHookForm(onValidWrapper, onInvalid)(e);
};

const saveButtonProps = {
disabled: formLoading,
onClick: (e: React.BaseSyntheticEvent) => {
handleSubmit(
(v) => onFinish(v).catch(() => {}),
(v) => onFinish(v as SubmissionVariablesType).catch(() => {}),
() => false,
)(e);
},
Expand Down
Loading