@tanstack/react-form-remix 1.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/LICENSE +21 -0
- package/README.md +35 -0
- package/dist/esm/createServerValidate.d.ts +8 -0
- package/dist/esm/createServerValidate.js +51 -0
- package/dist/esm/createServerValidate.js.map +1 -0
- package/dist/esm/error.d.ts +9 -0
- package/dist/esm/error.js +11 -0
- package/dist/esm/error.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/useTransform.d.ts +2 -0
- package/dist/esm/useTransform.js +10 -0
- package/dist/esm/useTransform.js.map +1 -0
- package/package.json +69 -0
- package/src/createServerValidate.ts +135 -0
- package/src/error.ts +27 -0
- package/src/index.ts +5 -0
- package/src/useTransform.ts +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-present Tanner Linsley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<img src="https://static.scarf.sh/a.png?x-pxid=be2d8a11-9712-4c1d-9963-580b2d4fb133" />
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Hooks for managing form state in React
|
|
6
|
+
|
|
7
|
+
<a href="https://twitter.com/intent/tweet?button_hashtag=TanStack" target="\_parent">
|
|
8
|
+
<img alt="#TanStack" src="https://img.shields.io/twitter/url?color=%2308a0e9&label=%23TanStack&style=social&url=https%3A%2F%2Ftwitter.com%2Fintent%2Ftweet%3Fbutton_hashtag%3DTanStack">
|
|
9
|
+
</a><a href="https://discord.com/invite/WrRKjPJ" target="\_parent">
|
|
10
|
+
<img alt="" src="https://img.shields.io/badge/Discord-TanStack-%235865F2" />
|
|
11
|
+
</a><a href="https://github.com/TanStack/form/actions?query=workflow%3A%22react-form+tests%22">
|
|
12
|
+
<img src="https://github.com/TanStack/form/workflows/react-form%20tests/badge.svg" />
|
|
13
|
+
</a><a href="https://www.npmjs.com/package/@tanstack/form-core" target="\_parent">
|
|
14
|
+
<img alt="" src="https://img.shields.io/npm/dm/@tanstack/form-core.svg" />
|
|
15
|
+
</a><a href="https://bundlephobia.com/package/@tanstack/react-form@latest" target="\_parent">
|
|
16
|
+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/react-form" />
|
|
17
|
+
</a><a href="#badge">
|
|
18
|
+
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
|
|
19
|
+
</a><a href="https://github.com/TanStack/form/discussions">
|
|
20
|
+
<img alt="Join the discussion on Github" src="https://img.shields.io/badge/Github%20Discussions%20%26%20Support-Chat%20now!-blue" />
|
|
21
|
+
</a><a href="https://bestofjs.org/projects/tanstack-form"><img alt="Best of JS" src="https://img.shields.io/endpoint?url=https://bestofjs-serverless.now.sh/api/project-badge?fullName=TanStack%form%26since=daily" /></a><a href="https://github.com/TanStack/form/" target="\_parent">
|
|
22
|
+
<img alt="" src="https://img.shields.io/github/stars/TanStack/form.svg?style=social&label=Star" />
|
|
23
|
+
</a><a href="https://twitter.com/tannerlinsley" target="\_parent">
|
|
24
|
+
<img alt="" src="https://img.shields.io/twitter/follow/tannerlinsley.svg?style=social&label=Follow" />
|
|
25
|
+
</a> <a href="https://gitpod.io/from-referrer/">
|
|
26
|
+
<img src="https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod" alt="Gitpod Ready-to-Code"/>
|
|
27
|
+
</a>
|
|
28
|
+
|
|
29
|
+
Enjoy this library? Try the entire [TanStack](https://tanstack.com)! [TanStack Table](https://github.com/TanStack/table), [TanStack Router](https://github.com/tanstack/router), [TanStack Virtual](https://github.com/tanstack/virtual), [React Charts](https://github.com/TanStack/react-charts), [React Ranger](https://github.com/TanStack/ranger)
|
|
30
|
+
|
|
31
|
+
## Visit [tanstack.com/form](https://tanstack.com/form) for docs, guides, API and more!
|
|
32
|
+
|
|
33
|
+
### [Become a Sponsor!](https://github.com/sponsors/tannerlinsley/)
|
|
34
|
+
|
|
35
|
+
<!-- Use the force, Luke -->
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata';
|
|
2
|
+
import { FormAsyncValidateOrFn, FormOptions, FormValidateOrFn, ServerFormState } from '@tanstack/react-form';
|
|
3
|
+
interface CreateServerValidateOptions<TFormData, TOnMount extends undefined | FormValidateOrFn<TFormData>, TOnChange extends undefined | FormValidateOrFn<TFormData>, TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnBlur extends undefined | FormValidateOrFn<TFormData>, TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnSubmit extends undefined | FormValidateOrFn<TFormData>, TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnDynamic extends undefined | FormValidateOrFn<TFormData>, TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>, TSubmitMeta> extends FormOptions<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta> {
|
|
4
|
+
onServerValidate: TOnServer;
|
|
5
|
+
}
|
|
6
|
+
export declare const createServerValidate: <TFormData, TOnMount extends undefined | FormValidateOrFn<TFormData>, TOnChange extends undefined | FormValidateOrFn<TFormData>, TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnBlur extends undefined | FormValidateOrFn<TFormData>, TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnSubmit extends undefined | FormValidateOrFn<TFormData>, TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnDynamic extends undefined | FormValidateOrFn<TFormData>, TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>, TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>, TSubmitMeta>(defaultOpts: CreateServerValidateOptions<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>) => (formData: FormData, info?: Parameters<typeof decode>[1]) => Promise<TFormData>;
|
|
7
|
+
export declare const initialFormState: ServerFormState<any, undefined>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { decode } from "decode-formdata";
|
|
2
|
+
import { isGlobalFormValidationError, isStandardSchemaValidator, standardSchemaValidators } from "@tanstack/react-form";
|
|
3
|
+
import { ServerValidateError } from "./error.js";
|
|
4
|
+
const createServerValidate = (defaultOpts) => async (formData, info) => {
|
|
5
|
+
const { onServerValidate } = defaultOpts;
|
|
6
|
+
const runValidator = async ({
|
|
7
|
+
value,
|
|
8
|
+
validationSource
|
|
9
|
+
}) => {
|
|
10
|
+
if (isStandardSchemaValidator(onServerValidate)) {
|
|
11
|
+
return await standardSchemaValidators.validateAsync(
|
|
12
|
+
{ value, validationSource },
|
|
13
|
+
onServerValidate
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
return onServerValidate({
|
|
17
|
+
value,
|
|
18
|
+
signal: void 0,
|
|
19
|
+
formApi: void 0
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
const values = decode(formData, info);
|
|
23
|
+
const onServerError = await runValidator({
|
|
24
|
+
value: values,
|
|
25
|
+
validationSource: "form"
|
|
26
|
+
});
|
|
27
|
+
if (!onServerError) return values;
|
|
28
|
+
const onServerErrorVal = isGlobalFormValidationError(onServerError) ? onServerError.form : onServerError;
|
|
29
|
+
const formState = {
|
|
30
|
+
errorMap: {
|
|
31
|
+
onServer: onServerError
|
|
32
|
+
},
|
|
33
|
+
values,
|
|
34
|
+
errors: onServerErrorVal ? [onServerErrorVal] : []
|
|
35
|
+
};
|
|
36
|
+
throw new ServerValidateError({
|
|
37
|
+
formState
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const initialFormState = {
|
|
41
|
+
errorMap: {
|
|
42
|
+
onServer: void 0
|
|
43
|
+
},
|
|
44
|
+
values: void 0,
|
|
45
|
+
errors: []
|
|
46
|
+
};
|
|
47
|
+
export {
|
|
48
|
+
createServerValidate,
|
|
49
|
+
initialFormState
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=createServerValidate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createServerValidate.js","sources":["../../src/createServerValidate.ts"],"sourcesContent":["import { decode } from 'decode-formdata'\nimport {\n isGlobalFormValidationError,\n isStandardSchemaValidator,\n standardSchemaValidators,\n} from '@tanstack/react-form'\nimport { ServerValidateError } from './error'\n\nimport type {\n FormAsyncValidateOrFn,\n FormOptions,\n FormValidateAsyncFn,\n FormValidateOrFn,\n ServerFormState,\n UnwrapFormAsyncValidateOrFn,\n} from '@tanstack/react-form'\n\ninterface CreateServerValidateOptions<\n TFormData,\n TOnMount extends undefined | FormValidateOrFn<TFormData>,\n TOnChange extends undefined | FormValidateOrFn<TFormData>,\n TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,\n TOnBlur extends undefined | FormValidateOrFn<TFormData>,\n TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,\n TOnSubmit extends undefined | FormValidateOrFn<TFormData>,\n TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,\n TOnDynamic extends undefined | FormValidateOrFn<TFormData>,\n TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,\n TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,\n TSubmitMeta,\n> extends FormOptions<\n TFormData,\n TOnMount,\n TOnChange,\n TOnChangeAsync,\n TOnBlur,\n TOnBlurAsync,\n TOnSubmit,\n TOnSubmitAsync,\n TOnDynamic,\n TOnDynamicAsync,\n TOnServer,\n TSubmitMeta\n > {\n onServerValidate: TOnServer\n}\n\nexport const createServerValidate =\n <\n TFormData,\n TOnMount extends undefined | FormValidateOrFn<TFormData>,\n TOnChange extends undefined | FormValidateOrFn<TFormData>,\n TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,\n TOnBlur extends undefined | FormValidateOrFn<TFormData>,\n TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,\n TOnSubmit extends undefined | FormValidateOrFn<TFormData>,\n TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,\n TOnDynamic extends undefined | FormValidateOrFn<TFormData>,\n TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,\n TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,\n TSubmitMeta,\n >(\n defaultOpts: CreateServerValidateOptions<\n TFormData,\n TOnMount,\n TOnChange,\n TOnChangeAsync,\n TOnBlur,\n TOnBlurAsync,\n TOnSubmit,\n TOnSubmitAsync,\n TOnDynamic,\n TOnDynamicAsync,\n TOnServer,\n TSubmitMeta\n >,\n ) =>\n async (formData: FormData, info?: Parameters<typeof decode>[1]) => {\n const { onServerValidate } = defaultOpts\n\n const runValidator = async ({\n value,\n validationSource,\n }: {\n value: TFormData\n validationSource: 'form'\n }) => {\n if (isStandardSchemaValidator(onServerValidate)) {\n return await standardSchemaValidators.validateAsync(\n { value, validationSource },\n onServerValidate,\n )\n }\n return (onServerValidate as FormValidateAsyncFn<TFormData>)({\n value,\n signal: undefined as never,\n formApi: undefined as never,\n })\n }\n\n const values = decode(formData, info) as never as TFormData\n\n const onServerError = (await runValidator({\n value: values,\n validationSource: 'form',\n })) as UnwrapFormAsyncValidateOrFn<TOnServer> | undefined\n\n if (!onServerError) return values\n\n const onServerErrorVal = (\n isGlobalFormValidationError(onServerError)\n ? onServerError.form\n : onServerError\n ) as UnwrapFormAsyncValidateOrFn<TOnServer>\n\n const formState: ServerFormState<TFormData, TOnServer> = {\n errorMap: {\n onServer: onServerError,\n },\n values,\n errors: onServerErrorVal ? [onServerErrorVal] : [],\n }\n\n throw new ServerValidateError({\n formState,\n })\n }\n\nexport const initialFormState: ServerFormState<any, undefined> = {\n errorMap: {\n onServer: undefined,\n },\n values: undefined,\n errors: [],\n}\n"],"names":[],"mappings":";;;AA+CO,MAAM,uBACX,CAcE,gBAeF,OAAO,UAAoB,SAAwC;AACjE,QAAM,EAAE,qBAAqB;AAE7B,QAAM,eAAe,OAAO;AAAA,IAC1B;AAAA,IACA;AAAA,EAAA,MAII;AACJ,QAAI,0BAA0B,gBAAgB,GAAG;AAC/C,aAAO,MAAM,yBAAyB;AAAA,QACpC,EAAE,OAAO,iBAAA;AAAA,QACT;AAAA,MAAA;AAAA,IAEJ;AACA,WAAQ,iBAAoD;AAAA,MAC1D;AAAA,MACA,QAAQ;AAAA,MACR,SAAS;AAAA,IAAA,CACV;AAAA,EACH;AAEA,QAAM,SAAS,OAAO,UAAU,IAAI;AAEpC,QAAM,gBAAiB,MAAM,aAAa;AAAA,IACxC,OAAO;AAAA,IACP,kBAAkB;AAAA,EAAA,CACnB;AAED,MAAI,CAAC,cAAe,QAAO;AAE3B,QAAM,mBACJ,4BAA4B,aAAa,IACrC,cAAc,OACd;AAGN,QAAM,YAAmD;AAAA,IACvD,UAAU;AAAA,MACR,UAAU;AAAA,IAAA;AAAA,IAEZ;AAAA,IACA,QAAQ,mBAAmB,CAAC,gBAAgB,IAAI,CAAA;AAAA,EAAC;AAGnD,QAAM,IAAI,oBAAoB;AAAA,IAC5B;AAAA,EAAA,CACD;AACH;AAEK,MAAM,mBAAoD;AAAA,EAC/D,UAAU;AAAA,IACR,UAAU;AAAA,EAAA;AAAA,EAEZ,QAAQ;AAAA,EACR,QAAQ,CAAA;AACV;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FormAsyncValidateOrFn, ServerFormState } from '@tanstack/react-form';
|
|
2
|
+
interface ServerValidateErrorState<TFormData, TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>> {
|
|
3
|
+
formState: ServerFormState<TFormData, TOnServer>;
|
|
4
|
+
}
|
|
5
|
+
export declare class ServerValidateError<TFormData, TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>> extends Error implements ServerValidateErrorState<TFormData, TOnServer> {
|
|
6
|
+
formState: ServerFormState<TFormData, TOnServer>;
|
|
7
|
+
constructor(options: ServerValidateErrorState<TFormData, TOnServer>);
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
class ServerValidateError extends Error {
|
|
2
|
+
constructor(options) {
|
|
3
|
+
super("Your form has errors. Please check the fields and try again.");
|
|
4
|
+
this.name = "ServerValidateError";
|
|
5
|
+
this.formState = options.formState;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
ServerValidateError
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error.js","sources":["../../src/error.ts"],"sourcesContent":["import type {\n FormAsyncValidateOrFn,\n ServerFormState,\n} from '@tanstack/react-form'\n\ninterface ServerValidateErrorState<\n TFormData,\n TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,\n> {\n formState: ServerFormState<TFormData, TOnServer>\n}\n\nexport class ServerValidateError<\n TFormData,\n TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,\n >\n extends Error\n implements ServerValidateErrorState<TFormData, TOnServer>\n{\n formState: ServerFormState<TFormData, TOnServer>\n\n constructor(options: ServerValidateErrorState<TFormData, TOnServer>) {\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":"AAYO,MAAM,4BAIH,MAEV;AAAA,EAGE,YAAY,SAAyD;AACnE,UAAM,8DAA8D;AACpE,SAAK,OAAO;AACZ,SAAK,YAAY,QAAQ;AAAA,EAC3B;AACF;"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "@tanstack/react-form";
|
|
2
|
+
import { createServerValidate, initialFormState } from "./createServerValidate.js";
|
|
3
|
+
import { ServerValidateError } from "./error.js";
|
|
4
|
+
import { useTransform } from "./useTransform.js";
|
|
5
|
+
export {
|
|
6
|
+
ServerValidateError,
|
|
7
|
+
createServerValidate,
|
|
8
|
+
initialFormState,
|
|
9
|
+
useTransform
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTransform.js","sources":["../../src/useTransform.ts"],"sourcesContent":["import type { AnyFormApi, FormTransform } from '@tanstack/react-form'\n\nexport function useTransform(\n fn: (formBase: AnyFormApi) => AnyFormApi,\n deps: unknown[],\n): FormTransform<any, any, any, any, any, any, any, any, any, any, any, any> {\n return {\n fn,\n deps,\n }\n}\n"],"names":[],"mappings":"AAEO,SAAS,aACd,IACA,MAC2E;AAC3E,SAAO;AAAA,IACL;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tanstack/react-form-remix",
|
|
3
|
+
"version": "1.25.0",
|
|
4
|
+
"description": "Powerful, type-safe forms for React.",
|
|
5
|
+
"author": "tannerlinsley",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/TanStack/form.git",
|
|
10
|
+
"directory": "packages/react-form-remix"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://tanstack.com/form",
|
|
13
|
+
"funding": {
|
|
14
|
+
"type": "github",
|
|
15
|
+
"url": "https://github.com/sponsors/tannerlinsley"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"types": "dist/esm/index.d.ts",
|
|
19
|
+
"module": "dist/esm/index.js",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/esm/index.d.ts",
|
|
24
|
+
"default": "./dist/esm/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"src"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"decode-formdata": "^0.9.0",
|
|
36
|
+
"@tanstack/react-form": "1.25.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@tanstack/react-start": "^1.134.9",
|
|
40
|
+
"@types/react": "^19.0.7",
|
|
41
|
+
"@vitejs/plugin-react": "^5.1.1",
|
|
42
|
+
"eslint-plugin-react-compiler": "19.1.0-rc.2",
|
|
43
|
+
"react": "^19.0.0",
|
|
44
|
+
"vite": "^7.2.2"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@tanstack/react-start": "^1.134.9",
|
|
48
|
+
"react": "^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependenciesMeta": {
|
|
51
|
+
"@tanstack/react-start": {
|
|
52
|
+
"optional": true
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"clean": "premove ./dist ./coverage",
|
|
57
|
+
"test:eslint": "eslint ./src ./tests",
|
|
58
|
+
"test:types": "pnpm run \"/^test:types:ts[0-9]{2}$/\"",
|
|
59
|
+
"test:types:ts54": "node ../../node_modules/typescript54/lib/tsc.js",
|
|
60
|
+
"test:types:ts55": "node ../../node_modules/typescript55/lib/tsc.js",
|
|
61
|
+
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js",
|
|
62
|
+
"test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js",
|
|
63
|
+
"test:types:ts58": "tsc",
|
|
64
|
+
"test:lib": "vitest",
|
|
65
|
+
"test:lib:dev": "pnpm run test:lib --watch",
|
|
66
|
+
"test:build": "publint --strict",
|
|
67
|
+
"build": "vite build"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { decode } from 'decode-formdata'
|
|
2
|
+
import {
|
|
3
|
+
isGlobalFormValidationError,
|
|
4
|
+
isStandardSchemaValidator,
|
|
5
|
+
standardSchemaValidators,
|
|
6
|
+
} from '@tanstack/react-form'
|
|
7
|
+
import { ServerValidateError } from './error'
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
FormAsyncValidateOrFn,
|
|
11
|
+
FormOptions,
|
|
12
|
+
FormValidateAsyncFn,
|
|
13
|
+
FormValidateOrFn,
|
|
14
|
+
ServerFormState,
|
|
15
|
+
UnwrapFormAsyncValidateOrFn,
|
|
16
|
+
} from '@tanstack/react-form'
|
|
17
|
+
|
|
18
|
+
interface CreateServerValidateOptions<
|
|
19
|
+
TFormData,
|
|
20
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
21
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
22
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
23
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
24
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
25
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
26
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
27
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
28
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
29
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
30
|
+
TSubmitMeta,
|
|
31
|
+
> extends FormOptions<
|
|
32
|
+
TFormData,
|
|
33
|
+
TOnMount,
|
|
34
|
+
TOnChange,
|
|
35
|
+
TOnChangeAsync,
|
|
36
|
+
TOnBlur,
|
|
37
|
+
TOnBlurAsync,
|
|
38
|
+
TOnSubmit,
|
|
39
|
+
TOnSubmitAsync,
|
|
40
|
+
TOnDynamic,
|
|
41
|
+
TOnDynamicAsync,
|
|
42
|
+
TOnServer,
|
|
43
|
+
TSubmitMeta
|
|
44
|
+
> {
|
|
45
|
+
onServerValidate: TOnServer
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const createServerValidate =
|
|
49
|
+
<
|
|
50
|
+
TFormData,
|
|
51
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
52
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
53
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
54
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
55
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
56
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
57
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
58
|
+
TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
|
|
59
|
+
TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
60
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
61
|
+
TSubmitMeta,
|
|
62
|
+
>(
|
|
63
|
+
defaultOpts: CreateServerValidateOptions<
|
|
64
|
+
TFormData,
|
|
65
|
+
TOnMount,
|
|
66
|
+
TOnChange,
|
|
67
|
+
TOnChangeAsync,
|
|
68
|
+
TOnBlur,
|
|
69
|
+
TOnBlurAsync,
|
|
70
|
+
TOnSubmit,
|
|
71
|
+
TOnSubmitAsync,
|
|
72
|
+
TOnDynamic,
|
|
73
|
+
TOnDynamicAsync,
|
|
74
|
+
TOnServer,
|
|
75
|
+
TSubmitMeta
|
|
76
|
+
>,
|
|
77
|
+
) =>
|
|
78
|
+
async (formData: FormData, info?: Parameters<typeof decode>[1]) => {
|
|
79
|
+
const { onServerValidate } = defaultOpts
|
|
80
|
+
|
|
81
|
+
const runValidator = async ({
|
|
82
|
+
value,
|
|
83
|
+
validationSource,
|
|
84
|
+
}: {
|
|
85
|
+
value: TFormData
|
|
86
|
+
validationSource: 'form'
|
|
87
|
+
}) => {
|
|
88
|
+
if (isStandardSchemaValidator(onServerValidate)) {
|
|
89
|
+
return await standardSchemaValidators.validateAsync(
|
|
90
|
+
{ value, validationSource },
|
|
91
|
+
onServerValidate,
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
return (onServerValidate as FormValidateAsyncFn<TFormData>)({
|
|
95
|
+
value,
|
|
96
|
+
signal: undefined as never,
|
|
97
|
+
formApi: undefined as never,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const values = decode(formData, info) as never as TFormData
|
|
102
|
+
|
|
103
|
+
const onServerError = (await runValidator({
|
|
104
|
+
value: values,
|
|
105
|
+
validationSource: 'form',
|
|
106
|
+
})) as UnwrapFormAsyncValidateOrFn<TOnServer> | undefined
|
|
107
|
+
|
|
108
|
+
if (!onServerError) return values
|
|
109
|
+
|
|
110
|
+
const onServerErrorVal = (
|
|
111
|
+
isGlobalFormValidationError(onServerError)
|
|
112
|
+
? onServerError.form
|
|
113
|
+
: onServerError
|
|
114
|
+
) as UnwrapFormAsyncValidateOrFn<TOnServer>
|
|
115
|
+
|
|
116
|
+
const formState: ServerFormState<TFormData, TOnServer> = {
|
|
117
|
+
errorMap: {
|
|
118
|
+
onServer: onServerError,
|
|
119
|
+
},
|
|
120
|
+
values,
|
|
121
|
+
errors: onServerErrorVal ? [onServerErrorVal] : [],
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
throw new ServerValidateError({
|
|
125
|
+
formState,
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export const initialFormState: ServerFormState<any, undefined> = {
|
|
130
|
+
errorMap: {
|
|
131
|
+
onServer: undefined,
|
|
132
|
+
},
|
|
133
|
+
values: undefined,
|
|
134
|
+
errors: [],
|
|
135
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FormAsyncValidateOrFn,
|
|
3
|
+
ServerFormState,
|
|
4
|
+
} from '@tanstack/react-form'
|
|
5
|
+
|
|
6
|
+
interface ServerValidateErrorState<
|
|
7
|
+
TFormData,
|
|
8
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
9
|
+
> {
|
|
10
|
+
formState: ServerFormState<TFormData, TOnServer>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class ServerValidateError<
|
|
14
|
+
TFormData,
|
|
15
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
16
|
+
>
|
|
17
|
+
extends Error
|
|
18
|
+
implements ServerValidateErrorState<TFormData, TOnServer>
|
|
19
|
+
{
|
|
20
|
+
formState: ServerFormState<TFormData, TOnServer>
|
|
21
|
+
|
|
22
|
+
constructor(options: ServerValidateErrorState<TFormData, TOnServer>) {
|
|
23
|
+
super('Your form has errors. Please check the fields and try again.')
|
|
24
|
+
this.name = 'ServerValidateError'
|
|
25
|
+
this.formState = options.formState
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { AnyFormApi, FormTransform } from '@tanstack/react-form'
|
|
2
|
+
|
|
3
|
+
export function useTransform(
|
|
4
|
+
fn: (formBase: AnyFormApi) => AnyFormApi,
|
|
5
|
+
deps: unknown[],
|
|
6
|
+
): FormTransform<any, any, any, any, any, any, any, any, any, any, any, any> {
|
|
7
|
+
return {
|
|
8
|
+
fn,
|
|
9
|
+
deps,
|
|
10
|
+
}
|
|
11
|
+
}
|