-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form.tsx
182 lines (158 loc) · 3.87 KB
/
Form.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {
type FieldSchemaBase,
type GetFieldSchema,
parse,
serialize,
setFieldErrors,
validateBeforeSubmit,
} from "@vtaits/form-schema";
import isPromise from "is-promise";
import { useCallback, useMemo } from "react";
import type { ReactElement } from "react";
import { useAsync } from "react-async-hook";
import { Form as FinalForm } from "react-final-form";
import type { FormProps as FinalFormProps } from "react-final-form";
import { FormSchemaContext } from "./FormSchemaContext";
import { IS_VALUES_READY_NAME } from "./constants";
import { makeSetError } from "./makeSetError";
import type { FormProps, FormSchemaContextType, MapErrors } from "./types";
export const defaultGetFieldSchema: GetFieldSchema<any> = (fieldSchema) =>
fieldSchema;
export const defaultMapErrors: MapErrors = (errors) => errors;
export function Form<
FieldSchema extends FieldSchemaBase,
Values extends Record<string, any>,
RawValues extends Record<string, any>,
SerializedValues extends Record<string, any>,
Errors extends Record<string, any>,
Payload,
>(
props: FormProps<
FieldSchema,
Values,
RawValues,
SerializedValues,
Errors,
Payload
>,
): ReactElement {
const {
names,
getFieldSchema = defaultGetFieldSchema as GetFieldSchema<FieldSchema>,
getFieldType,
initialValues: initialValuesProp = null,
initialValuesPlaceholder = undefined,
onSubmit: onSubmitProp,
mapErrors = defaultMapErrors as MapErrors<Values, SerializedValues, Errors>,
children,
...rest
} = props;
const initialValuesResult = useMemo(() => {
const rawInitialValues = initialValuesProp || {};
return parse({
values: rawInitialValues as RawValues,
names,
getFieldSchema,
getFieldType,
parents: [
{
values: rawInitialValues as RawValues,
},
],
});
}, [initialValuesProp, names, getFieldSchema, getFieldType]);
const { result: initialValues } = useAsync(
() => Promise.resolve(initialValuesResult),
[initialValuesResult],
);
const onSubmit = useCallback<FinalFormProps<Values, Values>["onSubmit"]>(
async (values) => {
const validationErrors = {} as Errors;
validateBeforeSubmit({
setError: makeSetError(validationErrors),
values,
names,
getFieldSchema,
getFieldType,
parents: [
{
values,
},
],
});
if (Object.keys(validationErrors).length > 0) {
return validationErrors;
}
const valuesForSubmit = serialize({
values,
names,
getFieldSchema,
getFieldType,
parents: [
{
values,
},
],
});
const rawErrors = await onSubmitProp(valuesForSubmit, values);
if (!rawErrors) {
return null;
}
const preparedErrors = mapErrors(
rawErrors as Errors,
valuesForSubmit,
values,
);
setFieldErrors({
setError: makeSetError(preparedErrors),
errors: preparedErrors,
names,
getFieldSchema,
getFieldType,
values: valuesForSubmit,
rawValues: values,
parents: [
{
values,
},
],
});
return preparedErrors;
},
[onSubmitProp, names, getFieldSchema, getFieldType, mapErrors],
);
const providedInitialValues = useMemo(() => {
const initialValuesClean = isPromise(initialValuesResult)
? initialValues
: initialValuesResult;
if (initialValuesClean) {
return {
...initialValuesClean,
[IS_VALUES_READY_NAME]: true,
};
}
return {
...initialValuesPlaceholder,
[IS_VALUES_READY_NAME]: false,
};
}, [initialValuesResult, initialValues, initialValuesPlaceholder]);
const providerValue = useMemo(
() =>
({
getFieldSchema,
getFieldType,
}) as FormSchemaContextType<unknown>,
[getFieldSchema, getFieldType],
);
return (
<FormSchemaContext.Provider value={providerValue}>
<FinalForm
{...rest}
onSubmit={onSubmit}
initialValues={providedInitialValues as unknown as Values}
>
{children}
</FinalForm>
</FormSchemaContext.Provider>
);
}