-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathuseForm.tsx
161 lines (148 loc) · 4.58 KB
/
useForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {
useForm as useFormReact,
FieldValues,
FormOpts,
FormApi,
} from "@rvf/react";
import { useRemixSubmit } from "./remix-submission-handling";
import {
FetcherWithComponents,
FormEncType,
SubmitOptions,
} from "react-router";
import { toPathObject } from "@rvf/set-get";
import {
GenericObject,
SubmitterOptions,
FORM_ID_FIELD_NAME,
StateSubmitHandler,
DomSubmitHandler,
preprocessFormData,
} from "@rvf/core";
import { useServerValidationErrors } from "./auto-server-hooks";
// Trying to manipulate the existing types for this breaks the type inference
// for the handleSubmit argument. So we'll just spell it out again
type FormSubmitOpts<FormOutputData, ResponseData> =
| {
submitSource: "state";
handleSubmit?: StateSubmitHandler<FormOutputData, ResponseData>;
}
| {
submitSource?: "dom";
handleSubmit?: DomSubmitHandler<FormOutputData, ResponseData>;
};
export type RemixFormOpts<
FormInputData extends FieldValues,
FormOutputData,
> = Omit<
FormOpts<FormInputData, FormOutputData, void>,
| keyof SubmitOptions
| "serverValidationErrors"
| "handleSubmit"
| "submitSource"
> &
Pick<
SubmitOptions,
| "method"
| "action"
| "encType"
| "fetcherKey"
| "replace"
| "state"
| "navigate"
| "preventScrollReset"
| "relative"
> &
FormSubmitOpts<FormOutputData, void> & {
fetcher?: FetcherWithComponents<unknown>;
};
/**
* Create and use an `FormScope`.
*/
export function useForm<FormInputData extends FieldValues, FormOutputData>(
rvfOpts: RemixFormOpts<FormInputData, FormOutputData>,
): FormApi<FormInputData> {
let rvf: FormApi<FormInputData>;
const { fetcher, submitSource = "dom" } = rvfOpts;
const serverStuff = useServerValidationErrors({
formId: rvfOpts.id,
defaultValues: rvfOpts.defaultValues as any,
fetcher: rvfOpts.fetcher,
});
const submitWithRemix = useRemixSubmit(
fetcher,
serverStuff.serverValidationErrors,
);
const handleSubmission = (
data: FormOutputData,
formDataOrOptions?: FormData | SubmitterOptions,
maybeOptions?: SubmitterOptions,
) => {
const { formData, submitterOpts } =
submitSource === "state"
? {
formData: undefined,
submitterOpts: formDataOrOptions as SubmitterOptions,
}
: {
formData: formDataOrOptions as FormData,
submitterOpts: maybeOptions,
};
const handleSubmit = rvfOpts?.handleSubmit as
| ((data: FormOutputData, formData: FormData) => Promise<void>)
| undefined;
// when the user provides a handleSubmit, we should use that instead
if (handleSubmit) {
return handleSubmit(data, formData as never);
}
const getData = () => {
if (submitSource === "dom") {
if (!formData)
throw new Error("Missing form data. This is likely a bug in RVF");
if (rvfOpts.id) formData.set(FORM_ID_FIELD_NAME, rvfOpts.id);
if (rvfOpts.encType === "application/json") {
return preprocessFormData(formData);
}
return formData;
}
if (rvfOpts.encType === "application/json") return data as GenericObject;
const pathObj = toPathObject(data as GenericObject);
if (rvfOpts.id) pathObj[FORM_ID_FIELD_NAME] = rvfOpts.id;
return pathObj;
};
const getFormAction = () => {
if (!submitterOpts?.formAction) return rvfOpts.action;
const url = new URL(submitterOpts.formAction);
// https://github.com/remix-run/remix/issues/4423#issuecomment-1293015814
return url.pathname + url.search;
};
return submitWithRemix(getData(), {
fetcherKey: rvfOpts.fetcherKey,
state: rvfOpts.state,
navigate: rvfOpts.navigate,
replace: rvfOpts.replace,
preventScrollReset: rvfOpts.preventScrollReset,
relative: rvfOpts.relative,
action: getFormAction(),
// Technically not type safe, but it isn't really possible to make it so.
// Can't really validate because remix doesn't provide a validator for it
// and I don't want to hardcode the types.
method:
(submitterOpts?.formMethod as typeof rvfOpts.method) ?? rvfOpts.method,
encType: (submitterOpts?.formEnctype as FormEncType) ?? rvfOpts.encType,
});
};
rvf = useFormReact<FormInputData, FormOutputData, void>({
...rvfOpts,
...serverStuff,
otherFormProps: {
method: rvfOpts.method,
encType: rvfOpts.encType,
...rvfOpts.otherFormProps,
},
submitSource,
handleSubmit:
(rvfOpts.handleSubmit as never) ?? (handleSubmission as never),
});
return rvf;
}