Skip to content

Commit d17fc2c

Browse files
committed
feat(useForm): improve type parameter specification to react-hook-form
1 parent 0d6d062 commit d17fc2c

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

packages/core/src/hooks/form/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export type {
5959
* @typeParam TResponseError - Custom error object that extends {@link https://refine.dev/docs/core/interface-references/#httperror `HttpError`}. Defaults to `TError`
6060
*
6161
*/
62+
63+
export {
64+
CreateFormVariables,
65+
ExtractFormVariables,
66+
ExtractSubmissionVariables,
67+
} from "./types";
6268
export const useForm = <
6369
TQueryFnData extends BaseRecord = BaseRecord,
6470
TError extends HttpError = HttpError,

packages/core/src/hooks/form/types.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import type { LiveModeProps } from "../../contexts/live/types";
2929
import type { SuccessErrorNotification } from "../../contexts/notification/types";
3030
import type { Action } from "../../contexts/router/types";
3131

32+
const FormVariablesSymbol = Symbol("FormVariables");
33+
const SubmissionVariablesSymbol = Symbol("SubmissionVariables");
34+
3235
export type FormAction = Extract<Action, "create" | "edit" | "clone">;
3336

3437
export type RedirectAction =
@@ -50,6 +53,22 @@ export type AutoSaveProps<TVariables> = {
5053
};
5154
};
5255

56+
export type CreateFormVariables<TFormVariables, TSubmissionVariables> = {
57+
[FormVariablesSymbol]: TFormVariables;
58+
[SubmissionVariablesSymbol]: TSubmissionVariables;
59+
};
60+
61+
export type ExtractFormVariables<T> = T extends {
62+
[FormVariablesSymbol]: infer F;
63+
}
64+
? F
65+
: T;
66+
export type ExtractSubmissionVariables<T> = T extends {
67+
[SubmissionVariablesSymbol]: infer S;
68+
}
69+
? S
70+
: T;
71+
5372
export type AutoSaveReturnType<
5473
TData extends BaseRecord = BaseRecord,
5574
TError extends HttpError = HttpError,

packages/react-hook-form/src/useForm/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import {
2020
useTranslate,
2121
useRefineContext,
2222
flattenObjectKeys,
23+
CreateFormVariables,
24+
ExtractFormVariables,
25+
ExtractSubmissionVariables,
2326
} from "@refinedev/core";
2427

2528
export type UseFormReturnType<
@@ -82,7 +85,7 @@ export type UseFormProps<
8285
export const useForm = <
8386
TQueryFnData extends BaseRecord = BaseRecord,
8487
TError extends HttpError = HttpError,
85-
TVariables extends FieldValues = FieldValues,
88+
TVariables extends FieldValues | CreateFormVariables<any, any> = FieldValues,
8689
TContext extends object = {},
8790
TData extends BaseRecord = TQueryFnData,
8891
TResponse extends BaseRecord = TData,
@@ -195,6 +198,9 @@ export const useForm = <
195198

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

201+
type FormVariablesType = ExtractFormVariables<TVariables>; // Added
202+
type SubmissionVariablesType = ExtractSubmissionVariables<TVariables>;
203+
198204
useEffect(() => {
199205
const data = query?.data?.data;
200206
if (!data) return;
@@ -248,17 +254,24 @@ export const useForm = <
248254
return changeValues;
249255
};
250256

251-
const handleSubmit: UseFormHandleSubmit<TVariables> =
257+
const handleSubmit: UseFormHandleSubmit<FormVariablesType> =
252258
(onValid, onInvalid) => async (e) => {
253259
setWarnWhen(false);
254-
return handleSubmitReactHookForm(onValid, onInvalid)(e);
260+
261+
const onValidWrapper = (variables: TVariables) => {
262+
const formVariables =
263+
variables as unknown as ExtractFormVariables<TVariables>;
264+
return onValid(formVariables);
265+
};
266+
267+
return handleSubmitReactHookForm(onValidWrapper, onInvalid)(e);
255268
};
256269

257270
const saveButtonProps = {
258271
disabled: formLoading,
259272
onClick: (e: React.BaseSyntheticEvent) => {
260273
handleSubmit(
261-
(v) => onFinish(v).catch(() => {}),
274+
(v) => onFinish(v as SubmissionVariablesType).catch(() => {}),
262275
() => false,
263276
)(e);
264277
},

0 commit comments

Comments
 (0)