remix-validated-form 3.0.0 → 3.1.0
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/browser/ValidatedForm.d.ts +6 -1
- package/browser/ValidatedForm.js +36 -1
- package/browser/hooks.d.ts +8 -1
- package/browser/hooks.js +8 -3
- package/browser/internal/MultiValueMap.d.ts +0 -0
- package/browser/internal/MultiValueMap.js +1 -0
- package/browser/internal/SingleTypeMultiValueMap.d.ts +8 -0
- package/browser/internal/SingleTypeMultiValueMap.js +40 -0
- package/browser/internal/formContext.d.ts +10 -0
- package/browser/internal/formContext.js +2 -0
- package/browser/server.d.ts +1 -1
- package/browser/server.js +11 -1
- package/browser/validation/types.d.ts +1 -0
- package/build/ValidatedForm.d.ts +6 -1
- package/build/ValidatedForm.js +36 -1
- package/build/hooks.d.ts +8 -1
- package/build/hooks.js +7 -2
- package/build/internal/SingleTypeMultiValueMap.d.ts +8 -0
- package/build/internal/SingleTypeMultiValueMap.js +45 -0
- package/build/internal/formContext.d.ts +10 -0
- package/build/internal/formContext.js +2 -0
- package/build/server.d.ts +1 -1
- package/build/server.js +11 -1
- package/build/validation/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/ValidatedForm.tsx +56 -0
- package/src/hooks.ts +26 -4
- package/src/internal/SingleTypeMultiValueMap.ts +37 -0
- package/src/internal/formContext.ts +12 -0
- package/src/server.ts +18 -2
- package/src/validation/types.ts +6 -0
package/.turbo/turbo-build.log
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
[2K[1G[2m$ npm run build:browser && npm run build:main[22m
|
2
2
|
|
3
|
-
> remix-validated-form@
|
3
|
+
> remix-validated-form@3.0.0 build:browser
|
4
4
|
> tsc --module ESNext --outDir ./browser
|
5
5
|
|
6
6
|
|
7
|
-
> remix-validated-form@
|
7
|
+
> remix-validated-form@3.0.0 build:main
|
8
8
|
> tsc --module CommonJS --outDir ./build
|
9
9
|
|
@@ -38,8 +38,13 @@ export declare type FormProps<DataType> = {
|
|
38
38
|
* and don't redirect in-between submissions.
|
39
39
|
*/
|
40
40
|
resetAfterSubmit?: boolean;
|
41
|
+
/**
|
42
|
+
* Normally, the first invalid input will be focused when the validation fails on form submit.
|
43
|
+
* Set this to `false` to disable this behavior.
|
44
|
+
*/
|
45
|
+
disableFocusOnError?: boolean;
|
41
46
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
42
47
|
/**
|
43
48
|
* The primary form component of `remix-validated-form`.
|
44
49
|
*/
|
45
|
-
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, ...rest }: FormProps<DataType>): JSX.Element;
|
50
|
+
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, disableFocusOnError, ...rest }: FormProps<DataType>): JSX.Element;
|
package/browser/ValidatedForm.js
CHANGED
@@ -3,6 +3,7 @@ import { Form as RemixForm, useActionData, useFormAction, useTransition, } from
|
|
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 { useMultiValueMap, } from "./internal/SingleTypeMultiValueMap";
|
6
7
|
import { useSubmitComplete } from "./internal/submissionCallbacks";
|
7
8
|
import { omit, mergeRefs } from "./internal/util";
|
8
9
|
function useFieldErrorsFromBackend(fetcher, subaction) {
|
@@ -64,10 +65,32 @@ function useDefaultValues(fieldErrors, defaultValues) {
|
|
64
65
|
const defaultsFromValidationError = fieldErrors === null || fieldErrors === void 0 ? void 0 : fieldErrors._submittedData;
|
65
66
|
return defaultsFromValidationError !== null && defaultsFromValidationError !== void 0 ? defaultsFromValidationError : defaultValues;
|
66
67
|
}
|
68
|
+
const focusFirstInvalidInput = (fieldErrors, customFocusHandlers, formElement) => {
|
69
|
+
const invalidInputSelector = Object.keys(fieldErrors)
|
70
|
+
.map((fieldName) => `input[name="${fieldName}"]`)
|
71
|
+
.join(",");
|
72
|
+
const invalidInputs = formElement.querySelectorAll(invalidInputSelector);
|
73
|
+
for (const element of invalidInputs) {
|
74
|
+
const input = element;
|
75
|
+
if (customFocusHandlers.has(input.name)) {
|
76
|
+
customFocusHandlers.getAll(input.name).forEach((handler) => {
|
77
|
+
handler();
|
78
|
+
});
|
79
|
+
break;
|
80
|
+
}
|
81
|
+
// We don't filter these out ahead of time because
|
82
|
+
// they could have a custom focus handler
|
83
|
+
if (input.type === "hidden") {
|
84
|
+
continue;
|
85
|
+
}
|
86
|
+
input.focus();
|
87
|
+
break;
|
88
|
+
}
|
89
|
+
};
|
67
90
|
/**
|
68
91
|
* The primary form component of `remix-validated-form`.
|
69
92
|
*/
|
70
|
-
export function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, ...rest }) {
|
93
|
+
export function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, disableFocusOnError, ...rest }) {
|
71
94
|
var _a;
|
72
95
|
const fieldErrorsFromBackend = useFieldErrorsFromBackend(fetcher, subaction);
|
73
96
|
const [fieldErrors, setFieldErrors] = useFieldErrors(fieldErrorsFromBackend);
|
@@ -80,11 +103,13 @@ export function ValidatedForm({ validator, onSubmit, children, fetcher, action,
|
|
80
103
|
(_a = formRef.current) === null || _a === void 0 ? void 0 : _a.reset();
|
81
104
|
}
|
82
105
|
});
|
106
|
+
const customFocusHandlers = useMultiValueMap();
|
83
107
|
const contextValue = useMemo(() => ({
|
84
108
|
fieldErrors,
|
85
109
|
action,
|
86
110
|
defaultValues: defaultsToUse,
|
87
111
|
isSubmitting: isSubmitting !== null && isSubmitting !== void 0 ? isSubmitting : false,
|
112
|
+
isValid: Object.keys(fieldErrors).length === 0,
|
88
113
|
clearError: (fieldName) => {
|
89
114
|
setFieldErrors((prev) => omit(prev, fieldName));
|
90
115
|
},
|
@@ -98,6 +123,12 @@ export function ValidatedForm({ validator, onSubmit, children, fetcher, action,
|
|
98
123
|
}));
|
99
124
|
}
|
100
125
|
},
|
126
|
+
registerReceiveFocus: (fieldName, handler) => {
|
127
|
+
customFocusHandlers().add(fieldName, handler);
|
128
|
+
return () => {
|
129
|
+
customFocusHandlers().remove(fieldName, handler);
|
130
|
+
};
|
131
|
+
},
|
101
132
|
}), [
|
102
133
|
fieldErrors,
|
103
134
|
action,
|
@@ -105,6 +136,7 @@ export function ValidatedForm({ validator, onSubmit, children, fetcher, action,
|
|
105
136
|
isSubmitting,
|
106
137
|
setFieldErrors,
|
107
138
|
validator,
|
139
|
+
customFocusHandlers,
|
108
140
|
]);
|
109
141
|
const Form = (_a = fetcher === null || fetcher === void 0 ? void 0 : fetcher.Form) !== null && _a !== void 0 ? _a : RemixForm;
|
110
142
|
return (_jsx(Form, { ref: mergeRefs([formRef, formRefProp]), ...rest, action: action, onSubmit: (event) => {
|
@@ -112,6 +144,9 @@ export function ValidatedForm({ validator, onSubmit, children, fetcher, action,
|
|
112
144
|
if (result.error) {
|
113
145
|
event.preventDefault();
|
114
146
|
setFieldErrors(result.error);
|
147
|
+
if (!disableFocusOnError) {
|
148
|
+
focusFirstInvalidInput(result.error, customFocusHandlers(), formRef.current);
|
149
|
+
}
|
115
150
|
}
|
116
151
|
else {
|
117
152
|
onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit(result.data, event);
|
package/browser/hooks.d.ts
CHANGED
@@ -19,7 +19,14 @@ export declare type FieldProps = {
|
|
19
19
|
/**
|
20
20
|
* Provides the data and helpers necessary to set up a field.
|
21
21
|
*/
|
22
|
-
export declare const useField: (name: string
|
22
|
+
export declare const useField: (name: string, options?: {
|
23
|
+
/**
|
24
|
+
* Allows you to configure a custom function that will be called
|
25
|
+
* when the input needs to receive focus due to a validation error.
|
26
|
+
* This is useful for custom components that use a hidden input.
|
27
|
+
*/
|
28
|
+
handleReceiveFocus?: (() => void) | undefined;
|
29
|
+
} | undefined) => FieldProps;
|
23
30
|
/**
|
24
31
|
* Provides access to the entire form context.
|
25
32
|
* This is not usually necessary, but can be useful for advanced use cases.
|
package/browser/hooks.js
CHANGED
@@ -1,12 +1,17 @@
|
|
1
1
|
import get from "lodash/get";
|
2
2
|
import toPath from "lodash/toPath";
|
3
|
-
import { useContext, useMemo } from "react";
|
3
|
+
import { useContext, useEffect, useMemo } from "react";
|
4
4
|
import { FormContext } from "./internal/formContext";
|
5
5
|
/**
|
6
6
|
* Provides the data and helpers necessary to set up a field.
|
7
7
|
*/
|
8
|
-
export const useField = (name) => {
|
9
|
-
const { fieldErrors, clearError, validateField, defaultValues } = useContext(FormContext);
|
8
|
+
export const useField = (name, options) => {
|
9
|
+
const { fieldErrors, clearError, validateField, defaultValues, registerReceiveFocus, } = useContext(FormContext);
|
10
|
+
const { handleReceiveFocus } = options !== null && options !== void 0 ? options : {};
|
11
|
+
useEffect(() => {
|
12
|
+
if (handleReceiveFocus)
|
13
|
+
return registerReceiveFocus(name, handleReceiveFocus);
|
14
|
+
}, [handleReceiveFocus, name, registerReceiveFocus]);
|
10
15
|
const field = useMemo(() => ({
|
11
16
|
error: fieldErrors[name],
|
12
17
|
clearError: () => {
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";
|
@@ -0,0 +1,8 @@
|
|
1
|
+
export declare class MultiValueMap<Key, Value> {
|
2
|
+
private dict;
|
3
|
+
add: (key: Key, value: Value) => void;
|
4
|
+
remove: (key: Key, value: Value) => void;
|
5
|
+
getAll: (key: Key) => Value[];
|
6
|
+
has: (key: Key) => boolean;
|
7
|
+
}
|
8
|
+
export declare const useMultiValueMap: <Key, Value>() => () => MultiValueMap<Key, Value>;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { useRef } from "react";
|
2
|
+
export class MultiValueMap {
|
3
|
+
constructor() {
|
4
|
+
this.dict = new Map();
|
5
|
+
this.add = (key, value) => {
|
6
|
+
var _a;
|
7
|
+
this.dict.set(key, [...((_a = this.dict.get(key)) !== null && _a !== void 0 ? _a : []), value]);
|
8
|
+
if (this.dict.has(key)) {
|
9
|
+
this.dict.get(key).push(value);
|
10
|
+
}
|
11
|
+
else {
|
12
|
+
this.dict.set(key, [value]);
|
13
|
+
}
|
14
|
+
};
|
15
|
+
this.remove = (key, value) => {
|
16
|
+
if (!this.dict.has(key))
|
17
|
+
return;
|
18
|
+
const array = this.dict.get(key);
|
19
|
+
const index = array.indexOf(value);
|
20
|
+
if (index !== -1)
|
21
|
+
array.splice(index, 1);
|
22
|
+
if (array.length === 0)
|
23
|
+
this.dict.delete(key);
|
24
|
+
};
|
25
|
+
this.getAll = (key) => {
|
26
|
+
var _a;
|
27
|
+
return (_a = this.dict.get(key)) !== null && _a !== void 0 ? _a : [];
|
28
|
+
};
|
29
|
+
this.has = (key) => this.dict.has(key);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
export const useMultiValueMap = () => {
|
33
|
+
const ref = useRef(null);
|
34
|
+
return () => {
|
35
|
+
if (ref.current)
|
36
|
+
return ref.current;
|
37
|
+
ref.current = new MultiValueMap();
|
38
|
+
return ref.current;
|
39
|
+
};
|
40
|
+
};
|
@@ -21,11 +21,21 @@ export declare type FormContextValue = {
|
|
21
21
|
* Whether or not the form is submitting.
|
22
22
|
*/
|
23
23
|
isSubmitting: boolean;
|
24
|
+
/**
|
25
|
+
* Whether or not the form is valid.
|
26
|
+
* This is a shortcut for `Object.keys(fieldErrors).length === 0`.
|
27
|
+
*/
|
28
|
+
isValid: boolean;
|
24
29
|
/**
|
25
30
|
* The default values of the form.
|
26
31
|
*/
|
27
32
|
defaultValues?: {
|
28
33
|
[fieldName: string]: any;
|
29
34
|
};
|
35
|
+
/**
|
36
|
+
* Register a custom focus handler to be used when
|
37
|
+
* the field needs to receive focus due to a validation error.
|
38
|
+
*/
|
39
|
+
registerReceiveFocus: (fieldName: string, handler: () => void) => () => void;
|
30
40
|
};
|
31
41
|
export declare const FormContext: import("react").Context<FormContextValue>;
|
package/browser/server.d.ts
CHANGED
@@ -4,4 +4,4 @@ import { FieldErrors } from "./validation/types";
|
|
4
4
|
* The `ValidatedForm` on the frontend will automatically display the errors
|
5
5
|
* if this is returned from the action.
|
6
6
|
*/
|
7
|
-
export declare const validationError: (errors: FieldErrors) => Response;
|
7
|
+
export declare const validationError: (errors: FieldErrors, submittedData?: unknown) => Response;
|
package/browser/server.js
CHANGED
@@ -4,4 +4,14 @@ import { json } from "@remix-run/server-runtime";
|
|
4
4
|
* The `ValidatedForm` on the frontend will automatically display the errors
|
5
5
|
* if this is returned from the action.
|
6
6
|
*/
|
7
|
-
export const validationError = (errors) =>
|
7
|
+
export const validationError = (errors, submittedData) => {
|
8
|
+
if (submittedData) {
|
9
|
+
return json({
|
10
|
+
fieldErrors: {
|
11
|
+
...errors,
|
12
|
+
_submittedData: submittedData,
|
13
|
+
},
|
14
|
+
}, { status: 422 });
|
15
|
+
}
|
16
|
+
return json({ fieldErrors: errors }, { status: 422 });
|
17
|
+
};
|
@@ -28,3 +28,4 @@ export declare type Validator<DataType> = {
|
|
28
28
|
validate: (unvalidatedData: GenericObject) => ValidationResult<DataType>;
|
29
29
|
validateField: (unvalidatedData: GenericObject, field: string) => ValidateFieldResult;
|
30
30
|
};
|
31
|
+
export declare type ValidatorData<T extends Validator<any>> = T extends Validator<infer U> ? U : never;
|
package/build/ValidatedForm.d.ts
CHANGED
@@ -38,8 +38,13 @@ export declare type FormProps<DataType> = {
|
|
38
38
|
* and don't redirect in-between submissions.
|
39
39
|
*/
|
40
40
|
resetAfterSubmit?: boolean;
|
41
|
+
/**
|
42
|
+
* Normally, the first invalid input will be focused when the validation fails on form submit.
|
43
|
+
* Set this to `false` to disable this behavior.
|
44
|
+
*/
|
45
|
+
disableFocusOnError?: boolean;
|
41
46
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
42
47
|
/**
|
43
48
|
* The primary form component of `remix-validated-form`.
|
44
49
|
*/
|
45
|
-
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, ...rest }: FormProps<DataType>): JSX.Element;
|
50
|
+
export declare function ValidatedForm<DataType>({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, disableFocusOnError, ...rest }: FormProps<DataType>): JSX.Element;
|
package/build/ValidatedForm.js
CHANGED
@@ -9,6 +9,7 @@ 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 SingleTypeMultiValueMap_1 = require("./internal/SingleTypeMultiValueMap");
|
12
13
|
const submissionCallbacks_1 = require("./internal/submissionCallbacks");
|
13
14
|
const util_1 = require("./internal/util");
|
14
15
|
function useFieldErrorsFromBackend(fetcher, subaction) {
|
@@ -70,10 +71,32 @@ function useDefaultValues(fieldErrors, defaultValues) {
|
|
70
71
|
const defaultsFromValidationError = fieldErrors === null || fieldErrors === void 0 ? void 0 : fieldErrors._submittedData;
|
71
72
|
return defaultsFromValidationError !== null && defaultsFromValidationError !== void 0 ? defaultsFromValidationError : defaultValues;
|
72
73
|
}
|
74
|
+
const focusFirstInvalidInput = (fieldErrors, customFocusHandlers, formElement) => {
|
75
|
+
const invalidInputSelector = Object.keys(fieldErrors)
|
76
|
+
.map((fieldName) => `input[name="${fieldName}"]`)
|
77
|
+
.join(",");
|
78
|
+
const invalidInputs = formElement.querySelectorAll(invalidInputSelector);
|
79
|
+
for (const element of invalidInputs) {
|
80
|
+
const input = element;
|
81
|
+
if (customFocusHandlers.has(input.name)) {
|
82
|
+
customFocusHandlers.getAll(input.name).forEach((handler) => {
|
83
|
+
handler();
|
84
|
+
});
|
85
|
+
break;
|
86
|
+
}
|
87
|
+
// We don't filter these out ahead of time because
|
88
|
+
// they could have a custom focus handler
|
89
|
+
if (input.type === "hidden") {
|
90
|
+
continue;
|
91
|
+
}
|
92
|
+
input.focus();
|
93
|
+
break;
|
94
|
+
}
|
95
|
+
};
|
73
96
|
/**
|
74
97
|
* The primary form component of `remix-validated-form`.
|
75
98
|
*/
|
76
|
-
function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, ...rest }) {
|
99
|
+
function ValidatedForm({ validator, onSubmit, children, fetcher, action, defaultValues, formRef: formRefProp, onReset, subaction, resetAfterSubmit, disableFocusOnError, ...rest }) {
|
77
100
|
var _a;
|
78
101
|
const fieldErrorsFromBackend = useFieldErrorsFromBackend(fetcher, subaction);
|
79
102
|
const [fieldErrors, setFieldErrors] = useFieldErrors(fieldErrorsFromBackend);
|
@@ -86,11 +109,13 @@ function ValidatedForm({ validator, onSubmit, children, fetcher, action, default
|
|
86
109
|
(_a = formRef.current) === null || _a === void 0 ? void 0 : _a.reset();
|
87
110
|
}
|
88
111
|
});
|
112
|
+
const customFocusHandlers = (0, SingleTypeMultiValueMap_1.useMultiValueMap)();
|
89
113
|
const contextValue = (0, react_2.useMemo)(() => ({
|
90
114
|
fieldErrors,
|
91
115
|
action,
|
92
116
|
defaultValues: defaultsToUse,
|
93
117
|
isSubmitting: isSubmitting !== null && isSubmitting !== void 0 ? isSubmitting : false,
|
118
|
+
isValid: Object.keys(fieldErrors).length === 0,
|
94
119
|
clearError: (fieldName) => {
|
95
120
|
setFieldErrors((prev) => (0, util_1.omit)(prev, fieldName));
|
96
121
|
},
|
@@ -104,6 +129,12 @@ function ValidatedForm({ validator, onSubmit, children, fetcher, action, default
|
|
104
129
|
}));
|
105
130
|
}
|
106
131
|
},
|
132
|
+
registerReceiveFocus: (fieldName, handler) => {
|
133
|
+
customFocusHandlers().add(fieldName, handler);
|
134
|
+
return () => {
|
135
|
+
customFocusHandlers().remove(fieldName, handler);
|
136
|
+
};
|
137
|
+
},
|
107
138
|
}), [
|
108
139
|
fieldErrors,
|
109
140
|
action,
|
@@ -111,6 +142,7 @@ function ValidatedForm({ validator, onSubmit, children, fetcher, action, default
|
|
111
142
|
isSubmitting,
|
112
143
|
setFieldErrors,
|
113
144
|
validator,
|
145
|
+
customFocusHandlers,
|
114
146
|
]);
|
115
147
|
const Form = (_a = fetcher === null || fetcher === void 0 ? void 0 : fetcher.Form) !== null && _a !== void 0 ? _a : react_1.Form;
|
116
148
|
return ((0, jsx_runtime_1.jsx)(Form, { ref: (0, util_1.mergeRefs)([formRef, formRefProp]), ...rest, action: action, onSubmit: (event) => {
|
@@ -118,6 +150,9 @@ function ValidatedForm({ validator, onSubmit, children, fetcher, action, default
|
|
118
150
|
if (result.error) {
|
119
151
|
event.preventDefault();
|
120
152
|
setFieldErrors(result.error);
|
153
|
+
if (!disableFocusOnError) {
|
154
|
+
focusFirstInvalidInput(result.error, customFocusHandlers(), formRef.current);
|
155
|
+
}
|
121
156
|
}
|
122
157
|
else {
|
123
158
|
onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit(result.data, event);
|
package/build/hooks.d.ts
CHANGED
@@ -19,7 +19,14 @@ export declare type FieldProps = {
|
|
19
19
|
/**
|
20
20
|
* Provides the data and helpers necessary to set up a field.
|
21
21
|
*/
|
22
|
-
export declare const useField: (name: string
|
22
|
+
export declare const useField: (name: string, options?: {
|
23
|
+
/**
|
24
|
+
* Allows you to configure a custom function that will be called
|
25
|
+
* when the input needs to receive focus due to a validation error.
|
26
|
+
* This is useful for custom components that use a hidden input.
|
27
|
+
*/
|
28
|
+
handleReceiveFocus?: (() => void) | undefined;
|
29
|
+
} | undefined) => FieldProps;
|
23
30
|
/**
|
24
31
|
* Provides access to the entire form context.
|
25
32
|
* This is not usually necessary, but can be useful for advanced use cases.
|
package/build/hooks.js
CHANGED
@@ -11,8 +11,13 @@ const formContext_1 = require("./internal/formContext");
|
|
11
11
|
/**
|
12
12
|
* Provides the data and helpers necessary to set up a field.
|
13
13
|
*/
|
14
|
-
const useField = (name) => {
|
15
|
-
const { fieldErrors, clearError, validateField, defaultValues } = (0, react_1.useContext)(formContext_1.FormContext);
|
14
|
+
const useField = (name, options) => {
|
15
|
+
const { fieldErrors, clearError, validateField, defaultValues, registerReceiveFocus, } = (0, react_1.useContext)(formContext_1.FormContext);
|
16
|
+
const { handleReceiveFocus } = options !== null && options !== void 0 ? options : {};
|
17
|
+
(0, react_1.useEffect)(() => {
|
18
|
+
if (handleReceiveFocus)
|
19
|
+
return registerReceiveFocus(name, handleReceiveFocus);
|
20
|
+
}, [handleReceiveFocus, name, registerReceiveFocus]);
|
16
21
|
const field = (0, react_1.useMemo)(() => ({
|
17
22
|
error: fieldErrors[name],
|
18
23
|
clearError: () => {
|
@@ -0,0 +1,8 @@
|
|
1
|
+
export declare class MultiValueMap<Key, Value> {
|
2
|
+
private dict;
|
3
|
+
add: (key: Key, value: Value) => void;
|
4
|
+
remove: (key: Key, value: Value) => void;
|
5
|
+
getAll: (key: Key) => Value[];
|
6
|
+
has: (key: Key) => boolean;
|
7
|
+
}
|
8
|
+
export declare const useMultiValueMap: <Key, Value>() => () => MultiValueMap<Key, Value>;
|
@@ -0,0 +1,45 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.useMultiValueMap = exports.MultiValueMap = void 0;
|
4
|
+
const react_1 = require("react");
|
5
|
+
class MultiValueMap {
|
6
|
+
constructor() {
|
7
|
+
this.dict = new Map();
|
8
|
+
this.add = (key, value) => {
|
9
|
+
var _a;
|
10
|
+
this.dict.set(key, [...((_a = this.dict.get(key)) !== null && _a !== void 0 ? _a : []), value]);
|
11
|
+
if (this.dict.has(key)) {
|
12
|
+
this.dict.get(key).push(value);
|
13
|
+
}
|
14
|
+
else {
|
15
|
+
this.dict.set(key, [value]);
|
16
|
+
}
|
17
|
+
};
|
18
|
+
this.remove = (key, value) => {
|
19
|
+
if (!this.dict.has(key))
|
20
|
+
return;
|
21
|
+
const array = this.dict.get(key);
|
22
|
+
const index = array.indexOf(value);
|
23
|
+
if (index !== -1)
|
24
|
+
array.splice(index, 1);
|
25
|
+
if (array.length === 0)
|
26
|
+
this.dict.delete(key);
|
27
|
+
};
|
28
|
+
this.getAll = (key) => {
|
29
|
+
var _a;
|
30
|
+
return (_a = this.dict.get(key)) !== null && _a !== void 0 ? _a : [];
|
31
|
+
};
|
32
|
+
this.has = (key) => this.dict.has(key);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
exports.MultiValueMap = MultiValueMap;
|
36
|
+
const useMultiValueMap = () => {
|
37
|
+
const ref = (0, react_1.useRef)(null);
|
38
|
+
return () => {
|
39
|
+
if (ref.current)
|
40
|
+
return ref.current;
|
41
|
+
ref.current = new MultiValueMap();
|
42
|
+
return ref.current;
|
43
|
+
};
|
44
|
+
};
|
45
|
+
exports.useMultiValueMap = useMultiValueMap;
|
@@ -21,11 +21,21 @@ export declare type FormContextValue = {
|
|
21
21
|
* Whether or not the form is submitting.
|
22
22
|
*/
|
23
23
|
isSubmitting: boolean;
|
24
|
+
/**
|
25
|
+
* Whether or not the form is valid.
|
26
|
+
* This is a shortcut for `Object.keys(fieldErrors).length === 0`.
|
27
|
+
*/
|
28
|
+
isValid: boolean;
|
24
29
|
/**
|
25
30
|
* The default values of the form.
|
26
31
|
*/
|
27
32
|
defaultValues?: {
|
28
33
|
[fieldName: string]: any;
|
29
34
|
};
|
35
|
+
/**
|
36
|
+
* Register a custom focus handler to be used when
|
37
|
+
* the field needs to receive focus due to a validation error.
|
38
|
+
*/
|
39
|
+
registerReceiveFocus: (fieldName: string, handler: () => void) => () => void;
|
30
40
|
};
|
31
41
|
export declare const FormContext: import("react").Context<FormContextValue>;
|
package/build/server.d.ts
CHANGED
@@ -4,4 +4,4 @@ import { FieldErrors } from "./validation/types";
|
|
4
4
|
* The `ValidatedForm` on the frontend will automatically display the errors
|
5
5
|
* if this is returned from the action.
|
6
6
|
*/
|
7
|
-
export declare const validationError: (errors: FieldErrors) => Response;
|
7
|
+
export declare const validationError: (errors: FieldErrors, submittedData?: unknown) => Response;
|
package/build/server.js
CHANGED
@@ -7,5 +7,15 @@ const server_runtime_1 = require("@remix-run/server-runtime");
|
|
7
7
|
* The `ValidatedForm` on the frontend will automatically display the errors
|
8
8
|
* if this is returned from the action.
|
9
9
|
*/
|
10
|
-
const validationError = (errors
|
10
|
+
const validationError = (errors, submittedData) => {
|
11
|
+
if (submittedData) {
|
12
|
+
return (0, server_runtime_1.json)({
|
13
|
+
fieldErrors: {
|
14
|
+
...errors,
|
15
|
+
_submittedData: submittedData,
|
16
|
+
},
|
17
|
+
}, { status: 422 });
|
18
|
+
}
|
19
|
+
return (0, server_runtime_1.json)({ fieldErrors: errors }, { status: 422 });
|
20
|
+
};
|
11
21
|
exports.validationError = validationError;
|
@@ -28,3 +28,4 @@ export declare type Validator<DataType> = {
|
|
28
28
|
validate: (unvalidatedData: GenericObject) => ValidationResult<DataType>;
|
29
29
|
validateField: (unvalidatedData: GenericObject, field: string) => ValidateFieldResult;
|
30
30
|
};
|
31
|
+
export declare type ValidatorData<T extends Validator<any>> = T extends Validator<infer U> ? U : never;
|
package/package.json
CHANGED
package/src/ValidatedForm.tsx
CHANGED
@@ -14,6 +14,10 @@ import React, {
|
|
14
14
|
} from "react";
|
15
15
|
import invariant from "tiny-invariant";
|
16
16
|
import { FormContext, FormContextValue } from "./internal/formContext";
|
17
|
+
import {
|
18
|
+
MultiValueMap,
|
19
|
+
useMultiValueMap,
|
20
|
+
} from "./internal/SingleTypeMultiValueMap";
|
17
21
|
import { useSubmitComplete } from "./internal/submissionCallbacks";
|
18
22
|
import { omit, mergeRefs } from "./internal/util";
|
19
23
|
import {
|
@@ -59,6 +63,11 @@ export type FormProps<DataType> = {
|
|
59
63
|
* and don't redirect in-between submissions.
|
60
64
|
*/
|
61
65
|
resetAfterSubmit?: boolean;
|
66
|
+
/**
|
67
|
+
* Normally, the first invalid input will be focused when the validation fails on form submit.
|
68
|
+
* Set this to `false` to disable this behavior.
|
69
|
+
*/
|
70
|
+
disableFocusOnError?: boolean;
|
62
71
|
} & Omit<ComponentProps<typeof RemixForm>, "onSubmit">;
|
63
72
|
|
64
73
|
function useFieldErrorsFromBackend(
|
@@ -136,6 +145,36 @@ function useDefaultValues<DataType>(
|
|
136
145
|
return defaultsFromValidationError ?? defaultValues;
|
137
146
|
}
|
138
147
|
|
148
|
+
const focusFirstInvalidInput = (
|
149
|
+
fieldErrors: FieldErrors,
|
150
|
+
customFocusHandlers: MultiValueMap<string, () => void>,
|
151
|
+
formElement: HTMLFormElement
|
152
|
+
) => {
|
153
|
+
const invalidInputSelector = Object.keys(fieldErrors)
|
154
|
+
.map((fieldName) => `input[name="${fieldName}"]`)
|
155
|
+
.join(",");
|
156
|
+
const invalidInputs = formElement.querySelectorAll(invalidInputSelector);
|
157
|
+
for (const element of invalidInputs) {
|
158
|
+
const input = element as HTMLInputElement;
|
159
|
+
|
160
|
+
if (customFocusHandlers.has(input.name)) {
|
161
|
+
customFocusHandlers.getAll(input.name).forEach((handler) => {
|
162
|
+
handler();
|
163
|
+
});
|
164
|
+
break;
|
165
|
+
}
|
166
|
+
|
167
|
+
// We don't filter these out ahead of time because
|
168
|
+
// they could have a custom focus handler
|
169
|
+
if (input.type === "hidden") {
|
170
|
+
continue;
|
171
|
+
}
|
172
|
+
|
173
|
+
input.focus();
|
174
|
+
break;
|
175
|
+
}
|
176
|
+
};
|
177
|
+
|
139
178
|
/**
|
140
179
|
* The primary form component of `remix-validated-form`.
|
141
180
|
*/
|
@@ -150,6 +189,7 @@ export function ValidatedForm<DataType>({
|
|
150
189
|
onReset,
|
151
190
|
subaction,
|
152
191
|
resetAfterSubmit,
|
192
|
+
disableFocusOnError,
|
153
193
|
...rest
|
154
194
|
}: FormProps<DataType>) {
|
155
195
|
const fieldErrorsFromBackend = useFieldErrorsFromBackend(fetcher, subaction);
|
@@ -162,6 +202,7 @@ export function ValidatedForm<DataType>({
|
|
162
202
|
formRef.current?.reset();
|
163
203
|
}
|
164
204
|
});
|
205
|
+
const customFocusHandlers = useMultiValueMap<string, () => void>();
|
165
206
|
|
166
207
|
const contextValue = useMemo<FormContextValue>(
|
167
208
|
() => ({
|
@@ -169,6 +210,7 @@ export function ValidatedForm<DataType>({
|
|
169
210
|
action,
|
170
211
|
defaultValues: defaultsToUse,
|
171
212
|
isSubmitting: isSubmitting ?? false,
|
213
|
+
isValid: Object.keys(fieldErrors).length === 0,
|
172
214
|
clearError: (fieldName) => {
|
173
215
|
setFieldErrors((prev) => omit(prev, fieldName));
|
174
216
|
},
|
@@ -185,6 +227,12 @@ export function ValidatedForm<DataType>({
|
|
185
227
|
}));
|
186
228
|
}
|
187
229
|
},
|
230
|
+
registerReceiveFocus: (fieldName, handler) => {
|
231
|
+
customFocusHandlers().add(fieldName, handler);
|
232
|
+
return () => {
|
233
|
+
customFocusHandlers().remove(fieldName, handler);
|
234
|
+
};
|
235
|
+
},
|
188
236
|
}),
|
189
237
|
[
|
190
238
|
fieldErrors,
|
@@ -193,6 +241,7 @@ export function ValidatedForm<DataType>({
|
|
193
241
|
isSubmitting,
|
194
242
|
setFieldErrors,
|
195
243
|
validator,
|
244
|
+
customFocusHandlers,
|
196
245
|
]
|
197
246
|
);
|
198
247
|
|
@@ -208,6 +257,13 @@ export function ValidatedForm<DataType>({
|
|
208
257
|
if (result.error) {
|
209
258
|
event.preventDefault();
|
210
259
|
setFieldErrors(result.error);
|
260
|
+
if (!disableFocusOnError) {
|
261
|
+
focusFirstInvalidInput(
|
262
|
+
result.error,
|
263
|
+
customFocusHandlers(),
|
264
|
+
formRef.current!
|
265
|
+
);
|
266
|
+
}
|
211
267
|
} else {
|
212
268
|
onSubmit?.(result.data, event);
|
213
269
|
}
|
package/src/hooks.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import get from "lodash/get";
|
2
2
|
import toPath from "lodash/toPath";
|
3
|
-
import { useContext, useMemo } from "react";
|
3
|
+
import { useContext, useEffect, useMemo } from "react";
|
4
4
|
import { FormContext } from "./internal/formContext";
|
5
5
|
|
6
6
|
export type FieldProps = {
|
@@ -25,9 +25,31 @@ export type FieldProps = {
|
|
25
25
|
/**
|
26
26
|
* Provides the data and helpers necessary to set up a field.
|
27
27
|
*/
|
28
|
-
export const useField = (
|
29
|
-
|
30
|
-
|
28
|
+
export const useField = (
|
29
|
+
name: string,
|
30
|
+
options?: {
|
31
|
+
/**
|
32
|
+
* Allows you to configure a custom function that will be called
|
33
|
+
* when the input needs to receive focus due to a validation error.
|
34
|
+
* This is useful for custom components that use a hidden input.
|
35
|
+
*/
|
36
|
+
handleReceiveFocus?: () => void;
|
37
|
+
}
|
38
|
+
): FieldProps => {
|
39
|
+
const {
|
40
|
+
fieldErrors,
|
41
|
+
clearError,
|
42
|
+
validateField,
|
43
|
+
defaultValues,
|
44
|
+
registerReceiveFocus,
|
45
|
+
} = useContext(FormContext);
|
46
|
+
|
47
|
+
const { handleReceiveFocus } = options ?? {};
|
48
|
+
|
49
|
+
useEffect(() => {
|
50
|
+
if (handleReceiveFocus)
|
51
|
+
return registerReceiveFocus(name, handleReceiveFocus);
|
52
|
+
}, [handleReceiveFocus, name, registerReceiveFocus]);
|
31
53
|
|
32
54
|
const field = useMemo<FieldProps>(
|
33
55
|
() => ({
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { useRef } from "react";
|
2
|
+
|
3
|
+
export class MultiValueMap<Key, Value> {
|
4
|
+
private dict: Map<Key, Value[]> = new Map();
|
5
|
+
|
6
|
+
add = (key: Key, value: Value) => {
|
7
|
+
this.dict.set(key, [...(this.dict.get(key) ?? []), value]);
|
8
|
+
if (this.dict.has(key)) {
|
9
|
+
this.dict.get(key)!.push(value);
|
10
|
+
} else {
|
11
|
+
this.dict.set(key, [value]);
|
12
|
+
}
|
13
|
+
};
|
14
|
+
|
15
|
+
remove = (key: Key, value: Value) => {
|
16
|
+
if (!this.dict.has(key)) return;
|
17
|
+
const array = this.dict.get(key)!;
|
18
|
+
const index = array.indexOf(value);
|
19
|
+
if (index !== -1) array.splice(index, 1);
|
20
|
+
if (array.length === 0) this.dict.delete(key);
|
21
|
+
};
|
22
|
+
|
23
|
+
getAll = (key: Key): Value[] => {
|
24
|
+
return this.dict.get(key) ?? [];
|
25
|
+
};
|
26
|
+
|
27
|
+
has = (key: Key): boolean => this.dict.has(key);
|
28
|
+
}
|
29
|
+
|
30
|
+
export const useMultiValueMap = <Key, Value>() => {
|
31
|
+
const ref = useRef<MultiValueMap<Key, Value> | null>(null);
|
32
|
+
return () => {
|
33
|
+
if (ref.current) return ref.current;
|
34
|
+
ref.current = new MultiValueMap();
|
35
|
+
return ref.current;
|
36
|
+
};
|
37
|
+
};
|
@@ -22,10 +22,20 @@ export type FormContextValue = {
|
|
22
22
|
* Whether or not the form is submitting.
|
23
23
|
*/
|
24
24
|
isSubmitting: boolean;
|
25
|
+
/**
|
26
|
+
* Whether or not the form is valid.
|
27
|
+
* This is a shortcut for `Object.keys(fieldErrors).length === 0`.
|
28
|
+
*/
|
29
|
+
isValid: boolean;
|
25
30
|
/**
|
26
31
|
* The default values of the form.
|
27
32
|
*/
|
28
33
|
defaultValues?: { [fieldName: string]: any };
|
34
|
+
/**
|
35
|
+
* Register a custom focus handler to be used when
|
36
|
+
* the field needs to receive focus due to a validation error.
|
37
|
+
*/
|
38
|
+
registerReceiveFocus: (fieldName: string, handler: () => void) => () => void;
|
29
39
|
};
|
30
40
|
|
31
41
|
export const FormContext = createContext<FormContextValue>({
|
@@ -33,4 +43,6 @@ export const FormContext = createContext<FormContextValue>({
|
|
33
43
|
clearError: () => {},
|
34
44
|
validateField: () => {},
|
35
45
|
isSubmitting: false,
|
46
|
+
isValid: true,
|
47
|
+
registerReceiveFocus: () => () => {},
|
36
48
|
});
|
package/src/server.ts
CHANGED
@@ -6,5 +6,21 @@ import { FieldErrors } from "./validation/types";
|
|
6
6
|
* The `ValidatedForm` on the frontend will automatically display the errors
|
7
7
|
* if this is returned from the action.
|
8
8
|
*/
|
9
|
-
export const validationError = (
|
10
|
-
|
9
|
+
export const validationError = (
|
10
|
+
errors: FieldErrors,
|
11
|
+
submittedData?: unknown
|
12
|
+
) => {
|
13
|
+
if (submittedData) {
|
14
|
+
return json(
|
15
|
+
{
|
16
|
+
fieldErrors: {
|
17
|
+
...errors,
|
18
|
+
_submittedData: submittedData,
|
19
|
+
},
|
20
|
+
},
|
21
|
+
{ status: 422 }
|
22
|
+
);
|
23
|
+
}
|
24
|
+
|
25
|
+
return json({ fieldErrors: errors }, { status: 422 });
|
26
|
+
};
|