remix-validated-form 2.0.0 → 2.1.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 +9 -0
- package/.turbo/turbo-dev.log +0 -0
- package/.turbo/turbo-test.log +36 -0
- package/README.md +3 -6
- package/browser/ValidatedForm.d.ts +13 -1
- package/browser/ValidatedForm.js +72 -19
- package/browser/hooks.d.ts +0 -0
- package/browser/hooks.js +0 -0
- package/browser/index.d.ts +0 -0
- package/browser/index.js +0 -0
- package/browser/internal/flatten.d.ts +0 -0
- package/browser/internal/flatten.js +0 -0
- package/browser/internal/formContext.d.ts +0 -0
- package/browser/internal/formContext.js +0 -0
- package/browser/internal/submissionCallbacks.d.ts +1 -0
- package/browser/internal/submissionCallbacks.js +13 -0
- package/browser/internal/util.d.ts +0 -0
- package/browser/internal/util.js +0 -0
- package/browser/server.d.ts +0 -0
- package/browser/server.js +0 -0
- package/browser/test-data/testFormData.d.ts +0 -0
- package/browser/test-data/testFormData.js +0 -0
- package/browser/validation/createValidator.d.ts +0 -0
- package/browser/validation/createValidator.js +12 -1
- package/browser/validation/types.d.ts +3 -0
- package/browser/validation/types.js +0 -0
- package/browser/validation/validation.test.d.ts +0 -0
- package/browser/validation/validation.test.js +5 -0
- package/browser/validation/withYup.d.ts +0 -0
- package/browser/validation/withYup.js +0 -0
- package/browser/validation/withZod.d.ts +0 -0
- package/browser/validation/withZod.js +0 -0
- package/build/ValidatedForm.d.ts +13 -1
- package/build/ValidatedForm.js +71 -18
- package/build/hooks.d.ts +0 -0
- package/build/hooks.js +0 -0
- package/build/index.d.ts +0 -0
- package/build/index.js +0 -0
- package/build/internal/flatten.d.ts +0 -0
- package/build/internal/flatten.js +0 -0
- package/build/internal/formContext.d.ts +0 -0
- package/build/internal/formContext.js +0 -0
- package/build/internal/submissionCallbacks.d.ts +1 -0
- package/build/internal/submissionCallbacks.js +17 -0
- package/build/internal/util.d.ts +0 -0
- package/build/internal/util.js +0 -0
- package/build/server.d.ts +0 -0
- package/build/server.js +0 -0
- package/build/test-data/testFormData.d.ts +0 -0
- package/build/test-data/testFormData.js +0 -0
- package/build/validation/createValidator.d.ts +0 -0
- package/build/validation/createValidator.js +12 -1
- package/build/validation/types.d.ts +3 -0
- package/build/validation/types.js +0 -0
- package/build/validation/validation.test.d.ts +0 -0
- package/build/validation/validation.test.js +5 -0
- package/build/validation/withYup.d.ts +0 -0
- package/build/validation/withYup.js +0 -0
- package/build/validation/withZod.d.ts +0 -0
- package/build/validation/withZod.js +0 -0
- package/jest.config.js +5 -0
- package/package.json +8 -33
- package/src/ValidatedForm.tsx +229 -0
- package/src/hooks.ts +60 -0
- package/src/index.ts +8 -0
- package/src/internal/flatten.ts +48 -0
- package/src/internal/formContext.ts +36 -0
- package/src/internal/submissionCallbacks.ts +15 -0
- package/src/internal/util.ts +23 -0
- package/src/server.ts +10 -0
- package/src/test-data/testFormData.ts +55 -0
- package/src/validation/createValidator.ts +34 -0
- package/src/validation/types.ts +28 -0
- package/src/validation/validation.test.ts +322 -0
- package/src/validation/withYup.ts +43 -0
- package/src/validation/withZod.ts +51 -0
- package/tsconfig.json +5 -0
- package/.eslintcache +0 -1
- package/.eslintignore +0 -1
- package/.prettierignore +0 -10
- package/LICENSE +0 -21
- package/browser/flatten.d.ts +0 -4
- package/browser/flatten.js +0 -35
- package/build/flatten.d.ts +0 -4
- package/build/flatten.js +0 -43
- package/sample-app/.env +0 -7
- package/sample-app/README.md +0 -53
- package/sample-app/app/components/ErrorBox.tsx +0 -34
- package/sample-app/app/components/FormInput.tsx +0 -40
- package/sample-app/app/components/FormSelect.tsx +0 -37
- package/sample-app/app/components/SubjectForm.tsx +0 -150
- package/sample-app/app/entry.client.tsx +0 -4
- package/sample-app/app/entry.server.tsx +0 -21
- package/sample-app/app/root.tsx +0 -92
- package/sample-app/app/routes/index.tsx +0 -5
- package/sample-app/app/routes/subjects/$id.edit.tsx +0 -98
- package/sample-app/app/routes/subjects/index.tsx +0 -112
- package/sample-app/app/routes/subjects/new.tsx +0 -46
- package/sample-app/app/services/db.server.ts +0 -23
- package/sample-app/app/types.ts +0 -6
- package/sample-app/package-lock.json +0 -10890
- package/sample-app/package.json +0 -36
- package/sample-app/prisma/dev.db +0 -0
- package/sample-app/prisma/schema.prisma +0 -34
- package/sample-app/public/favicon.ico +0 -0
- package/sample-app/remix.config.js +0 -10
- package/sample-app/remix.env.d.ts +0 -2
File without changes
|
@@ -0,0 +1,36 @@
|
|
1
|
+
[2K[1G[2m$ jest src[22m
|
2
|
+
[0m[7m[1m[32m PASS [39m[22m[27m[0m [2msrc/validation/[22m[1mvalidation.test.ts[22m
|
3
|
+
Validation
|
4
|
+
Adapter for yup
|
5
|
+
validate
|
6
|
+
[32m✓[39m [2mshould return the data when valid (2 ms)[22m
|
7
|
+
[32m✓[39m [2mshould return field errors when invalid (1 ms)[22m
|
8
|
+
[32m✓[39m [2mshould unflatten data when validating[22m
|
9
|
+
[32m✓[39m [2mshould accept FormData directly and return errors (1 ms)[22m
|
10
|
+
[32m✓[39m [2mshould accept FormData directly and return valid data (1 ms)[22m
|
11
|
+
validateField
|
12
|
+
[32m✓[39m [2mshould not return an error if field is valid[22m
|
13
|
+
[32m✓[39m [2mshould not return an error if a nested field is valid (1 ms)[22m
|
14
|
+
[32m✓[39m [2mshould return an error if field is invalid (2 ms)[22m
|
15
|
+
[32m✓[39m [2mshould return an error if a nested field is invalid[22m
|
16
|
+
Adapter for zod
|
17
|
+
validate
|
18
|
+
[32m✓[39m [2mshould return the data when valid (1 ms)[22m
|
19
|
+
[32m✓[39m [2mshould return field errors when invalid[22m
|
20
|
+
[32m✓[39m [2mshould unflatten data when validating (1 ms)[22m
|
21
|
+
[32m✓[39m [2mshould accept FormData directly and return errors[22m
|
22
|
+
[32m✓[39m [2mshould accept FormData directly and return valid data[22m
|
23
|
+
validateField
|
24
|
+
[32m✓[39m [2mshould not return an error if field is valid (1 ms)[22m
|
25
|
+
[32m✓[39m [2mshould not return an error if a nested field is valid[22m
|
26
|
+
[32m✓[39m [2mshould return an error if field is invalid[22m
|
27
|
+
[32m✓[39m [2mshould return an error if a nested field is invalid[22m
|
28
|
+
withZod
|
29
|
+
[32m✓[39m [2mreturns coherent errors for complex schemas (1 ms)[22m
|
30
|
+
[32m✓[39m [2mreturns errors for fields that are unions[22m
|
31
|
+
|
32
|
+
[1mTest Suites: [22m[1m[32m1 passed[39m[22m, 1 total
|
33
|
+
[1mTests: [22m[1m[32m20 passed[39m[22m, 20 total
|
34
|
+
[1mSnapshots: [22m0 total
|
35
|
+
[1mTime:[22m 1.1 s, estimated 2 s
|
36
|
+
[2mRan all test suites[22m[2m matching [22m/src/i[2m.[22m
|
package/README.md
CHANGED
@@ -18,12 +18,9 @@ To run `sample-app`:
|
|
18
18
|
|
19
19
|
```
|
20
20
|
git clone https://github.com/airjp73/remix-validated-form
|
21
|
-
cd remix-validated-form
|
22
|
-
|
23
|
-
|
24
|
-
npm i
|
25
|
-
cd ..
|
26
|
-
npm run sample-app
|
21
|
+
cd ./remix-validated-form
|
22
|
+
yarn install
|
23
|
+
yarn sample-app
|
27
24
|
```
|
28
25
|
|
29
26
|
# Getting started
|
@@ -26,8 +26,20 @@ export declare type FormProps<DataType> = {
|
|
26
26
|
* A ref to the form element.
|
27
27
|
*/
|
28
28
|
formRef?: React.RefObject<HTMLFormElement>;
|
29
|
+
/**
|
30
|
+
* An optional sub-action to use for the form.
|
31
|
+
* Setting a value here will cause the form to be submitted with an extra `subaction` value.
|
32
|
+
* This can be useful when there are multiple forms on the screen handled by the same action.
|
33
|
+
*/
|
34
|
+
subaction?: string;
|
35
|
+
/**
|
36
|
+
* Reset the form to the default values after the form has been successfully submitted.
|
37
|
+
* This is useful if you want to submit the same form multiple times,
|
38
|
+
* and don't redirect in-between submissions.
|
39
|
+
*/
|
40
|
+
resetAfterSubmit?: boolean;
|
29
41
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
30
42
|
/**
|
31
43
|
* The primary form component of `remix-validated-form`.
|
32
44
|
*/
|
33
|
-
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }: FormProps<DataType>): JSX.Element;
|
45
|
+
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, ...rest }: FormProps<DataType>): JSX.Element;
|
package/browser/ValidatedForm.js
CHANGED
@@ -1,41 +1,89 @@
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
2
2
|
import { Form as RemixForm, useActionData, useFormAction, useTransition, } from "@remix-run/react";
|
3
3
|
import { useEffect, useMemo, useRef, useState, } from "react";
|
4
4
|
import invariant from "tiny-invariant";
|
5
5
|
import { FormContext } from "./internal/formContext";
|
6
|
+
import { useSubmitComplete } from "./internal/submissionCallbacks";
|
6
7
|
import { omit, mergeRefs } from "./internal/util";
|
7
|
-
function
|
8
|
+
function useFieldErrorsFromBackend(fetcher, subaction) {
|
9
|
+
var _a, _b;
|
8
10
|
const actionData = useActionData();
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
if (fetcher)
|
12
|
+
return (_a = fetcher.data) === null || _a === void 0 ? void 0 : _a.fieldErrors;
|
13
|
+
if (!actionData)
|
14
|
+
return null;
|
15
|
+
if (actionData.fieldErrors) {
|
16
|
+
const submittedData = (_b = actionData.fieldErrors) === null || _b === void 0 ? void 0 : _b._submittedData;
|
17
|
+
const subactionsMatch = subaction
|
18
|
+
? subaction === (submittedData === null || submittedData === void 0 ? void 0 : submittedData.subaction)
|
19
|
+
: !(submittedData === null || submittedData === void 0 ? void 0 : submittedData.subaction);
|
20
|
+
return subactionsMatch ? actionData.fieldErrors : null;
|
21
|
+
}
|
22
|
+
return null;
|
23
|
+
}
|
24
|
+
function useFieldErrors(fieldErrorsFromBackend) {
|
25
|
+
const [fieldErrors, setFieldErrors] = useState(fieldErrorsFromBackend !== null && fieldErrorsFromBackend !== void 0 ? fieldErrorsFromBackend : {});
|
12
26
|
useEffect(() => {
|
13
|
-
if (
|
14
|
-
setFieldErrors(
|
15
|
-
}, [
|
27
|
+
if (fieldErrorsFromBackend)
|
28
|
+
setFieldErrors(fieldErrorsFromBackend);
|
29
|
+
}, [fieldErrorsFromBackend]);
|
16
30
|
return [fieldErrors, setFieldErrors];
|
17
31
|
}
|
18
|
-
const useIsSubmitting = (action, fetcher) => {
|
32
|
+
const useIsSubmitting = (action, subaction, fetcher) => {
|
19
33
|
const actionForCurrentPage = useFormAction();
|
20
34
|
const pendingFormSubmit = useTransition().submission;
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
35
|
+
if (fetcher)
|
36
|
+
return fetcher.state === "submitting";
|
37
|
+
if (!pendingFormSubmit)
|
38
|
+
return false;
|
39
|
+
const { formData, action: pendingAction } = pendingFormSubmit;
|
40
|
+
const pendingSubAction = formData.get("subaction");
|
41
|
+
const expectedAction = action !== null && action !== void 0 ? action : actionForCurrentPage;
|
42
|
+
if (subaction)
|
43
|
+
return expectedAction === pendingAction && subaction === pendingSubAction;
|
44
|
+
return expectedAction === pendingAction && !pendingSubAction;
|
25
45
|
};
|
26
46
|
const getDataFromForm = (el) => new FormData(el);
|
47
|
+
/**
|
48
|
+
* The purpose for this logic is to handle validation errors when javascript is disabled.
|
49
|
+
* Normally (without js), when a form is submitted and the action returns the validation errors,
|
50
|
+
* the form will be reset. The errors will be displayed on the correct fields,
|
51
|
+
* but all the values in the form will be gone. This is not good UX.
|
52
|
+
*
|
53
|
+
* To get around this, we return the submitted form data from the server,
|
54
|
+
* and use those to populate the form via `defaultValues`.
|
55
|
+
* This results in a more seamless UX akin to what you would see when js is enabled.
|
56
|
+
*
|
57
|
+
* One potential downside is that resetting the form will reset the form
|
58
|
+
* to the _new_ default values that were returned from the server with the validation errors.
|
59
|
+
* However, this case is less of a problem than the janky UX caused by losing the form values.
|
60
|
+
* It will only ever be a problem if the form includes a `<button type="reset" />`
|
61
|
+
* and only if JS is disabled.
|
62
|
+
*/
|
63
|
+
function useDefaultValues(fieldErrors, defaultValues) {
|
64
|
+
const defaultsFromValidationError = fieldErrors === null || fieldErrors === void 0 ? void 0 : fieldErrors._submittedData;
|
65
|
+
return defaultsFromValidationError !== null && defaultsFromValidationError !== void 0 ? defaultsFromValidationError : defaultValues;
|
66
|
+
}
|
27
67
|
/**
|
28
68
|
* The primary form component of `remix-validated-form`.
|
29
69
|
*/
|
30
|
-
export function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }) {
|
70
|
+
export function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, ...rest }) {
|
31
71
|
var _a;
|
32
|
-
const
|
33
|
-
const
|
72
|
+
const fieldErrorsFromBackend = useFieldErrorsFromBackend(fetcher, subaction);
|
73
|
+
const [fieldErrors, setFieldErrors] = useFieldErrors(fieldErrorsFromBackend);
|
74
|
+
const isSubmitting = useIsSubmitting(action, subaction, fetcher);
|
75
|
+
const defaultsToUse = useDefaultValues(fieldErrorsFromBackend, defaultValues);
|
34
76
|
const formRef = useRef(null);
|
77
|
+
useSubmitComplete(isSubmitting, () => {
|
78
|
+
var _a;
|
79
|
+
if (!fieldErrorsFromBackend && resetAfterSubmit) {
|
80
|
+
(_a = formRef.current) === null || _a === void 0 ? void 0 : _a.reset();
|
81
|
+
}
|
82
|
+
});
|
35
83
|
const contextValue = useMemo(() => ({
|
36
84
|
fieldErrors,
|
37
85
|
action,
|
38
|
-
defaultValues,
|
86
|
+
defaultValues: defaultsToUse,
|
39
87
|
isSubmitting: isSubmitting !== null && isSubmitting !== void 0 ? isSubmitting : false,
|
40
88
|
clearError: (fieldName) => {
|
41
89
|
setFieldErrors((prev) => omit(prev, fieldName));
|
@@ -53,7 +101,7 @@ export function ValidatedForm({ validator, onSubmit, children, fetcher, action,
|
|
53
101
|
}), [
|
54
102
|
fieldErrors,
|
55
103
|
action,
|
56
|
-
|
104
|
+
defaultsToUse,
|
57
105
|
isSubmitting,
|
58
106
|
setFieldErrors,
|
59
107
|
validator,
|
@@ -68,5 +116,10 @@ export function ValidatedForm({ validator, onSubmit, children, fetcher, action,
|
|
68
116
|
else {
|
69
117
|
onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit(result.data, event);
|
70
118
|
}
|
71
|
-
},
|
119
|
+
}, onReset: (event) => {
|
120
|
+
onReset === null || onReset === void 0 ? void 0 : onReset(event);
|
121
|
+
if (event.defaultPrevented)
|
122
|
+
return;
|
123
|
+
setFieldErrors({});
|
124
|
+
}, children: _jsxs(FormContext.Provider, { value: contextValue, children: [subaction && (_jsx("input", { type: "hidden", value: subaction, name: "subaction" }, void 0)), children] }, void 0) }, void 0));
|
72
125
|
}
|
package/browser/hooks.d.ts
CHANGED
File without changes
|
package/browser/hooks.js
CHANGED
File without changes
|
package/browser/index.d.ts
CHANGED
File without changes
|
package/browser/index.js
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function useSubmitComplete(isSubmitting: boolean, callback: () => void): void;
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { useEffect, useRef } from "react";
|
2
|
+
export function useSubmitComplete(isSubmitting, callback) {
|
3
|
+
const isPending = useRef(false);
|
4
|
+
useEffect(() => {
|
5
|
+
if (isSubmitting) {
|
6
|
+
isPending.current = true;
|
7
|
+
}
|
8
|
+
if (!isSubmitting && isPending.current) {
|
9
|
+
isPending.current = false;
|
10
|
+
callback();
|
11
|
+
}
|
12
|
+
});
|
13
|
+
}
|
File without changes
|
package/browser/internal/util.js
CHANGED
File without changes
|
package/browser/server.d.ts
CHANGED
File without changes
|
package/browser/server.js
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -13,7 +13,18 @@ const preprocessFormData = (data) => {
|
|
13
13
|
*/
|
14
14
|
export function createValidator(validator) {
|
15
15
|
return {
|
16
|
-
validate: (value) =>
|
16
|
+
validate: (value) => {
|
17
|
+
const data = preprocessFormData(value);
|
18
|
+
const result = validator.validate(data);
|
19
|
+
if (result.error) {
|
20
|
+
// Ideally, we should probably be returning a nested object like
|
21
|
+
// { fieldErrors: {}, submittedData: {} }
|
22
|
+
// We should do this in the next major version of the library
|
23
|
+
// but for now, we can sneak it in with the fieldErrors.
|
24
|
+
result.error._submittedData = data;
|
25
|
+
}
|
26
|
+
return result;
|
27
|
+
},
|
17
28
|
validateField: (data, field) => validator.validateField(preprocessFormData(data), field),
|
18
29
|
};
|
19
30
|
}
|
File without changes
|
File without changes
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import * as yup from "yup";
|
2
2
|
import { z } from "zod";
|
3
3
|
import { withYup } from "..";
|
4
|
+
import { objectFromPathEntries } from "../internal/flatten";
|
4
5
|
import { TestFormData } from "../test-data/testFormData";
|
5
6
|
import { withZod } from "./withZod";
|
6
7
|
const validationTestCases = [
|
@@ -78,6 +79,7 @@ describe("Validation", () => {
|
|
78
79
|
"address.country": anyString,
|
79
80
|
"address.streetAddress": anyString,
|
80
81
|
"pets[0].name": anyString,
|
82
|
+
_submittedData: obj,
|
81
83
|
},
|
82
84
|
});
|
83
85
|
});
|
@@ -119,6 +121,7 @@ describe("Validation", () => {
|
|
119
121
|
error: {
|
120
122
|
"address.city": anyString,
|
121
123
|
"pets[0].name": anyString,
|
124
|
+
_submittedData: objectFromPathEntries([...formData.entries()]),
|
122
125
|
},
|
123
126
|
});
|
124
127
|
});
|
@@ -239,6 +242,7 @@ describe("withZod", () => {
|
|
239
242
|
type: anyString,
|
240
243
|
bar: anyString,
|
241
244
|
foo: anyString,
|
245
|
+
_submittedData: obj,
|
242
246
|
},
|
243
247
|
});
|
244
248
|
});
|
@@ -257,6 +261,7 @@ describe("withZod", () => {
|
|
257
261
|
error: {
|
258
262
|
field1: anyString,
|
259
263
|
field2: anyString,
|
264
|
+
_submittedData: obj,
|
260
265
|
},
|
261
266
|
});
|
262
267
|
expect(validator.validateField(obj, "field1")).toEqual({
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
package/build/ValidatedForm.d.ts
CHANGED
@@ -26,8 +26,20 @@ export declare type FormProps<DataType> = {
|
|
26
26
|
* A ref to the form element.
|
27
27
|
*/
|
28
28
|
formRef?: React.RefObject<HTMLFormElement>;
|
29
|
+
/**
|
30
|
+
* An optional sub-action to use for the form.
|
31
|
+
* Setting a value here will cause the form to be submitted with an extra `subaction` value.
|
32
|
+
* This can be useful when there are multiple forms on the screen handled by the same action.
|
33
|
+
*/
|
34
|
+
subaction?: string;
|
35
|
+
/**
|
36
|
+
* Reset the form to the default values after the form has been successfully submitted.
|
37
|
+
* This is useful if you want to submit the same form multiple times,
|
38
|
+
* and don't redirect in-between submissions.
|
39
|
+
*/
|
40
|
+
resetAfterSubmit?: boolean;
|
29
41
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
30
42
|
/**
|
31
43
|
* The primary form component of `remix-validated-form`.
|
32
44
|
*/
|
33
|
-
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, ...rest }: FormProps<DataType>): JSX.Element;
|
45
|
+
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, ...rest }: FormProps<DataType>): JSX.Element;
|
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/hooks.d.ts
CHANGED
File without changes
|
package/build/hooks.js
CHANGED
File without changes
|
package/build/index.d.ts
CHANGED
File without changes
|
package/build/index.js
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -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;
|
package/build/internal/util.d.ts
CHANGED
File without changes
|
package/build/internal/util.js
CHANGED
File without changes
|
package/build/server.d.ts
CHANGED
File without changes
|
package/build/server.js
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -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
|
}
|
File without changes
|
File without changes
|
@@ -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({
|