@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.
- package/build/legacy/createFormFactory.cjs +42 -0
- package/build/legacy/createFormFactory.cjs.map +1 -0
- package/build/legacy/createFormFactory.d.cts +13 -0
- package/build/legacy/createFormFactory.d.ts +13 -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 +8 -0
- package/build/legacy/index.d.ts +8 -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 +7 -0
- package/build/legacy/types.d.ts +7 -0
- package/build/legacy/types.js +1 -0
- package/build/legacy/types.js.map +1 -0
- package/build/legacy/useField.cjs +77 -0
- package/build/legacy/useField.cjs.map +1 -0
- package/build/legacy/useField.d.cts +44 -0
- package/build/legacy/useField.d.ts +44 -0
- package/build/legacy/useField.js +53 -0
- package/build/legacy/useField.js.map +1 -0
- package/build/legacy/useForm.cjs +70 -0
- package/build/legacy/useForm.cjs.map +1 -0
- package/build/legacy/useForm.d.cts +23 -0
- package/build/legacy/useForm.d.ts +23 -0
- package/build/legacy/useForm.js +47 -0
- package/build/legacy/useForm.js.map +1 -0
- package/build/lib/createFormFactory.d.ts +8 -0
- package/build/lib/createFormFactory.js +12 -0
- package/build/lib/formContext.d.ts +11 -0
- package/build/lib/formContext.js +12 -0
- package/build/lib/index.d.ts +5 -0
- package/build/lib/index.js +5 -0
- package/build/lib/tests/useField.test.d.ts +2 -0
- package/build/lib/tests/useField.test.jsx +109 -0
- package/build/lib/tests/useForm.test.d.ts +2 -0
- package/build/lib/tests/useForm.test.jsx +71 -0
- package/build/lib/tests/utils.d.ts +1 -0
- package/build/lib/tests/utils.js +5 -0
- package/build/lib/types.d.ts +4 -0
- package/build/lib/types.js +1 -0
- package/build/lib/useField.d.ts +42 -0
- package/build/lib/useField.jsx +41 -0
- package/build/lib/useForm.d.ts +19 -0
- package/build/lib/useForm.jsx +35 -0
- package/build/modern/createFormFactory.cjs +42 -0
- package/build/modern/createFormFactory.cjs.map +1 -0
- package/build/modern/createFormFactory.d.cts +13 -0
- package/build/modern/createFormFactory.d.ts +13 -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 +8 -0
- package/build/modern/index.d.ts +8 -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 +7 -0
- package/build/modern/types.d.ts +7 -0
- package/build/modern/types.js +1 -0
- package/build/modern/types.js.map +1 -0
- package/build/modern/useField.cjs +77 -0
- package/build/modern/useField.cjs.map +1 -0
- package/build/modern/useField.d.cts +44 -0
- package/build/modern/useField.d.ts +44 -0
- package/build/modern/useField.js +53 -0
- package/build/modern/useField.js.map +1 -0
- package/build/modern/useForm.cjs +70 -0
- package/build/modern/useForm.cjs.map +1 -0
- package/build/modern/useForm.d.cts +23 -0
- package/build/modern/useForm.d.ts +23 -0
- package/build/modern/useForm.js +47 -0
- package/build/modern/useForm.js.map +1 -0
- package/package.json +2 -2
- package/src/types.ts +8 -2
- package/src/useField.tsx +74 -67
- package/src/useForm.tsx +1 -2
@@ -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":[]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@tanstack/vue-form",
|
3
|
-
"version": "0.3.
|
3
|
+
"version": "0.3.3",
|
4
4
|
"description": "Powerful, type-safe forms for Vue.",
|
5
5
|
"author": "tannerlinsley",
|
6
6
|
"license": "MIT",
|
@@ -45,7 +45,7 @@
|
|
45
45
|
"@tanstack/store": "0.1.3",
|
46
46
|
"@tanstack/vue-store": "0.1.3",
|
47
47
|
"vue-demi": "^0.14.6",
|
48
|
-
"@tanstack/form-core": "0.3.
|
48
|
+
"@tanstack/form-core": "0.3.3"
|
49
49
|
},
|
50
50
|
"devDependencies": {
|
51
51
|
"@vue/composition-api": "1.7.2",
|
package/src/types.ts
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
-
|
1
|
+
import type { FieldOptions, DeepKeys } from '@tanstack/form-core'
|
2
2
|
|
3
|
-
export type
|
3
|
+
export type UseFieldOptions<
|
4
|
+
TData,
|
5
|
+
TFormData,
|
6
|
+
TName = unknown extends TFormData ? string : DeepKeys<TFormData>,
|
7
|
+
> = FieldOptions<TData, TFormData, TName> & {
|
8
|
+
mode?: 'value' | 'array'
|
9
|
+
}
|
package/src/useField.tsx
CHANGED
@@ -1,28 +1,22 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
DeepValue,
|
6
|
-
FieldOptions,
|
7
|
-
Narrow,
|
1
|
+
import {
|
2
|
+
FieldApi,
|
3
|
+
type FieldApiOptions,
|
4
|
+
type FormApi,
|
8
5
|
} from '@tanstack/form-core'
|
6
|
+
import type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'
|
9
7
|
import { useStore } from '@tanstack/vue-store'
|
10
8
|
import { defineComponent, onMounted, onUnmounted, watch } from 'vue-demi'
|
11
9
|
import type { SlotsType, SetupContext, Ref } from 'vue-demi'
|
12
10
|
import { provideFormContext, useFormContext } from './formContext'
|
11
|
+
import type { UseFieldOptions } from './types'
|
13
12
|
|
14
13
|
declare module '@tanstack/form-core' {
|
15
14
|
// eslint-disable-next-line no-shadow
|
16
|
-
interface FieldApi<
|
17
|
-
Field: FieldComponent<
|
15
|
+
interface FieldApi<_TData, TFormData, Opts, TData> {
|
16
|
+
Field: FieldComponent<TFormData, TData>
|
18
17
|
}
|
19
18
|
}
|
20
19
|
|
21
|
-
export interface UseFieldOptions<TData, TFormData>
|
22
|
-
extends FieldOptions<TData, TFormData> {
|
23
|
-
mode?: 'value' | 'array'
|
24
|
-
}
|
25
|
-
|
26
20
|
export type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(
|
27
21
|
opts?: { name: Narrow<TField> } & UseFieldOptions<
|
28
22
|
DeepValue<TFormData, TField>,
|
@@ -30,25 +24,45 @@ export type UseField<TFormData> = <TField extends DeepKeys<TFormData>>(
|
|
30
24
|
>,
|
31
25
|
) => FieldApi<DeepValue<TFormData, TField>, TFormData>
|
32
26
|
|
33
|
-
export function useField<
|
34
|
-
|
27
|
+
export function useField<
|
28
|
+
TData,
|
29
|
+
TFormData,
|
30
|
+
TName extends unknown extends TFormData
|
31
|
+
? string
|
32
|
+
: DeepKeys<TFormData> = unknown extends TFormData
|
33
|
+
? string
|
34
|
+
: DeepKeys<TFormData>,
|
35
|
+
>(
|
36
|
+
opts: UseFieldOptions<TData, TFormData, TName>,
|
35
37
|
): {
|
36
|
-
api: FieldApi<
|
37
|
-
|
38
|
+
api: FieldApi<
|
39
|
+
TData,
|
40
|
+
TFormData,
|
41
|
+
Omit<typeof opts, 'onMount'> & {
|
42
|
+
form: FormApi<TFormData>
|
43
|
+
}
|
44
|
+
>
|
45
|
+
state: Readonly<
|
46
|
+
Ref<
|
47
|
+
FieldApi<
|
48
|
+
TData,
|
49
|
+
TFormData,
|
50
|
+
Omit<typeof opts, 'onMount'> & {
|
51
|
+
form: FormApi<TFormData>
|
52
|
+
}
|
53
|
+
>['state']
|
54
|
+
>
|
55
|
+
>
|
38
56
|
} {
|
39
57
|
// Get the form API either manually or from context
|
40
58
|
const { formApi, parentFieldName } = useFormContext()
|
41
59
|
|
42
60
|
const fieldApi = (() => {
|
43
|
-
const
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
)
|
48
|
-
.filter((d) => d !== undefined)
|
49
|
-
.join('.')
|
50
|
-
|
51
|
-
const api = new FieldApi({ ...opts, form: formApi, name: name as never })
|
61
|
+
const api = new FieldApi({
|
62
|
+
...opts,
|
63
|
+
form: formApi,
|
64
|
+
name: opts.name,
|
65
|
+
} as never)
|
52
66
|
|
53
67
|
api.Field = Field as never
|
54
68
|
|
@@ -77,56 +91,49 @@ export function useField<TData, TFormData>(
|
|
77
91
|
return { api: fieldApi, state: fieldState } as never
|
78
92
|
}
|
79
93
|
|
80
|
-
// export type FieldValue<TFormData, TField> = TFormData extends any[]
|
81
|
-
// ? TField extends `[${infer TIndex extends number | 'i'}].${infer TRest}`
|
82
|
-
// ? DeepValue<TFormData[TIndex extends 'i' ? number : TIndex], TRest>
|
83
|
-
// : TField extends `[${infer TIndex extends number | 'i'}]`
|
84
|
-
// ? TFormData[TIndex extends 'i' ? number : TIndex]
|
85
|
-
// : never
|
86
|
-
// : TField extends `${infer TPrefix}[${infer TIndex extends
|
87
|
-
// | number
|
88
|
-
// | 'i'}].${infer TRest}`
|
89
|
-
// ? DeepValue<
|
90
|
-
// DeepValue<TFormData, TPrefix>[TIndex extends 'i' ? number : TIndex],
|
91
|
-
// TRest
|
92
|
-
// >
|
93
|
-
// : TField extends `${infer TPrefix}[${infer TIndex extends number | 'i'}]`
|
94
|
-
// ? DeepValue<TFormData, TPrefix>[TIndex extends 'i' ? number : TIndex]
|
95
|
-
// : DeepValue<TFormData, TField>
|
96
|
-
|
97
94
|
export type FieldValue<TFormData, TField> = TFormData extends any[]
|
98
95
|
? unknown extends TField
|
99
96
|
? TFormData[number]
|
100
97
|
: DeepValue<TFormData[number], TField>
|
101
98
|
: DeepValue<TFormData, TField>
|
102
99
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
100
|
+
type FieldComponentProps<
|
101
|
+
TParentData,
|
102
|
+
TFormData,
|
103
|
+
TField,
|
104
|
+
TName extends unknown extends TFormData ? string : DeepKeys<TFormData>,
|
105
|
+
> = (TParentData extends any[]
|
106
|
+
? {
|
107
|
+
name?: TName
|
108
|
+
index: number
|
109
|
+
}
|
110
|
+
: {
|
111
|
+
name: TName
|
112
|
+
index?: never
|
113
|
+
}) &
|
114
|
+
Omit<UseFieldOptions<TField, TFormData, TName>, 'name' | 'index'>
|
115
|
+
|
116
|
+
export type FieldComponent<TParentData, TFormData> = <
|
117
|
+
// Type of the field
|
118
|
+
TField,
|
119
|
+
// Name of the field
|
120
|
+
TName extends unknown extends TFormData ? string : DeepKeys<TFormData>,
|
121
|
+
>(
|
122
|
+
fieldOptions: FieldComponentProps<TParentData, TFormData, TField, TName>,
|
124
123
|
context: SetupContext<
|
125
124
|
{},
|
126
125
|
SlotsType<{
|
127
126
|
default: {
|
128
|
-
field: FieldApi<
|
129
|
-
|
127
|
+
field: FieldApi<
|
128
|
+
TField,
|
129
|
+
TFormData,
|
130
|
+
FieldApiOptions<TField, TFormData, TName>
|
131
|
+
>
|
132
|
+
state: FieldApi<
|
133
|
+
TField,
|
134
|
+
TFormData,
|
135
|
+
FieldApiOptions<TField, TFormData, TName>
|
136
|
+
>['state']
|
130
137
|
}
|
131
138
|
}>
|
132
139
|
>,
|
package/src/useForm.tsx
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { FormApi, type FormState, type FormOptions } from '@tanstack/form-core'
|
2
|
-
import { useStore } from '@tanstack/vue-store'
|
2
|
+
import { type NoInfer, useStore } from '@tanstack/vue-store'
|
3
3
|
import { type UseField, type FieldComponent, Field, useField } from './useField'
|
4
4
|
import { provideFormContext } from './formContext'
|
5
5
|
import {
|
@@ -8,7 +8,6 @@ import {
|
|
8
8
|
type SetupContext,
|
9
9
|
defineComponent,
|
10
10
|
} from 'vue-demi'
|
11
|
-
import type { NoInfer } from './types'
|
12
11
|
|
13
12
|
declare module '@tanstack/form-core' {
|
14
13
|
// eslint-disable-next-line no-shadow
|