@tanstack/react-form 0.24.2 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +0 -3
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +0 -1
- package/dist/cjs/{createServerValidate.cjs → nextjs/createServerValidate.cjs} +12 -4
- package/dist/cjs/nextjs/createServerValidate.cjs.map +1 -0
- package/dist/cjs/nextjs/createServerValidate.d.cts +13 -0
- package/dist/cjs/nextjs/error.cjs +11 -0
- package/dist/cjs/nextjs/error.cjs.map +1 -0
- package/dist/cjs/nextjs/error.d.cts +10 -0
- package/dist/cjs/{nextjs-index.cjs → nextjs/index.cjs} +3 -1
- package/dist/cjs/nextjs/index.cjs.map +1 -0
- package/dist/cjs/nextjs/index.d.cts +3 -0
- package/dist/cjs/nextjs/types.d.cts +3 -0
- package/dist/cjs/start/createServerValidate.cjs +39 -0
- package/dist/cjs/start/createServerValidate.cjs.map +1 -0
- package/dist/cjs/start/createServerValidate.d.cts +13 -0
- package/dist/cjs/start/error.cjs +12 -0
- package/dist/cjs/start/error.cjs.map +1 -0
- package/dist/cjs/start/error.d.cts +12 -0
- package/dist/cjs/start/getFormData.cjs +22 -0
- package/dist/cjs/start/getFormData.cjs.map +1 -0
- package/dist/cjs/start/getFormData.d.cts +14 -0
- package/dist/cjs/start/index.cjs +10 -0
- package/dist/cjs/start/index.cjs.map +1 -0
- package/dist/cjs/start/index.d.cts +3 -0
- package/dist/cjs/start/types.d.cts +3 -0
- package/dist/cjs/start/utils.cjs +6 -0
- package/dist/cjs/start/utils.cjs.map +1 -0
- package/dist/cjs/start/utils.d.cts +1 -0
- package/dist/cjs/useField.cjs +3 -2
- package/dist/cjs/useField.cjs.map +1 -1
- package/dist/cjs/useField.d.cts +4 -9
- package/dist/cjs/useForm.cjs +6 -5
- package/dist/cjs/useForm.cjs.map +1 -1
- package/dist/cjs/useForm.d.cts +34 -35
- package/dist/cjs/useIsomorphicLayoutEffect.cjs +1 -4
- package/dist/cjs/useIsomorphicLayoutEffect.cjs.map +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +0 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/nextjs/createServerValidate.d.ts +13 -0
- package/dist/esm/{createServerValidate.js → nextjs/createServerValidate.js} +12 -4
- package/dist/esm/nextjs/createServerValidate.js.map +1 -0
- package/dist/esm/nextjs/error.d.ts +10 -0
- package/dist/esm/nextjs/error.js +11 -0
- package/dist/esm/nextjs/error.js.map +1 -0
- package/dist/esm/nextjs/index.d.ts +3 -0
- package/dist/esm/{nextjs-index.js → nextjs/index.js} +3 -1
- package/dist/esm/nextjs/index.js.map +1 -0
- package/dist/esm/nextjs/types.d.ts +3 -0
- package/dist/esm/start/createServerValidate.d.ts +13 -0
- package/dist/esm/start/createServerValidate.js +39 -0
- package/dist/esm/start/createServerValidate.js.map +1 -0
- package/dist/esm/start/error.d.ts +12 -0
- package/dist/esm/start/error.js +12 -0
- package/dist/esm/start/error.js.map +1 -0
- package/dist/esm/start/getFormData.d.ts +14 -0
- package/dist/esm/start/getFormData.js +22 -0
- package/dist/esm/start/getFormData.js.map +1 -0
- package/dist/esm/start/index.d.ts +3 -0
- package/dist/esm/start/index.js +10 -0
- package/dist/esm/start/index.js.map +1 -0
- package/dist/esm/start/types.d.ts +3 -0
- package/dist/esm/start/utils.d.ts +1 -0
- package/dist/esm/start/utils.js +6 -0
- package/dist/esm/start/utils.js.map +1 -0
- package/dist/esm/useField.d.ts +4 -9
- package/dist/esm/useField.js +3 -2
- package/dist/esm/useField.js.map +1 -1
- package/dist/esm/useForm.d.ts +34 -35
- package/dist/esm/useForm.js +6 -5
- package/dist/esm/useForm.js.map +1 -1
- package/dist/esm/useIsomorphicLayoutEffect.js +1 -4
- package/dist/esm/useIsomorphicLayoutEffect.js.map +1 -1
- package/package.json +26 -8
- package/src/index.ts +0 -1
- package/src/nextjs/createServerValidate.ts +66 -0
- package/src/nextjs/error.ts +18 -0
- package/src/nextjs/index.ts +4 -0
- package/src/nextjs/types.ts +6 -0
- package/src/start/createServerValidate.tsx +73 -0
- package/src/start/error.ts +21 -0
- package/src/start/getFormData.tsx +20 -0
- package/src/start/index.ts +3 -0
- package/src/start/types.ts +6 -0
- package/src/start/utils.ts +3 -0
- package/src/useField.tsx +15 -23
- package/src/useForm.tsx +37 -42
- package/src/useIsomorphicLayoutEffect.ts +0 -1
- package/dist/cjs/createServerValidate.cjs.map +0 -1
- package/dist/cjs/createServerValidate.d.cts +0 -21
- package/dist/cjs/nextjs-index.cjs.map +0 -1
- package/dist/cjs/nextjs-index.d.cts +0 -2
- package/dist/esm/createServerValidate.d.ts +0 -21
- package/dist/esm/createServerValidate.js.map +0 -1
- package/dist/esm/nextjs-index.d.ts +0 -2
- package/dist/esm/nextjs-index.js.map +0 -1
- package/src/createServerValidate.ts +0 -77
- package/src/nextjs-index.ts +0 -3
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,13 +3,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const formCore = require("@tanstack/form-core");
|
|
4
4
|
const useForm = require("./useForm.cjs");
|
|
5
5
|
const useField = require("./useField.cjs");
|
|
6
|
-
const createServerValidate = require("./createServerValidate.cjs");
|
|
7
6
|
const useTransform = require("./useTransform.cjs");
|
|
8
7
|
exports.useForm = useForm.useForm;
|
|
9
8
|
exports.Field = useField.Field;
|
|
10
9
|
exports.useField = useField.useField;
|
|
11
|
-
exports.createServerValidate = createServerValidate.createServerValidate;
|
|
12
|
-
exports.initialFormState = createServerValidate.initialFormState;
|
|
13
10
|
exports.useTransform = useTransform.useTransform;
|
|
14
11
|
Object.keys(formCore).forEach((k) => {
|
|
15
12
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -2,5 +2,4 @@ export * from '@tanstack/form-core';
|
|
|
2
2
|
export { useForm } from './useForm.cjs';
|
|
3
3
|
export type { UseField, FieldComponent } from './useField.cjs';
|
|
4
4
|
export { useField, Field } from './useField.cjs';
|
|
5
|
-
export { createServerValidate, initialFormState } from './createServerValidate.cjs';
|
|
6
5
|
export { useTransform } from './useTransform.cjs';
|
|
@@ -1,27 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const decodeFormdata = require("decode-formdata");
|
|
4
|
+
const error = require("./error.cjs");
|
|
4
5
|
const createServerValidate = (defaultOpts) => async (formData, info) => {
|
|
5
|
-
const { validatorAdapter, onServerValidate } = defaultOpts
|
|
6
|
+
const { validatorAdapter, onServerValidate } = defaultOpts;
|
|
6
7
|
const runValidator = (propsValue) => {
|
|
7
8
|
if (validatorAdapter && typeof onServerValidate !== "function") {
|
|
8
9
|
return validatorAdapter().validate(propsValue, onServerValidate);
|
|
9
10
|
}
|
|
10
11
|
return onServerValidate(propsValue);
|
|
11
12
|
};
|
|
12
|
-
const
|
|
13
|
-
const onServerError = runValidator({ value:
|
|
14
|
-
|
|
13
|
+
const values = decodeFormdata.decode(formData, info);
|
|
14
|
+
const onServerError = runValidator({ value: values });
|
|
15
|
+
if (!onServerError)
|
|
16
|
+
return;
|
|
17
|
+
const formState = {
|
|
15
18
|
errorMap: {
|
|
16
19
|
onServer: onServerError
|
|
17
20
|
},
|
|
21
|
+
values,
|
|
18
22
|
errors: onServerError ? [onServerError] : []
|
|
19
23
|
};
|
|
24
|
+
throw new error.ServerValidateError({
|
|
25
|
+
formState
|
|
26
|
+
});
|
|
20
27
|
};
|
|
21
28
|
const initialFormState = {
|
|
22
29
|
errorMap: {
|
|
23
30
|
onServer: void 0
|
|
24
31
|
},
|
|
32
|
+
values: void 0,
|
|
25
33
|
errors: []
|
|
26
34
|
};
|
|
27
35
|
exports.createServerValidate = createServerValidate;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.cjs","sources":["../../../src/nextjs/createServerValidate.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport { ServerValidateError } from './error'\nimport type {\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\nimport type { ServerFormState } from './types'\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ninterface CreateServerValidateOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate: OnServerValidateFn<TFormData>\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n >(\n defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,\n ) =>\n async (formData: FormData, info?: Parameters<typeof decode>[1]) => {\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 values = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: values })\n\n if (!onServerError) return\n\n const formState: ServerFormState<TFormData> = {\n errorMap: {\n onServer: onServerError,\n },\n values,\n errors: onServerError ? [onServerError] : [],\n }\n\n throw new ServerValidateError({\n formState,\n })\n }\n\nexport const initialFormState: ServerFormState<any> = {\n errorMap: {\n onServer: undefined,\n },\n values: undefined,\n errors: [],\n}\n"],"names":["decode","ServerValidateError"],"mappings":";;;;AAoBO,MAAM,uBACX,CAME,gBAEF,OAAO,UAAoB,SAAwC;AAC3D,QAAA,EAAE,kBAAkB,iBAAqB,IAAA;AAEzC,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,SAASA,eAAAA,OAAO,UAAU,IAAI;AAEpC,QAAM,gBAAgB,aAAa,EAAE,OAAO,OAAQ,CAAA;AAEpD,MAAI,CAAC;AAAe;AAEpB,QAAM,YAAwC;AAAA,IAC5C,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA;AAAA,IACA,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAG7C,QAAM,IAAIC,MAAAA,oBAAoB;AAAA,IAC5B;AAAA,EAAA,CACD;AACH;AAEK,MAAM,mBAAyC;AAAA,EACpD,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ;AAAA,EACR,QAAQ,CAAC;AACX;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
+
import { ServerFormState } from './types.cjs';
|
|
4
|
+
|
|
5
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
6
|
+
value: TFormData;
|
|
7
|
+
}) => ValidationError;
|
|
8
|
+
interface CreateServerValidateOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
9
|
+
onServerValidate: OnServerValidateFn<TFormData>;
|
|
10
|
+
}
|
|
11
|
+
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>) => (formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<void>;
|
|
12
|
+
export declare const initialFormState: ServerFormState<any>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class ServerValidateError extends Error {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
6
|
+
this.name = "ServerValidateError";
|
|
7
|
+
this.formState = options.formState;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.ServerValidateError = ServerValidateError;
|
|
11
|
+
//# sourceMappingURL=error.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.cjs","sources":["../../../src/nextjs/error.ts"],"sourcesContent":["import type { ServerFormState } from './types'\n\ninterface ServerValidateErrorState<TFormData> {\n formState: ServerFormState<TFormData>\n}\n\nexport class ServerValidateError<TFormData>\n extends Error\n implements ServerValidateErrorState<TFormData>\n{\n formState: ServerFormState<TFormData>\n\n constructor(options: ServerValidateErrorState<TFormData>) {\n super('Your form has errors. Please check the fields and try again.')\n this.name = 'ServerValidateError'\n this.formState = options.formState\n }\n}\n"],"names":[],"mappings":";;AAMO,MAAM,4BACH,MAEV;AAAA,EAGE,YAAY,SAA8C;AACxD,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,YAAY,QAAQ;AAAA,EAC3B;AACF;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ServerFormState } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>;
|
|
5
|
+
}
|
|
6
|
+
export declare class ServerValidateError<TFormData> extends Error implements ServerValidateErrorState<TFormData> {
|
|
7
|
+
formState: ServerFormState<TFormData>;
|
|
8
|
+
constructor(options: ServerValidateErrorState<TFormData>);
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const formCore = require("@tanstack/form-core");
|
|
4
4
|
const createServerValidate = require("./createServerValidate.cjs");
|
|
5
|
+
const error = require("./error.cjs");
|
|
5
6
|
exports.createServerValidate = createServerValidate.createServerValidate;
|
|
6
7
|
exports.initialFormState = createServerValidate.initialFormState;
|
|
8
|
+
exports.ServerValidateError = error.ServerValidateError;
|
|
7
9
|
Object.keys(formCore).forEach((k) => {
|
|
8
10
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
|
9
11
|
Object.defineProperty(exports, k, {
|
|
@@ -11,4 +13,4 @@ Object.keys(formCore).forEach((k) => {
|
|
|
11
13
|
get: () => formCore[k]
|
|
12
14
|
});
|
|
13
15
|
});
|
|
14
|
-
//# sourceMappingURL=
|
|
16
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const decodeFormdata = require("decode-formdata");
|
|
4
|
+
const utils = require("./utils.cjs");
|
|
5
|
+
const error = require("./error.cjs");
|
|
6
|
+
const createServerValidate = (defaultOpts) => async (ctx, formData, info) => {
|
|
7
|
+
const { validatorAdapter, onServerValidate } = defaultOpts;
|
|
8
|
+
const runValidator = (propsValue) => {
|
|
9
|
+
if (validatorAdapter && typeof onServerValidate !== "function") {
|
|
10
|
+
return validatorAdapter().validate(propsValue, onServerValidate);
|
|
11
|
+
}
|
|
12
|
+
return onServerValidate(propsValue);
|
|
13
|
+
};
|
|
14
|
+
const referer = ctx.request.headers.get("referer");
|
|
15
|
+
const data = decodeFormdata.decode(formData, info);
|
|
16
|
+
const onServerError = runValidator({ value: data });
|
|
17
|
+
if (!onServerError)
|
|
18
|
+
return;
|
|
19
|
+
const formState = {
|
|
20
|
+
errorMap: {
|
|
21
|
+
onServer: onServerError
|
|
22
|
+
},
|
|
23
|
+
values: data,
|
|
24
|
+
errors: onServerError ? [onServerError] : []
|
|
25
|
+
};
|
|
26
|
+
const cookie = await utils._tanstackInternalsCookie.serialize(formState);
|
|
27
|
+
throw new error.ServerValidateError({
|
|
28
|
+
response: new Response("ok", {
|
|
29
|
+
headers: {
|
|
30
|
+
Location: referer,
|
|
31
|
+
"Set-Cookie": cookie
|
|
32
|
+
},
|
|
33
|
+
status: 302
|
|
34
|
+
}),
|
|
35
|
+
formState
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
exports.createServerValidate = createServerValidate;
|
|
39
|
+
//# sourceMappingURL=createServerValidate.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.cjs","sources":["../../../src/start/createServerValidate.tsx"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport { _tanstackInternalsCookie } from './utils'\nimport { ServerValidateError } from './error'\nimport type {\n FormOptions,\n ValidationError,\n Validator,\n} from '@tanstack/form-core'\nimport type { FetchFn } from '@tanstack/start'\nimport type { ServerFormState } from './types'\n\ntype Ctx = Parameters<FetchFn<FormData, unknown>>[1]\n\ntype OnServerValidateFn<TFormData> = (props: {\n value: TFormData\n}) => ValidationError\n\ninterface CreateServerValidateOptions<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> extends FormOptions<TFormData, TFormValidator> {\n onServerValidate: OnServerValidateFn<TFormData>\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TFormValidator extends\n | Validator<TFormData, unknown>\n | undefined = undefined,\n >(\n defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>,\n ) =>\n async (ctx: Ctx, formData: FormData, info?: Parameters<typeof decode>[1]) => {\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 referer = ctx.request.headers.get('referer')!\n\n const data = decode(formData, info) as never as TFormData\n\n const onServerError = runValidator({ value: data })\n\n if (!onServerError) return\n\n const formState: ServerFormState<TFormData> = {\n errorMap: {\n onServer: onServerError,\n },\n values: data,\n errors: onServerError ? [onServerError] : [],\n }\n\n const cookie = await _tanstackInternalsCookie.serialize(formState)\n\n throw new ServerValidateError({\n response: new Response('ok', {\n headers: {\n Location: referer,\n 'Set-Cookie': cookie,\n },\n status: 302,\n }),\n formState: formState,\n })\n }\n"],"names":["decode","_tanstackInternalsCookie","ServerValidateError"],"mappings":";;;;;AAwBO,MAAM,uBACX,CAME,gBAEF,OAAO,KAAU,UAAoB,SAAwC;AACrE,QAAA,EAAE,kBAAkB,iBAAqB,IAAA;AAEzC,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;AAGvE,QAAM,UAAU,IAAI,QAAQ,QAAQ,IAAI,SAAS;AAE3C,QAAA,OAAOA,eAAAA,OAAO,UAAU,IAAI;AAElC,QAAM,gBAAgB,aAAa,EAAE,OAAO,KAAM,CAAA;AAElD,MAAI,CAAC;AAAe;AAEpB,QAAM,YAAwC;AAAA,IAC5C,UAAU;AAAA,MACR,UAAU;AAAA,IACZ;AAAA,IACA,QAAQ;AAAA,IACR,QAAQ,gBAAgB,CAAC,aAAa,IAAI,CAAC;AAAA,EAAA;AAG7C,QAAM,SAAS,MAAMC,MAAAA,yBAAyB,UAAU,SAAS;AAEjE,QAAM,IAAIC,MAAAA,oBAAoB;AAAA,IAC5B,UAAU,IAAI,SAAS,MAAM;AAAA,MAC3B,SAAS;AAAA,QACP,UAAU;AAAA,QACV,cAAc;AAAA,MAChB;AAAA,MACA,QAAQ;AAAA,IAAA,CACT;AAAA,IACD;AAAA,EAAA,CACD;AACH;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormOptions, ValidationError, Validator } from '@tanstack/form-core';
|
|
3
|
+
import { FetchFn } from '@tanstack/start';
|
|
4
|
+
|
|
5
|
+
type Ctx = Parameters<FetchFn<FormData, unknown>>[1];
|
|
6
|
+
type OnServerValidateFn<TFormData> = (props: {
|
|
7
|
+
value: TFormData;
|
|
8
|
+
}) => ValidationError;
|
|
9
|
+
interface CreateServerValidateOptions<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> extends FormOptions<TFormData, TFormValidator> {
|
|
10
|
+
onServerValidate: OnServerValidateFn<TFormData>;
|
|
11
|
+
}
|
|
12
|
+
export declare const createServerValidate: <TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts: CreateServerValidateOptions<TFormData, TFormValidator>) => (ctx: Ctx, formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<void>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class ServerValidateError extends Error {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
6
|
+
this.name = "ServerValidateError";
|
|
7
|
+
this.response = options.response;
|
|
8
|
+
this.formState = options.formState;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.ServerValidateError = ServerValidateError;
|
|
12
|
+
//# sourceMappingURL=error.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.cjs","sources":["../../../src/start/error.ts"],"sourcesContent":["import type { ServerFormState } from './types'\n\ninterface ServerValidateErrorState<TFormData> {\n formState: ServerFormState<TFormData>\n response: Response\n}\n\nexport class ServerValidateError<TFormData>\n extends Error\n implements ServerValidateErrorState<TFormData>\n{\n response: Response\n formState: ServerFormState<TFormData>\n\n constructor(options: ServerValidateErrorState<TFormData>) {\n super('Your form has errors. Please check the fields and try again.')\n this.name = 'ServerValidateError'\n this.response = options.response\n this.formState = options.formState\n }\n}\n"],"names":[],"mappings":";;AAOO,MAAM,4BACH,MAEV;AAAA,EAIE,YAAY,SAA8C;AACxD,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,WAAW,QAAQ;AACxB,SAAK,YAAY,QAAQ;AAAA,EAC3B;AACF;;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ServerFormState } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
interface ServerValidateErrorState<TFormData> {
|
|
4
|
+
formState: ServerFormState<TFormData>;
|
|
5
|
+
response: Response;
|
|
6
|
+
}
|
|
7
|
+
export declare class ServerValidateError<TFormData> extends Error implements ServerValidateErrorState<TFormData> {
|
|
8
|
+
response: Response;
|
|
9
|
+
formState: ServerFormState<TFormData>;
|
|
10
|
+
constructor(options: ServerValidateErrorState<TFormData>);
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const start = require("@tanstack/start");
|
|
4
|
+
const utils = require("./utils.cjs");
|
|
5
|
+
const initialFormState = {
|
|
6
|
+
errorMap: {
|
|
7
|
+
onServer: void 0
|
|
8
|
+
},
|
|
9
|
+
errors: []
|
|
10
|
+
};
|
|
11
|
+
const getFormData = start.createServerFn("GET", async (_, ctx) => {
|
|
12
|
+
const data = await utils._tanstackInternalsCookie.parse(
|
|
13
|
+
ctx.request.headers.get("Cookie")
|
|
14
|
+
);
|
|
15
|
+
ctx.request.headers.delete("Cookie");
|
|
16
|
+
if (!data)
|
|
17
|
+
return initialFormState;
|
|
18
|
+
return data;
|
|
19
|
+
});
|
|
20
|
+
exports.getFormData = getFormData;
|
|
21
|
+
exports.initialFormState = initialFormState;
|
|
22
|
+
//# sourceMappingURL=getFormData.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFormData.cjs","sources":["../../../src/start/getFormData.tsx"],"sourcesContent":["import { createServerFn } from '@tanstack/start'\nimport { _tanstackInternalsCookie } from './utils'\nimport type { ServerFormState } from './types'\n\nexport const initialFormState = {\n errorMap: {\n onServer: undefined,\n },\n errors: [],\n}\n\nexport const getFormData = createServerFn('GET', async (_, ctx) => {\n const data = (await _tanstackInternalsCookie.parse(\n ctx.request.headers.get('Cookie'),\n )) as undefined | ServerFormState<any>\n // Delete the cookie before it hits the client again¸\n ctx.request.headers.delete('Cookie')\n if (!data) return initialFormState\n return data\n})\n"],"names":["createServerFn","_tanstackInternalsCookie"],"mappings":";;;;AAIO,MAAM,mBAAmB;AAAA,EAC9B,UAAU;AAAA,IACR,UAAU;AAAA,EACZ;AAAA,EACA,QAAQ,CAAC;AACX;AAEO,MAAM,cAAcA,MAAAA,eAAe,OAAO,OAAO,GAAG,QAAQ;AAC3D,QAAA,OAAQ,MAAMC,MAAAA,yBAAyB;AAAA,IAC3C,IAAI,QAAQ,QAAQ,IAAI,QAAQ;AAAA,EAAA;AAG9B,MAAA,QAAQ,QAAQ,OAAO,QAAQ;AACnC,MAAI,CAAC;AAAa,WAAA;AACX,SAAA;AACT,CAAC;;;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ServerFormState } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
export declare const initialFormState: {
|
|
4
|
+
errorMap: {
|
|
5
|
+
onServer: undefined;
|
|
6
|
+
};
|
|
7
|
+
errors: never[];
|
|
8
|
+
};
|
|
9
|
+
export declare const getFormData: import('@tanstack/start').Fetcher<undefined, {
|
|
10
|
+
errorMap: {
|
|
11
|
+
onServer: undefined;
|
|
12
|
+
};
|
|
13
|
+
errors: never[];
|
|
14
|
+
} | ServerFormState<any>>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const createServerValidate = require("./createServerValidate.cjs");
|
|
4
|
+
const getFormData = require("./getFormData.cjs");
|
|
5
|
+
const error = require("./error.cjs");
|
|
6
|
+
exports.createServerValidate = createServerValidate.createServerValidate;
|
|
7
|
+
exports.getFormData = getFormData.getFormData;
|
|
8
|
+
exports.initialFormState = getFormData.initialFormState;
|
|
9
|
+
exports.ServerValidateError = error.ServerValidateError;
|
|
10
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const node = require("@remix-run/node");
|
|
4
|
+
const _tanstackInternalsCookie = node.createCookie("_tanstack_form_internals");
|
|
5
|
+
exports._tanstackInternalsCookie = _tanstackInternalsCookie;
|
|
6
|
+
//# sourceMappingURL=utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs","sources":["../../../src/start/utils.ts"],"sourcesContent":["import { createCookie } from '@remix-run/node'\n\nexport const _tanstackInternalsCookie = createCookie('_tanstack_form_internals')\n"],"names":["createCookie"],"mappings":";;;AAEa,MAAA,2BAA2BA,kBAAa,0BAA0B;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const _tanstackInternalsCookie: import('@remix-run/node').Cookie;
|
package/dist/cjs/useField.cjs
CHANGED
|
@@ -12,8 +12,9 @@ function useField(opts) {
|
|
|
12
12
|
form: opts.form,
|
|
13
13
|
name: opts.name
|
|
14
14
|
});
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
const extendedApi = api;
|
|
16
|
+
extendedApi.Field = Field;
|
|
17
|
+
return extendedApi;
|
|
17
18
|
});
|
|
18
19
|
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(fieldApi.mount, [fieldApi]);
|
|
19
20
|
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useField.cjs","sources":["../../src/useField.tsx"],"sourcesContent":["import React, { type FunctionComponent, useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport { FieldApi, functionalUpdate } from '@tanstack/form-core'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\nimport type { NodeType, UseFieldOptions } from './types'\nimport type { DeepKeys, DeepValue, Validator } from '@tanstack/form-core'\n\
|
|
1
|
+
{"version":3,"file":"useField.cjs","sources":["../../src/useField.tsx"],"sourcesContent":["import React, { type FunctionComponent, useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport { FieldApi, functionalUpdate } from '@tanstack/form-core'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\nimport type { NodeType, UseFieldOptions } from './types'\nimport type { DeepKeys, DeepValue, Validator } from '@tanstack/form-core'\n\ninterface ReactFieldApi<\n TParentData,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n> {\n /**\n * A pre-bound and type-safe sub-field component using this field as a root.\n */\n Field: FieldComponent<TParentData, TFormValidator>\n}\n\n/**\n * A type representing a hook for using a field in a form with the given form data type.\n *\n * A function that takes an optional object with a `name` property and field options, and returns a `FieldApi` instance for the specified field.\n */\nexport type UseField<\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 opts: Omit<\n UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>,\n 'form'\n >,\n) => FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>\n\n/**\n * A hook for managing a field in a form.\n * @param opts An object with field options.\n *\n * @returns The `FieldApi` instance for the specified field.\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 TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n>(\n opts: UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >,\n) {\n const [fieldApi] = useState(() => {\n const api = new FieldApi({\n ...opts,\n form: opts.form,\n name: opts.name,\n })\n\n const extendedApi: typeof api & ReactFieldApi<TParentData, TFormValidator> =\n api as never\n\n extendedApi.Field = Field as never\n\n return extendedApi\n })\n\n useIsomorphicLayoutEffect(fieldApi.mount, [fieldApi])\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)\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\n return fieldApi\n}\n\n/**\n * @param children A render function that takes a field API instance and returns a React element.\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 ) => NodeType\n} & UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>\n\n/**\n * A type alias representing a field component for a specific form data type.\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}: Omit<\n FieldComponentProps<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >,\n 'form'\n>) => NodeType\n\n/**\n * A function component that takes field options and a render function as children and returns a React component.\n *\n * The `Field` component uses the `useField` hook internally to manage the field instance.\n */\nexport const 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 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>): NodeType => {\n const fieldApi = useField(fieldOptions as any)\n\n return (<>{functionalUpdate(children, fieldApi as any)}</>) as never\n}) satisfies FunctionComponent<FieldComponentProps<any, any, any, any, any>>\n"],"names":["useState","FieldApi","useIsomorphicLayoutEffect","useStore","jsx","Fragment","functionalUpdate"],"mappings":";;;;;;;AAgDO,SAAS,SAWd,MAOA;AACA,QAAM,CAAC,QAAQ,IAAIA,MAAAA,SAAS,MAAM;AAC1B,UAAA,MAAM,IAAIC,kBAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM,KAAK;AAAA,MACX,MAAM,KAAK;AAAA,IAAA,CACZ;AAED,UAAM,cACJ;AAEF,gBAAY,QAAQ;AAEb,WAAA;AAAA,EAAA,CACR;AAEDC,4BAAAA,0BAA0B,SAAS,OAAO,CAAC,QAAQ,CAAC;AAMpDA,4BAAAA,0BAA0B,MAAM;AAC9B,aAAS,OAAO,IAAI;AAAA,EAAA,CACrB;AAEDC,aAAA;AAAA,IACE,SAAS;AAAA,IACT,KAAK,SAAS,UACV,CAAC,UAAU;AACF,aAAA,CAAC,MAAM,MAAM,OAAO,KAAK,MAAM,SAAS,CAAA,CAAE,EAAE,MAAM;AAAA,IAE3D,IAAA;AAAA,EAAA;AAGC,SAAA;AACT;AA4DO,MAAM,QAAS,CAUpB;AAAA,EACA;AAAA,EACA,GAAG;AACL,MAMgB;AACR,QAAA,WAAW,SAAS,YAAmB;AAE7C,SAAWC,2BAAA,IAAAC,WAAA,UAAA,EAAA,UAAAC,SAAA,iBAAiB,UAAU,QAAe,EAAE,CAAA;AACzD;;;"}
|
package/dist/cjs/useField.d.cts
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import { FieldApi, DeepKeys, DeepValue, Validator } from '@tanstack/form-core';
|
|
2
2
|
import { NodeType, UseFieldOptions } from './types.cjs';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
interface ReactFieldApi<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* A pre-bound and type-safe sub-field component using this field as a root.
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* A pre-bound and type-safe sub-field component using this field as a root.
|
|
11
|
-
*/
|
|
12
|
-
Field: FieldComponent<TParentData, TFormValidator>;
|
|
13
|
-
}
|
|
8
|
+
Field: FieldComponent<TParentData, TFormValidator>;
|
|
14
9
|
}
|
|
15
10
|
/**
|
|
16
11
|
* A type representing a hook for using a field in a form with the given form data type.
|
|
@@ -24,7 +19,7 @@ export type UseField<TParentData, TFormValidator extends Validator<TParentData,
|
|
|
24
19
|
*
|
|
25
20
|
* @returns The `FieldApi` instance for the specified field.
|
|
26
21
|
*/
|
|
27
|
-
export declare function useField<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>(opts: UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>): FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
|
22
|
+
export declare function useField<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>(opts: UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>): FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData> & ReactFieldApi<TParentData, TFormValidator>;
|
|
28
23
|
/**
|
|
29
24
|
* @param children A render function that takes a field API instance and returns a React element.
|
|
30
25
|
*/
|
package/dist/cjs/useForm.cjs
CHANGED
|
@@ -9,21 +9,22 @@ const useIsomorphicLayoutEffect = require("./useIsomorphicLayoutEffect.cjs");
|
|
|
9
9
|
function useForm(opts) {
|
|
10
10
|
const [formApi] = react.useState(() => {
|
|
11
11
|
const api = new formCore.FormApi(opts);
|
|
12
|
-
|
|
12
|
+
const extendedApi = api;
|
|
13
|
+
extendedApi.Field = function APIField(props) {
|
|
13
14
|
return /* @__PURE__ */ jsxRuntime.jsx(useField.Field, { ...props, form: api });
|
|
14
15
|
};
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
extendedApi.useField = (props) => useField.useField({ ...props, form: api });
|
|
17
|
+
extendedApi.useStore = (selector) => {
|
|
17
18
|
return reactStore.useStore(api.store, selector);
|
|
18
19
|
};
|
|
19
|
-
|
|
20
|
+
extendedApi.Subscribe = (props) => {
|
|
20
21
|
return formCore.functionalUpdate(
|
|
21
22
|
props.children,
|
|
22
23
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
23
24
|
reactStore.useStore(api.store, props.selector)
|
|
24
25
|
);
|
|
25
26
|
};
|
|
26
|
-
return
|
|
27
|
+
return extendedApi;
|
|
27
28
|
});
|
|
28
29
|
useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(formApi.mount, []);
|
|
29
30
|
formApi.useStore((state) => state.isSubmitting);
|
package/dist/cjs/useForm.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useForm.cjs","sources":["../../src/useForm.tsx"],"sourcesContent":["import { FormApi, functionalUpdate } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/react-store'\nimport React, { useState } from 'react'\nimport { Field, type FieldComponent, type UseField, useField } from './useField'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\nimport type { NoInfer } from '@tanstack/react-store'\nimport type { FormOptions, FormState, Validator } from '@tanstack/form-core'\nimport type { NodeType } from './types'\n\
|
|
1
|
+
{"version":3,"file":"useForm.cjs","sources":["../../src/useForm.tsx"],"sourcesContent":["import { FormApi, functionalUpdate } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/react-store'\nimport React, { useState } from 'react'\nimport { Field, type FieldComponent, type UseField, useField } from './useField'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\nimport type { NoInfer } from '@tanstack/react-store'\nimport type { FormOptions, FormState, Validator } from '@tanstack/form-core'\nimport type { NodeType } from './types'\n\n/**\n * Fields that are added onto the `FormAPI` from `@tanstack/form-core` and returned from `useForm`\n */\ninterface ReactFormApi<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> {\n /**\n * A React component to render form fields. With this, you can render and manage individual form fields.\n */\n Field: FieldComponent<TFormData, TFormValidator>\n /**\n * A custom React hook that provides functionalities related to individual form fields. It gives you access to field values, errors, and allows you to set or update field values.\n */\n useField: UseField<TFormData, TFormValidator>\n /**\n * A `useStore` hook that connects to the internal store of the form. It can be used to access the form's current state or any other related state information. You can optionally pass in a selector function to cherry-pick specific parts of the state\n */\n useStore: <TSelected = NoInfer<FormState<TFormData>>>(\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n ) => TSelected\n /**\n * A `Subscribe` function that allows you to listen and react to changes in the form's state. It's especially useful when you need to execute side effects or render specific components in response to state updates.\n */\n Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {\n /**\n TypeScript versions <=5.0.4 have a bug that prevents\n the type of the `TSelected` generic from being inferred\n from the return type of this method.\n\n In these versions, `TSelected` will fall back to the default\n type (or `unknown` if that's not defined).\n\n @see {@link https://github.com/TanStack/form/pull/606/files#r1506715714 | This discussion on GitHub for the details}\n @see {@link https://github.com/microsoft/TypeScript/issues/52786 | The bug report in `microsoft/TypeScript`}\n */\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected\n children: ((state: NoInfer<TSelected>) => NodeType) | NodeType\n }) => NodeType\n}\n\n/**\n * A custom React Hook that returns an extended instance of the `FormApi` class.\n *\n * This API encapsulates all the necessary functionalities related to the form. It allows you to manage form state, handle submissions, and interact with form fields\n */\nexport function useForm<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(opts?: FormOptions<TFormData, TFormValidator>) {\n const [formApi] = useState(() => {\n const api = new FormApi<TFormData, TFormValidator>(opts)\n\n const extendedApi: typeof api & ReactFormApi<TFormData, TFormValidator> =\n api as never\n extendedApi.Field = function APIField(props) {\n return (<Field {...props} form={api} />) as never\n }\n // eslint-disable-next-line react-hooks/rules-of-hooks\n extendedApi.useField = (props) => useField({ ...props, form: api })\n extendedApi.useStore = (selector) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return useStore(api.store as any, selector as any) as any\n }\n extendedApi.Subscribe = (props) => {\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 extendedApi\n })\n\n useIsomorphicLayoutEffect(formApi.mount, [])\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\n}\n"],"names":["useState","FormApi","jsx","Field","useField","useStore","functionalUpdate","useIsomorphicLayoutEffect"],"mappings":";;;;;;;;AAuDO,SAAS,QAGd,MAA+C;AAC/C,QAAM,CAAC,OAAO,IAAIA,MAAAA,SAAS,MAAM;AACzB,UAAA,MAAM,IAAIC,iBAAmC,IAAI;AAEvD,UAAM,cACJ;AACU,gBAAA,QAAQ,SAAS,SAAS,OAAO;AAC3C,aAASC,2BAAAA,IAAAC,SAAAA,OAAA,EAAO,GAAG,OAAO,MAAM,IAAK,CAAA;AAAA,IAAA;AAG3B,gBAAA,WAAW,CAAC,UAAUC,SAAA,SAAS,EAAE,GAAG,OAAO,MAAM,IAAA,CAAK;AACtD,gBAAA,WAAW,CAAC,aAAa;AAE5B,aAAAC,oBAAS,IAAI,OAAc,QAAe;AAAA,IAAA;AAEvC,gBAAA,YAAY,CAAC,UAAU;AAC1B,aAAAC,SAAA;AAAA,QACL,MAAM;AAAA;AAAA,QAEND,WAAAA,SAAS,IAAI,OAAc,MAAM,QAAe;AAAA,MAAA;AAAA,IAClD;AAGK,WAAA;AAAA,EAAA,CACR;AAEyBE,4BAAAA,0BAAA,QAAQ,OAAO,CAAA,CAAE;AAE3C,UAAQ,SAAS,CAAC,UAAU,MAAM,YAAY;AAM9CA,4BAAAA,0BAA0B,MAAM;AAC9B,YAAQ,OAAO,IAAI;AAAA,EAAA,CACpB;AAEM,SAAA;AACT;;"}
|
package/dist/cjs/useForm.d.cts
CHANGED
|
@@ -3,46 +3,45 @@ import { FieldComponent, UseField } from './useField.cjs';
|
|
|
3
3
|
import { NoInfer } from '@tanstack/react-store';
|
|
4
4
|
import { NodeType } from './types.cjs';
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Fields that are added onto the `FormAPI` from `@tanstack/form-core` and returned from `useForm`
|
|
8
|
+
*/
|
|
9
|
+
interface ReactFormApi<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> {
|
|
7
10
|
/**
|
|
8
|
-
*
|
|
11
|
+
* A React component to render form fields. With this, you can render and manage individual form fields.
|
|
9
12
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
Field: FieldComponent<TFormData, TFormValidator>;
|
|
14
|
+
/**
|
|
15
|
+
* A custom React hook that provides functionalities related to individual form fields. It gives you access to field values, errors, and allows you to set or update field values.
|
|
16
|
+
*/
|
|
17
|
+
useField: UseField<TFormData, TFormValidator>;
|
|
18
|
+
/**
|
|
19
|
+
* A `useStore` hook that connects to the internal store of the form. It can be used to access the form's current state or any other related state information. You can optionally pass in a selector function to cherry-pick specific parts of the state
|
|
20
|
+
*/
|
|
21
|
+
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => TSelected;
|
|
22
|
+
/**
|
|
23
|
+
* A `Subscribe` function that allows you to listen and react to changes in the form's state. It's especially useful when you need to execute side effects or render specific components in response to state updates.
|
|
24
|
+
*/
|
|
25
|
+
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
|
23
26
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
*/
|
|
38
|
-
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
|
39
|
-
children: ((state: NoInfer<TSelected>) => NodeType) | NodeType;
|
|
40
|
-
}) => NodeType;
|
|
41
|
-
}
|
|
27
|
+
TypeScript versions <=5.0.4 have a bug that prevents
|
|
28
|
+
the type of the `TSelected` generic from being inferred
|
|
29
|
+
from the return type of this method.
|
|
30
|
+
|
|
31
|
+
In these versions, `TSelected` will fall back to the default
|
|
32
|
+
type (or `unknown` if that's not defined).
|
|
33
|
+
|
|
34
|
+
@see {@link https://github.com/TanStack/form/pull/606/files#r1506715714 | This discussion on GitHub for the details}
|
|
35
|
+
@see {@link https://github.com/microsoft/TypeScript/issues/52786 | The bug report in `microsoft/TypeScript`}
|
|
36
|
+
*/
|
|
37
|
+
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
|
38
|
+
children: ((state: NoInfer<TSelected>) => NodeType) | NodeType;
|
|
39
|
+
}) => NodeType;
|
|
42
40
|
}
|
|
43
41
|
/**
|
|
44
|
-
* A custom React Hook that returns an instance of the `FormApi` class.
|
|
42
|
+
* A custom React Hook that returns an extended instance of the `FormApi` class.
|
|
45
43
|
*
|
|
46
44
|
* This API encapsulates all the necessary functionalities related to the form. It allows you to manage form state, handle submissions, and interact with form fields
|
|
47
45
|
*/
|
|
48
|
-
export declare function useForm<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(opts?: FormOptions<TFormData, TFormValidator>): FormApi<TFormData, TFormValidator>;
|
|
46
|
+
export declare function useForm<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(opts?: FormOptions<TFormData, TFormValidator>): FormApi<TFormData, TFormValidator> & ReactFormApi<TFormData, TFormValidator>;
|
|
47
|
+
export {};
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const react = require("react");
|
|
4
|
-
const useIsomorphicLayoutEffect =
|
|
5
|
-
// @ts-ignore
|
|
6
|
-
typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect
|
|
7
|
-
);
|
|
4
|
+
const useIsomorphicLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect;
|
|
8
5
|
exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect;
|
|
9
6
|
//# sourceMappingURL=useIsomorphicLayoutEffect.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIsomorphicLayoutEffect.cjs","sources":["../../src/useIsomorphicLayoutEffect.ts"],"sourcesContent":["import { useEffect, useLayoutEffect } from 'react'\n\nexport const useIsomorphicLayoutEffect =\n
|
|
1
|
+
{"version":3,"file":"useIsomorphicLayoutEffect.cjs","sources":["../../src/useIsomorphicLayoutEffect.ts"],"sourcesContent":["import { useEffect, useLayoutEffect } from 'react'\n\nexport const useIsomorphicLayoutEffect =\n typeof window !== 'undefined' ? useLayoutEffect : useEffect\n"],"names":["useLayoutEffect","useEffect"],"mappings":";;;AAEO,MAAM,4BACX,OAAO,WAAW,cAAcA,wBAAkBC,MAAAA;;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,5 +2,4 @@ export * from '@tanstack/form-core';
|
|
|
2
2
|
export { useForm } from './useForm.js';
|
|
3
3
|
export type { UseField, FieldComponent } from './useField.js';
|
|
4
4
|
export { useField, Field } from './useField.js';
|
|
5
|
-
export { createServerValidate, initialFormState } from './createServerValidate.js';
|
|
6
5
|
export { useTransform } from './useTransform.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
export * from "@tanstack/form-core";
|
|
2
2
|
import { useForm } from "./useForm.js";
|
|
3
3
|
import { Field, useField } from "./useField.js";
|
|
4
|
-
import { createServerValidate, initialFormState } from "./createServerValidate.js";
|
|
5
4
|
import { useTransform } from "./useTransform.js";
|
|
6
5
|
export {
|
|
7
6
|
Field,
|
|
8
|
-
createServerValidate,
|
|
9
|
-
initialFormState,
|
|
10
7
|
useField,
|
|
11
8
|
useForm,
|
|
12
9
|
useTransform
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|