@tanstack/vue-form 0.2.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/build/legacy/createFormFactory-161e85f9.d.ts +42 -0
- package/build/legacy/createFormFactory.cjs +42 -0
- package/build/legacy/createFormFactory.cjs.map +1 -0
- package/build/legacy/createFormFactory.d.cts +3 -0
- package/build/legacy/createFormFactory.d.ts +3 -0
- package/build/legacy/createFormFactory.js +17 -0
- package/build/legacy/createFormFactory.js.map +1 -0
- package/build/legacy/formContext.cjs +46 -0
- package/build/legacy/formContext.cjs.map +1 -0
- package/build/legacy/formContext.d.cts +14 -0
- package/build/legacy/formContext.d.ts +14 -0
- package/build/legacy/formContext.js +19 -0
- package/build/legacy/formContext.js.map +1 -0
- package/build/legacy/index.cjs +33 -0
- package/build/legacy/index.cjs.map +1 -0
- package/build/legacy/index.d.cts +23 -0
- package/build/legacy/index.d.ts +23 -0
- package/build/legacy/index.js +7 -0
- package/build/legacy/index.js.map +1 -0
- package/build/legacy/types.cjs +19 -0
- package/build/legacy/types.cjs.map +1 -0
- package/build/legacy/types.d.cts +3 -0
- package/build/legacy/types.d.ts +3 -0
- package/build/legacy/types.js +1 -0
- package/build/legacy/types.js.map +1 -0
- package/build/lib/createFormFactory.d.ts +9 -0
- package/build/lib/createFormFactory.d.ts.map +1 -0
- package/build/lib/createFormFactory.js +12 -0
- package/build/lib/formContext.d.ts +12 -0
- package/build/lib/formContext.d.ts.map +1 -0
- package/build/lib/formContext.js +12 -0
- package/build/lib/index.d.ts +6 -0
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.js +5 -0
- package/build/lib/tests/useField.test.d.ts +3 -0
- package/build/lib/tests/useField.test.d.ts.map +1 -0
- package/build/lib/tests/useField.test.jsx +109 -0
- package/build/lib/tests/useForm.test.d.ts +3 -0
- package/build/lib/tests/useForm.test.d.ts.map +1 -0
- package/build/lib/tests/useForm.test.jsx +71 -0
- package/build/lib/tests/utils.d.ts +2 -0
- package/build/lib/tests/utils.d.ts.map +1 -0
- package/build/lib/tests/utils.js +5 -0
- package/build/lib/types.d.ts +2 -0
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/types.js +1 -0
- package/build/lib/useField.d.ts +33 -0
- package/build/lib/useField.d.ts.map +1 -0
- package/build/lib/useField.jsx +39 -0
- package/build/lib/useForm.d.ts +17 -0
- package/build/lib/useForm.d.ts.map +1 -0
- package/build/lib/useForm.jsx +35 -0
- package/build/modern/createFormFactory-161e85f9.d.ts +42 -0
- package/build/modern/createFormFactory.cjs +42 -0
- package/build/modern/createFormFactory.cjs.map +1 -0
- package/build/modern/createFormFactory.d.cts +3 -0
- package/build/modern/createFormFactory.d.ts +3 -0
- package/build/modern/createFormFactory.js +17 -0
- package/build/modern/createFormFactory.js.map +1 -0
- package/build/modern/formContext.cjs +46 -0
- package/build/modern/formContext.cjs.map +1 -0
- package/build/modern/formContext.d.cts +14 -0
- package/build/modern/formContext.d.ts +14 -0
- package/build/modern/formContext.js +19 -0
- package/build/modern/formContext.js.map +1 -0
- package/build/modern/index.cjs +33 -0
- package/build/modern/index.cjs.map +1 -0
- package/build/modern/index.d.cts +23 -0
- package/build/modern/index.d.ts +23 -0
- package/build/modern/index.js +7 -0
- package/build/modern/index.js.map +1 -0
- package/build/modern/types.cjs +19 -0
- package/build/modern/types.cjs.map +1 -0
- package/build/modern/types.d.cts +3 -0
- package/build/modern/types.d.ts +3 -0
- package/build/modern/types.js +1 -0
- package/build/modern/types.js.map +1 -0
- package/package.json +78 -0
- package/src/createFormFactory.ts +23 -0
- package/src/formContext.ts +23 -0
- package/src/index.ts +5 -0
- package/src/tests/useField.test.tsx +240 -0
- package/src/tests/useForm.test.tsx +129 -0
- package/src/tests/utils.ts +5 -0
- package/src/types.ts +1 -0
- package/src/useField.tsx +151 -0
- package/src/useForm.tsx +63 -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 Vue
|
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%22vue-form+tests%22">
|
12
|
+
<img src="https://github.com/TanStack/form/workflows/vue-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/vue-form@latest" target="\_parent">
|
16
|
+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/vue-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,42 @@
|
|
1
|
+
import { FieldApi, DeepKeys, FieldOptions, Narrow, DeepValue, FormOptions, FormApi } from '@tanstack/form-core';
|
2
|
+
import { SetupContext, Ref } from 'vue-demi';
|
3
|
+
|
4
|
+
declare module '@tanstack/form-core' {
|
5
|
+
interface FieldApi<TData, TFormData> {
|
6
|
+
Field: FieldComponent<TData, TFormData>;
|
7
|
+
}
|
8
|
+
}
|
9
|
+
interface UseFieldOptions<TData, TFormData> extends FieldOptions<TData, TFormData> {
|
10
|
+
mode?: 'value' | 'array';
|
11
|
+
}
|
12
|
+
type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(opts?: {
|
13
|
+
name: Narrow<TField>;
|
14
|
+
} & UseFieldOptions<DeepValue<TFormData, TField>, TFormData>) => FieldApi<DeepValue<TFormData, TField>, TFormData>;
|
15
|
+
declare function useField<TData, TFormData>(opts: UseFieldOptions<TData, TFormData>): {
|
16
|
+
api: FieldApi<TData, TFormData>;
|
17
|
+
state: Readonly<Ref<FieldApi<TData, TFormData>['state']>>;
|
18
|
+
};
|
19
|
+
type FieldValue<TFormData, TField> = TFormData extends any[] ? unknown extends TField ? TFormData[number] : DeepValue<TFormData[number], TField> : DeepValue<TFormData, TField>;
|
20
|
+
type FieldComponent<TParentData, TFormData> = <TField>(fieldOptions: {
|
21
|
+
children?: (fieldApi: FieldApi<FieldValue<TParentData, TField>, TFormData>) => any;
|
22
|
+
} & Omit<UseFieldOptions<FieldValue<TParentData, TField>, TFormData>, 'name' | 'index'> & (TParentData extends any[] ? {
|
23
|
+
name?: TField extends undefined ? TField : DeepKeys<TParentData>;
|
24
|
+
index: number;
|
25
|
+
} : {
|
26
|
+
name: TField extends undefined ? TField : DeepKeys<TParentData>;
|
27
|
+
index?: never;
|
28
|
+
}), context: SetupContext) => any;
|
29
|
+
declare const Field: <TData, TFormData>(props: UseFieldOptions<TData, TFormData> & ({
|
30
|
+
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
31
|
+
} | {
|
32
|
+
[x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
|
33
|
+
})) => any;
|
34
|
+
|
35
|
+
type FormFactory<TFormData> = {
|
36
|
+
useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>;
|
37
|
+
useField: UseField<TFormData>;
|
38
|
+
Field: FieldComponent<TFormData, TFormData>;
|
39
|
+
};
|
40
|
+
declare function createFormFactory<TFormData>(defaultOpts?: FormOptions<TFormData>): FormFactory<TFormData>;
|
41
|
+
|
42
|
+
export { FieldComponent as F, UseField as U, FormFactory as a, UseFieldOptions as b, createFormFactory as c, FieldValue as d, Field as e, useField as u };
|
@@ -0,0 +1,42 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
|
20
|
+
// src/createFormFactory.ts
|
21
|
+
var createFormFactory_exports = {};
|
22
|
+
__export(createFormFactory_exports, {
|
23
|
+
createFormFactory: () => createFormFactory
|
24
|
+
});
|
25
|
+
module.exports = __toCommonJS(createFormFactory_exports);
|
26
|
+
var import_useField = require("./useField.cjs");
|
27
|
+
var import_useForm = require("./useForm.cjs");
|
28
|
+
function createFormFactory(defaultOpts) {
|
29
|
+
return {
|
30
|
+
useForm: (opts) => {
|
31
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
32
|
+
return (0, import_useForm.useForm)(formOptions);
|
33
|
+
},
|
34
|
+
useField: import_useField.useField,
|
35
|
+
Field: import_useField.Field
|
36
|
+
};
|
37
|
+
}
|
38
|
+
// Annotate the CommonJS export names for ESM import in node:
|
39
|
+
0 && (module.exports = {
|
40
|
+
createFormFactory
|
41
|
+
});
|
42
|
+
//# sourceMappingURL=createFormFactory.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\n\nexport type FormFactory<TFormData> = {\n useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormData>\n}\n\nexport function createFormFactory<TFormData>(\n defaultOpts?: FormOptions<TFormData>,\n): FormFactory<TFormData> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,sBAAoE;AACpE,qBAAwB;AAQjB,SAAS,kBACd,aACwB;AACxB,SAAO;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAC,GAAG,aAAa,IAAI;AACvD,iBAAO,wBAAmB,WAAW;AAAA,IACvC;AAAA,IACA,UAAU;AAAA,IACV,OAAO;AAAA,EACT;AACF;","names":[]}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
// src/createFormFactory.ts
|
2
|
+
import { Field, useField } from "./useField.js";
|
3
|
+
import { useForm } from "./useForm.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
|
+
};
|
13
|
+
}
|
14
|
+
export {
|
15
|
+
createFormFactory
|
16
|
+
};
|
17
|
+
//# sourceMappingURL=createFormFactory.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/createFormFactory.ts"],"sourcesContent":["import type { FormApi, FormOptions } from '@tanstack/form-core'\n\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { useForm } from './useForm'\n\nexport type FormFactory<TFormData> = {\n useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>\n useField: UseField<TFormData>\n Field: FieldComponent<TFormData, TFormData>\n}\n\nexport function createFormFactory<TFormData>(\n defaultOpts?: FormOptions<TFormData>,\n): FormFactory<TFormData> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData>(formOptions)\n },\n useField: useField as any,\n Field: Field as any,\n }\n}\n"],"mappings":";AAEA,SAA6C,OAAO,gBAAgB;AACpE,SAAS,eAAe;AAQjB,SAAS,kBACd,aACwB;AACxB,SAAO;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAC,GAAG,aAAa,IAAI;AACvD,aAAO,QAAmB,WAAW;AAAA,IACvC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
|
20
|
+
// src/formContext.ts
|
21
|
+
var formContext_exports = {};
|
22
|
+
__export(formContext_exports, {
|
23
|
+
formContext: () => formContext,
|
24
|
+
provideFormContext: () => provideFormContext,
|
25
|
+
useFormContext: () => useFormContext
|
26
|
+
});
|
27
|
+
module.exports = __toCommonJS(formContext_exports);
|
28
|
+
var import_vue_demi = require("vue-demi");
|
29
|
+
var formContext = Symbol("FormContext");
|
30
|
+
function provideFormContext(val) {
|
31
|
+
(0, import_vue_demi.provide)(formContext, val);
|
32
|
+
}
|
33
|
+
function useFormContext() {
|
34
|
+
const formApi = (0, import_vue_demi.inject)(formContext);
|
35
|
+
if (!formApi) {
|
36
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
37
|
+
}
|
38
|
+
return formApi;
|
39
|
+
}
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
41
|
+
0 && (module.exports = {
|
42
|
+
formContext,
|
43
|
+
provideFormContext,
|
44
|
+
useFormContext
|
45
|
+
});
|
46
|
+
//# sourceMappingURL=formContext.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/formContext.ts"],"sourcesContent":["import type { FormApi } from '@tanstack/form-core'\nimport { inject, provide } from 'vue-demi'\n\nexport type FormContext = {\n formApi: FormApi<any>\n parentFieldName?: string\n} | null\n\nexport const formContext = Symbol('FormContext')\n\nexport function provideFormContext(val: FormContext) {\n provide(formContext, val)\n}\n\nexport function useFormContext() {\n const formApi = inject(formContext) as 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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAAgC;AAOzB,IAAM,cAAc,OAAO,aAAa;AAExC,SAAS,mBAAmB,KAAkB;AACnD,+BAAQ,aAAa,GAAG;AAC1B;AAEO,SAAS,iBAAiB;AAC/B,QAAM,cAAU,wBAAO,WAAW;AAElC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEA,SAAO;AACT;","names":[]}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { FormApi } from '@tanstack/form-core';
|
2
|
+
|
3
|
+
type FormContext = {
|
4
|
+
formApi: FormApi<any>;
|
5
|
+
parentFieldName?: string;
|
6
|
+
} | null;
|
7
|
+
declare const formContext: unique symbol;
|
8
|
+
declare function provideFormContext(val: FormContext): void;
|
9
|
+
declare function useFormContext(): {
|
10
|
+
formApi: FormApi<any>;
|
11
|
+
parentFieldName?: string | undefined;
|
12
|
+
};
|
13
|
+
|
14
|
+
export { FormContext, formContext, provideFormContext, useFormContext };
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { FormApi } from '@tanstack/form-core';
|
2
|
+
|
3
|
+
type FormContext = {
|
4
|
+
formApi: FormApi<any>;
|
5
|
+
parentFieldName?: string;
|
6
|
+
} | null;
|
7
|
+
declare const formContext: unique symbol;
|
8
|
+
declare function provideFormContext(val: FormContext): void;
|
9
|
+
declare function useFormContext(): {
|
10
|
+
formApi: FormApi<any>;
|
11
|
+
parentFieldName?: string | undefined;
|
12
|
+
};
|
13
|
+
|
14
|
+
export { FormContext, formContext, provideFormContext, useFormContext };
|
@@ -0,0 +1,19 @@
|
|
1
|
+
// src/formContext.ts
|
2
|
+
import { inject, provide } from "vue-demi";
|
3
|
+
var formContext = Symbol("FormContext");
|
4
|
+
function provideFormContext(val) {
|
5
|
+
provide(formContext, val);
|
6
|
+
}
|
7
|
+
function useFormContext() {
|
8
|
+
const formApi = inject(formContext);
|
9
|
+
if (!formApi) {
|
10
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
11
|
+
}
|
12
|
+
return formApi;
|
13
|
+
}
|
14
|
+
export {
|
15
|
+
formContext,
|
16
|
+
provideFormContext,
|
17
|
+
useFormContext
|
18
|
+
};
|
19
|
+
//# sourceMappingURL=formContext.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/formContext.ts"],"sourcesContent":["import type { FormApi } from '@tanstack/form-core'\nimport { inject, provide } from 'vue-demi'\n\nexport type FormContext = {\n formApi: FormApi<any>\n parentFieldName?: string\n} | null\n\nexport const formContext = Symbol('FormContext')\n\nexport function provideFormContext(val: FormContext) {\n provide(formContext, val)\n}\n\nexport function useFormContext() {\n const formApi = inject(formContext) as 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"],"mappings":";AACA,SAAS,QAAQ,eAAe;AAOzB,IAAM,cAAc,OAAO,aAAa;AAExC,SAAS,mBAAmB,KAAkB;AACnD,UAAQ,aAAa,GAAG;AAC1B;AAEO,SAAS,iBAAiB;AAC/B,QAAM,UAAU,OAAO,WAAW;AAElC,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEA,SAAO;AACT;","names":[]}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
8
|
+
for (let key of __getOwnPropNames(from))
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
11
|
+
}
|
12
|
+
return to;
|
13
|
+
};
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
16
|
+
|
17
|
+
// src/index.ts
|
18
|
+
var src_exports = {};
|
19
|
+
module.exports = __toCommonJS(src_exports);
|
20
|
+
__reExport(src_exports, require("@tanstack/form-core"), module.exports);
|
21
|
+
__reExport(src_exports, require("./createFormFactory.cjs"), module.exports);
|
22
|
+
__reExport(src_exports, require("./formContext.cjs"), module.exports);
|
23
|
+
__reExport(src_exports, require("./useField.cjs"), module.exports);
|
24
|
+
__reExport(src_exports, require("./useForm.cjs"), module.exports);
|
25
|
+
// Annotate the CommonJS export names for ESM import in node:
|
26
|
+
0 && (module.exports = {
|
27
|
+
...require("@tanstack/form-core"),
|
28
|
+
...require("./createFormFactory.cjs"),
|
29
|
+
...require("./formContext.cjs"),
|
30
|
+
...require("./useField.cjs"),
|
31
|
+
...require("./useForm.cjs")
|
32
|
+
});
|
33
|
+
//# sourceMappingURL=index.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from '@tanstack/form-core'\nexport * from './createFormFactory'\nexport * from './formContext'\nexport * from './useField'\nexport * from './useForm'\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,gCAAd;AACA,wBAAc,oCADd;AAEA,wBAAc,8BAFd;AAGA,wBAAc,2BAHd;AAIA,wBAAc,0BAJd;","names":[]}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { FormState, FormOptions, FormApi } from '@tanstack/form-core';
|
2
|
+
export * from '@tanstack/form-core';
|
3
|
+
import { F as FieldComponent, U as UseField } from './createFormFactory-161e85f9.js';
|
4
|
+
export { e as Field, d as FieldValue, a as FormFactory, b as UseFieldOptions, c as createFormFactory, u as useField } from './createFormFactory-161e85f9.js';
|
5
|
+
export { FormContext, formContext, provideFormContext, useFormContext } from './formContext.cjs';
|
6
|
+
import { NoInfer } from './types.cjs';
|
7
|
+
import 'vue-demi';
|
8
|
+
|
9
|
+
declare module '@tanstack/form-core' {
|
10
|
+
interface FormApi<TFormData> {
|
11
|
+
Provider: (props: Record<string, any> & {}) => any;
|
12
|
+
provideFormContext: () => void;
|
13
|
+
Field: FieldComponent<TFormData, TFormData>;
|
14
|
+
useField: UseField<TFormData>;
|
15
|
+
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => TSelected;
|
16
|
+
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
17
|
+
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
18
|
+
}) => any;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
|
22
|
+
|
23
|
+
export { FieldComponent, UseField, useForm };
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import { FormState, FormOptions, FormApi } from '@tanstack/form-core';
|
2
|
+
export * from '@tanstack/form-core';
|
3
|
+
import { F as FieldComponent, U as UseField } from './createFormFactory-161e85f9.js';
|
4
|
+
export { e as Field, d as FieldValue, a as FormFactory, b as UseFieldOptions, c as createFormFactory, u as useField } from './createFormFactory-161e85f9.js';
|
5
|
+
export { FormContext, formContext, provideFormContext, useFormContext } from './formContext.js';
|
6
|
+
import { NoInfer } from './types.js';
|
7
|
+
import 'vue-demi';
|
8
|
+
|
9
|
+
declare module '@tanstack/form-core' {
|
10
|
+
interface FormApi<TFormData> {
|
11
|
+
Provider: (props: Record<string, any> & {}) => any;
|
12
|
+
provideFormContext: () => void;
|
13
|
+
Field: FieldComponent<TFormData, TFormData>;
|
14
|
+
useField: UseField<TFormData>;
|
15
|
+
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => TSelected;
|
16
|
+
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
17
|
+
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
18
|
+
}) => any;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
|
22
|
+
|
23
|
+
export { FieldComponent, UseField, useForm };
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export * from '@tanstack/form-core'\nexport * from './createFormFactory'\nexport * from './formContext'\nexport * from './useField'\nexport * from './useForm'\n"],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
8
|
+
for (let key of __getOwnPropNames(from))
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
11
|
+
}
|
12
|
+
return to;
|
13
|
+
};
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
15
|
+
|
16
|
+
// src/types.ts
|
17
|
+
var types_exports = {};
|
18
|
+
module.exports = __toCommonJS(types_exports);
|
19
|
+
//# sourceMappingURL=types.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/types.ts"],"sourcesContent":["export type NoInfer<T> = [T][T extends any ? 0 : never]\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import type { FormApi, FormOptions } from '@tanstack/form-core';
|
2
|
+
import { type UseField, type FieldComponent } from './useField';
|
3
|
+
export type FormFactory<TFormData> = {
|
4
|
+
useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>;
|
5
|
+
useField: UseField<TFormData>;
|
6
|
+
Field: FieldComponent<TFormData, TFormData>;
|
7
|
+
};
|
8
|
+
export declare function createFormFactory<TFormData>(defaultOpts?: FormOptions<TFormData>): FormFactory<TFormData>;
|
9
|
+
//# sourceMappingURL=createFormFactory.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"createFormFactory.d.ts","sourceRoot":"","sources":["../../src/createFormFactory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAE/D,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,cAAc,EAAmB,MAAM,YAAY,CAAA;AAGhF,MAAM,MAAM,WAAW,CAAC,SAAS,IAAI;IACnC,OAAO,EAAE,CAAC,IAAI,CAAC,EAAE,WAAW,CAAC,SAAS,CAAC,KAAK,OAAO,CAAC,SAAS,CAAC,CAAA;IAC9D,QAAQ,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAA;IAC7B,KAAK,EAAE,cAAc,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;CAC5C,CAAA;AAED,wBAAgB,iBAAiB,CAAC,SAAS,EACzC,WAAW,CAAC,EAAE,WAAW,CAAC,SAAS,CAAC,GACnC,WAAW,CAAC,SAAS,CAAC,CASxB"}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { Field, useField } from './useField';
|
2
|
+
import { useForm } from './useForm';
|
3
|
+
export function createFormFactory(defaultOpts) {
|
4
|
+
return {
|
5
|
+
useForm: (opts) => {
|
6
|
+
const formOptions = Object.assign({}, defaultOpts, opts);
|
7
|
+
return useForm(formOptions);
|
8
|
+
},
|
9
|
+
useField: useField,
|
10
|
+
Field: Field,
|
11
|
+
};
|
12
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { FormApi } from '@tanstack/form-core';
|
2
|
+
export type FormContext = {
|
3
|
+
formApi: FormApi<any>;
|
4
|
+
parentFieldName?: string;
|
5
|
+
} | null;
|
6
|
+
export declare const formContext: unique symbol;
|
7
|
+
export declare function provideFormContext(val: FormContext): void;
|
8
|
+
export declare function useFormContext(): {
|
9
|
+
formApi: FormApi<any>;
|
10
|
+
parentFieldName?: string | undefined;
|
11
|
+
};
|
12
|
+
//# sourceMappingURL=formContext.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"formContext.d.ts","sourceRoot":"","sources":["../../src/formContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAGlD,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAA;IACrB,eAAe,CAAC,EAAE,MAAM,CAAA;CACzB,GAAG,IAAI,CAAA;AAER,eAAO,MAAM,WAAW,eAAwB,CAAA;AAEhD,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,WAAW,QAElD;AAED,wBAAgB,cAAc;;;EAQ7B"}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { inject, provide } from 'vue-demi';
|
2
|
+
export const formContext = Symbol('FormContext');
|
3
|
+
export function provideFormContext(val) {
|
4
|
+
provide(formContext, val);
|
5
|
+
}
|
6
|
+
export function useFormContext() {
|
7
|
+
const formApi = inject(formContext);
|
8
|
+
if (!formApi) {
|
9
|
+
throw new Error(`You are trying to use the form API outside of a form!`);
|
10
|
+
}
|
11
|
+
return formApi;
|
12
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA;AACnC,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useField.test.d.ts","sourceRoot":"","sources":["../../../src/tests/useField.test.tsx"],"names":[],"mappings":";AAGA,OAAO,2BAA2B,CAAA"}
|
@@ -0,0 +1,109 @@
|
|
1
|
+
/// <reference lib="dom" />
|
2
|
+
import { defineComponent } from 'vue-demi';
|
3
|
+
import { render, waitFor } from '@testing-library/vue';
|
4
|
+
import '@testing-library/jest-dom';
|
5
|
+
import { createFormFactory, provideFormContext, } from '../index';
|
6
|
+
import userEvent from '@testing-library/user-event';
|
7
|
+
import { sleep } from './utils';
|
8
|
+
const user = userEvent.setup();
|
9
|
+
describe('useField', () => {
|
10
|
+
it('should allow to set default value', async () => {
|
11
|
+
const formFactory = createFormFactory();
|
12
|
+
const Comp = defineComponent(() => {
|
13
|
+
const form = formFactory.useForm();
|
14
|
+
provideFormContext({ formApi: form });
|
15
|
+
return () => (<form.Field name="firstName" defaultValue="FirstName">
|
16
|
+
{(field) => (<input data-testid={'fieldinput'} value={field.state.value} onBlur={field.handleBlur} onInput={(e) => field.handleChange(e.target.value)}/>)}
|
17
|
+
</form.Field>);
|
18
|
+
});
|
19
|
+
const { getByTestId } = render(Comp);
|
20
|
+
const input = getByTestId('fieldinput');
|
21
|
+
await waitFor(() => expect(input).toHaveValue('FirstName'));
|
22
|
+
});
|
23
|
+
it('should not validate on change if isTouched is false', async () => {
|
24
|
+
const error = 'Please enter a different value';
|
25
|
+
const formFactory = createFormFactory();
|
26
|
+
const Comp = defineComponent(() => {
|
27
|
+
const form = formFactory.useForm();
|
28
|
+
provideFormContext({ formApi: form });
|
29
|
+
return () => (<form.Field name="firstName" onChange={(value) => (value === 'other' ? error : undefined)}>
|
30
|
+
{(field) => (<div>
|
31
|
+
<input data-testid="fieldinput" name={field.name} value={field.state.value} onBlur={field.handleBlur} onInput={(e) => field.setValue(e.target.value)}/>
|
32
|
+
<p>{field.getMeta().error}</p>
|
33
|
+
</div>)}
|
34
|
+
</form.Field>);
|
35
|
+
});
|
36
|
+
const { getByTestId, queryByText } = render(Comp);
|
37
|
+
const input = getByTestId('fieldinput');
|
38
|
+
await user.type(input, 'other');
|
39
|
+
expect(queryByText(error)).not.toBeInTheDocument();
|
40
|
+
});
|
41
|
+
it('should validate on change if isTouched is true', async () => {
|
42
|
+
const error = 'Please enter a different value';
|
43
|
+
const formFactory = createFormFactory();
|
44
|
+
const Comp = defineComponent(() => {
|
45
|
+
const form = formFactory.useForm();
|
46
|
+
provideFormContext({ formApi: form });
|
47
|
+
return () => (<form.Field name="firstName" onChange={(value) => (value === 'other' ? error : undefined)}>
|
48
|
+
{(field) => (<div>
|
49
|
+
<input data-testid="fieldinput" name={field.name} value={field.state.value} onBlur={field.handleBlur} onInput={(e) => field.handleChange(e.target.value)}/>
|
50
|
+
<p>{field.getMeta().error}</p>
|
51
|
+
</div>)}
|
52
|
+
</form.Field>);
|
53
|
+
});
|
54
|
+
const { getByTestId, getByText, queryByText } = render(Comp);
|
55
|
+
const input = getByTestId('fieldinput');
|
56
|
+
expect(queryByText(error)).not.toBeInTheDocument();
|
57
|
+
await user.type(input, 'other');
|
58
|
+
expect(getByText(error)).toBeInTheDocument();
|
59
|
+
});
|
60
|
+
it('should validate async on change', async () => {
|
61
|
+
const error = 'Please enter a different value';
|
62
|
+
const formFactory = createFormFactory();
|
63
|
+
const Comp = defineComponent(() => {
|
64
|
+
const form = formFactory.useForm();
|
65
|
+
provideFormContext({ formApi: form });
|
66
|
+
return () => (<form.Field name="firstName" defaultMeta={{ isTouched: true }} onChangeAsync={async () => {
|
67
|
+
await sleep(10);
|
68
|
+
return error;
|
69
|
+
}}>
|
70
|
+
{(field) => (<div>
|
71
|
+
<input data-testid="fieldinput" name={field.name} value={field.state.value} onBlur={field.handleBlur} onInput={(e) => field.handleChange(e.target.value)}/>
|
72
|
+
<p>{field.getMeta().error}</p>
|
73
|
+
</div>)}
|
74
|
+
</form.Field>);
|
75
|
+
});
|
76
|
+
const { getByTestId, getByText, queryByText } = render(Comp);
|
77
|
+
const input = getByTestId('fieldinput');
|
78
|
+
expect(queryByText(error)).not.toBeInTheDocument();
|
79
|
+
await user.type(input, 'other');
|
80
|
+
await waitFor(() => getByText(error));
|
81
|
+
expect(getByText(error)).toBeInTheDocument();
|
82
|
+
});
|
83
|
+
it('should validate async on change with debounce', async () => {
|
84
|
+
const mockFn = vi.fn();
|
85
|
+
const error = 'Please enter a different value';
|
86
|
+
const formFactory = createFormFactory();
|
87
|
+
const Comp = defineComponent(() => {
|
88
|
+
const form = formFactory.useForm();
|
89
|
+
provideFormContext({ formApi: form });
|
90
|
+
return () => (<form.Field name="firstName" defaultMeta={{ isTouched: true }} onChangeAsyncDebounceMs={100} onChangeAsync={async () => {
|
91
|
+
mockFn();
|
92
|
+
await sleep(10);
|
93
|
+
return error;
|
94
|
+
}}>
|
95
|
+
{(field) => (<div>
|
96
|
+
<input data-testid="fieldinput" name={field.name} value={field.state.value} onBlur={field.handleBlur} onInput={(e) => field.handleChange(e.target.value)}/>
|
97
|
+
<p>{field.getMeta().error}</p>
|
98
|
+
</div>)}
|
99
|
+
</form.Field>);
|
100
|
+
});
|
101
|
+
const { getByTestId, getByText } = render(<Comp />);
|
102
|
+
const input = getByTestId('fieldinput');
|
103
|
+
await user.type(input, 'other');
|
104
|
+
// mockFn will have been called 5 times without onChangeAsyncDebounceMs
|
105
|
+
expect(mockFn).toHaveBeenCalledTimes(0);
|
106
|
+
await waitFor(() => getByText(error));
|
107
|
+
expect(getByText(error)).toBeInTheDocument();
|
108
|
+
});
|
109
|
+
});
|