@tanstack/angular-form 0.17.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/README.md +35 -0
- package/build/esm2022/index.mjs +4 -0
- package/build/esm2022/inject-form.mjs +8 -0
- package/build/esm2022/inject-store.mjs +5 -0
- package/build/esm2022/tanstack-angular-form.mjs +5 -0
- package/build/esm2022/tanstack-field.directive.mjs +76 -0
- package/build/fesm2022/tanstack-angular-form.mjs +94 -0
- package/build/fesm2022/tanstack-angular-form.mjs.map +1 -0
- package/build/index.d.ts +4 -0
- package/build/inject-form.d.ts +2 -0
- package/build/inject-store.d.ts +2 -0
- package/build/tanstack-field.directive.d.ts +22 -0
- package/package.json +71 -0
- package/src/index.ts +10 -0
- package/src/inject-form.ts +13 -0
- package/src/inject-store.ts +13 -0
- package/src/tanstack-field.directive.ts +93 -0
- package/src/test-setup.ts +13 -0
- package/src/tests/test.component.spec.ts +358 -0
- package/src/tests/utils.ts +5 -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
|
+
Directives for managing form state in Angular
|
|
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%22angular-form+tests%22">
|
|
12
|
+
<img src="https://github.com/TanStack/form/workflows/angular-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/angular-form@latest" target="\_parent">
|
|
16
|
+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/angular-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 -->
|
package/build/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
|
+
Directives for managing form state in Angular
|
|
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%22angular-form+tests%22">
|
|
12
|
+
<img src="https://github.com/TanStack/form/workflows/angular-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/angular-form@latest" target="\_parent">
|
|
16
|
+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/angular-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,4 @@
|
|
|
1
|
+
export { injectForm } from './inject-form';
|
|
2
|
+
export { TanStackField } from './tanstack-field.directive';
|
|
3
|
+
export { injectStore } from './inject-store';
|
|
4
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBT0EsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLGVBQWUsQ0FBQTtBQUMxQyxPQUFPLEVBQUUsYUFBYSxFQUFFLE1BQU0sNEJBQTRCLENBQUE7QUFDMUQsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLGdCQUFnQixDQUFBIiwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IHR5cGUge1xuICBGaWVsZEFwaSxcbiAgRm9ybU9wdGlvbnMsXG4gIEZpZWxkU3RhdGUsXG4gIEZpZWxkVmFsaWRhdGVGbixcbiAgRmllbGRWYWxpZGF0ZUFzeW5jRm4sXG59IGZyb20gJ0B0YW5zdGFjay9mb3JtLWNvcmUnXG5leHBvcnQgeyBpbmplY3RGb3JtIH0gZnJvbSAnLi9pbmplY3QtZm9ybSdcbmV4cG9ydCB7IFRhblN0YWNrRmllbGQgfSBmcm9tICcuL3RhbnN0YWNrLWZpZWxkLmRpcmVjdGl2ZSdcbmV4cG9ydCB7IGluamVjdFN0b3JlIH0gZnJvbSAnLi9pbmplY3Qtc3RvcmUnXG4iXX0=
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { FormApi } from '@tanstack/form-core';
|
|
2
|
+
import { injectStore } from '@tanstack/angular-store';
|
|
3
|
+
export function injectForm(opts) {
|
|
4
|
+
const api = new FormApi(opts);
|
|
5
|
+
injectStore(api.store, (state) => state.isSubmitting);
|
|
6
|
+
return api;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5qZWN0LWZvcm0uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5qZWN0LWZvcm0udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLE9BQU8sRUFBb0MsTUFBTSxxQkFBcUIsQ0FBQTtBQUMvRSxPQUFPLEVBQUUsV0FBVyxFQUFFLE1BQU0seUJBQXlCLENBQUE7QUFFckQsTUFBTSxVQUFVLFVBQVUsQ0FHeEIsSUFBNkM7SUFDN0MsTUFBTSxHQUFHLEdBQUcsSUFBSSxPQUFPLENBQTRCLElBQUksQ0FBQyxDQUFBO0lBRXhELFdBQVcsQ0FBQyxHQUFHLENBQUMsS0FBSyxFQUFFLENBQUMsS0FBSyxFQUFFLEVBQUUsQ0FBQyxLQUFLLENBQUMsWUFBWSxDQUFDLENBQUE7SUFFckQsT0FBTyxHQUFHLENBQUE7QUFDWixDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHsgRm9ybUFwaSwgdHlwZSBGb3JtT3B0aW9ucywgdHlwZSBWYWxpZGF0b3IgfSBmcm9tICdAdGFuc3RhY2svZm9ybS1jb3JlJ1xuaW1wb3J0IHsgaW5qZWN0U3RvcmUgfSBmcm9tICdAdGFuc3RhY2svYW5ndWxhci1zdG9yZSdcblxuZXhwb3J0IGZ1bmN0aW9uIGluamVjdEZvcm08XG4gIFRGb3JtRGF0YSxcbiAgVEZvcm1WYWxpZGF0b3IgZXh0ZW5kcyBWYWxpZGF0b3I8VEZvcm1EYXRhLCB1bmtub3duPiB8IHVuZGVmaW5lZCA9IHVuZGVmaW5lZCxcbj4ob3B0cz86IEZvcm1PcHRpb25zPFRGb3JtRGF0YSwgVEZvcm1WYWxpZGF0b3I+KSB7XG4gIGNvbnN0IGFwaSA9IG5ldyBGb3JtQXBpPFRGb3JtRGF0YSwgVEZvcm1WYWxpZGF0b3I+KG9wdHMpXG5cbiAgaW5qZWN0U3RvcmUoYXBpLnN0b3JlLCAoc3RhdGUpID0+IHN0YXRlLmlzU3VibWl0dGluZylcblxuICByZXR1cm4gYXBpXG59XG4iXX0=
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { injectStore as injectAngularStore } from '@tanstack/angular-store';
|
|
2
|
+
export function injectStore(form, selector) {
|
|
3
|
+
return injectAngularStore(form.store, selector);
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5qZWN0LXN0b3JlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2luamVjdC1zdG9yZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsV0FBVyxJQUFJLGtCQUFrQixFQUFFLE1BQU0seUJBQXlCLENBQUE7QUFHM0UsTUFBTSxVQUFVLFdBQVcsQ0FLekIsSUFBd0MsRUFDeEMsUUFBOEQ7SUFFOUQsT0FBTyxrQkFBa0IsQ0FBQyxJQUFJLENBQUMsS0FBSyxFQUFFLFFBQVEsQ0FBQyxDQUFBO0FBQ2pELENBQUMiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgeyBpbmplY3RTdG9yZSBhcyBpbmplY3RBbmd1bGFyU3RvcmUgfSBmcm9tICdAdGFuc3RhY2svYW5ndWxhci1zdG9yZSdcbmltcG9ydCB0eXBlIHsgRm9ybUFwaSwgRm9ybVN0YXRlLCBWYWxpZGF0b3IgfSBmcm9tICdAdGFuc3RhY2svZm9ybS1jb3JlJ1xuXG5leHBvcnQgZnVuY3Rpb24gaW5qZWN0U3RvcmU8XG4gIFRGb3JtRGF0YSxcbiAgVEZvcm1WYWxpZGF0b3IgZXh0ZW5kcyBWYWxpZGF0b3I8VEZvcm1EYXRhLCB1bmtub3duPiB8IHVuZGVmaW5lZCA9IHVuZGVmaW5lZCxcbiAgVFNlbGVjdGVkID0gTm9JbmZlcjxGb3JtU3RhdGU8VEZvcm1EYXRhPj4sXG4+KFxuICBmb3JtOiBGb3JtQXBpPFRGb3JtRGF0YSwgVEZvcm1WYWxpZGF0b3I+LFxuICBzZWxlY3Rvcj86IChzdGF0ZTogTm9JbmZlcjxGb3JtU3RhdGU8VEZvcm1EYXRhPj4pID0+IFRTZWxlY3RlZCxcbikge1xuICByZXR1cm4gaW5qZWN0QW5ndWxhclN0b3JlKGZvcm0uc3RvcmUsIHNlbGVjdG9yKVxufVxuIl19
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated bundle index. Do not edit.
|
|
3
|
+
*/
|
|
4
|
+
export * from './index';
|
|
5
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFuc3RhY2stYW5ndWxhci1mb3JtLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3RhbnN0YWNrLWFuZ3VsYXItZm9ybS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7R0FFRztBQUVILGNBQWMsU0FBUyxDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiLyoqXG4gKiBHZW5lcmF0ZWQgYnVuZGxlIGluZGV4LiBEbyBub3QgZWRpdC5cbiAqL1xuXG5leHBvcnQgKiBmcm9tICcuL2luZGV4JztcbiJdfQ==
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Directive, Input, } from '@angular/core';
|
|
2
|
+
import { FieldApi, } from '@tanstack/form-core';
|
|
3
|
+
import * as i0 from "@angular/core";
|
|
4
|
+
export class TanStackField {
|
|
5
|
+
name;
|
|
6
|
+
// Setting as NoInferHack as it's the same internal type cast as TanStack Form Core
|
|
7
|
+
// This can be removed when TanStack Form Core is moved to TS min of 5.4
|
|
8
|
+
// and the NoInfer internal util type is rm-rf'd
|
|
9
|
+
defaultValue;
|
|
10
|
+
asyncDebounceMs;
|
|
11
|
+
asyncAlways;
|
|
12
|
+
preserveValue;
|
|
13
|
+
validatorAdapter;
|
|
14
|
+
tanstackField;
|
|
15
|
+
validators;
|
|
16
|
+
defaultMeta;
|
|
17
|
+
api;
|
|
18
|
+
getOptions() {
|
|
19
|
+
return {
|
|
20
|
+
defaultValue: this.defaultValue,
|
|
21
|
+
asyncDebounceMs: this.asyncDebounceMs,
|
|
22
|
+
asyncAlways: this.asyncAlways,
|
|
23
|
+
preserveValue: this.preserveValue,
|
|
24
|
+
validatorAdapter: this.validatorAdapter,
|
|
25
|
+
validators: this.validators,
|
|
26
|
+
defaultMeta: this.defaultMeta,
|
|
27
|
+
name: this.name,
|
|
28
|
+
form: this.tanstackField,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
unmount;
|
|
32
|
+
ngOnInit() {
|
|
33
|
+
this.api = new FieldApi(this.getOptions());
|
|
34
|
+
this.unmount = this.api.mount();
|
|
35
|
+
}
|
|
36
|
+
ngOnDestroy() {
|
|
37
|
+
this.unmount?.();
|
|
38
|
+
}
|
|
39
|
+
ngOnChanges() {
|
|
40
|
+
const api = this.api;
|
|
41
|
+
if (!api)
|
|
42
|
+
return;
|
|
43
|
+
api.update(this.getOptions());
|
|
44
|
+
}
|
|
45
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: TanStackField, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
46
|
+
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.0", type: TanStackField, isStandalone: true, selector: "[tanstackField]", inputs: { name: "name", defaultValue: "defaultValue", asyncDebounceMs: "asyncDebounceMs", asyncAlways: "asyncAlways", preserveValue: "preserveValue", validatorAdapter: "validatorAdapter", tanstackField: "tanstackField", validators: "validators", defaultMeta: "defaultMeta" }, exportAs: ["field"], usesOnChanges: true, ngImport: i0 });
|
|
47
|
+
}
|
|
48
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: TanStackField, decorators: [{
|
|
49
|
+
type: Directive,
|
|
50
|
+
args: [{
|
|
51
|
+
selector: '[tanstackField]',
|
|
52
|
+
standalone: true,
|
|
53
|
+
exportAs: 'field',
|
|
54
|
+
}]
|
|
55
|
+
}], propDecorators: { name: [{
|
|
56
|
+
type: Input,
|
|
57
|
+
args: [{ required: true }]
|
|
58
|
+
}], defaultValue: [{
|
|
59
|
+
type: Input
|
|
60
|
+
}], asyncDebounceMs: [{
|
|
61
|
+
type: Input
|
|
62
|
+
}], asyncAlways: [{
|
|
63
|
+
type: Input
|
|
64
|
+
}], preserveValue: [{
|
|
65
|
+
type: Input
|
|
66
|
+
}], validatorAdapter: [{
|
|
67
|
+
type: Input
|
|
68
|
+
}], tanstackField: [{
|
|
69
|
+
type: Input,
|
|
70
|
+
args: [{ required: true }]
|
|
71
|
+
}], validators: [{
|
|
72
|
+
type: Input
|
|
73
|
+
}], defaultMeta: [{
|
|
74
|
+
type: Input
|
|
75
|
+
}] } });
|
|
76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BhcyB0eXBlb2YgdGhpcy5hcGkgfCB1bmRlZmluZWRcbiAgICBpZiAoIWFwaSkgcmV0dXJuXG4gICAgYXBpLnVwZGF0ZSh0aGlzLmdldE9wdGlvbnMoKSlcbiAgfVxufVxuIl19
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { FormApi, FieldApi } from '@tanstack/form-core';
|
|
2
|
+
import { injectStore as injectStore$1 } from '@tanstack/angular-store';
|
|
3
|
+
import * as i0 from '@angular/core';
|
|
4
|
+
import { Directive, Input } from '@angular/core';
|
|
5
|
+
|
|
6
|
+
function injectForm(opts) {
|
|
7
|
+
const api = new FormApi(opts);
|
|
8
|
+
injectStore$1(api.store, (state) => state.isSubmitting);
|
|
9
|
+
return api;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class TanStackField {
|
|
13
|
+
name;
|
|
14
|
+
// Setting as NoInferHack as it's the same internal type cast as TanStack Form Core
|
|
15
|
+
// This can be removed when TanStack Form Core is moved to TS min of 5.4
|
|
16
|
+
// and the NoInfer internal util type is rm-rf'd
|
|
17
|
+
defaultValue;
|
|
18
|
+
asyncDebounceMs;
|
|
19
|
+
asyncAlways;
|
|
20
|
+
preserveValue;
|
|
21
|
+
validatorAdapter;
|
|
22
|
+
tanstackField;
|
|
23
|
+
validators;
|
|
24
|
+
defaultMeta;
|
|
25
|
+
api;
|
|
26
|
+
getOptions() {
|
|
27
|
+
return {
|
|
28
|
+
defaultValue: this.defaultValue,
|
|
29
|
+
asyncDebounceMs: this.asyncDebounceMs,
|
|
30
|
+
asyncAlways: this.asyncAlways,
|
|
31
|
+
preserveValue: this.preserveValue,
|
|
32
|
+
validatorAdapter: this.validatorAdapter,
|
|
33
|
+
validators: this.validators,
|
|
34
|
+
defaultMeta: this.defaultMeta,
|
|
35
|
+
name: this.name,
|
|
36
|
+
form: this.tanstackField,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
unmount;
|
|
40
|
+
ngOnInit() {
|
|
41
|
+
this.api = new FieldApi(this.getOptions());
|
|
42
|
+
this.unmount = this.api.mount();
|
|
43
|
+
}
|
|
44
|
+
ngOnDestroy() {
|
|
45
|
+
this.unmount?.();
|
|
46
|
+
}
|
|
47
|
+
ngOnChanges() {
|
|
48
|
+
const api = this.api;
|
|
49
|
+
if (!api)
|
|
50
|
+
return;
|
|
51
|
+
api.update(this.getOptions());
|
|
52
|
+
}
|
|
53
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: TanStackField, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
54
|
+
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.0", type: TanStackField, isStandalone: true, selector: "[tanstackField]", inputs: { name: "name", defaultValue: "defaultValue", asyncDebounceMs: "asyncDebounceMs", asyncAlways: "asyncAlways", preserveValue: "preserveValue", validatorAdapter: "validatorAdapter", tanstackField: "tanstackField", validators: "validators", defaultMeta: "defaultMeta" }, exportAs: ["field"], usesOnChanges: true, ngImport: i0 });
|
|
55
|
+
}
|
|
56
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.0", ngImport: i0, type: TanStackField, decorators: [{
|
|
57
|
+
type: Directive,
|
|
58
|
+
args: [{
|
|
59
|
+
selector: '[tanstackField]',
|
|
60
|
+
standalone: true,
|
|
61
|
+
exportAs: 'field',
|
|
62
|
+
}]
|
|
63
|
+
}], propDecorators: { name: [{
|
|
64
|
+
type: Input,
|
|
65
|
+
args: [{ required: true }]
|
|
66
|
+
}], defaultValue: [{
|
|
67
|
+
type: Input
|
|
68
|
+
}], asyncDebounceMs: [{
|
|
69
|
+
type: Input
|
|
70
|
+
}], asyncAlways: [{
|
|
71
|
+
type: Input
|
|
72
|
+
}], preserveValue: [{
|
|
73
|
+
type: Input
|
|
74
|
+
}], validatorAdapter: [{
|
|
75
|
+
type: Input
|
|
76
|
+
}], tanstackField: [{
|
|
77
|
+
type: Input,
|
|
78
|
+
args: [{ required: true }]
|
|
79
|
+
}], validators: [{
|
|
80
|
+
type: Input
|
|
81
|
+
}], defaultMeta: [{
|
|
82
|
+
type: Input
|
|
83
|
+
}] } });
|
|
84
|
+
|
|
85
|
+
function injectStore(form, selector) {
|
|
86
|
+
return injectStore$1(form.store, selector);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Generated bundle index. Do not edit.
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
export { TanStackField, injectForm, injectStore };
|
|
94
|
+
//# sourceMappingURL=tanstack-angular-form.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tanstack-angular-form.mjs","sources":["../../src/inject-form.ts","../../src/tanstack-field.directive.ts","../../src/inject-store.ts","../../src/tanstack-angular-form.ts"],"sourcesContent":["import { FormApi, type FormOptions, type Validator } from '@tanstack/form-core'\nimport { injectStore } from '@tanstack/angular-store'\n\nexport function injectForm<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(opts?: FormOptions<TFormData, TFormValidator>) {\n const api = new FormApi<TFormData, TFormValidator>(opts)\n\n injectStore(api.store, (state) => state.isSubmitting)\n\n return api\n}\n","import {\n Directive,\n Input,\n type OnChanges,\n type OnDestroy,\n type OnInit,\n} from '@angular/core'\nimport {\n type DeepKeys,\n type DeepValue,\n FieldApi,\n type FieldMeta,\n type FieldOptions,\n type FieldValidators,\n FormApi,\n type NoInfer as NoInferHack,\n type Validator,\n} from '@tanstack/form-core'\n\n@Directive({\n selector: '[tanstackField]',\n standalone: true,\n exportAs: 'field',\n})\nexport class TanStackField<\n TParentData,\n const 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 implements\n OnInit,\n OnChanges,\n OnDestroy,\n FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>\n{\n @Input({ required: true }) name!: TName\n // Setting as NoInferHack as it's the same internal type cast as TanStack Form Core\n // This can be removed when TanStack Form Core is moved to TS min of 5.4\n // and the NoInfer internal util type is rm-rf'd\n @Input() defaultValue?: NoInferHack<TData>\n @Input() asyncDebounceMs?: number\n @Input() asyncAlways?: boolean\n @Input() preserveValue?: boolean\n @Input() validatorAdapter?: TFieldValidator\n @Input({ required: true }) tanstackField!: FormApi<\n TParentData,\n TFormValidator\n >\n @Input() validators?: NoInfer<\n FieldValidators<TParentData, TName, TFieldValidator, TFormValidator, TData>\n >\n @Input() defaultMeta?: Partial<FieldMeta>\n\n api!: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>\n\n private getOptions() {\n return {\n defaultValue: this.defaultValue,\n asyncDebounceMs: this.asyncDebounceMs,\n asyncAlways: this.asyncAlways,\n preserveValue: this.preserveValue,\n validatorAdapter: this.validatorAdapter,\n validators: this.validators,\n defaultMeta: this.defaultMeta,\n name: this.name,\n form: this.tanstackField,\n }\n }\n\n unmount?: () => void\n\n ngOnInit() {\n this.api = new FieldApi(this.getOptions())\n\n this.unmount = this.api.mount()\n }\n\n ngOnDestroy() {\n this.unmount?.()\n }\n\n ngOnChanges() {\n const api = this.api as typeof this.api | undefined\n if (!api) return\n api.update(this.getOptions())\n }\n}\n","import { injectStore as injectAngularStore } from '@tanstack/angular-store'\nimport type { FormApi, FormState, Validator } from '@tanstack/form-core'\n\nexport function injectStore<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n TSelected = NoInfer<FormState<TFormData>>,\n>(\n form: FormApi<TFormData, TFormValidator>,\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n) {\n return injectAngularStore(form.store, selector)\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":["injectStore","injectAngularStore"],"mappings":";;;;;AAGM,SAAU,UAAU,CAGxB,IA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,CAAA;sBAAnB,KAAK;;;ACtDQ,SAAA,WAAW,CAKzB,IAAwC,EACxC,QAA8D,EAAA;IAE9D,OAAOC,aAAkB,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;AACjD;;ACZA;;AAEG;;;;"}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { FormApi, type FormOptions, type Validator } from '@tanstack/form-core';
|
|
2
|
+
export declare function injectForm<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(opts?: FormOptions<TFormData, TFormValidator>): FormApi<TFormData, TFormValidator>;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { FormApi, FormState, Validator } from '@tanstack/form-core';
|
|
2
|
+
export declare function injectStore<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined, TSelected = NoInfer<FormState<TFormData>>>(form: FormApi<TFormData, TFormValidator>, selector?: (state: NoInfer<FormState<TFormData>>) => TSelected): import("@angular/core").Signal<TSelected>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type OnChanges, type OnDestroy, type OnInit } from '@angular/core';
|
|
2
|
+
import { type DeepKeys, type DeepValue, FieldApi, type FieldMeta, type FieldOptions, type FieldValidators, FormApi, type NoInfer as NoInferHack, type Validator } from '@tanstack/form-core';
|
|
3
|
+
import * as i0 from "@angular/core";
|
|
4
|
+
export declare class TanStackField<TParentData, const 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>> implements OnInit, OnChanges, OnDestroy, FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> {
|
|
5
|
+
name: TName;
|
|
6
|
+
defaultValue?: NoInferHack<TData>;
|
|
7
|
+
asyncDebounceMs?: number;
|
|
8
|
+
asyncAlways?: boolean;
|
|
9
|
+
preserveValue?: boolean;
|
|
10
|
+
validatorAdapter?: TFieldValidator;
|
|
11
|
+
tanstackField: FormApi<TParentData, TFormValidator>;
|
|
12
|
+
validators?: NoInfer<FieldValidators<TParentData, TName, TFieldValidator, TFormValidator, TData>>;
|
|
13
|
+
defaultMeta?: Partial<FieldMeta>;
|
|
14
|
+
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
|
15
|
+
private getOptions;
|
|
16
|
+
unmount?: () => void;
|
|
17
|
+
ngOnInit(): void;
|
|
18
|
+
ngOnDestroy(): void;
|
|
19
|
+
ngOnChanges(): void;
|
|
20
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TanStackField<any, any, any, any, any>, never>;
|
|
21
|
+
static ɵdir: i0.ɵɵDirectiveDeclaration<TanStackField<any, any, any, any, any>, "[tanstackField]", ["field"], { "name": { "alias": "name"; "required": true; }; "defaultValue": { "alias": "defaultValue"; "required": false; }; "asyncDebounceMs": { "alias": "asyncDebounceMs"; "required": false; }; "asyncAlways": { "alias": "asyncAlways"; "required": false; }; "preserveValue": { "alias": "preserveValue"; "required": false; }; "validatorAdapter": { "alias": "validatorAdapter"; "required": false; }; "tanstackField": { "alias": "tanstackField"; "required": true; }; "validators": { "alias": "validators"; "required": false; }; "defaultMeta": { "alias": "defaultMeta"; "required": false; }; }, {}, never, never, true, never>;
|
|
22
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tanstack/angular-form",
|
|
3
|
+
"version": "0.17.0",
|
|
4
|
+
"description": "Powerful, type-safe forms for Angular.",
|
|
5
|
+
"author": "tannerlinsley",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": "tanstack/form",
|
|
8
|
+
"homepage": "https://tanstack.com/form",
|
|
9
|
+
"funding": {
|
|
10
|
+
"type": "github",
|
|
11
|
+
"url": "https://github.com/sponsors/tannerlinsley"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"files": [
|
|
15
|
+
"build",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"nx": {
|
|
20
|
+
"targets": {
|
|
21
|
+
"test:build": {
|
|
22
|
+
"dependsOn": [
|
|
23
|
+
"build"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@analogjs/vite-plugin-angular": "^1.0.0",
|
|
30
|
+
"@angular/common": "^17.3.0",
|
|
31
|
+
"@angular/compiler": "^17.3.0",
|
|
32
|
+
"@angular/compiler-cli": "^17.3.0",
|
|
33
|
+
"@angular/core": "^17.3.0",
|
|
34
|
+
"@angular/platform-browser": "^17.3.0",
|
|
35
|
+
"@angular/platform-browser-dynamic": "^17.3.0",
|
|
36
|
+
"@testing-library/angular": "^15.2.0",
|
|
37
|
+
"ng-packagr": "^17.3.0",
|
|
38
|
+
"typescript": "5.4.2",
|
|
39
|
+
"zone.js": "~0.14.2"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@tanstack/angular-store": "^0.4.0",
|
|
43
|
+
"tslib": "^2.3.0",
|
|
44
|
+
"@tanstack/form-core": "0.16.2"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@angular/core": ">=17.3.0"
|
|
48
|
+
},
|
|
49
|
+
"module": "build/fesm2022/tanstack-angular-form.mjs",
|
|
50
|
+
"types": "build/index.d.ts",
|
|
51
|
+
"exports": {
|
|
52
|
+
"./package.json": {
|
|
53
|
+
"default": "./package.json"
|
|
54
|
+
},
|
|
55
|
+
".": {
|
|
56
|
+
"types": "./build/index.d.ts",
|
|
57
|
+
"esm2022": "./build/esm2022/tanstack-angular-form.mjs",
|
|
58
|
+
"esm": "./build/esm2022/tanstack-angular-form.mjs",
|
|
59
|
+
"default": "./build/fesm2022/tanstack-angular-form.mjs"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "ng-packagr -p ng-package.json -c tsconfig.build.json && rimraf ./build/package.json",
|
|
64
|
+
"clean": "rimraf ./build && rimraf ./coverage",
|
|
65
|
+
"test:eslint": "eslint --ext .ts,.tsx ./src",
|
|
66
|
+
"test:types": "tsc --noEmit",
|
|
67
|
+
"test:lib": "vitest",
|
|
68
|
+
"test:lib:dev": "pnpm run test:lib --watch",
|
|
69
|
+
"test:build": "publint --strict"
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
FieldApi,
|
|
3
|
+
FormOptions,
|
|
4
|
+
FieldState,
|
|
5
|
+
FieldValidateFn,
|
|
6
|
+
FieldValidateAsyncFn,
|
|
7
|
+
} from '@tanstack/form-core'
|
|
8
|
+
export { injectForm } from './inject-form'
|
|
9
|
+
export { TanStackField } from './tanstack-field.directive'
|
|
10
|
+
export { injectStore } from './inject-store'
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FormApi, type FormOptions, type Validator } from '@tanstack/form-core'
|
|
2
|
+
import { injectStore } from '@tanstack/angular-store'
|
|
3
|
+
|
|
4
|
+
export function injectForm<
|
|
5
|
+
TFormData,
|
|
6
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
7
|
+
>(opts?: FormOptions<TFormData, TFormValidator>) {
|
|
8
|
+
const api = new FormApi<TFormData, TFormValidator>(opts)
|
|
9
|
+
|
|
10
|
+
injectStore(api.store, (state) => state.isSubmitting)
|
|
11
|
+
|
|
12
|
+
return api
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { injectStore as injectAngularStore } from '@tanstack/angular-store'
|
|
2
|
+
import type { FormApi, FormState, Validator } from '@tanstack/form-core'
|
|
3
|
+
|
|
4
|
+
export function injectStore<
|
|
5
|
+
TFormData,
|
|
6
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
7
|
+
TSelected = NoInfer<FormState<TFormData>>,
|
|
8
|
+
>(
|
|
9
|
+
form: FormApi<TFormData, TFormValidator>,
|
|
10
|
+
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,
|
|
11
|
+
) {
|
|
12
|
+
return injectAngularStore(form.store, selector)
|
|
13
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Directive,
|
|
3
|
+
Input,
|
|
4
|
+
type OnChanges,
|
|
5
|
+
type OnDestroy,
|
|
6
|
+
type OnInit,
|
|
7
|
+
} from '@angular/core'
|
|
8
|
+
import {
|
|
9
|
+
type DeepKeys,
|
|
10
|
+
type DeepValue,
|
|
11
|
+
FieldApi,
|
|
12
|
+
type FieldMeta,
|
|
13
|
+
type FieldOptions,
|
|
14
|
+
type FieldValidators,
|
|
15
|
+
FormApi,
|
|
16
|
+
type NoInfer as NoInferHack,
|
|
17
|
+
type Validator,
|
|
18
|
+
} from '@tanstack/form-core'
|
|
19
|
+
|
|
20
|
+
@Directive({
|
|
21
|
+
selector: '[tanstackField]',
|
|
22
|
+
standalone: true,
|
|
23
|
+
exportAs: 'field',
|
|
24
|
+
})
|
|
25
|
+
export class TanStackField<
|
|
26
|
+
TParentData,
|
|
27
|
+
const TName extends DeepKeys<TParentData>,
|
|
28
|
+
TFieldValidator extends
|
|
29
|
+
| Validator<DeepValue<TParentData, TName>, unknown>
|
|
30
|
+
| undefined = undefined,
|
|
31
|
+
TFormValidator extends
|
|
32
|
+
| Validator<TParentData, unknown>
|
|
33
|
+
| undefined = undefined,
|
|
34
|
+
TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,
|
|
35
|
+
>
|
|
36
|
+
implements
|
|
37
|
+
OnInit,
|
|
38
|
+
OnChanges,
|
|
39
|
+
OnDestroy,
|
|
40
|
+
FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>
|
|
41
|
+
{
|
|
42
|
+
@Input({ required: true }) name!: TName
|
|
43
|
+
// Setting as NoInferHack as it's the same internal type cast as TanStack Form Core
|
|
44
|
+
// This can be removed when TanStack Form Core is moved to TS min of 5.4
|
|
45
|
+
// and the NoInfer internal util type is rm-rf'd
|
|
46
|
+
@Input() defaultValue?: NoInferHack<TData>
|
|
47
|
+
@Input() asyncDebounceMs?: number
|
|
48
|
+
@Input() asyncAlways?: boolean
|
|
49
|
+
@Input() preserveValue?: boolean
|
|
50
|
+
@Input() validatorAdapter?: TFieldValidator
|
|
51
|
+
@Input({ required: true }) tanstackField!: FormApi<
|
|
52
|
+
TParentData,
|
|
53
|
+
TFormValidator
|
|
54
|
+
>
|
|
55
|
+
@Input() validators?: NoInfer<
|
|
56
|
+
FieldValidators<TParentData, TName, TFieldValidator, TFormValidator, TData>
|
|
57
|
+
>
|
|
58
|
+
@Input() defaultMeta?: Partial<FieldMeta>
|
|
59
|
+
|
|
60
|
+
api!: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>
|
|
61
|
+
|
|
62
|
+
private getOptions() {
|
|
63
|
+
return {
|
|
64
|
+
defaultValue: this.defaultValue,
|
|
65
|
+
asyncDebounceMs: this.asyncDebounceMs,
|
|
66
|
+
asyncAlways: this.asyncAlways,
|
|
67
|
+
preserveValue: this.preserveValue,
|
|
68
|
+
validatorAdapter: this.validatorAdapter,
|
|
69
|
+
validators: this.validators,
|
|
70
|
+
defaultMeta: this.defaultMeta,
|
|
71
|
+
name: this.name,
|
|
72
|
+
form: this.tanstackField,
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
unmount?: () => void
|
|
77
|
+
|
|
78
|
+
ngOnInit() {
|
|
79
|
+
this.api = new FieldApi(this.getOptions())
|
|
80
|
+
|
|
81
|
+
this.unmount = this.api.mount()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
ngOnDestroy() {
|
|
85
|
+
this.unmount?.()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
ngOnChanges() {
|
|
89
|
+
const api = this.api as typeof this.api | undefined
|
|
90
|
+
if (!api) return
|
|
91
|
+
api.update(this.getOptions())
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import '@analogjs/vite-plugin-angular/setup-vitest'
|
|
2
|
+
import '@testing-library/jest-dom/vitest'
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
BrowserDynamicTestingModule,
|
|
6
|
+
platformBrowserDynamicTesting,
|
|
7
|
+
} from '@angular/platform-browser-dynamic/testing'
|
|
8
|
+
import { getTestBed } from '@angular/core/testing'
|
|
9
|
+
|
|
10
|
+
getTestBed().initTestEnvironment(
|
|
11
|
+
BrowserDynamicTestingModule,
|
|
12
|
+
platformBrowserDynamicTesting(),
|
|
13
|
+
)
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { render } from '@testing-library/angular'
|
|
2
|
+
import { Component } from '@angular/core'
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
import { userEvent } from '@testing-library/user-event'
|
|
5
|
+
import { injectForm } from '../inject-form'
|
|
6
|
+
import { TanStackField } from '../tanstack-field.directive'
|
|
7
|
+
import { sleep } from './utils'
|
|
8
|
+
import type { FieldValidateAsyncFn, FieldValidateFn } from '@tanstack/form-core'
|
|
9
|
+
|
|
10
|
+
const user = userEvent.setup()
|
|
11
|
+
|
|
12
|
+
describe('TanStackFieldDirective', () => {
|
|
13
|
+
it('should allow to set default value', async () => {
|
|
14
|
+
@Component({
|
|
15
|
+
selector: 'test-component',
|
|
16
|
+
standalone: true,
|
|
17
|
+
template: `
|
|
18
|
+
<ng-container [tanstackField]="form" name="firstName" #f="field">
|
|
19
|
+
<input
|
|
20
|
+
data-testid="fieldinput"
|
|
21
|
+
[value]="f.api.state.value"
|
|
22
|
+
(blur)="f.api.handleBlur()"
|
|
23
|
+
(input)="f.api.handleChange($any($event).target.value)"
|
|
24
|
+
/>
|
|
25
|
+
</ng-container>
|
|
26
|
+
`,
|
|
27
|
+
imports: [TanStackField],
|
|
28
|
+
})
|
|
29
|
+
class TestComponent {
|
|
30
|
+
form = injectForm({
|
|
31
|
+
defaultValues: {
|
|
32
|
+
firstName: 'FirstName',
|
|
33
|
+
lastName: 'LastName',
|
|
34
|
+
},
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { getByTestId } = await render(TestComponent)
|
|
39
|
+
|
|
40
|
+
const input = getByTestId('fieldinput')
|
|
41
|
+
expect(input).toHaveValue('FirstName')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should use field default value first', async () => {
|
|
45
|
+
type Person = {
|
|
46
|
+
firstName: string
|
|
47
|
+
lastName: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@Component({
|
|
51
|
+
selector: 'test-component',
|
|
52
|
+
standalone: true,
|
|
53
|
+
template: `
|
|
54
|
+
<ng-container
|
|
55
|
+
[tanstackField]="form"
|
|
56
|
+
name="firstName"
|
|
57
|
+
defaultValue="otherName"
|
|
58
|
+
#f="field"
|
|
59
|
+
>
|
|
60
|
+
<input
|
|
61
|
+
data-testid="fieldinput"
|
|
62
|
+
[value]="f.api.state.value"
|
|
63
|
+
(blur)="f.api.handleBlur()"
|
|
64
|
+
(input)="f.api.handleChange($any($event).target.value)"
|
|
65
|
+
/>
|
|
66
|
+
</ng-container>
|
|
67
|
+
`,
|
|
68
|
+
imports: [TanStackField],
|
|
69
|
+
})
|
|
70
|
+
class TestComponent {
|
|
71
|
+
form = injectForm<Person>({
|
|
72
|
+
defaultValues: {
|
|
73
|
+
firstName: 'FirstName',
|
|
74
|
+
lastName: 'LastName',
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const { getByTestId } = await render(TestComponent)
|
|
80
|
+
|
|
81
|
+
const input = getByTestId('fieldinput')
|
|
82
|
+
expect(input).toHaveValue('otherName')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('should not validate on change if isTouched is false', async () => {
|
|
86
|
+
type Person = {
|
|
87
|
+
firstName: string
|
|
88
|
+
lastName: string
|
|
89
|
+
}
|
|
90
|
+
const error = 'Please enter a different value'
|
|
91
|
+
|
|
92
|
+
@Component({
|
|
93
|
+
selector: 'test-component',
|
|
94
|
+
standalone: true,
|
|
95
|
+
template: `
|
|
96
|
+
<ng-container
|
|
97
|
+
[tanstackField]="form"
|
|
98
|
+
name="firstName"
|
|
99
|
+
[validators]="{ onChange: otherValidator }"
|
|
100
|
+
#f="field"
|
|
101
|
+
>
|
|
102
|
+
<input
|
|
103
|
+
data-testid="fieldinput"
|
|
104
|
+
[value]="f.api.state.value"
|
|
105
|
+
(blur)="f.api.handleBlur()"
|
|
106
|
+
(input)="f.api.setValue($any($event).target.value)"
|
|
107
|
+
/>
|
|
108
|
+
@for (error of f.api.getMeta().errors; track error) {
|
|
109
|
+
<p>{{ error }}</p>
|
|
110
|
+
}
|
|
111
|
+
</ng-container>
|
|
112
|
+
`,
|
|
113
|
+
imports: [TanStackField],
|
|
114
|
+
})
|
|
115
|
+
class TestComponent {
|
|
116
|
+
otherValidator: FieldValidateFn<Person, 'firstName'> = ({ value }) =>
|
|
117
|
+
value === 'other' ? error : undefined
|
|
118
|
+
|
|
119
|
+
form = injectForm<Person>({
|
|
120
|
+
defaultValues: {
|
|
121
|
+
firstName: 'FirstName',
|
|
122
|
+
lastName: 'LastName',
|
|
123
|
+
},
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const { getByTestId, queryByText } = await render(TestComponent)
|
|
128
|
+
const input = getByTestId('fieldinput')
|
|
129
|
+
await user.type(input, 'other')
|
|
130
|
+
expect(queryByText(error)).not.toBeInTheDocument()
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('should validate on change if isTouched is true', async () => {
|
|
134
|
+
type Person = {
|
|
135
|
+
firstName: string
|
|
136
|
+
lastName: string
|
|
137
|
+
}
|
|
138
|
+
const error = 'Please enter a different value'
|
|
139
|
+
|
|
140
|
+
@Component({
|
|
141
|
+
selector: 'test-component',
|
|
142
|
+
standalone: true,
|
|
143
|
+
template: `
|
|
144
|
+
<ng-container
|
|
145
|
+
[tanstackField]="form"
|
|
146
|
+
name="firstName"
|
|
147
|
+
[defaultMeta]="{ isTouched: true }"
|
|
148
|
+
[validators]="{ onChange: otherValidator }"
|
|
149
|
+
#f="field"
|
|
150
|
+
>
|
|
151
|
+
<input
|
|
152
|
+
data-testid="fieldinput"
|
|
153
|
+
[value]="f.api.state.value"
|
|
154
|
+
(blur)="f.api.handleBlur()"
|
|
155
|
+
(input)="f.api.handleChange($any($event).target.value)"
|
|
156
|
+
/>
|
|
157
|
+
<p>{{ f.api.getMeta().errorMap?.onChange }}</p>
|
|
158
|
+
</ng-container>
|
|
159
|
+
`,
|
|
160
|
+
imports: [TanStackField],
|
|
161
|
+
})
|
|
162
|
+
class TestComponent {
|
|
163
|
+
otherValidator: FieldValidateFn<Person, 'firstName'> = ({ value }) =>
|
|
164
|
+
value === 'other' ? error : undefined
|
|
165
|
+
|
|
166
|
+
form = injectForm<Person>({
|
|
167
|
+
defaultValues: {
|
|
168
|
+
firstName: '',
|
|
169
|
+
lastName: '',
|
|
170
|
+
},
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const { getByTestId, queryByText, getByText } = await render(TestComponent)
|
|
175
|
+
|
|
176
|
+
const input = getByTestId('fieldinput')
|
|
177
|
+
expect(queryByText(error)).not.toBeInTheDocument()
|
|
178
|
+
await user.type(input, 'other')
|
|
179
|
+
expect(getByText(error)).toBeInTheDocument()
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it('should validate on change and on blur', async () => {
|
|
183
|
+
type Person = {
|
|
184
|
+
firstName: string
|
|
185
|
+
lastName: string
|
|
186
|
+
}
|
|
187
|
+
const onChangeError = 'Please enter a different value (onChangeError)'
|
|
188
|
+
const onBlurError = 'Please enter a different value (onBlurError)'
|
|
189
|
+
|
|
190
|
+
@Component({
|
|
191
|
+
selector: 'test-component',
|
|
192
|
+
standalone: true,
|
|
193
|
+
template: `
|
|
194
|
+
<ng-container
|
|
195
|
+
[tanstackField]="form"
|
|
196
|
+
name="firstName"
|
|
197
|
+
[defaultMeta]="{ isTouched: true }"
|
|
198
|
+
[validators]="{ onChange: onChange, onBlur: onBlur }"
|
|
199
|
+
#f="field"
|
|
200
|
+
>
|
|
201
|
+
<input
|
|
202
|
+
data-testid="fieldinput"
|
|
203
|
+
[value]="f.api.state.value"
|
|
204
|
+
[name]="f.api.name"
|
|
205
|
+
(blur)="f.api.handleBlur()"
|
|
206
|
+
(input)="f.api.setValue($any($event).target.value)"
|
|
207
|
+
/>
|
|
208
|
+
<p>{{ f.api.getMeta().errorMap?.onChange }}</p>
|
|
209
|
+
<p>{{ f.api.getMeta().errorMap?.onBlur }}</p>
|
|
210
|
+
</ng-container>
|
|
211
|
+
`,
|
|
212
|
+
imports: [TanStackField],
|
|
213
|
+
})
|
|
214
|
+
class TestComponent {
|
|
215
|
+
onChange: FieldValidateFn<Person, 'firstName'> = ({ value }) =>
|
|
216
|
+
value === 'other' ? onChangeError : undefined
|
|
217
|
+
onBlur: FieldValidateFn<Person, 'firstName'> = ({ value }) =>
|
|
218
|
+
value === 'other' ? onBlurError : undefined
|
|
219
|
+
|
|
220
|
+
form = injectForm<Person>({
|
|
221
|
+
defaultValues: {
|
|
222
|
+
firstName: '',
|
|
223
|
+
lastName: '',
|
|
224
|
+
},
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const { getByTestId, getByText, queryByText } = await render(TestComponent)
|
|
229
|
+
const input = getByTestId('fieldinput')
|
|
230
|
+
expect(queryByText(onChangeError)).not.toBeInTheDocument()
|
|
231
|
+
expect(queryByText(onBlurError)).not.toBeInTheDocument()
|
|
232
|
+
await user.type(input, 'other')
|
|
233
|
+
expect(getByText(onChangeError)).toBeInTheDocument()
|
|
234
|
+
await user.click(document.body)
|
|
235
|
+
expect(queryByText(onBlurError)).toBeInTheDocument()
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
it('should validate async on change', async () => {
|
|
239
|
+
type Person = {
|
|
240
|
+
firstName: string
|
|
241
|
+
lastName: string
|
|
242
|
+
}
|
|
243
|
+
const error = 'Please enter a different value'
|
|
244
|
+
|
|
245
|
+
@Component({
|
|
246
|
+
selector: 'test-component',
|
|
247
|
+
standalone: true,
|
|
248
|
+
template: `
|
|
249
|
+
<ng-container
|
|
250
|
+
[tanstackField]="form"
|
|
251
|
+
name="firstName"
|
|
252
|
+
[defaultMeta]="{ isTouched: true }"
|
|
253
|
+
[validators]="{ onChangeAsync: onChangeAsync }"
|
|
254
|
+
#f="field"
|
|
255
|
+
>
|
|
256
|
+
<input
|
|
257
|
+
data-testid="fieldinput"
|
|
258
|
+
[name]="f.api.name"
|
|
259
|
+
[value]="f.api.state.value"
|
|
260
|
+
(blur)="f.api.handleBlur()"
|
|
261
|
+
(input)="f.api.handleChange($any($event).target.value)"
|
|
262
|
+
/>
|
|
263
|
+
<p>{{ f.api.getMeta().errorMap?.onChange }}</p>
|
|
264
|
+
</ng-container>
|
|
265
|
+
`,
|
|
266
|
+
imports: [TanStackField],
|
|
267
|
+
})
|
|
268
|
+
class TestComponent {
|
|
269
|
+
onChangeAsync: FieldValidateAsyncFn<Person, 'firstName'> = async () => {
|
|
270
|
+
await sleep(10)
|
|
271
|
+
return error
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
form = injectForm<Person>({
|
|
275
|
+
defaultValues: {
|
|
276
|
+
firstName: '',
|
|
277
|
+
lastName: '',
|
|
278
|
+
},
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const { getByTestId, getByText, findByText, queryByText } =
|
|
283
|
+
await render(TestComponent)
|
|
284
|
+
const input = getByTestId('fieldinput')
|
|
285
|
+
expect(queryByText(error)).not.toBeInTheDocument()
|
|
286
|
+
await user.type(input, 'other')
|
|
287
|
+
await findByText(error)
|
|
288
|
+
expect(getByText(error)).toBeInTheDocument()
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
it('should validate async on change and async on blur', async () => {
|
|
292
|
+
type Person = {
|
|
293
|
+
firstName: string
|
|
294
|
+
lastName: string
|
|
295
|
+
}
|
|
296
|
+
const onChangeError = 'Please enter a different value (onChangeError)'
|
|
297
|
+
const onBlurError = 'Please enter a different value (onBlurError)'
|
|
298
|
+
|
|
299
|
+
@Component({
|
|
300
|
+
selector: 'test-component',
|
|
301
|
+
standalone: true,
|
|
302
|
+
template: `
|
|
303
|
+
<ng-container
|
|
304
|
+
[tanstackField]="form"
|
|
305
|
+
name="firstName"
|
|
306
|
+
[defaultMeta]="{ isTouched: true }"
|
|
307
|
+
[validators]="{
|
|
308
|
+
onChangeAsync: onChangeAsync,
|
|
309
|
+
onBlurAsync: onBlurAsync
|
|
310
|
+
}"
|
|
311
|
+
#f="field"
|
|
312
|
+
>
|
|
313
|
+
<input
|
|
314
|
+
data-testid="fieldinput"
|
|
315
|
+
[name]="f.api.name"
|
|
316
|
+
[value]="f.api.state.value"
|
|
317
|
+
(blur)="f.api.handleBlur()"
|
|
318
|
+
(input)="f.api.handleChange($any($event).target.value)"
|
|
319
|
+
/>
|
|
320
|
+
<p>{{ f.api.getMeta().errorMap?.onChange }}</p>
|
|
321
|
+
<p>{{ f.api.getMeta().errorMap?.onBlur }}</p>
|
|
322
|
+
</ng-container>
|
|
323
|
+
`,
|
|
324
|
+
imports: [TanStackField],
|
|
325
|
+
})
|
|
326
|
+
class TestComponent {
|
|
327
|
+
onChangeAsync: FieldValidateAsyncFn<Person, 'firstName'> = async () => {
|
|
328
|
+
await sleep(10)
|
|
329
|
+
return onChangeError
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
onBlurAsync: FieldValidateAsyncFn<Person, 'firstName'> = async () => {
|
|
333
|
+
await sleep(10)
|
|
334
|
+
return onBlurError
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
form = injectForm<Person>({
|
|
338
|
+
defaultValues: {
|
|
339
|
+
firstName: '',
|
|
340
|
+
lastName: '',
|
|
341
|
+
},
|
|
342
|
+
})
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const { getByTestId, getByText, findByText, queryByText } =
|
|
346
|
+
await render(TestComponent)
|
|
347
|
+
const input = getByTestId('fieldinput')
|
|
348
|
+
|
|
349
|
+
expect(queryByText(onChangeError)).not.toBeInTheDocument()
|
|
350
|
+
expect(queryByText(onBlurError)).not.toBeInTheDocument()
|
|
351
|
+
await user.type(input, 'other')
|
|
352
|
+
await findByText(onChangeError)
|
|
353
|
+
expect(getByText(onChangeError)).toBeInTheDocument()
|
|
354
|
+
await user.click(document.body)
|
|
355
|
+
await findByText(onBlurError)
|
|
356
|
+
expect(getByText(onBlurError)).toBeInTheDocument()
|
|
357
|
+
})
|
|
358
|
+
})
|