remix-validated-form 2.0.1-beta.0 → 3.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +2 -2
- package/.turbo/turbo-test.log +36 -0
- package/README.md +232 -0
- package/browser/ValidatedForm.d.ts +13 -1
- package/browser/ValidatedForm.js +72 -19
- package/browser/index.d.ts +0 -2
- package/browser/index.js +0 -2
- package/browser/internal/submissionCallbacks.d.ts +1 -0
- package/browser/internal/submissionCallbacks.js +13 -0
- package/browser/validation/createValidator.js +12 -1
- package/browser/validation/types.d.ts +3 -0
- package/browser/validation/validation.test.js +5 -0
- package/build/ValidatedForm.d.ts +13 -1
- package/build/ValidatedForm.js +71 -18
- package/build/index.d.ts +0 -2
- package/build/index.js +0 -2
- package/build/internal/submissionCallbacks.d.ts +1 -0
- package/build/internal/submissionCallbacks.js +17 -0
- package/build/validation/createValidator.js +12 -1
- package/build/validation/types.d.ts +3 -0
- package/build/validation/validation.test.js +5 -0
- package/package.json +6 -9
- package/src/ValidatedForm.tsx +96 -18
- package/src/index.ts +0 -2
- package/src/internal/submissionCallbacks.ts +15 -0
- package/src/validation/createValidator.ts +12 -2
- package/src/validation/types.ts +2 -0
- package/jest.config.js +0 -10
- package/src/test-data/testFormData.ts +0 -55
- package/src/validation/validation.test.ts +0 -317
- package/src/validation/withYup.ts +0 -43
- package/src/validation/withZod.ts +0 -51
package/build/ValidatedForm.js
CHANGED
@@ -9,39 +9,87 @@ const react_1 = require("@remix-run/react");
|
|
9
9
|
const react_2 = require("react");
|
10
10
|
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
|
11
11
|
const formContext_1 = require("./internal/formContext");
|
12
|
+
const submissionCallbacks_1 = require("./internal/submissionCallbacks");
|
12
13
|
const util_1 = require("./internal/util");
|
13
|
-
function
|
14
|
+
function useFieldErrorsFromBackend(fetcher, subaction) {
|
15
|
+
var _a, _b;
|
14
16
|
const actionData = (0, react_1.useActionData)();
|
15
|
-
|
16
|
-
|
17
|
-
|
17
|
+
if (fetcher)
|
18
|
+
return (_a = fetcher.data) === null || _a === void 0 ? void 0 : _a.fieldErrors;
|
19
|
+
if (!actionData)
|
20
|
+
return null;
|
21
|
+
if (actionData.fieldErrors) {
|
22
|
+
const submittedData = (_b = actionData.fieldErrors) === null || _b === void 0 ? void 0 : _b._submittedData;
|
23
|
+
const subactionsMatch = subaction
|
24
|
+
? subaction === (submittedData === null || submittedData === void 0 ? void 0 : submittedData.subaction)
|
25
|
+
: !(submittedData === null || submittedData === void 0 ? void 0 : submittedData.subaction);
|
26
|
+
return subactionsMatch ? actionData.fieldErrors : null;
|
27
|
+
}
|
28
|
+
return null;
|
29
|
+
}
|
30
|
+
function useFieldErrors(fieldErrorsFromBackend) {
|
31
|
+
const [fieldErrors, setFieldErrors] = (0, react_2.useState)(fieldErrorsFromBackend !== null && fieldErrorsFromBackend !== void 0 ? fieldErrorsFromBackend : {});
|
18
32
|
(0, react_2.useEffect)(() => {
|
19
|
-
if (
|
20
|
-
setFieldErrors(
|
21
|
-
}, [
|
33
|
+
if (fieldErrorsFromBackend)
|
34
|
+
setFieldErrors(fieldErrorsFromBackend);
|
35
|
+
}, [fieldErrorsFromBackend]);
|
22
36
|
return [fieldErrors, setFieldErrors];
|
23
37
|
}
|
24
|
-
const useIsSubmitting = (action, fetcher) => {
|
38
|
+
const useIsSubmitting = (action, subaction, fetcher) => {
|
25
39
|
const actionForCurrentPage = (0, react_1.useFormAction)();
|
26
40
|
const pendingFormSubmit = (0, react_1.useTransition)().submission;
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
41
|
+
if (fetcher)
|
42
|
+
return fetcher.state === "submitting";
|
43
|
+
if (!pendingFormSubmit)
|
44
|
+
return false;
|
45
|
+
const { formData, action: pendingAction } = pendingFormSubmit;
|
46
|
+
const pendingSubAction = formData.get("subaction");
|
47
|
+
const expectedAction = action !== null && action !== void 0 ? action : actionForCurrentPage;
|
48
|
+
if (subaction)
|
49
|
+
return expectedAction === pendingAction && subaction === pendingSubAction;
|
50
|
+
return expectedAction === pendingAction && !pendingSubAction;
|
31
51
|
};
|
32
52
|
const getDataFromForm = (el) => new FormData(el);
|
53
|
+
/**
|
54
|
+
* The purpose for this logic is to handle validation errors when javascript is disabled.
|
55
|
+
* Normally (without js), when a form is submitted and the action returns the validation errors,
|
56
|
+
* the form will be reset. The errors will be displayed on the correct fields,
|
57
|
+
* but all the values in the form will be gone. This is not good UX.
|
58
|
+
*
|
59
|
+
* To get around this, we return the submitted form data from the server,
|
60
|
+
* and use those to populate the form via `defaultValues`.
|
61
|
+
* This results in a more seamless UX akin to what you would see when js is enabled.
|
62
|
+
*
|
63
|
+
* One potential downside is that resetting the form will reset the form
|
64
|
+
* to the _new_ default values that were returned from the server with the validation errors.
|
65
|
+
* However, this case is less of a problem than the janky UX caused by losing the form values.
|
66
|
+
* It will only ever be a problem if the form includes a `<button type="reset" />`
|
67
|
+
* and only if JS is disabled.
|
68
|
+
*/
|
69
|
+
function useDefaultValues(fieldErrors, defaultValues) {
|
70
|
+
const defaultsFromValidationError = fieldErrors === null || fieldErrors === void 0 ? void 0 : fieldErrors._submittedData;
|
71
|
+
return defaultsFromValidationError !== null && defaultsFromValidationError !== void 0 ? defaultsFromValidationError : defaultValues;
|
72
|
+
}
|
33
73
|
/**
|
34
74
|
* The primary form component of `remix-validated-form`.
|
35
75
|
*/
|
36
|
-
function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }) {
|
76
|
+
function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, ...rest }) {
|
37
77
|
var _a;
|
38
|
-
const
|
39
|
-
const
|
78
|
+
const fieldErrorsFromBackend = useFieldErrorsFromBackend(fetcher, subaction);
|
79
|
+
const [fieldErrors, setFieldErrors] = useFieldErrors(fieldErrorsFromBackend);
|
80
|
+
const isSubmitting = useIsSubmitting(action, subaction, fetcher);
|
81
|
+
const defaultsToUse = useDefaultValues(fieldErrorsFromBackend, defaultValues);
|
40
82
|
const formRef = (0, react_2.useRef)(null);
|
83
|
+
(0, submissionCallbacks_1.useSubmitComplete)(isSubmitting, () => {
|
84
|
+
var _a;
|
85
|
+
if (!fieldErrorsFromBackend && resetAfterSubmit) {
|
86
|
+
(_a = formRef.current) === null || _a === void 0 ? void 0 : _a.reset();
|
87
|
+
}
|
88
|
+
});
|
41
89
|
const contextValue = (0, react_2.useMemo)(() => ({
|
42
90
|
fieldErrors,
|
43
91
|
action,
|
44
|
-
defaultValues,
|
92
|
+
defaultValues: defaultsToUse,
|
45
93
|
isSubmitting: isSubmitting !== null && isSubmitting !== void 0 ? isSubmitting : false,
|
46
94
|
clearError: (fieldName) => {
|
47
95
|
setFieldErrors((prev) => (0, util_1.omit)(prev, fieldName));
|
@@ -59,7 +107,7 @@ function ValidatedForm({ validator, onSubmit, children, fetcher, action, default
|
|
59
107
|
}), [
|
60
108
|
fieldErrors,
|
61
109
|
action,
|
62
|
-
|
110
|
+
defaultsToUse,
|
63
111
|
isSubmitting,
|
64
112
|
setFieldErrors,
|
65
113
|
validator,
|
@@ -74,6 +122,11 @@ function ValidatedForm({ validator, onSubmit, children, fetcher, action, default
|
|
74
122
|
else {
|
75
123
|
onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit(result.data, event);
|
76
124
|
}
|
77
|
-
},
|
125
|
+
}, onReset: (event) => {
|
126
|
+
onReset === null || onReset === void 0 ? void 0 : onReset(event);
|
127
|
+
if (event.defaultPrevented)
|
128
|
+
return;
|
129
|
+
setFieldErrors({});
|
130
|
+
}, children: (0, jsx_runtime_1.jsxs)(formContext_1.FormContext.Provider, { value: contextValue, children: [subaction && ((0, jsx_runtime_1.jsx)("input", { type: "hidden", value: subaction, name: "subaction" }, void 0)), children] }, void 0) }, void 0));
|
78
131
|
}
|
79
132
|
exports.ValidatedForm = ValidatedForm;
|
package/build/index.d.ts
CHANGED
@@ -2,7 +2,5 @@ export * from "./hooks";
|
|
2
2
|
export * from "./server";
|
3
3
|
export * from "./ValidatedForm";
|
4
4
|
export * from "./validation/types";
|
5
|
-
export * from "./validation/withYup";
|
6
|
-
export * from "./validation/withZod";
|
7
5
|
export * from "./validation/createValidator";
|
8
6
|
export type { FormContextValue } from "./internal/formContext";
|
package/build/index.js
CHANGED
@@ -14,6 +14,4 @@ __exportStar(require("./hooks"), exports);
|
|
14
14
|
__exportStar(require("./server"), exports);
|
15
15
|
__exportStar(require("./ValidatedForm"), exports);
|
16
16
|
__exportStar(require("./validation/types"), exports);
|
17
|
-
__exportStar(require("./validation/withYup"), exports);
|
18
|
-
__exportStar(require("./validation/withZod"), exports);
|
19
17
|
__exportStar(require("./validation/createValidator"), exports);
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function useSubmitComplete(isSubmitting: boolean, callback: () => void): void;
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useSubmitComplete = void 0;
|
4
|
+
const react_1 = require("react");
|
5
|
+
function useSubmitComplete(isSubmitting, callback) {
|
6
|
+
const isPending = (0, react_1.useRef)(false);
|
7
|
+
(0, react_1.useEffect)(() => {
|
8
|
+
if (isSubmitting) {
|
9
|
+
isPending.current = true;
|
10
|
+
}
|
11
|
+
if (!isSubmitting && isPending.current) {
|
12
|
+
isPending.current = false;
|
13
|
+
callback();
|
14
|
+
}
|
15
|
+
});
|
16
|
+
}
|
17
|
+
exports.useSubmitComplete = useSubmitComplete;
|
@@ -16,7 +16,18 @@ const preprocessFormData = (data) => {
|
|
16
16
|
*/
|
17
17
|
function createValidator(validator) {
|
18
18
|
return {
|
19
|
-
validate: (value) =>
|
19
|
+
validate: (value) => {
|
20
|
+
const data = preprocessFormData(value);
|
21
|
+
const result = validator.validate(data);
|
22
|
+
if (result.error) {
|
23
|
+
// Ideally, we should probably be returning a nested object like
|
24
|
+
// { fieldErrors: {}, submittedData: {} }
|
25
|
+
// We should do this in the next major version of the library
|
26
|
+
// but for now, we can sneak it in with the fieldErrors.
|
27
|
+
result.error._submittedData = data;
|
28
|
+
}
|
29
|
+
return result;
|
30
|
+
},
|
20
31
|
validateField: (data, field) => validator.validateField(preprocessFormData(data), field),
|
21
32
|
};
|
22
33
|
}
|
@@ -22,6 +22,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
22
|
const yup = __importStar(require("yup"));
|
23
23
|
const zod_1 = require("zod");
|
24
24
|
const __1 = require("..");
|
25
|
+
const flatten_1 = require("../internal/flatten");
|
25
26
|
const testFormData_1 = require("../test-data/testFormData");
|
26
27
|
const withZod_1 = require("./withZod");
|
27
28
|
const validationTestCases = [
|
@@ -99,6 +100,7 @@ describe("Validation", () => {
|
|
99
100
|
"address.country": anyString,
|
100
101
|
"address.streetAddress": anyString,
|
101
102
|
"pets[0].name": anyString,
|
103
|
+
_submittedData: obj,
|
102
104
|
},
|
103
105
|
});
|
104
106
|
});
|
@@ -140,6 +142,7 @@ describe("Validation", () => {
|
|
140
142
|
error: {
|
141
143
|
"address.city": anyString,
|
142
144
|
"pets[0].name": anyString,
|
145
|
+
_submittedData: (0, flatten_1.objectFromPathEntries)([...formData.entries()]),
|
143
146
|
},
|
144
147
|
});
|
145
148
|
});
|
@@ -260,6 +263,7 @@ describe("withZod", () => {
|
|
260
263
|
type: anyString,
|
261
264
|
bar: anyString,
|
262
265
|
foo: anyString,
|
266
|
+
_submittedData: obj,
|
263
267
|
},
|
264
268
|
});
|
265
269
|
});
|
@@ -278,6 +282,7 @@ describe("withZod", () => {
|
|
278
282
|
error: {
|
279
283
|
field1: anyString,
|
280
284
|
field2: anyString,
|
285
|
+
_submittedData: obj,
|
281
286
|
},
|
282
287
|
});
|
283
288
|
expect(validator.validateField(obj, "field1")).toEqual({
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "remix-validated-form",
|
3
|
-
"version": "
|
3
|
+
"version": "3.0.0-beta.1",
|
4
4
|
"description": "Form component and utils for easy form validation in remix",
|
5
5
|
"browser": "./browser/index.js",
|
6
6
|
"main": "./build/index.js",
|
@@ -16,7 +16,8 @@
|
|
16
16
|
"build:main": "tsc --module CommonJS --outDir ./build",
|
17
17
|
"test": "jest src",
|
18
18
|
"test:watch": "jest src --watch",
|
19
|
-
"prepublishOnly": "
|
19
|
+
"prepublishOnly": "cp ../../README.md ./README.md && npm run build",
|
20
|
+
"postpublish": "rm ./README.md"
|
20
21
|
},
|
21
22
|
"author": {
|
22
23
|
"name": "Aaron Pettengill",
|
@@ -30,6 +31,7 @@
|
|
30
31
|
"react",
|
31
32
|
"form",
|
32
33
|
"yup",
|
34
|
+
"zod",
|
33
35
|
"validation"
|
34
36
|
],
|
35
37
|
"peerDependencies": {
|
@@ -40,20 +42,15 @@
|
|
40
42
|
"devDependencies": {
|
41
43
|
"@remix-run/react": "^1.0.6",
|
42
44
|
"@remix-run/server-runtime": "^1.0.6",
|
43
|
-
"@types/jest": "^27.0.3",
|
44
45
|
"@types/lodash": "^4.14.178",
|
45
46
|
"@types/react": "^17.0.37",
|
46
47
|
"fetch-blob": "^3.1.3",
|
47
48
|
"react": "^17.0.2",
|
48
|
-
"ts-jest": "^27.1.1",
|
49
49
|
"tsconfig": "*",
|
50
|
-
"typescript": "^4.5.3"
|
51
|
-
"yup": "^0.32.11",
|
52
|
-
"zod": "^3.11.6"
|
50
|
+
"typescript": "^4.5.3"
|
53
51
|
},
|
54
52
|
"dependencies": {
|
55
53
|
"lodash": "^4.17.21",
|
56
54
|
"tiny-invariant": "^1.2.0"
|
57
|
-
}
|
58
|
-
"gitHead": "dab8f221d7c0c5131504215f5c19e3ecdfef97fb"
|
55
|
+
}
|
59
56
|
}
|
package/src/ValidatedForm.tsx
CHANGED
@@ -14,8 +14,13 @@ import React, {
|
|
14
14
|
} from "react";
|
15
15
|
import invariant from "tiny-invariant";
|
16
16
|
import { FormContext, FormContextValue } from "./internal/formContext";
|
17
|
+
import { useSubmitComplete } from "./internal/submissionCallbacks";
|
17
18
|
import { omit, mergeRefs } from "./internal/util";
|
18
|
-
import {
|
19
|
+
import {
|
20
|
+
FieldErrors,
|
21
|
+
Validator,
|
22
|
+
FieldErrorsWithData,
|
23
|
+
} from "./validation/types";
|
19
24
|
|
20
25
|
export type FormProps<DataType> = {
|
21
26
|
/**
|
@@ -42,39 +47,95 @@ export type FormProps<DataType> = {
|
|
42
47
|
* A ref to the form element.
|
43
48
|
*/
|
44
49
|
formRef?: React.RefObject<HTMLFormElement>;
|
50
|
+
/**
|
51
|
+
* An optional sub-action to use for the form.
|
52
|
+
* Setting a value here will cause the form to be submitted with an extra `subaction` value.
|
53
|
+
* This can be useful when there are multiple forms on the screen handled by the same action.
|
54
|
+
*/
|
55
|
+
subaction?: string;
|
56
|
+
/**
|
57
|
+
* Reset the form to the default values after the form has been successfully submitted.
|
58
|
+
* This is useful if you want to submit the same form multiple times,
|
59
|
+
* and don't redirect in-between submissions.
|
60
|
+
*/
|
61
|
+
resetAfterSubmit?: boolean;
|
45
62
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
46
63
|
|
47
|
-
function
|
48
|
-
fetcher?: ReturnType<typeof useFetcher
|
49
|
-
|
64
|
+
function useFieldErrorsFromBackend(
|
65
|
+
fetcher?: ReturnType<typeof useFetcher>,
|
66
|
+
subaction?: string
|
67
|
+
): FieldErrorsWithData | null {
|
50
68
|
const actionData = useActionData<any>();
|
51
|
-
|
52
|
-
|
69
|
+
if (fetcher) return (fetcher.data as any)?.fieldErrors;
|
70
|
+
if (!actionData) return null;
|
71
|
+
if (actionData.fieldErrors) {
|
72
|
+
const submittedData = actionData.fieldErrors?._submittedData;
|
73
|
+
const subactionsMatch = subaction
|
74
|
+
? subaction === submittedData?.subaction
|
75
|
+
: !submittedData?.subaction;
|
76
|
+
return subactionsMatch ? actionData.fieldErrors : null;
|
77
|
+
}
|
78
|
+
return null;
|
79
|
+
}
|
53
80
|
|
81
|
+
function useFieldErrors(
|
82
|
+
fieldErrorsFromBackend?: any
|
83
|
+
): [FieldErrors, React.Dispatch<React.SetStateAction<FieldErrors>>] {
|
54
84
|
const [fieldErrors, setFieldErrors] = useState<FieldErrors>(
|
55
|
-
|
85
|
+
fieldErrorsFromBackend ?? {}
|
56
86
|
);
|
57
87
|
useEffect(() => {
|
58
|
-
if (
|
59
|
-
}, [
|
88
|
+
if (fieldErrorsFromBackend) setFieldErrors(fieldErrorsFromBackend);
|
89
|
+
}, [fieldErrorsFromBackend]);
|
60
90
|
|
61
91
|
return [fieldErrors, setFieldErrors];
|
62
92
|
}
|
63
93
|
|
64
94
|
const useIsSubmitting = (
|
65
95
|
action?: string,
|
96
|
+
subaction?: string,
|
66
97
|
fetcher?: ReturnType<typeof useFetcher>
|
67
98
|
) => {
|
68
99
|
const actionForCurrentPage = useFormAction();
|
69
100
|
const pendingFormSubmit = useTransition().submission;
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
101
|
+
|
102
|
+
if (fetcher) return fetcher.state === "submitting";
|
103
|
+
if (!pendingFormSubmit) return false;
|
104
|
+
|
105
|
+
const { formData, action: pendingAction } = pendingFormSubmit;
|
106
|
+
const pendingSubAction = formData.get("subaction");
|
107
|
+
const expectedAction = action ?? actionForCurrentPage;
|
108
|
+
if (subaction)
|
109
|
+
return expectedAction === pendingAction && subaction === pendingSubAction;
|
110
|
+
return expectedAction === pendingAction && !pendingSubAction;
|
74
111
|
};
|
75
112
|
|
76
113
|
const getDataFromForm = (el: HTMLFormElement) => new FormData(el);
|
77
114
|
|
115
|
+
/**
|
116
|
+
* The purpose for this logic is to handle validation errors when javascript is disabled.
|
117
|
+
* Normally (without js), when a form is submitted and the action returns the validation errors,
|
118
|
+
* the form will be reset. The errors will be displayed on the correct fields,
|
119
|
+
* but all the values in the form will be gone. This is not good UX.
|
120
|
+
*
|
121
|
+
* To get around this, we return the submitted form data from the server,
|
122
|
+
* and use those to populate the form via `defaultValues`.
|
123
|
+
* This results in a more seamless UX akin to what you would see when js is enabled.
|
124
|
+
*
|
125
|
+
* One potential downside is that resetting the form will reset the form
|
126
|
+
* to the _new_ default values that were returned from the server with the validation errors.
|
127
|
+
* However, this case is less of a problem than the janky UX caused by losing the form values.
|
128
|
+
* It will only ever be a problem if the form includes a `<button type="reset" />`
|
129
|
+
* and only if JS is disabled.
|
130
|
+
*/
|
131
|
+
function useDefaultValues<DataType>(
|
132
|
+
fieldErrors?: FieldErrorsWithData | null,
|
133
|
+
defaultValues?: Partial<DataType>
|
134
|
+
) {
|
135
|
+
const defaultsFromValidationError = fieldErrors?._submittedData;
|
136
|
+
return defaultsFromValidationError ?? defaultValues;
|
137
|
+
}
|
138
|
+
|
78
139
|
/**
|
79
140
|
* The primary form component of `remix-validated-form`.
|
80
141
|
*/
|
@@ -86,18 +147,27 @@ export function ValidatedForm<DataType>({
|
|
86
147
|
action,
|
87
148
|
defaultValues,
|
88
149
|
formRef: formRefProp,
|
150
|
+
onReset,
|
151
|
+
subaction,
|
152
|
+
resetAfterSubmit,
|
89
153
|
...rest
|
90
154
|
}: FormProps<DataType>) {
|
91
|
-
const
|
92
|
-
const
|
93
|
-
|
155
|
+
const fieldErrorsFromBackend = useFieldErrorsFromBackend(fetcher, subaction);
|
156
|
+
const [fieldErrors, setFieldErrors] = useFieldErrors(fieldErrorsFromBackend);
|
157
|
+
const isSubmitting = useIsSubmitting(action, subaction, fetcher);
|
158
|
+
const defaultsToUse = useDefaultValues(fieldErrorsFromBackend, defaultValues);
|
94
159
|
const formRef = useRef<HTMLFormElement>(null);
|
160
|
+
useSubmitComplete(isSubmitting, () => {
|
161
|
+
if (!fieldErrorsFromBackend && resetAfterSubmit) {
|
162
|
+
formRef.current?.reset();
|
163
|
+
}
|
164
|
+
});
|
95
165
|
|
96
166
|
const contextValue = useMemo<FormContextValue>(
|
97
167
|
() => ({
|
98
168
|
fieldErrors,
|
99
169
|
action,
|
100
|
-
defaultValues,
|
170
|
+
defaultValues: defaultsToUse,
|
101
171
|
isSubmitting: isSubmitting ?? false,
|
102
172
|
clearError: (fieldName) => {
|
103
173
|
setFieldErrors((prev) => omit(prev, fieldName));
|
@@ -119,7 +189,7 @@ export function ValidatedForm<DataType>({
|
|
119
189
|
[
|
120
190
|
fieldErrors,
|
121
191
|
action,
|
122
|
-
|
192
|
+
defaultsToUse,
|
123
193
|
isSubmitting,
|
124
194
|
setFieldErrors,
|
125
195
|
validator,
|
@@ -142,8 +212,16 @@ export function ValidatedForm<DataType>({
|
|
142
212
|
onSubmit?.(result.data, event);
|
143
213
|
}
|
144
214
|
}}
|
215
|
+
onReset={(event) => {
|
216
|
+
onReset?.(event);
|
217
|
+
if (event.defaultPrevented) return;
|
218
|
+
setFieldErrors({});
|
219
|
+
}}
|
145
220
|
>
|
146
221
|
<FormContext.Provider value={contextValue}>
|
222
|
+
{subaction && (
|
223
|
+
<input type="hidden" value={subaction} name="subaction" />
|
224
|
+
)}
|
147
225
|
{children}
|
148
226
|
</FormContext.Provider>
|
149
227
|
</Form>
|
package/src/index.ts
CHANGED
@@ -2,7 +2,5 @@ export * from "./hooks";
|
|
2
2
|
export * from "./server";
|
3
3
|
export * from "./ValidatedForm";
|
4
4
|
export * from "./validation/types";
|
5
|
-
export * from "./validation/withYup";
|
6
|
-
export * from "./validation/withZod";
|
7
5
|
export * from "./validation/createValidator";
|
8
6
|
export type { FormContextValue } from "./internal/formContext";
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { useEffect, useRef } from "react";
|
2
|
+
|
3
|
+
export function useSubmitComplete(isSubmitting: boolean, callback: () => void) {
|
4
|
+
const isPending = useRef(false);
|
5
|
+
useEffect(() => {
|
6
|
+
if (isSubmitting) {
|
7
|
+
isPending.current = true;
|
8
|
+
}
|
9
|
+
|
10
|
+
if (!isSubmitting && isPending.current) {
|
11
|
+
isPending.current = false;
|
12
|
+
callback();
|
13
|
+
}
|
14
|
+
});
|
15
|
+
}
|
@@ -16,8 +16,18 @@ const preprocessFormData = (data: GenericObject | FormData): GenericObject => {
|
|
16
16
|
*/
|
17
17
|
export function createValidator<T>(validator: Validator<T>): Validator<T> {
|
18
18
|
return {
|
19
|
-
validate: (value: GenericObject | FormData) =>
|
20
|
-
|
19
|
+
validate: (value: GenericObject | FormData) => {
|
20
|
+
const data = preprocessFormData(value);
|
21
|
+
const result = validator.validate(data);
|
22
|
+
if (result.error) {
|
23
|
+
// Ideally, we should probably be returning a nested object like
|
24
|
+
// { fieldErrors: {}, submittedData: {} }
|
25
|
+
// We should do this in the next major version of the library
|
26
|
+
// but for now, we can sneak it in with the fieldErrors.
|
27
|
+
result.error._submittedData = data as any;
|
28
|
+
}
|
29
|
+
return result;
|
30
|
+
},
|
21
31
|
validateField: (data: GenericObject | FormData, field: string) =>
|
22
32
|
validator.validateField(preprocessFormData(data), field),
|
23
33
|
};
|
package/src/validation/types.ts
CHANGED
package/jest.config.js
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
// Copied from remix to use in tests
|
2
|
-
// https://github.com/remix-run/remix/blob/a69a631cb5add72d5fb24211ab2a0be367b6f2fd/packages/remix-node/form-data.ts
|
3
|
-
export class TestFormData implements FormData {
|
4
|
-
private _params: URLSearchParams;
|
5
|
-
|
6
|
-
constructor(body?: string) {
|
7
|
-
this._params = new URLSearchParams(body);
|
8
|
-
}
|
9
|
-
append(name: string, value: string | Blob, fileName?: string): void {
|
10
|
-
if (typeof value !== "string") {
|
11
|
-
throw new Error("formData.append can only accept a string");
|
12
|
-
}
|
13
|
-
this._params.append(name, value);
|
14
|
-
}
|
15
|
-
delete(name: string): void {
|
16
|
-
this._params.delete(name);
|
17
|
-
}
|
18
|
-
get(name: string): FormDataEntryValue | null {
|
19
|
-
return this._params.get(name);
|
20
|
-
}
|
21
|
-
getAll(name: string): FormDataEntryValue[] {
|
22
|
-
return this._params.getAll(name);
|
23
|
-
}
|
24
|
-
has(name: string): boolean {
|
25
|
-
return this._params.has(name);
|
26
|
-
}
|
27
|
-
set(name: string, value: string | Blob, fileName?: string): void {
|
28
|
-
if (typeof value !== "string") {
|
29
|
-
throw new Error("formData.set can only accept a string");
|
30
|
-
}
|
31
|
-
this._params.set(name, value);
|
32
|
-
}
|
33
|
-
forEach(
|
34
|
-
callbackfn: (
|
35
|
-
value: FormDataEntryValue,
|
36
|
-
key: string,
|
37
|
-
parent: FormData
|
38
|
-
) => void,
|
39
|
-
thisArg?: any
|
40
|
-
): void {
|
41
|
-
this._params.forEach(callbackfn, thisArg);
|
42
|
-
}
|
43
|
-
entries(): IterableIterator<[string, FormDataEntryValue]> {
|
44
|
-
return this._params.entries();
|
45
|
-
}
|
46
|
-
keys(): IterableIterator<string> {
|
47
|
-
return this._params.keys();
|
48
|
-
}
|
49
|
-
values(): IterableIterator<FormDataEntryValue> {
|
50
|
-
return this._params.values();
|
51
|
-
}
|
52
|
-
*[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]> {
|
53
|
-
yield* this._params;
|
54
|
-
}
|
55
|
-
}
|