remix-validated-form 4.1.1 → 4.1.2
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/internal/hooks-valtio.d.ts +18 -0
- package/browser/internal/hooks-valtio.js +110 -0
- package/browser/internal/hooks-zustand.d.ts +16 -0
- package/browser/internal/hooks-zustand.js +100 -0
- package/browser/internal/hydratable.d.ts +14 -0
- package/browser/internal/hydratable.js +14 -0
- package/browser/internal/immerMiddleware.d.ts +6 -0
- package/browser/internal/immerMiddleware.js +7 -0
- package/browser/internal/logic/setInputValueInForm.d.ts +1 -0
- package/browser/internal/logic/setInputValueInForm.js +40 -0
- package/browser/internal/state-valtio.d.ts +62 -0
- package/browser/internal/state-valtio.js +69 -0
- package/browser/internal/state-zustand.d.ts +47 -0
- package/browser/internal/state-zustand.js +85 -0
- package/browser/internal/util.js +1 -1
- package/build/internal/hooks-valtio.d.ts +18 -0
- package/build/internal/hooks-valtio.js +128 -0
- package/build/internal/hooks-zustand.d.ts +16 -0
- package/build/internal/hooks-zustand.js +117 -0
- package/build/internal/hydratable.d.ts +14 -0
- package/build/internal/hydratable.js +17 -0
- package/build/internal/immerMiddleware.d.ts +6 -0
- package/build/internal/immerMiddleware.js +14 -0
- package/build/internal/logic/setFieldValue.js +3 -3
- package/build/internal/logic/setInputValueInForm.d.ts +1 -0
- package/build/internal/logic/setInputValueInForm.js +47 -0
- package/build/internal/state-valtio.d.ts +62 -0
- package/build/internal/state-valtio.js +83 -0
- package/build/internal/state-zustand.d.ts +47 -0
- package/build/internal/state-zustand.js +91 -0
- package/build/internal/util.js +5 -2
- package/package.json +1 -1
- package/src/internal/util.ts +1 -1
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@4.1.
|
3
|
+
> remix-validated-form@4.1.1 build:browser
|
4
4
|
> tsc --module ESNext --outDir ./browser
|
5
5
|
|
6
6
|
|
7
|
-
> remix-validated-form@4.1.
|
7
|
+
> remix-validated-form@4.1.1 build:main
|
8
8
|
> tsc --module CommonJS --outDir ./build
|
9
9
|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { useUpdateAtom } from "jotai/utils";
|
2
|
+
import { FieldErrors, ValidationErrorResponseData } from "..";
|
3
|
+
import { InternalFormContextValue } from "./formContext";
|
4
|
+
import { Hydratable } from "./hydratable";
|
5
|
+
export declare const useInternalFormContext: (formId?: string | symbol | undefined, hookName?: string | undefined) => InternalFormContextValue;
|
6
|
+
export declare function useErrorResponseForForm({ fetcher, subaction, formId, }: InternalFormContextValue): ValidationErrorResponseData | null;
|
7
|
+
export declare const useFieldErrorsForForm: (context: InternalFormContextValue) => Hydratable<FieldErrors | undefined>;
|
8
|
+
export declare const useDefaultValuesFromLoader: ({ formId, }: InternalFormContextValue) => any;
|
9
|
+
export declare const useDefaultValuesForForm: (context: InternalFormContextValue) => Hydratable<{
|
10
|
+
[fieldName: string]: any;
|
11
|
+
}>;
|
12
|
+
export declare const useHasActiveFormSubmit: ({ fetcher, }: InternalFormContextValue) => boolean;
|
13
|
+
export declare const useFieldTouched: (name: string, { formId }: InternalFormContextValue) => boolean;
|
14
|
+
export declare const useFieldError: (name: string, context: InternalFormContextValue) => string | undefined;
|
15
|
+
export declare const useFieldDefaultValue: (name: string, context: InternalFormContextValue) => any;
|
16
|
+
export declare const useFormUpdateAtom: typeof useUpdateAtom;
|
17
|
+
export declare const useClearError: (context: InternalFormContextValue) => (name: string) => void;
|
18
|
+
export declare const useSetTouched: (context: InternalFormContextValue) => (name: string, touched: boolean) => void;
|
@@ -0,0 +1,110 @@
|
|
1
|
+
import { useActionData, useMatches, useTransition } from "@remix-run/react";
|
2
|
+
import { useUpdateAtom } from "jotai/utils";
|
3
|
+
import lodashGet from "lodash/get";
|
4
|
+
import { useCallback, useContext } from "react";
|
5
|
+
import invariant from "tiny-invariant";
|
6
|
+
import { formDefaultValuesKey } from "./constants";
|
7
|
+
import { InternalFormContext } from "./formContext";
|
8
|
+
import { hydratable } from "./hydratable";
|
9
|
+
import { ATOM_SCOPE, clearErrorAtom, formRegistry, setTouchedAtom, } from "./state";
|
10
|
+
import { useFormData } from "./state-valtio";
|
11
|
+
export const useInternalFormContext = (formId, hookName) => {
|
12
|
+
const formContext = useContext(InternalFormContext);
|
13
|
+
if (formId)
|
14
|
+
return { formId };
|
15
|
+
if (formContext)
|
16
|
+
return formContext;
|
17
|
+
throw new Error(`Unable to determine form for ${hookName}. Please use it inside a form or pass a 'formId'.`);
|
18
|
+
};
|
19
|
+
export function useErrorResponseForForm({ fetcher, subaction, formId, }) {
|
20
|
+
var _a;
|
21
|
+
const actionData = useActionData();
|
22
|
+
if (fetcher) {
|
23
|
+
if ((_a = fetcher.data) === null || _a === void 0 ? void 0 : _a.fieldErrors)
|
24
|
+
return fetcher.data;
|
25
|
+
return null;
|
26
|
+
}
|
27
|
+
if (!(actionData === null || actionData === void 0 ? void 0 : actionData.fieldErrors))
|
28
|
+
return null;
|
29
|
+
// If there's an explicit id, we should ignore data that has the wrong id
|
30
|
+
if (typeof formId === "string" && actionData.formId)
|
31
|
+
return actionData.formId === formId ? actionData : null;
|
32
|
+
if ((!subaction && !actionData.subaction) ||
|
33
|
+
actionData.subaction === subaction)
|
34
|
+
return actionData;
|
35
|
+
return null;
|
36
|
+
}
|
37
|
+
export const useFieldErrorsForForm = (context) => {
|
38
|
+
const response = useErrorResponseForForm(context);
|
39
|
+
const form = useFormData(context.formId);
|
40
|
+
return hydratable.from(response === null || response === void 0 ? void 0 : response.fieldErrors, form.hydrated);
|
41
|
+
};
|
42
|
+
export const useDefaultValuesFromLoader = ({ formId, }) => {
|
43
|
+
const matches = useMatches();
|
44
|
+
if (typeof formId === "string") {
|
45
|
+
const dataKey = formDefaultValuesKey(formId);
|
46
|
+
// If multiple loaders declare the same default values,
|
47
|
+
// we should use the data from the deepest route.
|
48
|
+
const match = matches
|
49
|
+
.reverse()
|
50
|
+
.find((match) => match.data && dataKey in match.data);
|
51
|
+
return match === null || match === void 0 ? void 0 : match.data[dataKey];
|
52
|
+
}
|
53
|
+
return null;
|
54
|
+
};
|
55
|
+
export const useDefaultValuesForForm = (context) => {
|
56
|
+
const { formId, defaultValuesProp } = context;
|
57
|
+
const form = useFormData(formId);
|
58
|
+
const errorResponse = useErrorResponseForForm(context);
|
59
|
+
const defaultValuesFromLoader = useDefaultValuesFromLoader(context);
|
60
|
+
// Typical flow is:
|
61
|
+
// - Default values only available from props or server
|
62
|
+
// - Props have a higher priority than server
|
63
|
+
// - State gets hydrated with default values
|
64
|
+
// - After submit, we may need to use values from the error
|
65
|
+
if (form.hydrated)
|
66
|
+
return hydratable.hydratedData();
|
67
|
+
if (errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.repopulateFields) {
|
68
|
+
invariant(typeof errorResponse.repopulateFields === "object", "repopulateFields returned something other than an object");
|
69
|
+
return hydratable.serverData(errorResponse.repopulateFields);
|
70
|
+
}
|
71
|
+
if (defaultValuesProp)
|
72
|
+
return hydratable.serverData(defaultValuesProp);
|
73
|
+
return hydratable.serverData(defaultValuesFromLoader);
|
74
|
+
};
|
75
|
+
export const useHasActiveFormSubmit = ({ fetcher, }) => {
|
76
|
+
const transition = useTransition();
|
77
|
+
const hasActiveSubmission = fetcher
|
78
|
+
? fetcher.state === "submitting"
|
79
|
+
: !!transition.submission;
|
80
|
+
return hasActiveSubmission;
|
81
|
+
};
|
82
|
+
export const useFieldTouched = (name, { formId }) => {
|
83
|
+
const form = useFormData(formId);
|
84
|
+
return form.touchedFields[name];
|
85
|
+
};
|
86
|
+
export const useFieldError = (name, context) => {
|
87
|
+
const fieldErrors = useFieldErrorsForForm(context);
|
88
|
+
const form = useFormData(context.formId);
|
89
|
+
return fieldErrors
|
90
|
+
.map((fieldErrors) => fieldErrors === null || fieldErrors === void 0 ? void 0 : fieldErrors[name])
|
91
|
+
.hydrateTo(form.fieldErrors[name]);
|
92
|
+
};
|
93
|
+
export const useFieldDefaultValue = (name, context) => {
|
94
|
+
const defaultValues = useDefaultValuesForForm(context);
|
95
|
+
const state = useFormData(context.formId);
|
96
|
+
return defaultValues.map((val) => lodashGet(val, name)).hydrateTo(state);
|
97
|
+
};
|
98
|
+
export const useFormUpdateAtom = (atom) => useUpdateAtom(atom, ATOM_SCOPE);
|
99
|
+
export const useClearError = (context) => {
|
100
|
+
const clearError = useFormUpdateAtom(clearErrorAtom);
|
101
|
+
return useCallback((name) => {
|
102
|
+
clearError({ name, formAtom: formRegistry(context.formId) });
|
103
|
+
}, [clearError, context.formId]);
|
104
|
+
};
|
105
|
+
export const useSetTouched = (context) => {
|
106
|
+
const setTouched = useFormUpdateAtom(setTouchedAtom);
|
107
|
+
return useCallback((name, touched) => {
|
108
|
+
setTouched({ name, formAtom: formRegistry(context.formId), touched });
|
109
|
+
}, [setTouched, context.formId]);
|
110
|
+
};
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { FieldErrors, ValidationErrorResponseData } from "..";
|
2
|
+
import { InternalFormContextValue } from "./formContext";
|
3
|
+
import { Hydratable } from "./hydratable";
|
4
|
+
export declare const useInternalFormContext: (formId?: string | symbol | undefined, hookName?: string | undefined) => InternalFormContextValue;
|
5
|
+
export declare function useErrorResponseForForm({ fetcher, subaction, formId, }: InternalFormContextValue): ValidationErrorResponseData | null;
|
6
|
+
export declare const useFieldErrorsForForm: (context: InternalFormContextValue) => Hydratable<FieldErrors | undefined>;
|
7
|
+
export declare const useDefaultValuesFromLoader: ({ formId, }: InternalFormContextValue) => any;
|
8
|
+
export declare const useDefaultValuesForForm: (context: InternalFormContextValue) => Hydratable<{
|
9
|
+
[fieldName: string]: any;
|
10
|
+
}>;
|
11
|
+
export declare const useHasActiveFormSubmit: ({ fetcher, }: InternalFormContextValue) => boolean;
|
12
|
+
export declare const useFieldTouched: (name: string, { formId }: InternalFormContextValue) => boolean;
|
13
|
+
export declare const useFieldError: (name: string, context: InternalFormContextValue) => string | undefined;
|
14
|
+
export declare const useFieldDefaultValue: (name: string, context: InternalFormContextValue) => any;
|
15
|
+
export declare const useClearError: (context: InternalFormContextValue) => (name: string) => void;
|
16
|
+
export declare const useSetTouched: (context: InternalFormContextValue) => (name: string, touched: boolean) => void;
|
@@ -0,0 +1,100 @@
|
|
1
|
+
import { useActionData, useMatches, useTransition } from "@remix-run/react";
|
2
|
+
import lodashGet from "lodash/get";
|
3
|
+
import { useContext } from "react";
|
4
|
+
import invariant from "tiny-invariant";
|
5
|
+
import { formDefaultValuesKey } from "./constants";
|
6
|
+
import { InternalFormContext } from "./formContext";
|
7
|
+
import { hydratable } from "./hydratable";
|
8
|
+
import { useStore } from "./state-zustand";
|
9
|
+
export const useInternalFormContext = (formId, hookName) => {
|
10
|
+
const formContext = useContext(InternalFormContext);
|
11
|
+
if (formId)
|
12
|
+
return { formId };
|
13
|
+
if (formContext)
|
14
|
+
return formContext;
|
15
|
+
throw new Error(`Unable to determine form for ${hookName}. Please use it inside a form or pass a 'formId'.`);
|
16
|
+
};
|
17
|
+
export function useErrorResponseForForm({ fetcher, subaction, formId, }) {
|
18
|
+
var _a;
|
19
|
+
const actionData = useActionData();
|
20
|
+
if (fetcher) {
|
21
|
+
if ((_a = fetcher.data) === null || _a === void 0 ? void 0 : _a.fieldErrors)
|
22
|
+
return fetcher.data;
|
23
|
+
return null;
|
24
|
+
}
|
25
|
+
if (!(actionData === null || actionData === void 0 ? void 0 : actionData.fieldErrors))
|
26
|
+
return null;
|
27
|
+
// If there's an explicit id, we should ignore data that has the wrong id
|
28
|
+
if (typeof formId === "string" && actionData.formId)
|
29
|
+
return actionData.formId === formId ? actionData : null;
|
30
|
+
if ((!subaction && !actionData.subaction) ||
|
31
|
+
actionData.subaction === subaction)
|
32
|
+
return actionData;
|
33
|
+
return null;
|
34
|
+
}
|
35
|
+
export const useFieldErrorsForForm = (context) => {
|
36
|
+
const response = useErrorResponseForForm(context);
|
37
|
+
const hydrated = useStore((state) => state.form(context.formId).hydrated);
|
38
|
+
return hydratable.from(response === null || response === void 0 ? void 0 : response.fieldErrors, hydrated);
|
39
|
+
};
|
40
|
+
export const useDefaultValuesFromLoader = ({ formId, }) => {
|
41
|
+
const matches = useMatches();
|
42
|
+
if (typeof formId === "string") {
|
43
|
+
const dataKey = formDefaultValuesKey(formId);
|
44
|
+
// If multiple loaders declare the same default values,
|
45
|
+
// we should use the data from the deepest route.
|
46
|
+
const match = matches
|
47
|
+
.reverse()
|
48
|
+
.find((match) => match.data && dataKey in match.data);
|
49
|
+
return match === null || match === void 0 ? void 0 : match.data[dataKey];
|
50
|
+
}
|
51
|
+
return null;
|
52
|
+
};
|
53
|
+
export const useDefaultValuesForForm = (context) => {
|
54
|
+
const { formId, defaultValuesProp } = context;
|
55
|
+
const hydrated = useStore((state) => state.form(formId).hydrated);
|
56
|
+
const errorResponse = useErrorResponseForForm(context);
|
57
|
+
const defaultValuesFromLoader = useDefaultValuesFromLoader(context);
|
58
|
+
// Typical flow is:
|
59
|
+
// - Default values only available from props or server
|
60
|
+
// - Props have a higher priority than server
|
61
|
+
// - State gets hydrated with default values
|
62
|
+
// - After submit, we may need to use values from the error
|
63
|
+
if (hydrated)
|
64
|
+
return hydratable.hydratedData();
|
65
|
+
if (errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.repopulateFields) {
|
66
|
+
invariant(typeof errorResponse.repopulateFields === "object", "repopulateFields returned something other than an object");
|
67
|
+
return hydratable.serverData(errorResponse.repopulateFields);
|
68
|
+
}
|
69
|
+
if (defaultValuesProp)
|
70
|
+
return hydratable.serverData(defaultValuesProp);
|
71
|
+
return hydratable.serverData(defaultValuesFromLoader);
|
72
|
+
};
|
73
|
+
export const useHasActiveFormSubmit = ({ fetcher, }) => {
|
74
|
+
const transition = useTransition();
|
75
|
+
const hasActiveSubmission = fetcher
|
76
|
+
? fetcher.state === "submitting"
|
77
|
+
: !!transition.submission;
|
78
|
+
return hasActiveSubmission;
|
79
|
+
};
|
80
|
+
export const useFieldTouched = (name, { formId }) => {
|
81
|
+
return useStore((state) => state.form(formId).touchedFields[name]);
|
82
|
+
};
|
83
|
+
export const useFieldError = (name, context) => {
|
84
|
+
const state = useStore((state) => state.form(context.formId).fieldErrors[name]);
|
85
|
+
return useFieldErrorsForForm(context)
|
86
|
+
.map((fieldErrors) => fieldErrors === null || fieldErrors === void 0 ? void 0 : fieldErrors[name])
|
87
|
+
.hydrateTo(state);
|
88
|
+
};
|
89
|
+
export const useFieldDefaultValue = (name, context) => {
|
90
|
+
const state = useStore((state) => state.form(context.formId).defaultValues[name]);
|
91
|
+
return useDefaultValuesForForm(context)
|
92
|
+
.map((val) => lodashGet(val, name))
|
93
|
+
.hydrateTo(state);
|
94
|
+
};
|
95
|
+
export const useClearError = (context) => {
|
96
|
+
return useStore((state) => state.helpers(context.formId).clearError);
|
97
|
+
};
|
98
|
+
export const useSetTouched = (context) => {
|
99
|
+
return useStore((state) => state.helpers(context.formId).setTouched);
|
100
|
+
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
/**
|
2
|
+
* The purpose of this type is to simplify the logic
|
3
|
+
* around data that needs to come from the server initially,
|
4
|
+
* but from the internal state after hydration.
|
5
|
+
*/
|
6
|
+
export declare type Hydratable<T> = {
|
7
|
+
hydrateTo: (data: T) => T;
|
8
|
+
map: <U>(fn: (data: T) => U) => Hydratable<U>;
|
9
|
+
};
|
10
|
+
export declare const hydratable: {
|
11
|
+
serverData: <T>(data: T) => Hydratable<T>;
|
12
|
+
hydratedData: <T_1>() => Hydratable<T_1>;
|
13
|
+
from: <T_2>(data: T_2, hydrated: boolean) => Hydratable<T_2>;
|
14
|
+
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
const serverData = (data) => ({
|
2
|
+
hydrateTo: () => data,
|
3
|
+
map: (fn) => serverData(fn(data)),
|
4
|
+
});
|
5
|
+
const hydratedData = () => ({
|
6
|
+
hydrateTo: (hydratedData) => hydratedData,
|
7
|
+
map: () => hydratedData(),
|
8
|
+
});
|
9
|
+
const from = (data, hydrated) => hydrated ? hydratedData() : serverData(data);
|
10
|
+
export const hydratable = {
|
11
|
+
serverData,
|
12
|
+
hydratedData,
|
13
|
+
from,
|
14
|
+
};
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import { Draft } from "immer";
|
2
|
+
import { State, StateCreator } from "zustand";
|
3
|
+
declare type TImmerConfigFn<T extends State> = (partial: ((draft: Draft<T>) => void) | T, replace?: boolean) => void;
|
4
|
+
declare type TImmerConfig<T extends State> = StateCreator<T, TImmerConfigFn<T>>;
|
5
|
+
export declare const immer: <T extends object>(config: TImmerConfig<T>) => StateCreator<T, import("zustand").SetState<T>, import("zustand").GetState<T>, import("zustand").StoreApi<T>>;
|
6
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const setInputValueInForm: (formElement: HTMLFormElement, name: string, value: unknown) => void;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import invariant from "tiny-invariant";
|
2
|
+
import { getCheckboxChecked } from "./getCheckboxChecked";
|
3
|
+
import { getRadioChecked } from "./getRadioChecked";
|
4
|
+
const setElementValue = (element, value, name) => {
|
5
|
+
if (element instanceof HTMLSelectElement && element.multiple) {
|
6
|
+
invariant(Array.isArray(value), "Must specify an array to set the value for a multi-select");
|
7
|
+
for (const option of element.options) {
|
8
|
+
option.selected = value.includes(option.value);
|
9
|
+
}
|
10
|
+
return;
|
11
|
+
}
|
12
|
+
if (element instanceof HTMLInputElement && element.type === "checkbox") {
|
13
|
+
const newChecked = getCheckboxChecked(element.value, value);
|
14
|
+
invariant(newChecked !== undefined, `Unable to determine if checkbox should be checked. Provided value was ${value} for checkbox ${name}.`);
|
15
|
+
element.checked = newChecked;
|
16
|
+
return;
|
17
|
+
}
|
18
|
+
if (element instanceof HTMLInputElement && element.type === "radio") {
|
19
|
+
const newChecked = getRadioChecked(element.value, value);
|
20
|
+
invariant(newChecked !== undefined, `Unable to determine if radio should be checked. Provided value was ${value} for radio ${name}.`);
|
21
|
+
element.checked = newChecked;
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
invariant(typeof value === "string", `Invalid value for field "${name}" which is an ${element.constructor.name}. Expected string but received ${typeof value}`);
|
25
|
+
const input = element;
|
26
|
+
input.value = value;
|
27
|
+
};
|
28
|
+
export const setInputValueInForm = (formElement, name, value) => {
|
29
|
+
const controlElement = formElement.elements.namedItem(name);
|
30
|
+
if (!controlElement)
|
31
|
+
return;
|
32
|
+
if (controlElement instanceof RadioNodeList) {
|
33
|
+
for (const element of controlElement) {
|
34
|
+
setElementValue(element, value, name);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
setElementValue(controlElement, value, name);
|
39
|
+
}
|
40
|
+
};
|
@@ -0,0 +1,62 @@
|
|
1
|
+
import { FieldErrors, TouchedFields } from "..";
|
2
|
+
export declare type InternalFormState = {
|
3
|
+
hydrated: boolean;
|
4
|
+
fieldErrors: FieldErrors;
|
5
|
+
isSubmitting: boolean;
|
6
|
+
hasBeenSubmitted: boolean;
|
7
|
+
touchedFields: TouchedFields;
|
8
|
+
action?: string;
|
9
|
+
subaction?: string;
|
10
|
+
defaultValues: {
|
11
|
+
[fieldName: string]: any;
|
12
|
+
};
|
13
|
+
validateField: (fieldName: string) => Promise<string | null>;
|
14
|
+
registerReceiveFocus: (fieldName: string, handler: () => void) => () => void;
|
15
|
+
setFieldValue: (fieldName: string, value: unknown) => void;
|
16
|
+
};
|
17
|
+
declare type SyncFormArgs = {
|
18
|
+
defaultValues?: {
|
19
|
+
[fieldName: string]: any;
|
20
|
+
};
|
21
|
+
action?: string;
|
22
|
+
subaction?: string;
|
23
|
+
validateField: InternalFormState["validateField"];
|
24
|
+
registerReceiveFocus: InternalFormState["registerReceiveFocus"];
|
25
|
+
setFieldValueForForm: InternalFormState["setFieldValue"];
|
26
|
+
};
|
27
|
+
declare type StoreState = {
|
28
|
+
forms: {
|
29
|
+
[formId: string | symbol]: InternalFormState;
|
30
|
+
};
|
31
|
+
};
|
32
|
+
export declare const state: StoreState;
|
33
|
+
export declare const registerFormSlice: (formId: string | symbol, { registerReceiveFocus, setFieldValueForForm, validateField, action, defaultValues, subaction, }: SyncFormArgs) => void;
|
34
|
+
export declare const unregisterFormSlice: (formId: string | symbol) => void;
|
35
|
+
export declare const useFormData: (formId: string | symbol) => {
|
36
|
+
readonly hydrated: boolean;
|
37
|
+
readonly fieldErrors: {
|
38
|
+
readonly [x: string]: string;
|
39
|
+
};
|
40
|
+
readonly isSubmitting: boolean;
|
41
|
+
readonly hasBeenSubmitted: boolean;
|
42
|
+
readonly touchedFields: {
|
43
|
+
readonly [x: string]: boolean;
|
44
|
+
};
|
45
|
+
readonly action?: string | undefined;
|
46
|
+
readonly subaction?: string | undefined;
|
47
|
+
readonly defaultValues: {
|
48
|
+
readonly [x: string]: any;
|
49
|
+
};
|
50
|
+
readonly validateField: (fieldName: string) => Promise<string | null>;
|
51
|
+
readonly registerReceiveFocus: (fieldName: string, handler: () => void) => () => void;
|
52
|
+
readonly setFieldValue: (fieldName: string, value: unknown) => void;
|
53
|
+
};
|
54
|
+
export declare const startSubmit: (formId: string | symbol) => void;
|
55
|
+
export declare const endSubmit: (formId: string | symbol) => void;
|
56
|
+
export declare const sync: (formId: string | symbol, { defaultValues, action, subaction, registerReceiveFocus, validateField, setFieldValueForForm, }: SyncFormArgs) => void;
|
57
|
+
export declare const clearError: (formId: string | symbol, fieldName: string) => void;
|
58
|
+
export declare const addError: (formId: string | symbol, fieldName: string, error: string) => void;
|
59
|
+
export declare const setTouched: (formId: string | symbol, fieldName: string, touched: boolean) => void;
|
60
|
+
export declare const reset: (formId: string | symbol) => void;
|
61
|
+
export declare const setFieldErrors: (formId: string | symbol, fieldErrors: FieldErrors) => void;
|
62
|
+
export {};
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import { proxy, useSnapshot } from "valtio";
|
2
|
+
export const state = proxy({ forms: {} });
|
3
|
+
export const registerFormSlice = (formId, { registerReceiveFocus, setFieldValueForForm, validateField, action, defaultValues, subaction, }) => {
|
4
|
+
state.forms[formId] = {
|
5
|
+
hydrated: true,
|
6
|
+
defaultValues: defaultValues !== null && defaultValues !== void 0 ? defaultValues : {},
|
7
|
+
fieldErrors: {},
|
8
|
+
hasBeenSubmitted: false,
|
9
|
+
isSubmitting: false,
|
10
|
+
touchedFields: {},
|
11
|
+
registerReceiveFocus,
|
12
|
+
setFieldValue: setFieldValueForForm,
|
13
|
+
validateField,
|
14
|
+
action,
|
15
|
+
subaction,
|
16
|
+
};
|
17
|
+
};
|
18
|
+
export const unregisterFormSlice = (formId) => {
|
19
|
+
delete state.forms[formId];
|
20
|
+
};
|
21
|
+
const unhydratedFormState = {
|
22
|
+
hydrated: false,
|
23
|
+
fieldErrors: {},
|
24
|
+
isSubmitting: false,
|
25
|
+
hasBeenSubmitted: false,
|
26
|
+
touchedFields: {},
|
27
|
+
defaultValues: {},
|
28
|
+
validateField: () => Promise.resolve(null),
|
29
|
+
registerReceiveFocus: () => () => { },
|
30
|
+
setFieldValue: () => { },
|
31
|
+
};
|
32
|
+
export const useFormData = (formId) => {
|
33
|
+
var _a;
|
34
|
+
const snapshot = useSnapshot(state);
|
35
|
+
return (_a = snapshot.forms[formId]) !== null && _a !== void 0 ? _a : unhydratedFormState;
|
36
|
+
};
|
37
|
+
export const startSubmit = (formId) => {
|
38
|
+
state.forms[formId].isSubmitting = true;
|
39
|
+
state.forms[formId].hasBeenSubmitted = true;
|
40
|
+
};
|
41
|
+
export const endSubmit = (formId) => {
|
42
|
+
state.forms[formId].isSubmitting = false;
|
43
|
+
};
|
44
|
+
export const sync = (formId, { defaultValues, action, subaction, registerReceiveFocus, validateField, setFieldValueForForm, }) => {
|
45
|
+
state.forms[formId].defaultValues = defaultValues !== null && defaultValues !== void 0 ? defaultValues : {};
|
46
|
+
state.forms[formId].action = action;
|
47
|
+
state.forms[formId].subaction = subaction;
|
48
|
+
state.forms[formId].registerReceiveFocus = registerReceiveFocus;
|
49
|
+
state.forms[formId].validateField = validateField;
|
50
|
+
state.forms[formId].hydrated = true;
|
51
|
+
state.forms[formId].setFieldValue = setFieldValueForForm;
|
52
|
+
};
|
53
|
+
export const clearError = (formId, fieldName) => {
|
54
|
+
delete state.forms[formId].fieldErrors[fieldName];
|
55
|
+
};
|
56
|
+
export const addError = (formId, fieldName, error) => {
|
57
|
+
state.forms[formId].fieldErrors[fieldName] = error;
|
58
|
+
};
|
59
|
+
export const setTouched = (formId, fieldName, touched) => {
|
60
|
+
state.forms[formId].touchedFields[fieldName] = touched;
|
61
|
+
};
|
62
|
+
export const reset = (formId) => {
|
63
|
+
state.forms[formId].fieldErrors = {};
|
64
|
+
state.forms[formId].touchedFields = {};
|
65
|
+
state.forms[formId].hasBeenSubmitted = false;
|
66
|
+
};
|
67
|
+
export const setFieldErrors = (formId, fieldErrors) => {
|
68
|
+
state.forms[formId].fieldErrors = fieldErrors;
|
69
|
+
};
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import { FieldErrors, TouchedFields } from "..";
|
2
|
+
export declare type InternalFormState = {
|
3
|
+
hydrated: boolean;
|
4
|
+
fieldErrors: FieldErrors;
|
5
|
+
isSubmitting: boolean;
|
6
|
+
hasBeenSubmitted: boolean;
|
7
|
+
touchedFields: TouchedFields;
|
8
|
+
action?: string;
|
9
|
+
subaction?: string;
|
10
|
+
defaultValues: {
|
11
|
+
[fieldName: string]: any;
|
12
|
+
};
|
13
|
+
validateField: (fieldName: string) => Promise<string | null>;
|
14
|
+
registerReceiveFocus: (fieldName: string, handler: () => void) => () => void;
|
15
|
+
setFieldValue: (fieldName: string, value: unknown) => void;
|
16
|
+
};
|
17
|
+
declare type Helpers = {
|
18
|
+
startSubmit: () => void;
|
19
|
+
endSubmit: () => void;
|
20
|
+
sync: (args: SyncFormArgs) => void;
|
21
|
+
clearError: (name: string) => void;
|
22
|
+
addError: (name: string, error: string) => void;
|
23
|
+
setTouched: (name: string, touched: boolean) => void;
|
24
|
+
reset: () => void;
|
25
|
+
setFieldErrors: (fieldErrors: FieldErrors) => void;
|
26
|
+
register: (init: SyncFormArgs) => void;
|
27
|
+
unregister: () => void;
|
28
|
+
};
|
29
|
+
declare type SyncFormArgs = {
|
30
|
+
defaultValues?: {
|
31
|
+
[fieldName: string]: any;
|
32
|
+
};
|
33
|
+
action?: string;
|
34
|
+
subaction?: string;
|
35
|
+
validateField: InternalFormState["validateField"];
|
36
|
+
registerReceiveFocus: InternalFormState["registerReceiveFocus"];
|
37
|
+
setFieldValueForForm: InternalFormState["setFieldValue"];
|
38
|
+
};
|
39
|
+
declare type StoreState = {
|
40
|
+
forms: {
|
41
|
+
[formId: string | symbol]: InternalFormState;
|
42
|
+
};
|
43
|
+
form: (formId: string | symbol) => InternalFormState;
|
44
|
+
helpers: (formId: string | symbol) => Helpers;
|
45
|
+
};
|
46
|
+
export declare const useStore: import("zustand").UseBoundStore<StoreState, import("zustand").StoreApi<StoreState>>;
|
47
|
+
export {};
|
@@ -0,0 +1,85 @@
|
|
1
|
+
import create from "zustand";
|
2
|
+
import { immer } from "./immerMiddleware";
|
3
|
+
const unhydratedFormState = {
|
4
|
+
hydrated: false,
|
5
|
+
fieldErrors: {},
|
6
|
+
isSubmitting: false,
|
7
|
+
hasBeenSubmitted: false,
|
8
|
+
touchedFields: {},
|
9
|
+
defaultValues: {},
|
10
|
+
validateField: () => Promise.resolve(null),
|
11
|
+
registerReceiveFocus: () => () => { },
|
12
|
+
setFieldValue: () => { },
|
13
|
+
// clearError: () => {},
|
14
|
+
// addError: () => {},
|
15
|
+
// setTouched: () => {},
|
16
|
+
// reset: () => {},
|
17
|
+
// startSubmit: () => {},
|
18
|
+
// endSubmit: () => {},
|
19
|
+
// sync: () => {},
|
20
|
+
// setFieldErrors: () => {},
|
21
|
+
};
|
22
|
+
export const useStore = create(immer((set, get) => ({
|
23
|
+
forms: {},
|
24
|
+
form: (formId) => { var _a; return (_a = get().forms[formId]) !== null && _a !== void 0 ? _a : unhydratedFormState; },
|
25
|
+
helpers: (formId) => ({
|
26
|
+
clearError: (name) => set((state) => {
|
27
|
+
delete state.forms[formId].fieldErrors[name];
|
28
|
+
}),
|
29
|
+
addError: (name, error) => set((state) => {
|
30
|
+
state.forms[formId].fieldErrors[name] = error;
|
31
|
+
}),
|
32
|
+
setTouched: (name, touched) => set((state) => {
|
33
|
+
state.forms[formId].touchedFields[name] = touched;
|
34
|
+
}),
|
35
|
+
reset: () => set((state) => {
|
36
|
+
state.forms[formId].fieldErrors = {};
|
37
|
+
state.forms[formId].touchedFields = {};
|
38
|
+
state.forms[formId].hasBeenSubmitted = false;
|
39
|
+
}),
|
40
|
+
startSubmit: () => set((state) => {
|
41
|
+
state.forms[formId].hasBeenSubmitted = true;
|
42
|
+
state.forms[formId].isSubmitting = true;
|
43
|
+
}),
|
44
|
+
endSubmit: () => set((state) => {
|
45
|
+
state.forms[formId].isSubmitting = false;
|
46
|
+
}),
|
47
|
+
setFieldErrors: (fieldErrors) => {
|
48
|
+
set((state) => {
|
49
|
+
state.forms[formId].fieldErrors = fieldErrors;
|
50
|
+
});
|
51
|
+
},
|
52
|
+
sync: ({ defaultValues, action, subaction, validateField, registerReceiveFocus, setFieldValueForForm, }) => set((state) => {
|
53
|
+
state.forms[formId].defaultValues = defaultValues !== null && defaultValues !== void 0 ? defaultValues : {};
|
54
|
+
state.forms[formId].action = action;
|
55
|
+
state.forms[formId].subaction = subaction;
|
56
|
+
state.forms[formId].registerReceiveFocus = registerReceiveFocus;
|
57
|
+
state.forms[formId].validateField = validateField;
|
58
|
+
state.forms[formId].hydrated = true;
|
59
|
+
state.forms[formId].setFieldValue = setFieldValueForForm;
|
60
|
+
}),
|
61
|
+
unregister: () => {
|
62
|
+
set((state) => {
|
63
|
+
delete state.forms[formId];
|
64
|
+
});
|
65
|
+
},
|
66
|
+
register: ({ defaultValues, action, subaction, validateField, registerReceiveFocus, setFieldValueForForm, }) => {
|
67
|
+
set((state) => {
|
68
|
+
state.forms[formId] = {
|
69
|
+
defaultValues: defaultValues !== null && defaultValues !== void 0 ? defaultValues : {},
|
70
|
+
setFieldValue: setFieldValueForForm,
|
71
|
+
registerReceiveFocus,
|
72
|
+
validateField,
|
73
|
+
action,
|
74
|
+
subaction,
|
75
|
+
hydrated: true,
|
76
|
+
fieldErrors: {},
|
77
|
+
isSubmitting: false,
|
78
|
+
hasBeenSubmitted: false,
|
79
|
+
touchedFields: {},
|
80
|
+
// helpers
|
81
|
+
};
|
82
|
+
});
|
83
|
+
},
|
84
|
+
}),
|
85
|
+
})));
|
package/browser/internal/util.js
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
import { useUpdateAtom } from "jotai/utils";
|
2
|
+
import { FieldErrors, ValidationErrorResponseData } from "..";
|
3
|
+
import { InternalFormContextValue } from "./formContext";
|
4
|
+
import { Hydratable } from "./hydratable";
|
5
|
+
export declare const useInternalFormContext: (formId?: string | symbol | undefined, hookName?: string | undefined) => InternalFormContextValue;
|
6
|
+
export declare function useErrorResponseForForm({ fetcher, subaction, formId, }: InternalFormContextValue): ValidationErrorResponseData | null;
|
7
|
+
export declare const useFieldErrorsForForm: (context: InternalFormContextValue) => Hydratable<FieldErrors | undefined>;
|
8
|
+
export declare const useDefaultValuesFromLoader: ({ formId, }: InternalFormContextValue) => any;
|
9
|
+
export declare const useDefaultValuesForForm: (context: InternalFormContextValue) => Hydratable<{
|
10
|
+
[fieldName: string]: any;
|
11
|
+
}>;
|
12
|
+
export declare const useHasActiveFormSubmit: ({ fetcher, }: InternalFormContextValue) => boolean;
|
13
|
+
export declare const useFieldTouched: (name: string, { formId }: InternalFormContextValue) => boolean;
|
14
|
+
export declare const useFieldError: (name: string, context: InternalFormContextValue) => string | undefined;
|
15
|
+
export declare const useFieldDefaultValue: (name: string, context: InternalFormContextValue) => any;
|
16
|
+
export declare const useFormUpdateAtom: typeof useUpdateAtom;
|
17
|
+
export declare const useClearError: (context: InternalFormContextValue) => (name: string) => void;
|
18
|
+
export declare const useSetTouched: (context: InternalFormContextValue) => (name: string, touched: boolean) => void;
|