remix-validated-form 3.0.0 → 3.2.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/README.md +24 -11
- package/browser/ValidatedForm.d.ts +6 -1
- package/browser/ValidatedForm.js +52 -1
- package/browser/hooks.d.ts +25 -1
- package/browser/hooks.js +44 -13
- 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 +25 -1
- package/browser/internal/formContext.js +5 -0
- package/browser/internal/getInputProps.d.ts +21 -0
- package/browser/internal/getInputProps.js +39 -0
- package/browser/internal/test.d.ts +1 -0
- package/browser/internal/test.js +10 -0
- package/browser/server.d.ts +1 -1
- package/browser/server.js +11 -1
- package/browser/validation/types.d.ts +2 -0
- package/build/ValidatedForm.d.ts +6 -1
- package/build/ValidatedForm.js +52 -1
- package/build/hooks.d.ts +25 -1
- package/build/hooks.js +43 -12
- package/build/internal/SingleTypeMultiValueMap.d.ts +8 -0
- package/build/internal/SingleTypeMultiValueMap.js +45 -0
- package/build/internal/formContext.d.ts +25 -1
- package/build/internal/formContext.js +5 -0
- package/build/internal/getInputProps.d.ts +21 -0
- package/build/internal/getInputProps.js +43 -0
- package/build/internal/test.d.ts +1 -0
- package/build/internal/test.js +12 -0
- package/build/server.d.ts +1 -1
- package/build/server.js +11 -1
- package/build/validation/types.d.ts +2 -0
- package/package.json +1 -1
- package/src/ValidatedForm.tsx +73 -0
- package/src/hooks.ts +77 -9
- package/src/internal/SingleTypeMultiValueMap.ts +37 -0
- package/src/internal/formContext.ts +30 -1
- package/src/internal/getInputProps.ts +73 -0
- package/src/server.ts +18 -2
- package/src/validation/types.ts +8 -0
package/src/hooks.ts
CHANGED
@@ -1,7 +1,12 @@
|
|
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
|
+
import {
|
6
|
+
createGetInputProps,
|
7
|
+
GetInputProps,
|
8
|
+
ValidationBehaviorOptions,
|
9
|
+
} from "./internal/getInputProps";
|
5
10
|
|
6
11
|
export type FieldProps = {
|
7
12
|
/**
|
@@ -20,17 +25,59 @@ export type FieldProps = {
|
|
20
25
|
* The default value of the field, if there is one.
|
21
26
|
*/
|
22
27
|
defaultValue?: any;
|
28
|
+
/**
|
29
|
+
* Whether or not the field has been touched.
|
30
|
+
*/
|
31
|
+
touched: boolean;
|
32
|
+
/**
|
33
|
+
* Helper to set the touched state of the field.
|
34
|
+
*/
|
35
|
+
setTouched: (touched: boolean) => void;
|
36
|
+
/**
|
37
|
+
* Helper to get all the props necessary for a regular input.
|
38
|
+
*/
|
39
|
+
getInputProps: GetInputProps;
|
23
40
|
};
|
24
41
|
|
25
42
|
/**
|
26
43
|
* Provides the data and helpers necessary to set up a field.
|
27
44
|
*/
|
28
|
-
export const useField = (
|
29
|
-
|
30
|
-
|
45
|
+
export const useField = (
|
46
|
+
name: string,
|
47
|
+
options?: {
|
48
|
+
/**
|
49
|
+
* Allows you to configure a custom function that will be called
|
50
|
+
* when the input needs to receive focus due to a validation error.
|
51
|
+
* This is useful for custom components that use a hidden input.
|
52
|
+
*/
|
53
|
+
handleReceiveFocus?: () => void;
|
54
|
+
/**
|
55
|
+
* Allows you to specify when a field gets validated (when using getInputProps)
|
56
|
+
*/
|
57
|
+
validationBehavior?: Partial<ValidationBehaviorOptions>;
|
58
|
+
}
|
59
|
+
): FieldProps => {
|
60
|
+
const {
|
61
|
+
fieldErrors,
|
62
|
+
clearError,
|
63
|
+
validateField,
|
64
|
+
defaultValues,
|
65
|
+
registerReceiveFocus,
|
66
|
+
touchedFields,
|
67
|
+
setFieldTouched,
|
68
|
+
hasBeenSubmitted,
|
69
|
+
} = useContext(FormContext);
|
70
|
+
|
71
|
+
const isTouched = !!touchedFields[name];
|
72
|
+
const { handleReceiveFocus } = options ?? {};
|
73
|
+
|
74
|
+
useEffect(() => {
|
75
|
+
if (handleReceiveFocus)
|
76
|
+
return registerReceiveFocus(name, handleReceiveFocus);
|
77
|
+
}, [handleReceiveFocus, name, registerReceiveFocus]);
|
31
78
|
|
32
|
-
const field = useMemo<FieldProps>(
|
33
|
-
|
79
|
+
const field = useMemo<FieldProps>(() => {
|
80
|
+
const helpers = {
|
34
81
|
error: fieldErrors[name],
|
35
82
|
clearError: () => {
|
36
83
|
clearError(name);
|
@@ -39,9 +86,30 @@ export const useField = (name: string): FieldProps => {
|
|
39
86
|
defaultValue: defaultValues
|
40
87
|
? get(defaultValues, toPath(name), undefined)
|
41
88
|
: undefined,
|
42
|
-
|
43
|
-
|
44
|
-
|
89
|
+
touched: isTouched,
|
90
|
+
setTouched: (touched: boolean) => setFieldTouched(name, touched),
|
91
|
+
};
|
92
|
+
const getInputProps = createGetInputProps({
|
93
|
+
...helpers,
|
94
|
+
name,
|
95
|
+
hasBeenSubmitted,
|
96
|
+
validationBehavior: options?.validationBehavior,
|
97
|
+
});
|
98
|
+
return {
|
99
|
+
...helpers,
|
100
|
+
getInputProps,
|
101
|
+
};
|
102
|
+
}, [
|
103
|
+
fieldErrors,
|
104
|
+
name,
|
105
|
+
defaultValues,
|
106
|
+
isTouched,
|
107
|
+
hasBeenSubmitted,
|
108
|
+
options?.validationBehavior,
|
109
|
+
clearError,
|
110
|
+
validateField,
|
111
|
+
setFieldTouched,
|
112
|
+
]);
|
45
113
|
|
46
114
|
return field;
|
47
115
|
};
|
@@ -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
|
+
};
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { createContext } from "react";
|
2
|
-
import { FieldErrors } from "../validation/types";
|
2
|
+
import { FieldErrors, TouchedFields } from "../validation/types";
|
3
3
|
|
4
4
|
export type FormContextValue = {
|
5
5
|
/**
|
@@ -22,10 +22,34 @@ export type FormContextValue = {
|
|
22
22
|
* Whether or not the form is submitting.
|
23
23
|
*/
|
24
24
|
isSubmitting: boolean;
|
25
|
+
/**
|
26
|
+
* Whether or not a submission has been attempted.
|
27
|
+
* This is true once the form has been submitted, even if there were validation errors.
|
28
|
+
* Resets to false when the form is reset.
|
29
|
+
*/
|
30
|
+
hasBeenSubmitted: boolean;
|
31
|
+
/**
|
32
|
+
* Whether or not the form is valid.
|
33
|
+
* This is a shortcut for `Object.keys(fieldErrors).length === 0`.
|
34
|
+
*/
|
35
|
+
isValid: boolean;
|
25
36
|
/**
|
26
37
|
* The default values of the form.
|
27
38
|
*/
|
28
39
|
defaultValues?: { [fieldName: string]: any };
|
40
|
+
/**
|
41
|
+
* Register a custom focus handler to be used when
|
42
|
+
* the field needs to receive focus due to a validation error.
|
43
|
+
*/
|
44
|
+
registerReceiveFocus: (fieldName: string, handler: () => void) => () => void;
|
45
|
+
/**
|
46
|
+
* Any fields that have been touched by the user.
|
47
|
+
*/
|
48
|
+
touchedFields: TouchedFields;
|
49
|
+
/**
|
50
|
+
* Change the touched state of the specified field.
|
51
|
+
*/
|
52
|
+
setFieldTouched: (fieldName: string, touched: boolean) => void;
|
29
53
|
};
|
30
54
|
|
31
55
|
export const FormContext = createContext<FormContextValue>({
|
@@ -33,4 +57,9 @@ export const FormContext = createContext<FormContextValue>({
|
|
33
57
|
clearError: () => {},
|
34
58
|
validateField: () => {},
|
35
59
|
isSubmitting: false,
|
60
|
+
hasBeenSubmitted: false,
|
61
|
+
isValid: true,
|
62
|
+
registerReceiveFocus: () => () => {},
|
63
|
+
touchedFields: {},
|
64
|
+
setFieldTouched: () => {},
|
36
65
|
});
|
@@ -0,0 +1,73 @@
|
|
1
|
+
export type ValidationBehavior = "onBlur" | "onChange" | "onSubmit";
|
2
|
+
|
3
|
+
export type ValidationBehaviorOptions = {
|
4
|
+
initial: ValidationBehavior;
|
5
|
+
whenTouched: ValidationBehavior;
|
6
|
+
whenSubmitted: ValidationBehavior;
|
7
|
+
};
|
8
|
+
|
9
|
+
export type CreateGetInputPropsOptions = {
|
10
|
+
clearError: () => void;
|
11
|
+
validate: () => void;
|
12
|
+
defaultValue?: any;
|
13
|
+
touched: boolean;
|
14
|
+
setTouched: (touched: boolean) => void;
|
15
|
+
hasBeenSubmitted: boolean;
|
16
|
+
validationBehavior?: Partial<ValidationBehaviorOptions>;
|
17
|
+
name: string;
|
18
|
+
};
|
19
|
+
|
20
|
+
type HandledProps = "name" | "defaultValue";
|
21
|
+
type Callbacks = "onChange" | "onBlur";
|
22
|
+
|
23
|
+
export type GetInputProps = <T extends Record<string, any>>(
|
24
|
+
props?: Omit<T, HandledProps | Callbacks> & Partial<Pick<T, Callbacks>>
|
25
|
+
) => T;
|
26
|
+
|
27
|
+
const defaultValidationBehavior: ValidationBehaviorOptions = {
|
28
|
+
initial: "onBlur",
|
29
|
+
whenTouched: "onChange",
|
30
|
+
whenSubmitted: "onChange",
|
31
|
+
};
|
32
|
+
|
33
|
+
export const createGetInputProps = ({
|
34
|
+
clearError,
|
35
|
+
validate,
|
36
|
+
defaultValue,
|
37
|
+
touched,
|
38
|
+
setTouched,
|
39
|
+
hasBeenSubmitted,
|
40
|
+
validationBehavior,
|
41
|
+
name,
|
42
|
+
}: CreateGetInputPropsOptions): GetInputProps => {
|
43
|
+
const validationBehaviors = {
|
44
|
+
...defaultValidationBehavior,
|
45
|
+
...validationBehavior,
|
46
|
+
};
|
47
|
+
|
48
|
+
return <T extends Record<string, any>>(props = {} as any) => {
|
49
|
+
const behavior = hasBeenSubmitted
|
50
|
+
? validationBehaviors.whenSubmitted
|
51
|
+
: touched
|
52
|
+
? validationBehaviors.whenTouched
|
53
|
+
: validationBehaviors.initial;
|
54
|
+
|
55
|
+
const result: T = {
|
56
|
+
...props,
|
57
|
+
onChange: (...args: unknown[]) => {
|
58
|
+
if (behavior === "onChange") validate();
|
59
|
+
else clearError();
|
60
|
+
return props?.onChange?.(...args);
|
61
|
+
},
|
62
|
+
onBlur: (...args: unknown[]) => {
|
63
|
+
if (behavior === "onBlur") validate();
|
64
|
+
setTouched(true);
|
65
|
+
return props?.onBlur?.(...args);
|
66
|
+
},
|
67
|
+
defaultValue,
|
68
|
+
name,
|
69
|
+
};
|
70
|
+
|
71
|
+
return result;
|
72
|
+
};
|
73
|
+
};
|
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
|
+
};
|
package/src/validation/types.ts
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
export type FieldErrors = Record<string, string>;
|
2
2
|
|
3
|
+
export type TouchedFields = Record<string, boolean>;
|
4
|
+
|
3
5
|
export type FieldErrorsWithData = FieldErrors & { _submittedData: any };
|
4
6
|
|
5
7
|
export type GenericObject = { [key: string]: any };
|
@@ -26,3 +28,9 @@ export type Validator<DataType> = {
|
|
26
28
|
field: string
|
27
29
|
) => ValidateFieldResult;
|
28
30
|
};
|
31
|
+
|
32
|
+
export type ValidatorData<T extends Validator<any>> = T extends Validator<
|
33
|
+
infer U
|
34
|
+
>
|
35
|
+
? U
|
36
|
+
: never;
|