@tanstack/react-form 0.13.0 → 0.13.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/dist/cjs/createFormFactory.cjs +24 -0
- package/dist/cjs/createFormFactory.cjs.map +1 -0
- package/dist/cjs/formContext.cjs +14 -0
- package/dist/cjs/formContext.cjs.map +1 -0
- package/dist/cjs/index.cjs +9 -161
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/useField.cjs +62 -0
- package/dist/cjs/useField.cjs.map +1 -0
- package/dist/cjs/useForm.cjs +38 -0
- package/dist/cjs/useForm.cjs.map +1 -0
- package/dist/cjs/useIsomorphicEffectOnce.cjs +30 -0
- package/dist/cjs/useIsomorphicEffectOnce.cjs.map +1 -0
- package/dist/cjs/useIsomorphicLayoutEffect.cjs +9 -0
- package/dist/cjs/useIsomorphicLayoutEffect.cjs.map +1 -0
- package/dist/cjs/useTransform.cjs +10 -0
- package/dist/cjs/useTransform.cjs.map +1 -0
- package/dist/cjs/validateFormData.cjs +22 -0
- package/dist/cjs/validateFormData.cjs.map +1 -0
- package/dist/{mjs/validateFormData.d.ts → cjs/validateFormData.d.cts} +2 -0
- package/dist/esm/createFormFactory.js +24 -0
- package/dist/esm/createFormFactory.js.map +1 -0
- package/dist/esm/formContext.js +14 -0
- package/dist/esm/formContext.js.map +1 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/useField.js +62 -0
- package/dist/esm/useField.js.map +1 -0
- package/dist/esm/useForm.js +38 -0
- package/dist/esm/useForm.js.map +1 -0
- package/dist/esm/useIsomorphicEffectOnce.js +30 -0
- package/dist/esm/useIsomorphicEffectOnce.js.map +1 -0
- package/dist/esm/useIsomorphicLayoutEffect.js +9 -0
- package/dist/esm/useIsomorphicLayoutEffect.js.map +1 -0
- package/dist/esm/useTransform.js +10 -0
- package/dist/esm/useTransform.js.map +1 -0
- package/dist/{cjs → esm}/validateFormData.d.ts +2 -0
- package/dist/esm/validateFormData.js +22 -0
- package/dist/esm/validateFormData.js.map +1 -0
- package/package.json +13 -15
- package/dist/cjs/index.js +0 -181
- package/dist/cjs/tests/createFormFactory.test.d.ts +0 -1
- package/dist/cjs/tests/useField.test-d.d.ts +0 -1
- package/dist/cjs/tests/useField.test.d.ts +0 -1
- package/dist/cjs/tests/useForm.test-d.d.ts +0 -1
- package/dist/cjs/tests/useForm.test.d.ts +0 -1
- package/dist/cjs/tests/utils.d.ts +0 -1
- package/dist/mjs/index.d.mts +0 -8
- package/dist/mjs/index.d.ts +0 -8
- package/dist/mjs/index.js +0 -170
- package/dist/mjs/index.mjs +0 -170
- package/dist/mjs/index.mjs.map +0 -1
- package/dist/mjs/tests/createFormFactory.test.d.ts +0 -1
- package/dist/mjs/tests/useField.test-d.d.ts +0 -1
- package/dist/mjs/tests/useField.test.d.ts +0 -1
- package/dist/mjs/tests/useForm.test-d.d.ts +0 -1
- package/dist/mjs/tests/useForm.test.d.ts +0 -1
- package/dist/mjs/tests/utils.d.ts +0 -1
- /package/dist/cjs/{createFormFactory.d.ts → createFormFactory.d.cts} +0 -0
- /package/dist/cjs/{formContext.d.ts → formContext.d.cts} +0 -0
- /package/dist/cjs/{types.d.ts → types.d.cts} +0 -0
- /package/dist/cjs/{useField.d.ts → useField.d.cts} +0 -0
- /package/dist/cjs/{useForm.d.ts → useForm.d.cts} +0 -0
- /package/dist/cjs/{useIsomorphicEffectOnce.d.ts → useIsomorphicEffectOnce.d.cts} +0 -0
- /package/dist/cjs/{useIsomorphicLayoutEffect.d.ts → useIsomorphicLayoutEffect.d.cts} +0 -0
- /package/dist/cjs/{useTransform.d.ts → useTransform.d.cts} +0 -0
- /package/dist/{mjs → esm}/createFormFactory.d.ts +0 -0
- /package/dist/{mjs → esm}/formContext.d.ts +0 -0
- /package/dist/{cjs → esm}/index.d.ts +0 -0
- /package/dist/{mjs → esm}/types.d.ts +0 -0
- /package/dist/{mjs → esm}/useField.d.ts +0 -0
- /package/dist/{mjs → esm}/useForm.d.ts +0 -0
- /package/dist/{mjs → esm}/useIsomorphicEffectOnce.d.ts +0 -0
- /package/dist/{mjs → esm}/useIsomorphicLayoutEffect.d.ts +0 -0
- /package/dist/{mjs → esm}/useTransform.d.ts +0 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const useField = require("./useField.cjs");
|
|
4
|
+
const useForm = require("./useForm.cjs");
|
|
5
|
+
const validateFormData = require("./validateFormData.cjs");
|
|
6
|
+
function createFormFactory(defaultOpts) {
|
|
7
|
+
return {
|
|
8
|
+
useForm: (opts) => {
|
|
9
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
10
|
+
return useForm.useForm(formOptions);
|
|
11
|
+
},
|
|
12
|
+
useField: useField.useField,
|
|
13
|
+
Field: useField.Field,
|
|
14
|
+
validateFormData: validateFormData.getValidateFormData(defaultOpts),
|
|
15
|
+
initialFormState: {
|
|
16
|
+
errorMap: {
|
|
17
|
+
onServer: void 0
|
|
18
|
+
},
|
|
19
|
+
errors: []
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.createFormFactory = createFormFactory;
|
|
24
|
+
//# sourceMappingURL=createFormFactory.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createFormFactory.cjs","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\nimport { type ValidateFormData, getValidateFormData } from './validateFormData'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormValidator>\n validateFormData: ValidateFormData<TFormData, TFormValidator>\n initialFormState: Partial<FormApi<TFormData, TFormValidator>['state']>\n}\n\nexport function createFormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n): FormFactory<TFormData, TFormValidator> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData, TFormValidator>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n validateFormData: getValidateFormData(defaultOpts) as never,\n initialFormState: {\n errorMap: {\n onServer: undefined,\n },\n errors: [],\n },\n }\n}\n"],"names":["useForm","useField","Field","getValidateFormData"],"mappings":";;;;;AAmBO,SAAS,kBAId,aACwC;AACjC,SAAA;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAA,GAAI,aAAa,IAAI;AACvD,aAAOA,QAAAA,QAAmC,WAAW;AAAA,IACvD;AAAA,IAAA,UACAC,SAAA;AAAA,IAAA,OACAC,SAAA;AAAA,IACA,kBAAkBC,qCAAoB,WAAW;AAAA,IACjD,kBAAkB;AAAA,MAChB,UAAU;AAAA,QACR,UAAU;AAAA,MACZ;AAAA,MACA,QAAQ,CAAC;AAAA,IACX;AAAA,EAAA;AAEJ;;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const rehackt = require("rehackt");
|
|
4
|
+
const formContext = rehackt.createContext(null);
|
|
5
|
+
function useFormContext() {
|
|
6
|
+
const formApi = rehackt.useContext(formContext);
|
|
7
|
+
if (!formApi) {
|
|
8
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
|
9
|
+
}
|
|
10
|
+
return formApi;
|
|
11
|
+
}
|
|
12
|
+
exports.formContext = formContext;
|
|
13
|
+
exports.useFormContext = useFormContext;
|
|
14
|
+
//# sourceMappingURL=formContext.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formContext.cjs","sources":["../../src/formContext.ts"],"sourcesContent":["import type { FormApi, Validator } from '@tanstack/form-core'\nimport { createContext, useContext } from 'rehackt'\n\nexport const formContext = createContext<{\n formApi: FormApi<any, Validator<any, unknown> | undefined>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = useContext(formContext)\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n"],"names":["createContext","useContext"],"mappings":";;;AAGa,MAAA,cAAcA,sBAGjB,IAAK;AAER,SAAS,iBAAiB;AACzB,QAAA,UAAUC,mBAAW,WAAW;AAEtC,MAAI,CAAC,SAAS;AACN,UAAA,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEO,SAAA;AACT;;;"}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,162 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const formCore = require("@tanstack/form-core");
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
function useFormContext() {
|
|
9
|
-
const formApi = React.useContext(formContext);
|
|
10
|
-
if (!formApi) {
|
|
11
|
-
throw new Error(`You are trying to use the form API outside of a form!`);
|
|
12
|
-
}
|
|
13
|
-
return formApi;
|
|
14
|
-
}
|
|
15
|
-
const useIsomorphicLayoutEffect = (
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect
|
|
18
|
-
);
|
|
19
|
-
const useIsomorphicEffectOnce = (effect) => {
|
|
20
|
-
const destroyFunc = React.useRef();
|
|
21
|
-
const effectCalled = React.useRef(false);
|
|
22
|
-
const renderAfterCalled = React.useRef(false);
|
|
23
|
-
const [val, setVal] = React.useState(0);
|
|
24
|
-
if (effectCalled.current) {
|
|
25
|
-
renderAfterCalled.current = true;
|
|
26
|
-
}
|
|
27
|
-
useIsomorphicLayoutEffect(() => {
|
|
28
|
-
if (!effectCalled.current) {
|
|
29
|
-
destroyFunc.current = effect();
|
|
30
|
-
effectCalled.current = true;
|
|
31
|
-
}
|
|
32
|
-
setVal((v) => v + 1);
|
|
33
|
-
return () => {
|
|
34
|
-
if (!renderAfterCalled.current) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
if (destroyFunc.current) {
|
|
38
|
-
destroyFunc.current();
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}, []);
|
|
42
|
-
};
|
|
43
|
-
function useField(opts) {
|
|
44
|
-
const { formApi, parentFieldName } = useFormContext();
|
|
45
|
-
const [fieldApi] = React.useState(() => {
|
|
46
|
-
const name = (typeof opts.index === "number" ? [parentFieldName, opts.index, opts.name] : [parentFieldName, opts.name]).filter((d) => d !== void 0).join(".");
|
|
47
|
-
const api = new formCore.FieldApi({
|
|
48
|
-
...opts,
|
|
49
|
-
form: formApi,
|
|
50
|
-
// TODO: Fix typings to include `index` and `parentFieldName`, if present
|
|
51
|
-
name
|
|
52
|
-
});
|
|
53
|
-
api.Field = Field;
|
|
54
|
-
return api;
|
|
55
|
-
});
|
|
56
|
-
useIsomorphicLayoutEffect(() => {
|
|
57
|
-
fieldApi.update({ ...opts, form: formApi });
|
|
58
|
-
});
|
|
59
|
-
reactStore.useStore(
|
|
60
|
-
fieldApi.store,
|
|
61
|
-
opts.mode === "array" ? (state) => {
|
|
62
|
-
return [state.meta, Object.keys(state.value).length];
|
|
63
|
-
} : void 0
|
|
64
|
-
);
|
|
65
|
-
const unmountFn = React.useRef(null);
|
|
66
|
-
useIsomorphicEffectOnce(() => {
|
|
67
|
-
return () => {
|
|
68
|
-
var _a;
|
|
69
|
-
(_a = unmountFn.current) == null ? void 0 : _a.call(unmountFn);
|
|
70
|
-
};
|
|
71
|
-
});
|
|
72
|
-
if (!unmountFn.current) {
|
|
73
|
-
unmountFn.current = fieldApi.mount();
|
|
74
|
-
}
|
|
75
|
-
return fieldApi;
|
|
76
|
-
}
|
|
77
|
-
function Field({
|
|
78
|
-
children,
|
|
79
|
-
...fieldOptions
|
|
80
|
-
}) {
|
|
81
|
-
const fieldApi = useField(fieldOptions);
|
|
82
|
-
return /* @__PURE__ */ React.createElement(
|
|
83
|
-
formContext.Provider,
|
|
84
|
-
{
|
|
85
|
-
value: {
|
|
86
|
-
formApi: fieldApi.form,
|
|
87
|
-
parentFieldName: fieldApi.name
|
|
88
|
-
},
|
|
89
|
-
children: formCore.functionalUpdate(children, fieldApi)
|
|
90
|
-
}
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
function useForm(opts) {
|
|
94
|
-
const [formApi] = React.useState(() => {
|
|
95
|
-
const api = new formCore.FormApi(opts);
|
|
96
|
-
api.Provider = function Provider(props) {
|
|
97
|
-
useIsomorphicLayoutEffect(api.mount, []);
|
|
98
|
-
return /* @__PURE__ */ React.createElement(formContext.Provider, { ...props, value: { formApi: api } });
|
|
99
|
-
};
|
|
100
|
-
api.Field = Field;
|
|
101
|
-
api.useField = useField;
|
|
102
|
-
api.useStore = (selector) => {
|
|
103
|
-
return reactStore.useStore(api.store, selector);
|
|
104
|
-
};
|
|
105
|
-
api.Subscribe = (props) => {
|
|
106
|
-
return formCore.functionalUpdate(
|
|
107
|
-
props.children,
|
|
108
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
109
|
-
reactStore.useStore(api.store, props.selector)
|
|
110
|
-
);
|
|
111
|
-
};
|
|
112
|
-
return api;
|
|
113
|
-
});
|
|
114
|
-
formApi.useStore((state) => state.isSubmitting);
|
|
115
|
-
useIsomorphicLayoutEffect(() => {
|
|
116
|
-
formApi.update(opts);
|
|
117
|
-
});
|
|
118
|
-
return formApi;
|
|
119
|
-
}
|
|
120
|
-
const getValidateFormData = (defaultOpts) => async (formData, info) => {
|
|
121
|
-
const { validatorAdapter, onServerValidate } = defaultOpts || {};
|
|
122
|
-
const runValidator = (propsValue) => {
|
|
123
|
-
if (validatorAdapter && typeof onServerValidate !== "function") {
|
|
124
|
-
return validatorAdapter().validate(propsValue, onServerValidate);
|
|
125
|
-
}
|
|
126
|
-
return onServerValidate(propsValue);
|
|
127
|
-
};
|
|
128
|
-
const data = decodeFormdata.decode(formData, info);
|
|
129
|
-
const onServerError = runValidator({ value: data });
|
|
130
|
-
return {
|
|
131
|
-
errorMap: {
|
|
132
|
-
onServer: onServerError
|
|
133
|
-
},
|
|
134
|
-
errors: onServerError ? [onServerError] : []
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
function createFormFactory(defaultOpts) {
|
|
138
|
-
return {
|
|
139
|
-
useForm: (opts) => {
|
|
140
|
-
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
141
|
-
return useForm(formOptions);
|
|
142
|
-
},
|
|
143
|
-
useField,
|
|
144
|
-
Field,
|
|
145
|
-
validateFormData: getValidateFormData(defaultOpts),
|
|
146
|
-
initialFormState: {
|
|
147
|
-
errorMap: {
|
|
148
|
-
onServer: void 0
|
|
149
|
-
},
|
|
150
|
-
errors: []
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
function useTransform(fn, deps) {
|
|
155
|
-
return {
|
|
156
|
-
fn,
|
|
157
|
-
deps
|
|
158
|
-
};
|
|
159
|
-
}
|
|
4
|
+
const useForm = require("./useForm.cjs");
|
|
5
|
+
const useField = require("./useField.cjs");
|
|
6
|
+
const createFormFactory = require("./createFormFactory.cjs");
|
|
7
|
+
const useTransform = require("./useTransform.cjs");
|
|
160
8
|
Object.defineProperty(exports, "FieldApi", {
|
|
161
9
|
enumerable: true,
|
|
162
10
|
get: () => formCore.FieldApi
|
|
@@ -173,9 +21,9 @@ Object.defineProperty(exports, "mergeForm", {
|
|
|
173
21
|
enumerable: true,
|
|
174
22
|
get: () => formCore.mergeForm
|
|
175
23
|
});
|
|
176
|
-
exports.
|
|
177
|
-
exports.
|
|
178
|
-
exports.useField = useField;
|
|
179
|
-
exports.
|
|
180
|
-
exports.useTransform = useTransform;
|
|
24
|
+
exports.useForm = useForm.useForm;
|
|
25
|
+
exports.Field = useField.Field;
|
|
26
|
+
exports.useField = useField.useField;
|
|
27
|
+
exports.createFormFactory = createFormFactory.createFormFactory;
|
|
28
|
+
exports.useTransform = useTransform.useTransform;
|
|
181
29
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/formContext.ts","../../src/useIsomorphicLayoutEffect.ts","../../src/useIsomorphicEffectOnce.ts","../../src/useField.tsx","../../src/useForm.tsx","../../src/validateFormData.ts","../../src/createFormFactory.ts","../../src/useTransform.ts"],"sourcesContent":["import type { FormApi, Validator } from '@tanstack/form-core'\nimport { createContext, useContext } from 'rehackt'\n\nexport const formContext = createContext<{\n formApi: FormApi<any, Validator<any, unknown> | undefined>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = useContext(formContext)\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n","import { useEffect, useLayoutEffect } from 'rehackt'\n\nexport const useIsomorphicLayoutEffect =\n // @ts-ignore\n typeof window !== 'undefined' ? useLayoutEffect : useEffect\n","import { useRef, useState, type EffectCallback } from 'rehackt'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\n\n/**\n * This hook handles StrictMode and prod mode\n */\nexport const useIsomorphicEffectOnce = (effect: EffectCallback) => {\n const destroyFunc = useRef<void | (() => void)>()\n const effectCalled = useRef(false)\n const renderAfterCalled = useRef(false)\n const [val, setVal] = useState(0)\n\n if (effectCalled.current) {\n renderAfterCalled.current = true\n }\n\n useIsomorphicLayoutEffect(() => {\n // only execute the effect first time around\n if (!effectCalled.current) {\n destroyFunc.current = effect()\n effectCalled.current = true\n }\n\n // this forces one render after the effect is run\n setVal((v) => v + 1)\n\n return () => {\n // if the comp didn't render since the useEffect was called,\n // we know it's the dummy React cycle\n if (!renderAfterCalled.current) {\n return\n }\n if (destroyFunc.current) {\n destroyFunc.current()\n }\n }\n }, [])\n}\n","import React, { useRef, useState } from 'rehackt'\nimport { useStore } from '@tanstack/react-store'\nimport type {\n DeepKeys,\n DeepValue,\n Narrow,\n Validator,\n} from '@tanstack/form-core'\nimport { FieldApi, functionalUpdate } from '@tanstack/form-core'\nimport { useFormContext, formContext } from './formContext'\nimport type { UseFieldOptions } from './types'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\nimport { useIsomorphicEffectOnce } from './useIsomorphicEffectOnce'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FieldApi<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n > {\n Field: FieldComponent<TParentData, TFormValidator>\n }\n}\n\nexport type UseField<TParentData> = <\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n>(\n opts?: { name: Narrow<TName> } & UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator\n >,\n) => FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n DeepValue<TParentData, TName>\n>\n\nexport function useField<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n>(\n opts: UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,\n): FieldApi<TParentData, TName, TFieldValidator, TFormValidator> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState(() => {\n const name = (\n typeof opts.index === 'number'\n ? [parentFieldName, opts.index, opts.name]\n : [parentFieldName, opts.name]\n )\n .filter((d) => d !== undefined)\n .join('.')\n\n const api = new FieldApi({\n ...opts,\n form: formApi as never,\n // TODO: Fix typings to include `index` and `parentFieldName`, if present\n name: name as typeof opts.name as never,\n })\n\n api.Field = Field as never\n\n return api\n })\n\n /**\n * fieldApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n fieldApi.update({ ...opts, form: formApi } as never)\n })\n\n useStore(\n fieldApi.store,\n opts.mode === 'array'\n ? (state) => {\n return [state.meta, Object.keys(state.value).length]\n }\n : undefined,\n )\n const unmountFn = useRef<(() => void) | null>(null)\n\n useIsomorphicEffectOnce(() => {\n return () => {\n unmountFn.current?.()\n }\n })\n\n // We have to mount it right as soon as it renders, otherwise we get:\n // https://github.com/TanStack/form/issues/523\n if (!unmountFn.current) {\n unmountFn.current = fieldApi.mount()\n }\n\n return fieldApi as never\n}\n\ntype FieldComponentProps<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n> = {\n children: (\n fieldApi: FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >,\n ) => any\n} & (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<\n UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,\n 'name' | 'index'\n >\n\nexport type FieldComponent<\n TParentData,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n> = <\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n>({\n children,\n ...fieldOptions\n}: FieldComponentProps<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n>) => any\n\nexport function Field<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n>({\n children,\n ...fieldOptions\n}: {\n children: (\n fieldApi: FieldApi<TParentData, TName, TFieldValidator, TFormValidator>,\n ) => any\n} & UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>) {\n const fieldApi = useField(fieldOptions as any)\n\n return (\n <formContext.Provider\n value={{\n formApi: fieldApi.form as never,\n parentFieldName: fieldApi.name,\n }}\n children={functionalUpdate(children, fieldApi as any)}\n />\n )\n}\n","import type { FormState, FormOptions, Validator } from '@tanstack/form-core'\nimport { FormApi, functionalUpdate } from '@tanstack/form-core'\nimport type { NoInfer } from '@tanstack/react-store'\nimport { useStore } from '@tanstack/react-store'\nimport React, {\n type PropsWithChildren,\n type ReactNode,\n useState,\n} from 'rehackt'\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { formContext } from './formContext'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData, TFormValidator> {\n Provider: (props: PropsWithChildren) => JSX.Element\n Field: FieldComponent<TFormData, TFormValidator>\n useField: UseField<TFormData>\n useStore: <TSelected = NoInfer<FormState<TFormData>>>(\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n ) => TSelected\n Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected\n children: ((state: NoInfer<TSelected>) => ReactNode) | ReactNode\n }) => JSX.Element\n }\n}\n\nexport function useForm<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n opts?: FormOptions<TFormData, TFormValidator>,\n): FormApi<TFormData, TFormValidator> {\n const [formApi] = useState(() => {\n // @ts-ignore\n const api = new FormApi<TFormData, TFormValidator>(opts)\n\n api.Provider = function Provider(props) {\n useIsomorphicLayoutEffect(api.mount, [])\n return (\n <formContext.Provider {...props} value={{ formApi: api as never }} />\n )\n }\n api.Field = Field as any\n api.useField = useField as any\n api.useStore = (\n // @ts-ignore\n selector,\n ) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useStore(api.store as any, selector as any) as any\n }\n api.Subscribe = (\n // @ts-ignore\n props,\n ) => {\n return functionalUpdate(\n props.children,\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useStore(api.store as any, props.selector as any),\n ) as any\n }\n\n return api\n })\n\n formApi.useStore((state) => state.isSubmitting)\n\n /**\n * formApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n formApi.update(opts)\n })\n\n return formApi as any\n}\n","import { decode } from 'decode-formdata'\nimport type {\n FormApi,\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ntype OnServerValidateOrFn<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = TFormValidator extends Validator<TFormData, infer FFN>\n ? FFN | OnServerValidateFn<TFormData>\n : OnServerValidateFn<TFormData>\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormOptions<\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n > {\n onServerValidate?: OnServerValidateOrFn<TFormData, TFormValidator>\n }\n}\n\nexport type ValidateFormData<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = (\n formData: FormData,\n info?: Parameters<typeof decode>[1],\n) => Promise<Partial<FormApi<TFormData, TFormValidator>['state']>>\n\nexport const getValidateFormData = <\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n) =>\n (async (\n formData: FormData,\n info?: Parameters<typeof decode>[1],\n ): Promise<Partial<FormApi<TFormData, TFormValidator>['state']>> => {\n const { validatorAdapter, onServerValidate } = defaultOpts || {}\n\n const runValidator = (propsValue: { value: TFormData }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validate(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const data = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: data })\n\n return {\n errorMap: {\n onServer: onServerError,\n },\n errors: onServerError ? [onServerError] : [],\n }\n }) as ValidateFormData<TFormData, TFormValidator>\n","import type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\nimport { type ValidateFormData, getValidateFormData } from './validateFormData'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormValidator>\n validateFormData: ValidateFormData<TFormData, TFormValidator>\n initialFormState: Partial<FormApi<TFormData, TFormValidator>['state']>\n}\n\nexport function createFormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n): FormFactory<TFormData, TFormValidator> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData, TFormValidator>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n validateFormData: getValidateFormData(defaultOpts) as never,\n initialFormState: {\n errorMap: {\n onServer: undefined,\n },\n errors: [],\n },\n }\n}\n","import type { FormApi, Validator } from '@tanstack/form-core'\n\nexport function useTransform<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n fn: (\n formBase: FormApi<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>,\n deps: unknown[],\n) {\n return {\n fn,\n deps,\n }\n}\n"],"names":["createContext","useContext","useLayoutEffect","useEffect","useRef","useState","FieldApi","useStore","functionalUpdate","FormApi","decode"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
+
const rehackt = require("rehackt");
|
|
5
|
+
const reactStore = require("@tanstack/react-store");
|
|
6
|
+
const formCore = require("@tanstack/form-core");
|
|
7
|
+
const formContext = require("./formContext.cjs");
|
|
8
|
+
const useIsomorphicLayoutEffect = require("./useIsomorphicLayoutEffect.cjs");
|
|
9
|
+
const useIsomorphicEffectOnce = require("./useIsomorphicEffectOnce.cjs");
|
|
10
|
+
function useField(opts) {
|
|
11
|
+
const { formApi, parentFieldName } = formContext.useFormContext();
|
|
12
|
+
const [fieldApi] = rehackt.useState(() => {
|
|
13
|
+
const name = (typeof opts.index === "number" ? [parentFieldName, opts.index, opts.name] : [parentFieldName, opts.name]).filter((d) => d !== void 0).join(".");
|
|
14
|
+
const api = new formCore.FieldApi({
|
|
15
|
+
...opts,
|
|
16
|
+
form: formApi,
|
|
17
|
+
// TODO: Fix typings to include `index` and `parentFieldName`, if present
|
|
18
|
+
name
|
|
19
|
+
});
|
|
20
|
+
api.Field = Field;
|
|
21
|
+
return api;
|
|
22
|
+
});
|
|
23
|
+
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
|
|
24
|
+
fieldApi.update({ ...opts, form: formApi });
|
|
25
|
+
});
|
|
26
|
+
reactStore.useStore(
|
|
27
|
+
fieldApi.store,
|
|
28
|
+
opts.mode === "array" ? (state) => {
|
|
29
|
+
return [state.meta, Object.keys(state.value).length];
|
|
30
|
+
} : void 0
|
|
31
|
+
);
|
|
32
|
+
const unmountFn = rehackt.useRef(null);
|
|
33
|
+
useIsomorphicEffectOnce.useIsomorphicEffectOnce(() => {
|
|
34
|
+
return () => {
|
|
35
|
+
var _a;
|
|
36
|
+
(_a = unmountFn.current) == null ? void 0 : _a.call(unmountFn);
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
if (!unmountFn.current) {
|
|
40
|
+
unmountFn.current = fieldApi.mount();
|
|
41
|
+
}
|
|
42
|
+
return fieldApi;
|
|
43
|
+
}
|
|
44
|
+
function Field({
|
|
45
|
+
children,
|
|
46
|
+
...fieldOptions
|
|
47
|
+
}) {
|
|
48
|
+
const fieldApi = useField(fieldOptions);
|
|
49
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
50
|
+
formContext.formContext.Provider,
|
|
51
|
+
{
|
|
52
|
+
value: {
|
|
53
|
+
formApi: fieldApi.form,
|
|
54
|
+
parentFieldName: fieldApi.name
|
|
55
|
+
},
|
|
56
|
+
children: formCore.functionalUpdate(children, fieldApi)
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
exports.Field = Field;
|
|
61
|
+
exports.useField = useField;
|
|
62
|
+
//# sourceMappingURL=useField.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useField.cjs","sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useRef, useState } from 'rehackt'\nimport { useStore } from '@tanstack/react-store'\nimport type {\n DeepKeys,\n DeepValue,\n Narrow,\n Validator,\n} from '@tanstack/form-core'\nimport { FieldApi, functionalUpdate } from '@tanstack/form-core'\nimport { useFormContext, formContext } from './formContext'\nimport type { UseFieldOptions } from './types'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\nimport { useIsomorphicEffectOnce } from './useIsomorphicEffectOnce'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FieldApi<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n > {\n Field: FieldComponent<TParentData, TFormValidator>\n }\n}\n\nexport type UseField<TParentData> = <\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n>(\n opts?: { name: Narrow<TName> } & UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator\n >,\n) => FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n DeepValue<TParentData, TName>\n>\n\nexport function useField<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n>(\n opts: UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,\n): FieldApi<TParentData, TName, TFieldValidator, TFormValidator> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState(() => {\n const name = (\n typeof opts.index === 'number'\n ? [parentFieldName, opts.index, opts.name]\n : [parentFieldName, opts.name]\n )\n .filter((d) => d !== undefined)\n .join('.')\n\n const api = new FieldApi({\n ...opts,\n form: formApi as never,\n // TODO: Fix typings to include `index` and `parentFieldName`, if present\n name: name as typeof opts.name as never,\n })\n\n api.Field = Field as never\n\n return api\n })\n\n /**\n * fieldApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n fieldApi.update({ ...opts, form: formApi } as never)\n })\n\n useStore(\n fieldApi.store,\n opts.mode === 'array'\n ? (state) => {\n return [state.meta, Object.keys(state.value).length]\n }\n : undefined,\n )\n const unmountFn = useRef<(() => void) | null>(null)\n\n useIsomorphicEffectOnce(() => {\n return () => {\n unmountFn.current?.()\n }\n })\n\n // We have to mount it right as soon as it renders, otherwise we get:\n // https://github.com/TanStack/form/issues/523\n if (!unmountFn.current) {\n unmountFn.current = fieldApi.mount()\n }\n\n return fieldApi as never\n}\n\ntype FieldComponentProps<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n> = {\n children: (\n fieldApi: FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >,\n ) => any\n} & (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<\n UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,\n 'name' | 'index'\n >\n\nexport type FieldComponent<\n TParentData,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n> = <\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n>({\n children,\n ...fieldOptions\n}: FieldComponentProps<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n>) => any\n\nexport function Field<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n>({\n children,\n ...fieldOptions\n}: {\n children: (\n fieldApi: FieldApi<TParentData, TName, TFieldValidator, TFormValidator>,\n ) => any\n} & UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>) {\n const fieldApi = useField(fieldOptions as any)\n\n return (\n <formContext.Provider\n value={{\n formApi: fieldApi.form as never,\n parentFieldName: fieldApi.name,\n }}\n children={functionalUpdate(children, fieldApi as any)}\n />\n )\n}\n"],"names":["useFormContext","useState","FieldApi","useIsomorphicLayoutEffect","useStore","useRef","useIsomorphicEffectOnce","jsx","formContext","functionalUpdate"],"mappings":";;;;;;;;;AAsDO,SAAS,SAUd,MAC+D;AAE/D,QAAM,EAAE,SAAS,gBAAgB,IAAIA,YAAe,eAAA;AAEpD,QAAM,CAAC,QAAQ,IAAIC,QAAAA,SAAS,MAAM;AAC1B,UAAA,QACJ,OAAO,KAAK,UAAU,WAClB,CAAC,iBAAiB,KAAK,OAAO,KAAK,IAAI,IACvC,CAAC,iBAAiB,KAAK,IAAI,GAE9B,OAAO,CAAC,MAAM,MAAM,MAAS,EAC7B,KAAK,GAAG;AAEL,UAAA,MAAM,IAAIC,kBAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA;AAAA,MAEN;AAAA,IAAA,CACD;AAED,QAAI,QAAQ;AAEL,WAAA;AAAA,EAAA,CACR;AAMDC,4BAAAA,0BAA0B,MAAM;AAC9B,aAAS,OAAO,EAAE,GAAG,MAAM,MAAM,SAAkB;AAAA,EAAA,CACpD;AAEDC,aAAA;AAAA,IACE,SAAS;AAAA,IACT,KAAK,SAAS,UACV,CAAC,UAAU;AACF,aAAA,CAAC,MAAM,MAAM,OAAO,KAAK,MAAM,KAAK,EAAE,MAAM;AAAA,IAErD,IAAA;AAAA,EAAA;AAEA,QAAA,YAAYC,eAA4B,IAAI;AAElDC,0BAAAA,wBAAwB,MAAM;AAC5B,WAAO,MAAM;;AACX,sBAAU,YAAV;AAAA,IAAoB;AAAA,EACtB,CACD;AAIG,MAAA,CAAC,UAAU,SAAS;AACZ,cAAA,UAAU,SAAS;EAC/B;AAEO,SAAA;AACT;AA0DO,SAAS,MASd;AAAA,EACA;AAAA,EACA,GAAG;AACL,GAI0E;AAClE,QAAA,WAAW,SAAS,YAAmB;AAG3C,SAAAC,2BAAA;AAAA,IAACC,YAAAA,YAAY;AAAA,IAAZ;AAAA,MACC,OAAO;AAAA,QACL,SAAS,SAAS;AAAA,QAClB,iBAAiB,SAAS;AAAA,MAC5B;AAAA,MACA,UAAUC,SAAAA,iBAAiB,UAAU,QAAe;AAAA,IAAA;AAAA,EAAA;AAG1D;;;"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
+
const formCore = require("@tanstack/form-core");
|
|
5
|
+
const reactStore = require("@tanstack/react-store");
|
|
6
|
+
const rehackt = require("rehackt");
|
|
7
|
+
const useField = require("./useField.cjs");
|
|
8
|
+
const formContext = require("./formContext.cjs");
|
|
9
|
+
const useIsomorphicLayoutEffect = require("./useIsomorphicLayoutEffect.cjs");
|
|
10
|
+
function useForm(opts) {
|
|
11
|
+
const [formApi] = rehackt.useState(() => {
|
|
12
|
+
const api = new formCore.FormApi(opts);
|
|
13
|
+
api.Provider = function Provider(props) {
|
|
14
|
+
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(api.mount, []);
|
|
15
|
+
return /* @__PURE__ */ jsxRuntime.jsx(formContext.formContext.Provider, { ...props, value: { formApi: api } });
|
|
16
|
+
};
|
|
17
|
+
api.Field = useField.Field;
|
|
18
|
+
api.useField = useField.useField;
|
|
19
|
+
api.useStore = (selector) => {
|
|
20
|
+
return reactStore.useStore(api.store, selector);
|
|
21
|
+
};
|
|
22
|
+
api.Subscribe = (props) => {
|
|
23
|
+
return formCore.functionalUpdate(
|
|
24
|
+
props.children,
|
|
25
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
26
|
+
reactStore.useStore(api.store, props.selector)
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
return api;
|
|
30
|
+
});
|
|
31
|
+
formApi.useStore((state) => state.isSubmitting);
|
|
32
|
+
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
|
|
33
|
+
formApi.update(opts);
|
|
34
|
+
});
|
|
35
|
+
return formApi;
|
|
36
|
+
}
|
|
37
|
+
exports.useForm = useForm;
|
|
38
|
+
//# sourceMappingURL=useForm.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useForm.cjs","sources":["../../src/useForm.tsx"],"sourcesContent":["import type { FormState, FormOptions, Validator } from '@tanstack/form-core'\nimport { FormApi, functionalUpdate } from '@tanstack/form-core'\nimport type { NoInfer } from '@tanstack/react-store'\nimport { useStore } from '@tanstack/react-store'\nimport React, {\n type PropsWithChildren,\n type ReactNode,\n useState,\n} from 'rehackt'\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { formContext } from './formContext'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData, TFormValidator> {\n Provider: (props: PropsWithChildren) => JSX.Element\n Field: FieldComponent<TFormData, TFormValidator>\n useField: UseField<TFormData>\n useStore: <TSelected = NoInfer<FormState<TFormData>>>(\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n ) => TSelected\n Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected\n children: ((state: NoInfer<TSelected>) => ReactNode) | ReactNode\n }) => JSX.Element\n }\n}\n\nexport function useForm<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n opts?: FormOptions<TFormData, TFormValidator>,\n): FormApi<TFormData, TFormValidator> {\n const [formApi] = useState(() => {\n // @ts-ignore\n const api = new FormApi<TFormData, TFormValidator>(opts)\n\n api.Provider = function Provider(props) {\n useIsomorphicLayoutEffect(api.mount, [])\n return (\n <formContext.Provider {...props} value={{ formApi: api as never }} />\n )\n }\n api.Field = Field as any\n api.useField = useField as any\n api.useStore = (\n // @ts-ignore\n selector,\n ) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useStore(api.store as any, selector as any) as any\n }\n api.Subscribe = (\n // @ts-ignore\n props,\n ) => {\n return functionalUpdate(\n props.children,\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useStore(api.store as any, props.selector as any),\n ) as any\n }\n\n return api\n })\n\n formApi.useStore((state) => state.isSubmitting)\n\n /**\n * formApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n formApi.update(opts)\n })\n\n return formApi as any\n}\n"],"names":["useState","FormApi","useIsomorphicLayoutEffect","jsx","formContext","Field","useField","useStore","functionalUpdate"],"mappings":";;;;;;;;;AA6BO,SAAS,QAId,MACoC;AACpC,QAAM,CAAC,OAAO,IAAIA,QAAAA,SAAS,MAAM;AAEzB,UAAA,MAAM,IAAIC,iBAAmC,IAAI;AAEnD,QAAA,WAAW,SAAS,SAAS,OAAO;AACZC,gCAAAA,0BAAA,IAAI,OAAO,CAAA,CAAE;AAErC,aAAAC,+BAACC,YAAAA,YAAY,UAAZ,EAAsB,GAAG,OAAO,OAAO,EAAE,SAAS,IAAgB,EAAA,CAAA;AAAA,IAAA;AAGvE,QAAI,QAAQC;AACZ,QAAI,WAAWC;AACX,QAAA,WAAW,CAEb,aACG;AAEI,aAAAC,oBAAS,IAAI,OAAc,QAAe;AAAA,IAAA;AAE/C,QAAA,YAAY,CAEd,UACG;AACI,aAAAC,SAAA;AAAA,QACL,MAAM;AAAA;AAAA,QAEND,WAAAA,SAAS,IAAI,OAAc,MAAM,QAAe;AAAA,MAAA;AAAA,IAClD;AAGK,WAAA;AAAA,EAAA,CACR;AAED,UAAQ,SAAS,CAAC,UAAU,MAAM,YAAY;AAM9CL,4BAAAA,0BAA0B,MAAM;AAC9B,YAAQ,OAAO,IAAI;AAAA,EAAA,CACpB;AAEM,SAAA;AACT;;"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const rehackt = require("rehackt");
|
|
4
|
+
const useIsomorphicLayoutEffect = require("./useIsomorphicLayoutEffect.cjs");
|
|
5
|
+
const useIsomorphicEffectOnce = (effect) => {
|
|
6
|
+
const destroyFunc = rehackt.useRef();
|
|
7
|
+
const effectCalled = rehackt.useRef(false);
|
|
8
|
+
const renderAfterCalled = rehackt.useRef(false);
|
|
9
|
+
const [val, setVal] = rehackt.useState(0);
|
|
10
|
+
if (effectCalled.current) {
|
|
11
|
+
renderAfterCalled.current = true;
|
|
12
|
+
}
|
|
13
|
+
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
|
|
14
|
+
if (!effectCalled.current) {
|
|
15
|
+
destroyFunc.current = effect();
|
|
16
|
+
effectCalled.current = true;
|
|
17
|
+
}
|
|
18
|
+
setVal((v) => v + 1);
|
|
19
|
+
return () => {
|
|
20
|
+
if (!renderAfterCalled.current) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (destroyFunc.current) {
|
|
24
|
+
destroyFunc.current();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}, []);
|
|
28
|
+
};
|
|
29
|
+
exports.useIsomorphicEffectOnce = useIsomorphicEffectOnce;
|
|
30
|
+
//# sourceMappingURL=useIsomorphicEffectOnce.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useIsomorphicEffectOnce.cjs","sources":["../../src/useIsomorphicEffectOnce.ts"],"sourcesContent":["import { useRef, useState, type EffectCallback } from 'rehackt'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\n\n/**\n * This hook handles StrictMode and prod mode\n */\nexport const useIsomorphicEffectOnce = (effect: EffectCallback) => {\n const destroyFunc = useRef<void | (() => void)>()\n const effectCalled = useRef(false)\n const renderAfterCalled = useRef(false)\n const [val, setVal] = useState(0)\n\n if (effectCalled.current) {\n renderAfterCalled.current = true\n }\n\n useIsomorphicLayoutEffect(() => {\n // only execute the effect first time around\n if (!effectCalled.current) {\n destroyFunc.current = effect()\n effectCalled.current = true\n }\n\n // this forces one render after the effect is run\n setVal((v) => v + 1)\n\n return () => {\n // if the comp didn't render since the useEffect was called,\n // we know it's the dummy React cycle\n if (!renderAfterCalled.current) {\n return\n }\n if (destroyFunc.current) {\n destroyFunc.current()\n }\n }\n }, [])\n}\n"],"names":["useRef","useState","useIsomorphicLayoutEffect"],"mappings":";;;;AAMa,MAAA,0BAA0B,CAAC,WAA2B;AACjE,QAAM,cAAcA,QAAAA;AACd,QAAA,eAAeA,eAAO,KAAK;AAC3B,QAAA,oBAAoBA,eAAO,KAAK;AACtC,QAAM,CAAC,KAAK,MAAM,IAAIC,iBAAS,CAAC;AAEhC,MAAI,aAAa,SAAS;AACxB,sBAAkB,UAAU;AAAA,EAC9B;AAEAC,4BAAAA,0BAA0B,MAAM;AAE1B,QAAA,CAAC,aAAa,SAAS;AACzB,kBAAY,UAAU;AACtB,mBAAa,UAAU;AAAA,IACzB;AAGO,WAAA,CAAC,MAAM,IAAI,CAAC;AAEnB,WAAO,MAAM;AAGP,UAAA,CAAC,kBAAkB,SAAS;AAC9B;AAAA,MACF;AACA,UAAI,YAAY,SAAS;AACvB,oBAAY,QAAQ;AAAA,MACtB;AAAA,IAAA;AAAA,EAEJ,GAAG,CAAE,CAAA;AACP;;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const rehackt = require("rehackt");
|
|
4
|
+
const useIsomorphicLayoutEffect = (
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
typeof window !== "undefined" ? rehackt.useLayoutEffect : rehackt.useEffect
|
|
7
|
+
);
|
|
8
|
+
exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect;
|
|
9
|
+
//# sourceMappingURL=useIsomorphicLayoutEffect.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useIsomorphicLayoutEffect.cjs","sources":["../../src/useIsomorphicLayoutEffect.ts"],"sourcesContent":["import { useEffect, useLayoutEffect } from 'rehackt'\n\nexport const useIsomorphicLayoutEffect =\n // @ts-ignore\n typeof window !== 'undefined' ? useLayoutEffect : useEffect\n"],"names":["useLayoutEffect","useEffect"],"mappings":";;;AAEa,MAAA;AAAA;AAAA,EAEX,OAAO,WAAW,cAAcA,QAAAA,kBAAkBC,QAAA;AAAA;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTransform.cjs","sources":["../../src/useTransform.ts"],"sourcesContent":["import type { FormApi, Validator } from '@tanstack/form-core'\n\nexport function useTransform<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n fn: (\n formBase: FormApi<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>,\n deps: unknown[],\n) {\n return {\n fn,\n deps,\n }\n}\n"],"names":[],"mappings":";;AAEgB,SAAA,aAId,IAGA,MACA;AACO,SAAA;AAAA,IACL;AAAA,IACA;AAAA,EAAA;AAEJ;;"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const decodeFormdata = require("decode-formdata");
|
|
4
|
+
const getValidateFormData = (defaultOpts) => async (formData, info) => {
|
|
5
|
+
const { validatorAdapter, onServerValidate } = defaultOpts || {};
|
|
6
|
+
const runValidator = (propsValue) => {
|
|
7
|
+
if (validatorAdapter && typeof onServerValidate !== "function") {
|
|
8
|
+
return validatorAdapter().validate(propsValue, onServerValidate);
|
|
9
|
+
}
|
|
10
|
+
return onServerValidate(propsValue);
|
|
11
|
+
};
|
|
12
|
+
const data = decodeFormdata.decode(formData, info);
|
|
13
|
+
const onServerError = runValidator({ value: data });
|
|
14
|
+
return {
|
|
15
|
+
errorMap: {
|
|
16
|
+
onServer: onServerError
|
|
17
|
+
},
|
|
18
|
+
errors: onServerError ? [onServerError] : []
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
exports.getValidateFormData = getValidateFormData;
|
|
22
|
+
//# sourceMappingURL=validateFormData.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateFormData.cjs","sources":["../../src/validateFormData.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport type {\n FormApi,\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ntype OnServerValidateOrFn<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = TFormValidator extends Validator<TFormData, infer FFN>\n ? FFN | OnServerValidateFn<TFormData>\n : OnServerValidateFn<TFormData>\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormOptions<\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n > {\n onServerValidate?: OnServerValidateOrFn<TFormData, TFormValidator>\n }\n}\n\nexport type ValidateFormData<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = (\n formData: FormData,\n info?: Parameters<typeof decode>[1],\n) => Promise<Partial<FormApi<TFormData, TFormValidator>['state']>>\n\nexport const getValidateFormData = <\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n) =>\n (async (\n formData: FormData,\n info?: Parameters<typeof decode>[1],\n ): Promise<Partial<FormApi<TFormData, TFormValidator>['state']>> => {\n const { validatorAdapter, onServerValidate } = defaultOpts || {}\n\n const runValidator = (propsValue: { value: TFormData }) => {\n if (validatorAdapter && typeof onServerValidate !== 'function') {\n return validatorAdapter().validate(propsValue, onServerValidate)\n }\n\n return (onServerValidate as OnServerValidateFn<TFormData>)(propsValue)\n }\n\n const data = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: data })\n\n return {\n errorMap: {\n onServer: onServerError,\n },\n errors: onServerError ? [onServerError] : [],\n }\n }) as ValidateFormData<TFormData, TFormValidator>\n"],"names":["decode"],"mappings":";;;AAuCO,MAAM,sBAAsB,CAIjC,gBAEC,OACC,UACA,SACkE;AAClE,QAAM,EAAE,kBAAkB,qBAAqB,eAAe,CAAA;AAExD,QAAA,eAAe,CAAC,eAAqC;AACrD,QAAA,oBAAoB,OAAO,qBAAqB,YAAY;AAC9D,aAAO,iBAAiB,EAAE,SAAS,YAAY,gBAAgB;AAAA,IACjE;AAEA,WAAQ,iBAAmD,UAAU;AAAA,EAAA;AAGjE,QAAA,OAAOA,eAAAA,OAAO,UAAU,IAAI;AAElC,QAAM,gBAAgB,aAAa,EAAE,OAAO,KAAM,CAAA;AAE3C,SAAA;AAAA,IACL,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAE/C;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useField, Field } from "./useField.js";
|
|
2
|
+
import { useForm } from "./useForm.js";
|
|
3
|
+
import { getValidateFormData } from "./validateFormData.js";
|
|
4
|
+
function createFormFactory(defaultOpts) {
|
|
5
|
+
return {
|
|
6
|
+
useForm: (opts) => {
|
|
7
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
|
8
|
+
return useForm(formOptions);
|
|
9
|
+
},
|
|
10
|
+
useField,
|
|
11
|
+
Field,
|
|
12
|
+
validateFormData: getValidateFormData(defaultOpts),
|
|
13
|
+
initialFormState: {
|
|
14
|
+
errorMap: {
|
|
15
|
+
onServer: void 0
|
|
16
|
+
},
|
|
17
|
+
errors: []
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
createFormFactory
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=createFormFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createFormFactory.js","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\nimport { type ValidateFormData, getValidateFormData } from './validateFormData'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormValidator>\n validateFormData: ValidateFormData<TFormData, TFormValidator>\n initialFormState: Partial<FormApi<TFormData, TFormValidator>['state']>\n}\n\nexport function createFormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n): FormFactory<TFormData, TFormValidator> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData, TFormValidator>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n validateFormData: getValidateFormData(defaultOpts) as never,\n initialFormState: {\n errorMap: {\n onServer: undefined,\n },\n errors: [],\n },\n }\n}\n"],"names":[],"mappings":";;;AAmBO,SAAS,kBAId,aACwC;AACjC,SAAA;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAA,GAAI,aAAa,IAAI;AACvD,aAAO,QAAmC,WAAW;AAAA,IACvD;AAAA,IACA;AAAA,IACA;AAAA,IACA,kBAAkB,oBAAoB,WAAW;AAAA,IACjD,kBAAkB;AAAA,MAChB,UAAU;AAAA,QACR,UAAU;AAAA,MACZ;AAAA,MACA,QAAQ,CAAC;AAAA,IACX;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createContext, useContext } from "rehackt";
|
|
2
|
+
const formContext = createContext(null);
|
|
3
|
+
function useFormContext() {
|
|
4
|
+
const formApi = useContext(formContext);
|
|
5
|
+
if (!formApi) {
|
|
6
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
|
7
|
+
}
|
|
8
|
+
return formApi;
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
formContext,
|
|
12
|
+
useFormContext
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=formContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formContext.js","sources":["../../src/formContext.ts"],"sourcesContent":["import type { FormApi, Validator } from '@tanstack/form-core'\nimport { createContext, useContext } from 'rehackt'\n\nexport const formContext = createContext<{\n formApi: FormApi<any, Validator<any, unknown> | undefined>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = useContext(formContext)\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n"],"names":[],"mappings":";AAGa,MAAA,cAAc,cAGjB,IAAK;AAER,SAAS,iBAAiB;AACzB,QAAA,UAAU,WAAW,WAAW;AAEtC,MAAI,CAAC,SAAS;AACN,UAAA,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEO,SAAA;AACT;"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FieldApi, FormApi, functionalUpdate, mergeForm } from "@tanstack/form-core";
|
|
2
|
+
import { useForm } from "./useForm.js";
|
|
3
|
+
import { Field, useField } from "./useField.js";
|
|
4
|
+
import { createFormFactory } from "./createFormFactory.js";
|
|
5
|
+
import { useTransform } from "./useTransform.js";
|
|
6
|
+
export {
|
|
7
|
+
Field,
|
|
8
|
+
FieldApi,
|
|
9
|
+
FormApi,
|
|
10
|
+
createFormFactory,
|
|
11
|
+
functionalUpdate,
|
|
12
|
+
mergeForm,
|
|
13
|
+
useField,
|
|
14
|
+
useForm,
|
|
15
|
+
useTransform
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;"}
|