@tanstack/vue-form 0.3.1 → 0.3.3

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.
Files changed (94) hide show
  1. package/build/legacy/createFormFactory.cjs +42 -0
  2. package/build/legacy/createFormFactory.cjs.map +1 -0
  3. package/build/legacy/createFormFactory.d.cts +13 -0
  4. package/build/legacy/createFormFactory.d.ts +13 -0
  5. package/build/legacy/createFormFactory.js +17 -0
  6. package/build/legacy/createFormFactory.js.map +1 -0
  7. package/build/legacy/formContext.cjs +46 -0
  8. package/build/legacy/formContext.cjs.map +1 -0
  9. package/build/legacy/formContext.d.cts +14 -0
  10. package/build/legacy/formContext.d.ts +14 -0
  11. package/build/legacy/formContext.js +19 -0
  12. package/build/legacy/formContext.js.map +1 -0
  13. package/build/legacy/index.cjs +33 -0
  14. package/build/legacy/index.cjs.map +1 -0
  15. package/build/legacy/index.d.cts +8 -0
  16. package/build/legacy/index.d.ts +8 -0
  17. package/build/legacy/index.js +7 -0
  18. package/build/legacy/index.js.map +1 -0
  19. package/build/legacy/types.cjs +19 -0
  20. package/build/legacy/types.cjs.map +1 -0
  21. package/build/legacy/types.d.cts +7 -0
  22. package/build/legacy/types.d.ts +7 -0
  23. package/build/legacy/types.js +1 -0
  24. package/build/legacy/types.js.map +1 -0
  25. package/build/legacy/useField.cjs +77 -0
  26. package/build/legacy/useField.cjs.map +1 -0
  27. package/build/legacy/useField.d.cts +44 -0
  28. package/build/legacy/useField.d.ts +44 -0
  29. package/build/legacy/useField.js +53 -0
  30. package/build/legacy/useField.js.map +1 -0
  31. package/build/legacy/useForm.cjs +70 -0
  32. package/build/legacy/useForm.cjs.map +1 -0
  33. package/build/legacy/useForm.d.cts +23 -0
  34. package/build/legacy/useForm.d.ts +23 -0
  35. package/build/legacy/useForm.js +47 -0
  36. package/build/legacy/useForm.js.map +1 -0
  37. package/build/lib/createFormFactory.d.ts +8 -0
  38. package/build/lib/createFormFactory.js +12 -0
  39. package/build/lib/formContext.d.ts +11 -0
  40. package/build/lib/formContext.js +12 -0
  41. package/build/lib/index.d.ts +5 -0
  42. package/build/lib/index.js +5 -0
  43. package/build/lib/tests/useField.test.d.ts +2 -0
  44. package/build/lib/tests/useField.test.jsx +109 -0
  45. package/build/lib/tests/useForm.test.d.ts +2 -0
  46. package/build/lib/tests/useForm.test.jsx +71 -0
  47. package/build/lib/tests/utils.d.ts +1 -0
  48. package/build/lib/tests/utils.js +5 -0
  49. package/build/lib/types.d.ts +4 -0
  50. package/build/lib/types.js +1 -0
  51. package/build/lib/useField.d.ts +42 -0
  52. package/build/lib/useField.jsx +41 -0
  53. package/build/lib/useForm.d.ts +19 -0
  54. package/build/lib/useForm.jsx +35 -0
  55. package/build/modern/createFormFactory.cjs +42 -0
  56. package/build/modern/createFormFactory.cjs.map +1 -0
  57. package/build/modern/createFormFactory.d.cts +13 -0
  58. package/build/modern/createFormFactory.d.ts +13 -0
  59. package/build/modern/createFormFactory.js +17 -0
  60. package/build/modern/createFormFactory.js.map +1 -0
  61. package/build/modern/formContext.cjs +46 -0
  62. package/build/modern/formContext.cjs.map +1 -0
  63. package/build/modern/formContext.d.cts +14 -0
  64. package/build/modern/formContext.d.ts +14 -0
  65. package/build/modern/formContext.js +19 -0
  66. package/build/modern/formContext.js.map +1 -0
  67. package/build/modern/index.cjs +33 -0
  68. package/build/modern/index.cjs.map +1 -0
  69. package/build/modern/index.d.cts +8 -0
  70. package/build/modern/index.d.ts +8 -0
  71. package/build/modern/index.js +7 -0
  72. package/build/modern/index.js.map +1 -0
  73. package/build/modern/types.cjs +19 -0
  74. package/build/modern/types.cjs.map +1 -0
  75. package/build/modern/types.d.cts +7 -0
  76. package/build/modern/types.d.ts +7 -0
  77. package/build/modern/types.js +1 -0
  78. package/build/modern/types.js.map +1 -0
  79. package/build/modern/useField.cjs +77 -0
  80. package/build/modern/useField.cjs.map +1 -0
  81. package/build/modern/useField.d.cts +44 -0
  82. package/build/modern/useField.d.ts +44 -0
  83. package/build/modern/useField.js +53 -0
  84. package/build/modern/useField.js.map +1 -0
  85. package/build/modern/useForm.cjs +70 -0
  86. package/build/modern/useForm.cjs.map +1 -0
  87. package/build/modern/useForm.d.cts +23 -0
  88. package/build/modern/useForm.d.ts +23 -0
  89. package/build/modern/useForm.js +47 -0
  90. package/build/modern/useForm.js.map +1 -0
  91. package/package.json +2 -2
  92. package/src/types.ts +8 -2
  93. package/src/useField.tsx +74 -67
  94. package/src/useForm.tsx +1 -2
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/useForm.tsx"],"sourcesContent":["import { FormApi, type FormState, type FormOptions } from '@tanstack/form-core'\nimport { type NoInfer, useStore } from '@tanstack/vue-store'\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { provideFormContext } from './formContext'\nimport {\n type EmitsOptions,\n type SlotsType,\n type SetupContext,\n defineComponent,\n} from 'vue-demi'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData> {\n Provider: (props: Record<string, any> & {}) => any\n provideFormContext: () => void\n Field: FieldComponent<TFormData, TFormData>\n useField: UseField<TFormData>\n useStore: <TSelected = NoInfer<FormState<TFormData>>>(\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n ) => TSelected\n Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(\n props: {\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected\n },\n context: SetupContext<\n EmitsOptions,\n SlotsType<{ default: NoInfer<FormState<TFormData>> }>\n >,\n ) => any\n }\n}\n\nexport function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData> {\n const formApi = (() => {\n const api = new FormApi<TData>(opts)\n\n api.Provider = defineComponent(\n (_, context) => {\n provideFormContext({ formApi })\n return () => context.slots.default!()\n },\n { name: 'Provider' },\n )\n api.provideFormContext = () => {\n provideFormContext({ formApi })\n }\n api.Field = Field as never\n api.useField = useField as never\n api.useStore = (selector) => {\n return useStore(api.store as never, selector as never) as never\n }\n api.Subscribe = defineComponent(\n (props, context) => {\n const allProps = { ...props, ...context.attrs }\n const selector = allProps.selector ?? ((state) => state)\n const data = useStore(api.store as never, selector as never)\n return () => context.slots.default!(data.value)\n },\n {\n name: 'Subscribe',\n inheritAttrs: false,\n },\n )\n\n return api\n })()\n\n // formApi.useStore((state) => state.isSubmitting)\n formApi.update(opts)\n\n return formApi as never\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAA0D;AAC1D,uBAAuC;AACvC,sBAAoE;AACpE,yBAAmC;AACnC,sBAKO;AAwBA,SAAS,QAAe,MAA2C;AACxE,QAAM,WAAW,MAAM;AACrB,UAAM,MAAM,IAAI,yBAAe,IAAI;AAEnC,QAAI,eAAW;AAAA,MACb,CAAC,GAAG,YAAY;AACd,mDAAmB,EAAE,QAAQ,CAAC;AAC9B,eAAO,MAAM,QAAQ,MAAM,QAAS;AAAA,MACtC;AAAA,MACA,EAAE,MAAM,WAAW;AAAA,IACrB;AACA,QAAI,qBAAqB,MAAM;AAC7B,iDAAmB,EAAE,QAAQ,CAAC;AAAA,IAChC;AACA,QAAI,QAAQ;AACZ,QAAI,WAAW;AACf,QAAI,WAAW,CAAC,aAAa;AAC3B,iBAAO,2BAAS,IAAI,OAAgB,QAAiB;AAAA,IACvD;AACA,QAAI,gBAAY;AAAA,MACd,CAAC,OAAO,YAAY;AAClB,cAAM,WAAW,EAAE,GAAG,OAAO,GAAG,QAAQ,MAAM;AAC9C,cAAM,WAAW,SAAS,aAAa,CAAC,UAAU;AAClD,cAAM,WAAO,2BAAS,IAAI,OAAgB,QAAiB;AAC3D,eAAO,MAAM,QAAQ,MAAM,QAAS,KAAK,KAAK;AAAA,MAChD;AAAA,MACA;AAAA,QACE,MAAM;AAAA,QACN,cAAc;AAAA,MAChB;AAAA,IACF;AAEA,WAAO;AAAA,EACT,GAAG;AAGH,UAAQ,OAAO,IAAI;AAEnB,SAAO;AACT;","names":[]}
@@ -0,0 +1,23 @@
1
+ import { FormState, FormOptions, FormApi } from '@tanstack/form-core';
2
+ import { NoInfer } from '@tanstack/vue-store';
3
+ import { FieldComponent, UseField } from './useField.cjs';
4
+ import { SetupContext, EmitsOptions, SlotsType } from 'vue-demi';
5
+ import './types.cjs';
6
+
7
+ declare module '@tanstack/form-core' {
8
+ interface FormApi<TFormData> {
9
+ Provider: (props: Record<string, any> & {}) => any;
10
+ provideFormContext: () => void;
11
+ Field: FieldComponent<TFormData, TFormData>;
12
+ useField: UseField<TFormData>;
13
+ useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => TSelected;
14
+ Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
15
+ selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
16
+ }, context: SetupContext<EmitsOptions, SlotsType<{
17
+ default: NoInfer<FormState<TFormData>>;
18
+ }>>) => any;
19
+ }
20
+ }
21
+ declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
22
+
23
+ export { useForm };
@@ -0,0 +1,23 @@
1
+ import { FormState, FormOptions, FormApi } from '@tanstack/form-core';
2
+ import { NoInfer } from '@tanstack/vue-store';
3
+ import { FieldComponent, UseField } from './useField.js';
4
+ import { SetupContext, EmitsOptions, SlotsType } from 'vue-demi';
5
+ import './types.js';
6
+
7
+ declare module '@tanstack/form-core' {
8
+ interface FormApi<TFormData> {
9
+ Provider: (props: Record<string, any> & {}) => any;
10
+ provideFormContext: () => void;
11
+ Field: FieldComponent<TFormData, TFormData>;
12
+ useField: UseField<TFormData>;
13
+ useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => TSelected;
14
+ Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
15
+ selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
16
+ }, context: SetupContext<EmitsOptions, SlotsType<{
17
+ default: NoInfer<FormState<TFormData>>;
18
+ }>>) => any;
19
+ }
20
+ }
21
+ declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
22
+
23
+ export { useForm };
@@ -0,0 +1,47 @@
1
+ // src/useForm.tsx
2
+ import { FormApi } from "@tanstack/form-core";
3
+ import { useStore } from "@tanstack/vue-store";
4
+ import { Field, useField } from "./useField.js";
5
+ import { provideFormContext } from "./formContext.js";
6
+ import {
7
+ defineComponent
8
+ } from "vue-demi";
9
+ function useForm(opts) {
10
+ const formApi = (() => {
11
+ const api = new FormApi(opts);
12
+ api.Provider = defineComponent(
13
+ (_, context) => {
14
+ provideFormContext({ formApi });
15
+ return () => context.slots.default();
16
+ },
17
+ { name: "Provider" }
18
+ );
19
+ api.provideFormContext = () => {
20
+ provideFormContext({ formApi });
21
+ };
22
+ api.Field = Field;
23
+ api.useField = useField;
24
+ api.useStore = (selector) => {
25
+ return useStore(api.store, selector);
26
+ };
27
+ api.Subscribe = defineComponent(
28
+ (props, context) => {
29
+ const allProps = { ...props, ...context.attrs };
30
+ const selector = allProps.selector ?? ((state) => state);
31
+ const data = useStore(api.store, selector);
32
+ return () => context.slots.default(data.value);
33
+ },
34
+ {
35
+ name: "Subscribe",
36
+ inheritAttrs: false
37
+ }
38
+ );
39
+ return api;
40
+ })();
41
+ formApi.update(opts);
42
+ return formApi;
43
+ }
44
+ export {
45
+ useForm
46
+ };
47
+ //# sourceMappingURL=useForm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/useForm.tsx"],"sourcesContent":["import { FormApi, type FormState, type FormOptions } from '@tanstack/form-core'\nimport { type NoInfer, useStore } from '@tanstack/vue-store'\nimport { type UseField, type FieldComponent, Field, useField } from './useField'\nimport { provideFormContext } from './formContext'\nimport {\n type EmitsOptions,\n type SlotsType,\n type SetupContext,\n defineComponent,\n} from 'vue-demi'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData> {\n Provider: (props: Record<string, any> & {}) => any\n provideFormContext: () => void\n Field: FieldComponent<TFormData, TFormData>\n useField: UseField<TFormData>\n useStore: <TSelected = NoInfer<FormState<TFormData>>>(\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n ) => TSelected\n Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(\n props: {\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected\n },\n context: SetupContext<\n EmitsOptions,\n SlotsType<{ default: NoInfer<FormState<TFormData>> }>\n >,\n ) => any\n }\n}\n\nexport function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData> {\n const formApi = (() => {\n const api = new FormApi<TData>(opts)\n\n api.Provider = defineComponent(\n (_, context) => {\n provideFormContext({ formApi })\n return () => context.slots.default!()\n },\n { name: 'Provider' },\n )\n api.provideFormContext = () => {\n provideFormContext({ formApi })\n }\n api.Field = Field as never\n api.useField = useField as never\n api.useStore = (selector) => {\n return useStore(api.store as never, selector as never) as never\n }\n api.Subscribe = defineComponent(\n (props, context) => {\n const allProps = { ...props, ...context.attrs }\n const selector = allProps.selector ?? ((state) => state)\n const data = useStore(api.store as never, selector as never)\n return () => context.slots.default!(data.value)\n },\n {\n name: 'Subscribe',\n inheritAttrs: false,\n },\n )\n\n return api\n })()\n\n // formApi.useStore((state) => state.isSubmitting)\n formApi.update(opts)\n\n return formApi as never\n}\n"],"mappings":";AAAA,SAAS,eAAiD;AAC1D,SAAuB,gBAAgB;AACvC,SAA6C,OAAO,gBAAgB;AACpE,SAAS,0BAA0B;AACnC;AAAA,EAIE;AAAA,OACK;AAwBA,SAAS,QAAe,MAA2C;AACxE,QAAM,WAAW,MAAM;AACrB,UAAM,MAAM,IAAI,QAAe,IAAI;AAEnC,QAAI,WAAW;AAAA,MACb,CAAC,GAAG,YAAY;AACd,2BAAmB,EAAE,QAAQ,CAAC;AAC9B,eAAO,MAAM,QAAQ,MAAM,QAAS;AAAA,MACtC;AAAA,MACA,EAAE,MAAM,WAAW;AAAA,IACrB;AACA,QAAI,qBAAqB,MAAM;AAC7B,yBAAmB,EAAE,QAAQ,CAAC;AAAA,IAChC;AACA,QAAI,QAAQ;AACZ,QAAI,WAAW;AACf,QAAI,WAAW,CAAC,aAAa;AAC3B,aAAO,SAAS,IAAI,OAAgB,QAAiB;AAAA,IACvD;AACA,QAAI,YAAY;AAAA,MACd,CAAC,OAAO,YAAY;AAClB,cAAM,WAAW,EAAE,GAAG,OAAO,GAAG,QAAQ,MAAM;AAC9C,cAAM,WAAW,SAAS,aAAa,CAAC,UAAU;AAClD,cAAM,OAAO,SAAS,IAAI,OAAgB,QAAiB;AAC3D,eAAO,MAAM,QAAQ,MAAM,QAAS,KAAK,KAAK;AAAA,MAChD;AAAA,MACA;AAAA,QACE,MAAM;AAAA,QACN,cAAc;AAAA,MAChB;AAAA,IACF;AAEA,WAAO;AAAA,EACT,GAAG;AAGH,UAAQ,OAAO,IAAI;AAEnB,SAAO;AACT;","names":[]}
@@ -0,0 +1,8 @@
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>;
@@ -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,11 @@
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
+ };
@@ -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,5 @@
1
+ export * from '@tanstack/form-core';
2
+ export * from './createFormFactory';
3
+ export * from './formContext';
4
+ export * from './useField';
5
+ export * from './useForm';
@@ -0,0 +1,5 @@
1
+ export * from '@tanstack/form-core';
2
+ export * from './createFormFactory';
3
+ export * from './formContext';
4
+ export * from './useField';
5
+ export * from './useForm';
@@ -0,0 +1,2 @@
1
+ /// <reference lib="dom" />
2
+ import '@testing-library/jest-dom';
@@ -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().errors}</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().errors}</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().errors}</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().errors}</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
+ });
@@ -0,0 +1,2 @@
1
+ /// <reference lib="dom" />
2
+ import '@testing-library/jest-dom';
@@ -0,0 +1,71 @@
1
+ /// <reference lib="dom" />
2
+ import { defineComponent, ref } from 'vue-demi';
3
+ import { render, waitFor } from '@testing-library/vue';
4
+ import '@testing-library/jest-dom';
5
+ import { createFormFactory, provideFormContext, useForm, } from '../index';
6
+ import userEvent from '@testing-library/user-event';
7
+ const user = userEvent.setup();
8
+ describe('useForm', () => {
9
+ it('preserved field state', async () => {
10
+ const formFactory = createFormFactory();
11
+ const Comp = defineComponent(() => {
12
+ const form = formFactory.useForm();
13
+ provideFormContext({ formApi: form });
14
+ return () => (<form.Field name="firstName" defaultValue="">
15
+ {({ field }) => (<input data-testid={'fieldinput'} value={field.state.value} onBlur={field.handleBlur} onInput={(e) => field.handleChange(e.target.value)}/>)}
16
+ </form.Field>);
17
+ });
18
+ const { getByTestId, queryByText } = render(Comp);
19
+ const input = getByTestId('fieldinput');
20
+ expect(queryByText('FirstName')).not.toBeInTheDocument();
21
+ await user.type(input, 'FirstName');
22
+ expect(input).toHaveValue('FirstName');
23
+ });
24
+ it('should allow default values to be set', async () => {
25
+ const formFactory = createFormFactory();
26
+ const Comp = defineComponent(() => {
27
+ const form = formFactory.useForm({
28
+ defaultValues: {
29
+ firstName: 'FirstName',
30
+ lastName: 'LastName',
31
+ },
32
+ });
33
+ form.provideFormContext();
34
+ return () => (<form.Field name="firstName" defaultValue="">
35
+ {({ field }) => (<p>{field.state.value}</p>)}
36
+ </form.Field>);
37
+ });
38
+ const { findByText, queryByText } = render(Comp);
39
+ expect(await findByText('FirstName')).toBeInTheDocument();
40
+ expect(queryByText('LastName')).not.toBeInTheDocument();
41
+ });
42
+ it('should handle submitting properly', async () => {
43
+ const Comp = defineComponent(() => {
44
+ const submittedData = ref();
45
+ const form = useForm({
46
+ defaultValues: {
47
+ firstName: 'FirstName',
48
+ },
49
+ onSubmit: (data) => {
50
+ submittedData.value = data;
51
+ },
52
+ });
53
+ form.provideFormContext();
54
+ return () => (<form.Provider>
55
+ <form.Field name="firstName">
56
+ {({ field, }) => {
57
+ return (<input value={field.state.value} onBlur={field.handleBlur} onChange={(e) => field.handleChange(e.target.value)} placeholder={'First name'}/>);
58
+ }}
59
+ </form.Field>
60
+ <button onClick={form.handleSubmit}>Submit</button>
61
+ {submittedData.value && (<p>Submitted data: {submittedData.value.firstName}</p>)}
62
+ </form.Provider>);
63
+ });
64
+ const { findByPlaceholderText, getByText } = render(Comp);
65
+ const input = await findByPlaceholderText('First name');
66
+ await user.clear(input);
67
+ await user.type(input, 'OtherName');
68
+ await user.click(getByText('Submit'));
69
+ await waitFor(() => expect(getByText('Submitted data: OtherName')).toBeInTheDocument());
70
+ });
71
+ });
@@ -0,0 +1 @@
1
+ export declare function sleep(timeout: number): Promise<void>;
@@ -0,0 +1,5 @@
1
+ export function sleep(timeout) {
2
+ return new Promise((resolve, _reject) => {
3
+ setTimeout(resolve, timeout);
4
+ });
5
+ }
@@ -0,0 +1,4 @@
1
+ import type { FieldOptions, DeepKeys } from '@tanstack/form-core';
2
+ export type UseFieldOptions<TData, TFormData, TName = unknown extends TFormData ? string : DeepKeys<TFormData>> = FieldOptions<TData, TFormData, TName> & {
3
+ mode?: 'value' | 'array';
4
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ import { FieldApi, type FieldApiOptions, type FormApi } from '@tanstack/form-core';
2
+ import type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core';
3
+ import type { SlotsType, SetupContext, Ref } from 'vue-demi';
4
+ import type { UseFieldOptions } from './types';
5
+ declare module '@tanstack/form-core' {
6
+ interface FieldApi<_TData, TFormData, Opts, TData> {
7
+ Field: FieldComponent<TFormData, TData>;
8
+ }
9
+ }
10
+ export type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(opts?: {
11
+ name: Narrow<TField>;
12
+ } & UseFieldOptions<DeepValue<TFormData, TField>, TFormData>) => FieldApi<DeepValue<TFormData, TField>, TFormData>;
13
+ export declare function useField<TData, TFormData, TName extends unknown extends TFormData ? string : DeepKeys<TFormData> = unknown extends TFormData ? string : DeepKeys<TFormData>>(opts: UseFieldOptions<TData, TFormData, TName>): {
14
+ api: FieldApi<TData, TFormData, Omit<typeof opts, 'onMount'> & {
15
+ form: FormApi<TFormData>;
16
+ }>;
17
+ state: Readonly<Ref<FieldApi<TData, TFormData, Omit<typeof opts, 'onMount'> & {
18
+ form: FormApi<TFormData>;
19
+ }>['state']>>;
20
+ };
21
+ export type FieldValue<TFormData, TField> = TFormData extends any[] ? unknown extends TField ? TFormData[number] : DeepValue<TFormData[number], TField> : DeepValue<TFormData, TField>;
22
+ type FieldComponentProps<TParentData, TFormData, TField, TName extends unknown extends TFormData ? string : DeepKeys<TFormData>> = (TParentData extends any[] ? {
23
+ name?: TName;
24
+ index: number;
25
+ } : {
26
+ name: TName;
27
+ index?: never;
28
+ }) & Omit<UseFieldOptions<TField, TFormData, TName>, 'name' | 'index'>;
29
+ export type FieldComponent<TParentData, TFormData> = <TField, TName extends unknown extends TFormData ? string : DeepKeys<TFormData>>(fieldOptions: FieldComponentProps<TParentData, TFormData, TField, TName>, context: SetupContext<{}, SlotsType<{
30
+ default: {
31
+ field: FieldApi<TField, TFormData, FieldApiOptions<TField, TFormData, TName>>;
32
+ state: FieldApi<TField, TFormData, FieldApiOptions<TField, TFormData, TName>>['state'];
33
+ };
34
+ }>>) => any;
35
+ export declare const Field: <TData, TFormData>(props: import("@tanstack/form-core").FieldOptions<TData, TFormData, unknown extends TFormData ? string : DeepKeys<TFormData>, unknown extends TData ? DeepValue<TFormData, unknown extends TFormData ? string : DeepKeys<TFormData>> : TData> & {
36
+ mode?: "value" | "array" | undefined;
37
+ } & ({
38
+ [x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
39
+ } | {
40
+ [x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
41
+ })) => any;
42
+ export {};
@@ -0,0 +1,41 @@
1
+ import { FieldApi, } from '@tanstack/form-core';
2
+ import { useStore } from '@tanstack/vue-store';
3
+ import { defineComponent, onMounted, onUnmounted, watch } from 'vue-demi';
4
+ import { provideFormContext, useFormContext } from './formContext';
5
+ export function useField(opts) {
6
+ // Get the form API either manually or from context
7
+ const { formApi, parentFieldName } = useFormContext();
8
+ const fieldApi = (() => {
9
+ const api = new FieldApi({
10
+ ...opts,
11
+ form: formApi,
12
+ name: opts.name,
13
+ });
14
+ api.Field = Field;
15
+ return api;
16
+ })();
17
+ const fieldState = useStore(fieldApi.store, (state) => state);
18
+ let cleanup;
19
+ onMounted(() => {
20
+ cleanup = fieldApi.mount();
21
+ });
22
+ onUnmounted(() => {
23
+ cleanup();
24
+ });
25
+ watch(() => opts, () => {
26
+ // Keep options up to date as they are rendered
27
+ fieldApi.update({ ...opts, form: formApi });
28
+ });
29
+ return { api: fieldApi, state: fieldState };
30
+ }
31
+ export const Field = defineComponent((fieldOptions, context) => {
32
+ const fieldApi = useField({ ...fieldOptions, ...context.attrs });
33
+ provideFormContext({
34
+ formApi: fieldApi.api.form,
35
+ parentFieldName: fieldApi.api.name,
36
+ });
37
+ return () => context.slots.default({
38
+ field: fieldApi.api,
39
+ state: fieldApi.state.value,
40
+ });
41
+ }, { name: 'Field', inheritAttrs: false });
@@ -0,0 +1,19 @@
1
+ import { FormApi, type FormState, type FormOptions } from '@tanstack/form-core';
2
+ import { type NoInfer } from '@tanstack/vue-store';
3
+ import { type UseField, type FieldComponent } from './useField';
4
+ import { type EmitsOptions, type SlotsType, type SetupContext } from 'vue-demi';
5
+ declare module '@tanstack/form-core' {
6
+ interface FormApi<TFormData> {
7
+ Provider: (props: Record<string, any> & {}) => any;
8
+ provideFormContext: () => void;
9
+ Field: FieldComponent<TFormData, TFormData>;
10
+ useField: UseField<TFormData>;
11
+ useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => TSelected;
12
+ Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
13
+ selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
14
+ }, context: SetupContext<EmitsOptions, SlotsType<{
15
+ default: NoInfer<FormState<TFormData>>;
16
+ }>>) => any;
17
+ }
18
+ }
19
+ export declare function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData>;
@@ -0,0 +1,35 @@
1
+ import { FormApi } from '@tanstack/form-core';
2
+ import { useStore } from '@tanstack/vue-store';
3
+ import { Field, useField } from './useField';
4
+ import { provideFormContext } from './formContext';
5
+ import { defineComponent, } from 'vue-demi';
6
+ export function useForm(opts) {
7
+ const formApi = (() => {
8
+ const api = new FormApi(opts);
9
+ api.Provider = defineComponent((_, context) => {
10
+ provideFormContext({ formApi });
11
+ return () => context.slots.default();
12
+ }, { name: 'Provider' });
13
+ api.provideFormContext = () => {
14
+ provideFormContext({ formApi });
15
+ };
16
+ api.Field = Field;
17
+ api.useField = useField;
18
+ api.useStore = (selector) => {
19
+ return useStore(api.store, selector);
20
+ };
21
+ api.Subscribe = defineComponent((props, context) => {
22
+ const allProps = { ...props, ...context.attrs };
23
+ const selector = allProps.selector ?? ((state) => state);
24
+ const data = useStore(api.store, selector);
25
+ return () => context.slots.default(data.value);
26
+ }, {
27
+ name: 'Subscribe',
28
+ inheritAttrs: false,
29
+ });
30
+ return api;
31
+ })();
32
+ // formApi.useStore((state) => state.isSubmitting)
33
+ formApi.update(opts);
34
+ return formApi;
35
+ }
@@ -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,13 @@
1
+ import { FormOptions, FormApi } from '@tanstack/form-core';
2
+ import { UseField, FieldComponent } from './useField.cjs';
3
+ import 'vue-demi';
4
+ import './types.cjs';
5
+
6
+ type FormFactory<TFormData> = {
7
+ useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>;
8
+ useField: UseField<TFormData>;
9
+ Field: FieldComponent<TFormData, TFormData>;
10
+ };
11
+ declare function createFormFactory<TFormData>(defaultOpts?: FormOptions<TFormData>): FormFactory<TFormData>;
12
+
13
+ export { FormFactory, createFormFactory };
@@ -0,0 +1,13 @@
1
+ import { FormOptions, FormApi } from '@tanstack/form-core';
2
+ import { UseField, FieldComponent } from './useField.js';
3
+ import 'vue-demi';
4
+ import './types.js';
5
+
6
+ type FormFactory<TFormData> = {
7
+ useForm: (opts?: FormOptions<TFormData>) => FormApi<TFormData>;
8
+ useField: UseField<TFormData>;
9
+ Field: FieldComponent<TFormData, TFormData>;
10
+ };
11
+ declare function createFormFactory<TFormData>(defaultOpts?: FormOptions<TFormData>): FormFactory<TFormData>;
12
+
13
+ export { FormFactory, createFormFactory };
@@ -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":[]}