@tanstack/vue-form 0.13.7 → 0.16.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/dist/cjs/createFormFactory.cjs.map +1 -1
- package/dist/cjs/createFormFactory.d.cts +3 -3
- package/dist/cjs/index.cjs +0 -4
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +0 -1
- package/dist/cjs/types.d.cts +2 -2
- package/dist/cjs/useField.cjs +2 -8
- package/dist/cjs/useField.cjs.map +1 -1
- package/dist/cjs/useField.d.cts +10 -16
- package/dist/cjs/useForm.cjs +13 -13
- package/dist/cjs/useForm.cjs.map +1 -1
- package/dist/cjs/useForm.d.cts +0 -2
- package/dist/esm/createFormFactory.d.ts +3 -3
- package/dist/esm/createFormFactory.js.map +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +1 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.d.ts +2 -2
- package/dist/esm/useField.d.ts +10 -16
- package/dist/esm/useField.js +2 -8
- package/dist/esm/useField.js.map +1 -1
- package/dist/esm/useForm.d.ts +0 -2
- package/dist/esm/useForm.js +14 -14
- package/dist/esm/useForm.js.map +1 -1
- package/package.json +3 -3
- package/src/createFormFactory.ts +4 -4
- package/src/index.ts +0 -1
- package/src/tests/useField.test.tsx +87 -13
- package/src/tests/useForm.test.tsx +2 -28
- package/src/types.ts +8 -2
- package/src/useField.tsx +25 -58
- package/src/useForm.tsx +16 -16
- package/dist/cjs/formContext.cjs +0 -18
- package/dist/cjs/formContext.cjs.map +0 -1
- package/dist/cjs/formContext.d.cts +0 -11
- package/dist/esm/formContext.d.ts +0 -11
- package/dist/esm/formContext.js +0 -18
- package/dist/esm/formContext.js.map +0 -1
- package/src/formContext.ts +0 -29
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"createFormFactory.cjs","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import { Field, useField } from './useField'\nimport { useForm } from './useForm'\nimport type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\nimport type { FieldComponent, UseField } from './useField'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField:
|
1
|
+
{"version":3,"file":"createFormFactory.cjs","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import { Field, useField } from './useField'\nimport { useForm } from './useForm'\nimport type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\nimport type { FieldComponent, UseField } from './useField'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField: typeof useField\n Field: typeof Field\n}\n\nexport function createFormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n): FormFactory<TFormData, TFormValidator> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData, TFormValidator>(formOptions)\n },\n useField: useField,\n Field: Field,\n }\n}\n"],"names":["useForm","useField","Field"],"mappings":";;;;AAgBO,SAAS,kBAId,aACwC;AACjC,SAAA;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAA,GAAI,aAAa,IAAI;AACvD,aAAOA,QAAAA,QAAmC,WAAW;AAAA,IACvD;AAAA,IAAA,UACAC,SAAA;AAAA,IAAA,OACAC,SAAA;AAAA,EAAA;AAEJ;;"}
|
@@ -1,8 +1,8 @@
|
|
1
|
+
import { Field, useField } from './useField.cjs';
|
1
2
|
import type { FormApi, FormOptions, Validator } from '@tanstack/form-core';
|
2
|
-
import type { FieldComponent, UseField } from './useField.cjs';
|
3
3
|
export type FormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = {
|
4
4
|
useForm: (opts?: FormOptions<TFormData, TFormValidator>) => FormApi<TFormData, TFormValidator>;
|
5
|
-
useField:
|
6
|
-
Field:
|
5
|
+
useField: typeof useField;
|
6
|
+
Field: typeof Field;
|
7
7
|
};
|
8
8
|
export declare function createFormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts?: FormOptions<TFormData, TFormValidator>): FormFactory<TFormData, TFormValidator>;
|
package/dist/cjs/index.cjs
CHANGED
@@ -2,13 +2,9 @@
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
3
3
|
const formCore = require("@tanstack/form-core");
|
4
4
|
const createFormFactory = require("./createFormFactory.cjs");
|
5
|
-
const formContext = require("./formContext.cjs");
|
6
5
|
const useField = require("./useField.cjs");
|
7
6
|
const useForm = require("./useForm.cjs");
|
8
7
|
exports.createFormFactory = createFormFactory.createFormFactory;
|
9
|
-
exports.formContext = formContext.formContext;
|
10
|
-
exports.provideFormContext = formContext.provideFormContext;
|
11
|
-
exports.useFormContext = formContext.useFormContext;
|
12
8
|
exports.Field = useField.Field;
|
13
9
|
exports.useField = useField.useField;
|
14
10
|
exports.useForm = useForm.useForm;
|
package/dist/cjs/index.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
CHANGED
package/dist/cjs/types.d.cts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { DeepKeys, DeepValue,
|
2
|
-
export type UseFieldOptions<TParentData, 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>> =
|
1
|
+
import type { DeepKeys, DeepValue, FieldApiOptions, Validator } from '@tanstack/form-core';
|
2
|
+
export type UseFieldOptions<TParentData, 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>> = FieldApiOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> & {
|
3
3
|
mode?: 'value' | 'array';
|
4
4
|
};
|
package/dist/cjs/useField.cjs
CHANGED
@@ -3,13 +3,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const formCore = require("@tanstack/form-core");
|
4
4
|
const vueStore = require("@tanstack/vue-store");
|
5
5
|
const vue = require("vue");
|
6
|
-
const formContext = require("./formContext.cjs");
|
7
6
|
function useField(opts) {
|
8
|
-
const { formApi, parentFieldName } = formContext.useFormContext();
|
9
7
|
const fieldApi = (() => {
|
10
8
|
const api = new formCore.FieldApi({
|
11
9
|
...opts,
|
12
|
-
form:
|
10
|
+
form: opts.form,
|
13
11
|
name: opts.name
|
14
12
|
});
|
15
13
|
api.Field = Field;
|
@@ -26,7 +24,7 @@ function useField(opts) {
|
|
26
24
|
vue.watch(
|
27
25
|
() => opts,
|
28
26
|
() => {
|
29
|
-
fieldApi.update({ ...opts, form:
|
27
|
+
fieldApi.update({ ...opts, form: opts.form });
|
30
28
|
}
|
31
29
|
);
|
32
30
|
return { api: fieldApi, state: fieldState };
|
@@ -34,10 +32,6 @@ function useField(opts) {
|
|
34
32
|
const Field = vue.defineComponent(
|
35
33
|
(fieldOptions, context) => {
|
36
34
|
const fieldApi = useField({ ...fieldOptions, ...context.attrs });
|
37
|
-
formContext.provideFormContext({
|
38
|
-
formApi: fieldApi.api.form,
|
39
|
-
parentFieldName: fieldApi.api.name
|
40
|
-
});
|
41
35
|
return () => context.slots.default({
|
42
36
|
field: fieldApi.api,
|
43
37
|
state: fieldApi.state.value
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useField.cjs","sources":["../../src/useField.tsx"],"sourcesContent":["import { FieldApi } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted, onUnmounted, watch } from 'vue'\nimport
|
1
|
+
{"version":3,"file":"useField.cjs","sources":["../../src/useField.tsx"],"sourcesContent":["import { FieldApi } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted, onUnmounted, watch } from 'vue'\nimport type {\n DeepKeys,\n DeepValue,\n Narrow,\n Validator,\n} from '@tanstack/form-core'\nimport type { Ref, SetupContext, SlotsType } from 'vue'\nimport type { UseFieldOptions } from './types'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FieldApi<\n TParentData,\n 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 = DeepValue<TParentData, TName>,\n > {\n Field: FieldComponent<TParentData, TFormValidator>\n }\n}\n\nexport type UseField<\n TParentData,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n> = <\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n>(\n opts: Omit<\n UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,\n 'form'\n >,\n) => {\n api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>\n state: Readonly<\n Ref<\n FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >['state']\n >\n >\n}\n\nexport function useField<\n TParentData,\n 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 opts: UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >,\n): {\n api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>\n state: Readonly<\n Ref<\n FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >['state']\n >\n >\n} {\n const fieldApi = (() => {\n const api = new FieldApi({\n ...opts,\n form: opts.form,\n name: opts.name,\n } as never)\n\n api.Field = Field as never\n\n return api\n })()\n\n const fieldState = useStore(fieldApi.store, (state) => state)\n\n let cleanup!: () => void\n onMounted(() => {\n cleanup = fieldApi.mount()\n })\n\n onUnmounted(() => {\n cleanup()\n })\n\n watch(\n () => opts,\n () => {\n // Keep options up to date as they are rendered\n fieldApi.update({ ...opts, form: opts.form } as never)\n },\n )\n\n return { api: fieldApi, state: fieldState } as never\n}\n\ntype FieldComponentProps<\n TParentData,\n 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> = UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>\n\nexport type FieldComponent<\n TParentData,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n> = <\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n>(\n fieldOptions: Omit<\n FieldComponentProps<TParentData, TName, TFieldValidator, TFormValidator>,\n 'form'\n >,\n context: SetupContext<\n {},\n SlotsType<{\n default: {\n field: FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >\n state: FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >['state']\n }\n }>\n >,\n) => any\n\nexport const Field = defineComponent(\n <\n TParentData,\n 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 >(\n fieldOptions: UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator\n >,\n context: SetupContext,\n ) => {\n const fieldApi = useField({ ...fieldOptions, ...context.attrs } as any)\n\n return () =>\n context.slots.default!({\n field: fieldApi.api,\n state: fieldApi.state.value,\n })\n },\n { name: 'Field', inheritAttrs: false },\n)\n"],"names":["FieldApi","useStore","onMounted","onUnmounted","watch","defineComponent"],"mappings":";;;;;AA4DO,SAAS,SAWd,MAoBA;AACA,QAAM,YAAY,MAAM;AAChB,UAAA,MAAM,IAAIA,kBAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM,KAAK;AAAA,MACX,MAAM,KAAK;AAAA,IAAA,CACH;AAEV,QAAI,QAAQ;AAEL,WAAA;AAAA,EAAA;AAGT,QAAM,aAAaC,SAAAA,SAAS,SAAS,OAAO,CAAC,UAAU,KAAK;AAExD,MAAA;AACJC,MAAAA,UAAU,MAAM;AACd,cAAU,SAAS;EAAM,CAC1B;AAEDC,MAAAA,YAAY,MAAM;AACR;EAAA,CACT;AAEDC,MAAA;AAAA,IACE,MAAM;AAAA,IACN,MAAM;AAEJ,eAAS,OAAO,EAAE,GAAG,MAAM,MAAM,KAAK,MAAe;AAAA,IACvD;AAAA,EAAA;AAGF,SAAO,EAAE,KAAK,UAAU,OAAO,WAAW;AAC5C;AAoDO,MAAM,QAAQC,IAAA;AAAA,EACnB,CAUE,cAMA,YACG;AACG,UAAA,WAAW,SAAS,EAAE,GAAG,cAAc,GAAG,QAAQ,OAAc;AAE/D,WAAA,MACL,QAAQ,MAAM,QAAS;AAAA,MACrB,OAAO,SAAS;AAAA,MAChB,OAAO,SAAS,MAAM;AAAA,IAAA,CACvB;AAAA,EACL;AAAA,EACA,EAAE,MAAM,SAAS,cAAc,MAAM;AACvC;;;"}
|
package/dist/cjs/useField.d.cts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { FieldApi } from '@tanstack/form-core';
|
2
|
-
import type { DeepKeys, DeepValue,
|
2
|
+
import type { DeepKeys, DeepValue, Validator } from '@tanstack/form-core';
|
3
3
|
import type { Ref, SetupContext, SlotsType } from 'vue';
|
4
4
|
import type { UseFieldOptions } from './types.cjs';
|
5
5
|
declare module '@tanstack/form-core' {
|
@@ -7,32 +7,26 @@ declare module '@tanstack/form-core' {
|
|
7
7
|
Field: FieldComponent<TParentData, TFormValidator>;
|
8
8
|
}
|
9
9
|
}
|
10
|
-
export type UseField<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined>(opts
|
11
|
-
|
12
|
-
|
10
|
+
export type UseField<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>(opts: Omit<UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>, 'form'>) => {
|
11
|
+
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
12
|
+
state: Readonly<Ref<FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state']>>;
|
13
|
+
};
|
13
14
|
export declare function useField<TParentData, 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>>(opts: UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>): {
|
14
15
|
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
15
16
|
state: Readonly<Ref<FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state']>>;
|
16
17
|
};
|
17
|
-
|
18
|
-
type
|
19
|
-
name?: TName;
|
20
|
-
index: number;
|
21
|
-
} : {
|
22
|
-
name: TName;
|
23
|
-
index?: never;
|
24
|
-
}) & Omit<UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>, 'name' | 'index'>;
|
25
|
-
export type FieldComponent<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>(fieldOptions: FieldComponentProps<TParentData, TName, TFieldValidator, TFormValidator>, context: SetupContext<{}, SlotsType<{
|
18
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>;
|
19
|
+
export type FieldComponent<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>(fieldOptions: Omit<FieldComponentProps<TParentData, TName, TFieldValidator, TFormValidator>, 'form'>, context: SetupContext<{}, SlotsType<{
|
26
20
|
default: {
|
27
21
|
field: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
28
22
|
state: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state'];
|
29
23
|
};
|
30
24
|
}>>) => any;
|
31
|
-
export declare const Field: <TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined>(props: import("@tanstack/form-core").
|
25
|
+
export declare const Field: <TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined>(props: import("@tanstack/form-core").FieldApiOptions<TParentData, TName, TFieldValidator, TFormValidator, DeepValue<TParentData, TName>> & {
|
32
26
|
mode?: "value" | "array" | undefined;
|
33
27
|
} & ({
|
34
|
-
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
35
|
-
} | {
|
36
28
|
[x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
|
29
|
+
} | {
|
30
|
+
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
37
31
|
})) => any;
|
38
32
|
export {};
|
package/dist/cjs/useForm.cjs
CHANGED
@@ -4,24 +4,23 @@ const formCore = require("@tanstack/form-core");
|
|
4
4
|
const vueStore = require("@tanstack/vue-store");
|
5
5
|
const vue = require("vue");
|
6
6
|
const useField = require("./useField.cjs");
|
7
|
-
const formContext = require("./formContext.cjs");
|
8
7
|
function useForm(opts) {
|
9
8
|
const formApi = (() => {
|
10
9
|
const api = new formCore.FormApi(opts);
|
11
|
-
api.
|
12
|
-
(
|
13
|
-
vue.
|
14
|
-
|
15
|
-
|
10
|
+
api.Field = vue.defineComponent(
|
11
|
+
(props, context) => {
|
12
|
+
return () => vue.h(
|
13
|
+
useField.Field,
|
14
|
+
{ ...props, ...context.attrs, form: api },
|
15
|
+
context.slots
|
16
|
+
);
|
16
17
|
},
|
17
|
-
{
|
18
|
+
{
|
19
|
+
name: "APIField",
|
20
|
+
inheritAttrs: false
|
21
|
+
}
|
18
22
|
);
|
19
|
-
api.
|
20
|
-
vue.onMounted(api.mount);
|
21
|
-
formContext.provideFormContext({ formApi });
|
22
|
-
};
|
23
|
-
api.Field = useField.Field;
|
24
|
-
api.useField = useField.useField;
|
23
|
+
api.useField = (props) => useField.useField({ ...props, form: api });
|
25
24
|
api.useStore = (selector) => {
|
26
25
|
return vueStore.useStore(api.store, selector);
|
27
26
|
};
|
@@ -39,6 +38,7 @@ function useForm(opts) {
|
|
39
38
|
);
|
40
39
|
return api;
|
41
40
|
})();
|
41
|
+
vue.onMounted(formApi.mount);
|
42
42
|
formApi.update(opts);
|
43
43
|
return formApi;
|
44
44
|
}
|
package/dist/cjs/useForm.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useForm.cjs","sources":["../../src/useForm.tsx"],"sourcesContent":["import { FormApi } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted } from 'vue'\nimport { Field, useField } from './useField'\nimport
|
1
|
+
{"version":3,"file":"useForm.cjs","sources":["../../src/useForm.tsx"],"sourcesContent":["import { FormApi } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, h, onMounted } from 'vue'\nimport { Field, useField } from './useField'\nimport type { FormOptions, FormState, Validator } from '@tanstack/form-core'\nimport type { NoInfer } from '@tanstack/vue-store'\nimport type { EmitsOptions, Ref, SetupContext, SlotsType } from 'vue'\nimport type { FieldComponent, UseField } from './useField'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData, TFormValidator> {\n Field: FieldComponent<TFormData, TFormValidator>\n useField: UseField<TFormData, TFormValidator>\n useStore: <TSelected = NoInfer<FormState<TFormData>>>(\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n ) => Readonly<Ref<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<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n opts?: FormOptions<TFormData, TFormValidator>,\n): FormApi<TFormData, TFormValidator> {\n const formApi = (() => {\n const api = new FormApi<TFormData, TFormValidator>(opts)\n\n api.Field = defineComponent(\n (props, context) => {\n return () =>\n h(\n Field as never,\n { ...props, ...context.attrs, form: api },\n context.slots,\n )\n },\n {\n name: 'APIField',\n inheritAttrs: false,\n },\n )\n api.useField = (props) => useField({ ...props, form: api })\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 onMounted(formApi.mount)\n\n // formApi.useStore((state) => state.isSubmitting)\n formApi.update(opts)\n\n return formApi as never\n}\n"],"names":["FormApi","defineComponent","h","Field","useField","useStore","onMounted"],"mappings":";;;;;;AA6BO,SAAS,QAId,MACoC;AACpC,QAAM,WAAW,MAAM;AACf,UAAA,MAAM,IAAIA,iBAAmC,IAAI;AAEvD,QAAI,QAAQC,IAAA;AAAA,MACV,CAAC,OAAO,YAAY;AAClB,eAAO,MACLC,IAAA;AAAA,UACEC,SAAA;AAAA,UACA,EAAE,GAAG,OAAO,GAAG,QAAQ,OAAO,MAAM,IAAI;AAAA,UACxC,QAAQ;AAAA,QAAA;AAAA,MAEd;AAAA,MACA;AAAA,QACE,MAAM;AAAA,QACN,cAAc;AAAA,MAChB;AAAA,IAAA;AAEE,QAAA,WAAW,CAAC,UAAUC,SAAA,SAAS,EAAE,GAAG,OAAO,MAAM,IAAA,CAAK;AACtD,QAAA,WAAW,CAAC,aAAa;AACpB,aAAAC,kBAAS,IAAI,OAAgB,QAAiB;AAAA,IAAA;AAEvD,QAAI,YAAYJ,IAAA;AAAA,MACd,CAAC,OAAO,YAAY;AAClB,cAAM,WAAW,EAAE,GAAG,OAAO,GAAG,QAAQ,MAAM;AAC9C,cAAM,WAAW,SAAS,aAAa,CAAC,UAAU;AAClD,cAAM,OAAOI,SAAA,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,IAAA;AAGK,WAAA;AAAA,EAAA;AAGTC,gBAAU,QAAQ,KAAK;AAGvB,UAAQ,OAAO,IAAI;AAEZ,SAAA;AACT;;"}
|
package/dist/cjs/useForm.d.cts
CHANGED
@@ -5,8 +5,6 @@ import type { EmitsOptions, Ref, SetupContext, SlotsType } from 'vue';
|
|
5
5
|
import type { FieldComponent, UseField } from './useField.cjs';
|
6
6
|
declare module '@tanstack/form-core' {
|
7
7
|
interface FormApi<TFormData, TFormValidator> {
|
8
|
-
Provider: (props: Record<string, any> & {}) => any;
|
9
|
-
provideFormContext: () => void;
|
10
8
|
Field: FieldComponent<TFormData, TFormValidator>;
|
11
9
|
useField: UseField<TFormData, TFormValidator>;
|
12
10
|
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => Readonly<Ref<TSelected>>;
|
@@ -1,8 +1,8 @@
|
|
1
|
+
import { Field, useField } from './useField.js';
|
1
2
|
import type { FormApi, FormOptions, Validator } from '@tanstack/form-core';
|
2
|
-
import type { FieldComponent, UseField } from './useField.js';
|
3
3
|
export type FormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = {
|
4
4
|
useForm: (opts?: FormOptions<TFormData, TFormValidator>) => FormApi<TFormData, TFormValidator>;
|
5
|
-
useField:
|
6
|
-
Field:
|
5
|
+
useField: typeof useField;
|
6
|
+
Field: typeof Field;
|
7
7
|
};
|
8
8
|
export declare function createFormFactory<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(defaultOpts?: FormOptions<TFormData, TFormValidator>): FormFactory<TFormData, TFormValidator>;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"createFormFactory.js","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import { Field, useField } from './useField'\nimport { useForm } from './useForm'\nimport type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\nimport type { FieldComponent, UseField } from './useField'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField:
|
1
|
+
{"version":3,"file":"createFormFactory.js","sources":["../../src/createFormFactory.ts"],"sourcesContent":["import { Field, useField } from './useField'\nimport { useForm } from './useForm'\nimport type { FormApi, FormOptions, Validator } from '@tanstack/form-core'\nimport type { FieldComponent, UseField } from './useField'\n\nexport type FormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n useForm: (\n opts?: FormOptions<TFormData, TFormValidator>,\n ) => FormApi<TFormData, TFormValidator>\n useField: typeof useField\n Field: typeof Field\n}\n\nexport function createFormFactory<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n defaultOpts?: FormOptions<TFormData, TFormValidator>,\n): FormFactory<TFormData, TFormValidator> {\n return {\n useForm: (opts) => {\n const formOptions = Object.assign({}, defaultOpts, opts)\n return useForm<TFormData, TFormValidator>(formOptions)\n },\n useField: useField,\n Field: Field,\n }\n}\n"],"names":[],"mappings":";;AAgBO,SAAS,kBAId,aACwC;AACjC,SAAA;AAAA,IACL,SAAS,CAAC,SAAS;AACjB,YAAM,cAAc,OAAO,OAAO,CAAA,GAAI,aAAa,IAAI;AACvD,aAAO,QAAmC,WAAW;AAAA,IACvD;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
@@ -1,15 +1,11 @@
|
|
1
1
|
export * from "@tanstack/form-core";
|
2
2
|
import { createFormFactory } from "./createFormFactory.js";
|
3
|
-
import { formContext, provideFormContext, useFormContext } from "./formContext.js";
|
4
3
|
import { Field, useField } from "./useField.js";
|
5
4
|
import { useForm } from "./useForm.js";
|
6
5
|
export {
|
7
6
|
Field,
|
8
7
|
createFormFactory,
|
9
|
-
formContext,
|
10
|
-
provideFormContext,
|
11
8
|
useField,
|
12
|
-
useForm
|
13
|
-
useFormContext
|
9
|
+
useForm
|
14
10
|
};
|
15
11
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
|
package/dist/esm/types.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { DeepKeys, DeepValue,
|
2
|
-
export type UseFieldOptions<TParentData, 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>> =
|
1
|
+
import type { DeepKeys, DeepValue, FieldApiOptions, Validator } from '@tanstack/form-core';
|
2
|
+
export type UseFieldOptions<TParentData, 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>> = FieldApiOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> & {
|
3
3
|
mode?: 'value' | 'array';
|
4
4
|
};
|
package/dist/esm/useField.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { FieldApi } from '@tanstack/form-core';
|
2
|
-
import type { DeepKeys, DeepValue,
|
2
|
+
import type { DeepKeys, DeepValue, Validator } from '@tanstack/form-core';
|
3
3
|
import type { Ref, SetupContext, SlotsType } from 'vue';
|
4
4
|
import type { UseFieldOptions } from './types.js';
|
5
5
|
declare module '@tanstack/form-core' {
|
@@ -7,32 +7,26 @@ declare module '@tanstack/form-core' {
|
|
7
7
|
Field: FieldComponent<TParentData, TFormValidator>;
|
8
8
|
}
|
9
9
|
}
|
10
|
-
export type UseField<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined>(opts
|
11
|
-
|
12
|
-
|
10
|
+
export type UseField<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>(opts: Omit<UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>, 'form'>) => {
|
11
|
+
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
12
|
+
state: Readonly<Ref<FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state']>>;
|
13
|
+
};
|
13
14
|
export declare function useField<TParentData, 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>>(opts: UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData>): {
|
14
15
|
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
15
16
|
state: Readonly<Ref<FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state']>>;
|
16
17
|
};
|
17
|
-
|
18
|
-
type
|
19
|
-
name?: TName;
|
20
|
-
index: number;
|
21
|
-
} : {
|
22
|
-
name: TName;
|
23
|
-
index?: never;
|
24
|
-
}) & Omit<UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>, 'name' | 'index'>;
|
25
|
-
export type FieldComponent<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>(fieldOptions: FieldComponentProps<TParentData, TName, TFieldValidator, TFormValidator>, context: SetupContext<{}, SlotsType<{
|
18
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>;
|
19
|
+
export type FieldComponent<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>>(fieldOptions: Omit<FieldComponentProps<TParentData, TName, TFieldValidator, TFormValidator>, 'form'>, context: SetupContext<{}, SlotsType<{
|
26
20
|
default: {
|
27
21
|
field: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
28
22
|
state: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state'];
|
29
23
|
};
|
30
24
|
}>>) => any;
|
31
|
-
export declare const Field: <TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined>(props: import("@tanstack/form-core").
|
25
|
+
export declare const Field: <TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined>(props: import("@tanstack/form-core").FieldApiOptions<TParentData, TName, TFieldValidator, TFormValidator, DeepValue<TParentData, TName>> & {
|
32
26
|
mode?: "value" | "array" | undefined;
|
33
27
|
} & ({
|
34
|
-
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
35
|
-
} | {
|
36
28
|
[x: `on${Capitalize<string>}`]: ((...args: any[]) => any) | undefined;
|
29
|
+
} | {
|
30
|
+
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
37
31
|
})) => any;
|
38
32
|
export {};
|
package/dist/esm/useField.js
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
import { FieldApi } from "@tanstack/form-core";
|
2
2
|
import { useStore } from "@tanstack/vue-store";
|
3
3
|
import { onMounted, onUnmounted, watch, defineComponent } from "vue";
|
4
|
-
import { useFormContext, provideFormContext } from "./formContext.js";
|
5
4
|
function useField(opts) {
|
6
|
-
const { formApi, parentFieldName } = useFormContext();
|
7
5
|
const fieldApi = (() => {
|
8
6
|
const api = new FieldApi({
|
9
7
|
...opts,
|
10
|
-
form:
|
8
|
+
form: opts.form,
|
11
9
|
name: opts.name
|
12
10
|
});
|
13
11
|
api.Field = Field;
|
@@ -24,7 +22,7 @@ function useField(opts) {
|
|
24
22
|
watch(
|
25
23
|
() => opts,
|
26
24
|
() => {
|
27
|
-
fieldApi.update({ ...opts, form:
|
25
|
+
fieldApi.update({ ...opts, form: opts.form });
|
28
26
|
}
|
29
27
|
);
|
30
28
|
return { api: fieldApi, state: fieldState };
|
@@ -32,10 +30,6 @@ function useField(opts) {
|
|
32
30
|
const Field = defineComponent(
|
33
31
|
(fieldOptions, context) => {
|
34
32
|
const fieldApi = useField({ ...fieldOptions, ...context.attrs });
|
35
|
-
provideFormContext({
|
36
|
-
formApi: fieldApi.api.form,
|
37
|
-
parentFieldName: fieldApi.api.name
|
38
|
-
});
|
39
33
|
return () => context.slots.default({
|
40
34
|
field: fieldApi.api,
|
41
35
|
state: fieldApi.state.value
|
package/dist/esm/useField.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useField.js","sources":["../../src/useField.tsx"],"sourcesContent":["import { FieldApi } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted, onUnmounted, watch } from 'vue'\nimport
|
1
|
+
{"version":3,"file":"useField.js","sources":["../../src/useField.tsx"],"sourcesContent":["import { FieldApi } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted, onUnmounted, watch } from 'vue'\nimport type {\n DeepKeys,\n DeepValue,\n Narrow,\n Validator,\n} from '@tanstack/form-core'\nimport type { Ref, SetupContext, SlotsType } from 'vue'\nimport type { UseFieldOptions } from './types'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FieldApi<\n TParentData,\n 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 = DeepValue<TParentData, TName>,\n > {\n Field: FieldComponent<TParentData, TFormValidator>\n }\n}\n\nexport type UseField<\n TParentData,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n> = <\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n>(\n opts: Omit<\n UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,\n 'form'\n >,\n) => {\n api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>\n state: Readonly<\n Ref<\n FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >['state']\n >\n >\n}\n\nexport function useField<\n TParentData,\n 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 opts: UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >,\n): {\n api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>\n state: Readonly<\n Ref<\n FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >['state']\n >\n >\n} {\n const fieldApi = (() => {\n const api = new FieldApi({\n ...opts,\n form: opts.form,\n name: opts.name,\n } as never)\n\n api.Field = Field as never\n\n return api\n })()\n\n const fieldState = useStore(fieldApi.store, (state) => state)\n\n let cleanup!: () => void\n onMounted(() => {\n cleanup = fieldApi.mount()\n })\n\n onUnmounted(() => {\n cleanup()\n })\n\n watch(\n () => opts,\n () => {\n // Keep options up to date as they are rendered\n fieldApi.update({ ...opts, form: opts.form } as never)\n },\n )\n\n return { api: fieldApi, state: fieldState } as never\n}\n\ntype FieldComponentProps<\n TParentData,\n 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> = UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>\n\nexport type FieldComponent<\n TParentData,\n TFormValidator extends\n | Validator<TParentData, unknown>\n | undefined = undefined,\n> = <\n TName extends DeepKeys<TParentData>,\n TFieldValidator extends\n | Validator<DeepValue<TParentData, TName>, unknown>\n | undefined = undefined,\n TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,\n>(\n fieldOptions: Omit<\n FieldComponentProps<TParentData, TName, TFieldValidator, TFormValidator>,\n 'form'\n >,\n context: SetupContext<\n {},\n SlotsType<{\n default: {\n field: FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >\n state: FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >['state']\n }\n }>\n >,\n) => any\n\nexport const Field = defineComponent(\n <\n TParentData,\n 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 >(\n fieldOptions: UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator\n >,\n context: SetupContext,\n ) => {\n const fieldApi = useField({ ...fieldOptions, ...context.attrs } as any)\n\n return () =>\n context.slots.default!({\n field: fieldApi.api,\n state: fieldApi.state.value,\n })\n },\n { name: 'Field', inheritAttrs: false },\n)\n"],"names":[],"mappings":";;;AA4DO,SAAS,SAWd,MAoBA;AACA,QAAM,YAAY,MAAM;AAChB,UAAA,MAAM,IAAI,SAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM,KAAK;AAAA,MACX,MAAM,KAAK;AAAA,IAAA,CACH;AAEV,QAAI,QAAQ;AAEL,WAAA;AAAA,EAAA;AAGT,QAAM,aAAa,SAAS,SAAS,OAAO,CAAC,UAAU,KAAK;AAExD,MAAA;AACJ,YAAU,MAAM;AACd,cAAU,SAAS;EAAM,CAC1B;AAED,cAAY,MAAM;AACR;EAAA,CACT;AAED;AAAA,IACE,MAAM;AAAA,IACN,MAAM;AAEJ,eAAS,OAAO,EAAE,GAAG,MAAM,MAAM,KAAK,MAAe;AAAA,IACvD;AAAA,EAAA;AAGF,SAAO,EAAE,KAAK,UAAU,OAAO,WAAW;AAC5C;AAoDO,MAAM,QAAQ;AAAA,EACnB,CAUE,cAMA,YACG;AACG,UAAA,WAAW,SAAS,EAAE,GAAG,cAAc,GAAG,QAAQ,OAAc;AAE/D,WAAA,MACL,QAAQ,MAAM,QAAS;AAAA,MACrB,OAAO,SAAS;AAAA,MAChB,OAAO,SAAS,MAAM;AAAA,IAAA,CACvB;AAAA,EACL;AAAA,EACA,EAAE,MAAM,SAAS,cAAc,MAAM;AACvC;"}
|
package/dist/esm/useForm.d.ts
CHANGED
@@ -5,8 +5,6 @@ import type { EmitsOptions, Ref, SetupContext, SlotsType } from 'vue';
|
|
5
5
|
import type { FieldComponent, UseField } from './useField.js';
|
6
6
|
declare module '@tanstack/form-core' {
|
7
7
|
interface FormApi<TFormData, TFormValidator> {
|
8
|
-
Provider: (props: Record<string, any> & {}) => any;
|
9
|
-
provideFormContext: () => void;
|
10
8
|
Field: FieldComponent<TFormData, TFormValidator>;
|
11
9
|
useField: UseField<TFormData, TFormValidator>;
|
12
10
|
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => Readonly<Ref<TSelected>>;
|
package/dist/esm/useForm.js
CHANGED
@@ -1,25 +1,24 @@
|
|
1
1
|
import { FormApi } from "@tanstack/form-core";
|
2
2
|
import { useStore } from "@tanstack/vue-store";
|
3
|
-
import { defineComponent, onMounted } from "vue";
|
3
|
+
import { defineComponent, h, onMounted } from "vue";
|
4
4
|
import { Field, useField } from "./useField.js";
|
5
|
-
import { provideFormContext } from "./formContext.js";
|
6
5
|
function useForm(opts) {
|
7
6
|
const formApi = (() => {
|
8
7
|
const api = new FormApi(opts);
|
9
|
-
api.
|
10
|
-
(
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
api.Field = defineComponent(
|
9
|
+
(props, context) => {
|
10
|
+
return () => h(
|
11
|
+
Field,
|
12
|
+
{ ...props, ...context.attrs, form: api },
|
13
|
+
context.slots
|
14
|
+
);
|
14
15
|
},
|
15
|
-
{
|
16
|
+
{
|
17
|
+
name: "APIField",
|
18
|
+
inheritAttrs: false
|
19
|
+
}
|
16
20
|
);
|
17
|
-
api.
|
18
|
-
onMounted(api.mount);
|
19
|
-
provideFormContext({ formApi });
|
20
|
-
};
|
21
|
-
api.Field = Field;
|
22
|
-
api.useField = useField;
|
21
|
+
api.useField = (props) => useField({ ...props, form: api });
|
23
22
|
api.useStore = (selector) => {
|
24
23
|
return useStore(api.store, selector);
|
25
24
|
};
|
@@ -37,6 +36,7 @@ function useForm(opts) {
|
|
37
36
|
);
|
38
37
|
return api;
|
39
38
|
})();
|
39
|
+
onMounted(formApi.mount);
|
40
40
|
formApi.update(opts);
|
41
41
|
return formApi;
|
42
42
|
}
|
package/dist/esm/useForm.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useForm.js","sources":["../../src/useForm.tsx"],"sourcesContent":["import { FormApi } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted } from 'vue'\nimport { Field, useField } from './useField'\nimport
|
1
|
+
{"version":3,"file":"useForm.js","sources":["../../src/useForm.tsx"],"sourcesContent":["import { FormApi } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, h, onMounted } from 'vue'\nimport { Field, useField } from './useField'\nimport type { FormOptions, FormState, Validator } from '@tanstack/form-core'\nimport type { NoInfer } from '@tanstack/vue-store'\nimport type { EmitsOptions, Ref, SetupContext, SlotsType } from 'vue'\nimport type { FieldComponent, UseField } from './useField'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData, TFormValidator> {\n Field: FieldComponent<TFormData, TFormValidator>\n useField: UseField<TFormData, TFormValidator>\n useStore: <TSelected = NoInfer<FormState<TFormData>>>(\n selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,\n ) => Readonly<Ref<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<\n TFormData,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(\n opts?: FormOptions<TFormData, TFormValidator>,\n): FormApi<TFormData, TFormValidator> {\n const formApi = (() => {\n const api = new FormApi<TFormData, TFormValidator>(opts)\n\n api.Field = defineComponent(\n (props, context) => {\n return () =>\n h(\n Field as never,\n { ...props, ...context.attrs, form: api },\n context.slots,\n )\n },\n {\n name: 'APIField',\n inheritAttrs: false,\n },\n )\n api.useField = (props) => useField({ ...props, form: api })\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 onMounted(formApi.mount)\n\n // formApi.useStore((state) => state.isSubmitting)\n formApi.update(opts)\n\n return formApi as never\n}\n"],"names":[],"mappings":";;;;AA6BO,SAAS,QAId,MACoC;AACpC,QAAM,WAAW,MAAM;AACf,UAAA,MAAM,IAAI,QAAmC,IAAI;AAEvD,QAAI,QAAQ;AAAA,MACV,CAAC,OAAO,YAAY;AAClB,eAAO,MACL;AAAA,UACE;AAAA,UACA,EAAE,GAAG,OAAO,GAAG,QAAQ,OAAO,MAAM,IAAI;AAAA,UACxC,QAAQ;AAAA,QAAA;AAAA,MAEd;AAAA,MACA;AAAA,QACE,MAAM;AAAA,QACN,cAAc;AAAA,MAChB;AAAA,IAAA;AAEE,QAAA,WAAW,CAAC,UAAU,SAAS,EAAE,GAAG,OAAO,MAAM,IAAA,CAAK;AACtD,QAAA,WAAW,CAAC,aAAa;AACpB,aAAA,SAAS,IAAI,OAAgB,QAAiB;AAAA,IAAA;AAEvD,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,IAAA;AAGK,WAAA;AAAA,EAAA;AAGT,YAAU,QAAQ,KAAK;AAGvB,UAAQ,OAAO,IAAI;AAEZ,SAAA;AACT;"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@tanstack/vue-form",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.16.0",
|
4
4
|
"description": "Powerful, type-safe forms for Vue.",
|
5
5
|
"author": "tannerlinsley",
|
6
6
|
"license": "MIT",
|
@@ -34,10 +34,10 @@
|
|
34
34
|
],
|
35
35
|
"dependencies": {
|
36
36
|
"@tanstack/vue-store": "^0.3.1",
|
37
|
-
"@tanstack/form-core": "0.
|
37
|
+
"@tanstack/form-core": "0.16.0"
|
38
38
|
},
|
39
39
|
"devDependencies": {
|
40
|
-
"@vitejs/plugin-vue": "^5.0.
|
40
|
+
"@vitejs/plugin-vue": "^5.0.4",
|
41
41
|
"vue": "^3.3.4"
|
42
42
|
},
|
43
43
|
"peerDependencies": {
|
package/src/createFormFactory.ts
CHANGED
@@ -10,8 +10,8 @@ export type FormFactory<
|
|
10
10
|
useForm: (
|
11
11
|
opts?: FormOptions<TFormData, TFormValidator>,
|
12
12
|
) => FormApi<TFormData, TFormValidator>
|
13
|
-
useField:
|
14
|
-
Field:
|
13
|
+
useField: typeof useField
|
14
|
+
Field: typeof Field
|
15
15
|
}
|
16
16
|
|
17
17
|
export function createFormFactory<
|
@@ -25,7 +25,7 @@ export function createFormFactory<
|
|
25
25
|
const formOptions = Object.assign({}, defaultOpts, opts)
|
26
26
|
return useForm<TFormData, TFormValidator>(formOptions)
|
27
27
|
},
|
28
|
-
useField: useField
|
29
|
-
Field: Field
|
28
|
+
useField: useField,
|
29
|
+
Field: Field,
|
30
30
|
}
|
31
31
|
}
|
package/src/index.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
2
2
|
import { defineComponent, h } from 'vue'
|
3
3
|
import { render, waitFor } from '@testing-library/vue'
|
4
|
-
import '@testing-library/jest-dom'
|
4
|
+
import '@testing-library/jest-dom/vitest'
|
5
5
|
import userEvent from '@testing-library/user-event'
|
6
|
-
import { createFormFactory,
|
6
|
+
import { createFormFactory, useForm } from '../index'
|
7
7
|
import { sleep } from './utils'
|
8
8
|
import type { FieldApi } from '../index'
|
9
9
|
|
@@ -21,8 +21,6 @@ describe('useField', () => {
|
|
21
21
|
const Comp = defineComponent(() => {
|
22
22
|
const form = formFactory.useForm()
|
23
23
|
|
24
|
-
provideFormContext({ formApi: form })
|
25
|
-
|
26
24
|
return () => (
|
27
25
|
<form.Field name="firstName" defaultValue="FirstName">
|
28
26
|
{({
|
@@ -60,8 +58,6 @@ describe('useField', () => {
|
|
60
58
|
const Comp = defineComponent(() => {
|
61
59
|
const form = formFactory.useForm()
|
62
60
|
|
63
|
-
provideFormContext({ formApi: form })
|
64
|
-
|
65
61
|
return () => (
|
66
62
|
<form.Field
|
67
63
|
name="firstName"
|
@@ -109,8 +105,6 @@ describe('useField', () => {
|
|
109
105
|
const Comp = defineComponent(() => {
|
110
106
|
const form = formFactory.useForm()
|
111
107
|
|
112
|
-
provideFormContext({ formApi: form })
|
113
|
-
|
114
108
|
return () => (
|
115
109
|
<form.Field
|
116
110
|
name="firstName"
|
@@ -159,8 +153,6 @@ describe('useField', () => {
|
|
159
153
|
const Comp = defineComponent(() => {
|
160
154
|
const form = formFactory.useForm()
|
161
155
|
|
162
|
-
provideFormContext({ formApi: form })
|
163
|
-
|
164
156
|
return () => (
|
165
157
|
<form.Field
|
166
158
|
name="firstName"
|
@@ -215,8 +207,6 @@ describe('useField', () => {
|
|
215
207
|
const Comp = defineComponent(() => {
|
216
208
|
const form = formFactory.useForm()
|
217
209
|
|
218
|
-
provideFormContext({ formApi: form })
|
219
|
-
|
220
210
|
return () => (
|
221
211
|
<form.Field
|
222
212
|
name="firstName"
|
@@ -260,4 +250,88 @@ describe('useField', () => {
|
|
260
250
|
await waitFor(() => getByText(error))
|
261
251
|
expect(getByText(error)).toBeInTheDocument()
|
262
252
|
})
|
253
|
+
|
254
|
+
it('should handle arrays with subvalues', async () => {
|
255
|
+
const fn = vi.fn()
|
256
|
+
|
257
|
+
type CompVal = { people: Array<{ age: number; name: string }> }
|
258
|
+
|
259
|
+
const Comp = defineComponent(() => {
|
260
|
+
const form = useForm({
|
261
|
+
defaultValues: {
|
262
|
+
people: [],
|
263
|
+
} as CompVal,
|
264
|
+
onSubmit: ({ value }) => fn(value),
|
265
|
+
})
|
266
|
+
|
267
|
+
return () => (
|
268
|
+
<div>
|
269
|
+
<form
|
270
|
+
onSubmit={(e) => {
|
271
|
+
e.preventDefault()
|
272
|
+
e.stopPropagation()
|
273
|
+
void form.handleSubmit()
|
274
|
+
}}
|
275
|
+
>
|
276
|
+
<form.Field name="people">
|
277
|
+
{({
|
278
|
+
field,
|
279
|
+
}: {
|
280
|
+
field: FieldApi<CompVal, 'people', never, never>
|
281
|
+
}) => (
|
282
|
+
<div>
|
283
|
+
{field.state.value.map((_, i) => {
|
284
|
+
return (
|
285
|
+
<form.Field key={i} name={`people[${i}].name`}>
|
286
|
+
{({
|
287
|
+
field: subField,
|
288
|
+
}: {
|
289
|
+
field: FieldApi<
|
290
|
+
CompVal,
|
291
|
+
`people[${number}].name`,
|
292
|
+
never,
|
293
|
+
never
|
294
|
+
>
|
295
|
+
}) => (
|
296
|
+
<div>
|
297
|
+
<label>
|
298
|
+
<div>Name for person {i}</div>
|
299
|
+
<input
|
300
|
+
value={subField.state.value}
|
301
|
+
onChange={(e) =>
|
302
|
+
subField.handleChange(
|
303
|
+
(e.target as HTMLInputElement).value,
|
304
|
+
)
|
305
|
+
}
|
306
|
+
/>
|
307
|
+
</label>
|
308
|
+
</div>
|
309
|
+
)}
|
310
|
+
</form.Field>
|
311
|
+
)
|
312
|
+
})}
|
313
|
+
<button
|
314
|
+
onClick={() => field.pushValue({ name: '', age: 0 })}
|
315
|
+
type="button"
|
316
|
+
>
|
317
|
+
Add person
|
318
|
+
</button>
|
319
|
+
</div>
|
320
|
+
)}
|
321
|
+
</form.Field>
|
322
|
+
<button type="submit">Submit</button>
|
323
|
+
</form>
|
324
|
+
</div>
|
325
|
+
)
|
326
|
+
})
|
327
|
+
|
328
|
+
const { queryByText, getByText, findByLabelText, findByText } = render(Comp)
|
329
|
+
expect(queryByText('Name for person 0')).not.toBeInTheDocument()
|
330
|
+
await user.click(getByText('Add person'))
|
331
|
+
const input = await findByLabelText('Name for person 0')
|
332
|
+
expect(input).toBeInTheDocument()
|
333
|
+
await user.type(input, 'John')
|
334
|
+
await user.click(await findByText('Submit'))
|
335
|
+
expect(fn).toHaveBeenCalledWith({ people: [{ name: 'John', age: 0 }] })
|
336
|
+
})
|
263
337
|
})
|
@@ -1,14 +1,8 @@
|
|
1
|
-
|
2
|
-
import '@testing-library/jest-dom'
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
3
2
|
import userEvent from '@testing-library/user-event'
|
4
3
|
import { render, waitFor } from '@testing-library/vue'
|
5
4
|
import { defineComponent, h, ref } from 'vue'
|
6
|
-
import {
|
7
|
-
ValidationError,
|
8
|
-
createFormFactory,
|
9
|
-
provideFormContext,
|
10
|
-
useForm,
|
11
|
-
} from '../index'
|
5
|
+
import { createFormFactory, useForm } from '../index'
|
12
6
|
import { sleep } from './utils'
|
13
7
|
|
14
8
|
import type { FieldApi, ValidationErrorMap } from '../index'
|
@@ -27,8 +21,6 @@ describe('useForm', () => {
|
|
27
21
|
const Comp = defineComponent(() => {
|
28
22
|
const form = formFactory.useForm()
|
29
23
|
|
30
|
-
provideFormContext({ formApi: form })
|
31
|
-
|
32
24
|
return () => (
|
33
25
|
<form.Field name="firstName" defaultValue="">
|
34
26
|
{({
|
@@ -66,7 +58,6 @@ describe('useForm', () => {
|
|
66
58
|
lastName: 'LastName',
|
67
59
|
},
|
68
60
|
})
|
69
|
-
form.provideFormContext()
|
70
61
|
|
71
62
|
return () => (
|
72
63
|
<form.Field name="firstName">
|
@@ -96,7 +87,6 @@ describe('useForm', () => {
|
|
96
87
|
submittedData.value = value
|
97
88
|
},
|
98
89
|
})
|
99
|
-
form.provideFormContext()
|
100
90
|
|
101
91
|
return () => (
|
102
92
|
<div>
|
@@ -153,8 +143,6 @@ describe('useForm', () => {
|
|
153
143
|
},
|
154
144
|
})
|
155
145
|
|
156
|
-
form.provideFormContext()
|
157
|
-
|
158
146
|
return () =>
|
159
147
|
mountForm.value ? (
|
160
148
|
<div>
|
@@ -184,8 +172,6 @@ describe('useForm', () => {
|
|
184
172
|
},
|
185
173
|
})
|
186
174
|
|
187
|
-
form.provideFormContext()
|
188
|
-
|
189
175
|
return () => (
|
190
176
|
<div>
|
191
177
|
<form.Field name="firstName">
|
@@ -234,8 +220,6 @@ describe('useForm', () => {
|
|
234
220
|
|
235
221
|
const errors = form.useStore((s) => s.errors)
|
236
222
|
|
237
|
-
form.provideFormContext()
|
238
|
-
|
239
223
|
return () => (
|
240
224
|
<div>
|
241
225
|
<form.Field name="firstName">
|
@@ -283,8 +267,6 @@ describe('useForm', () => {
|
|
283
267
|
|
284
268
|
const errors = form.useStore((s) => s.errorMap)
|
285
269
|
|
286
|
-
form.provideFormContext()
|
287
|
-
|
288
270
|
return () => (
|
289
271
|
<div>
|
290
272
|
<form.Field name="firstName" defaultMeta={{ isTouched: true }}>
|
@@ -341,8 +323,6 @@ describe('useForm', () => {
|
|
341
323
|
|
342
324
|
const errors = form.useStore((s) => s.errorMap)
|
343
325
|
|
344
|
-
form.provideFormContext()
|
345
|
-
|
346
326
|
return () => (
|
347
327
|
<div>
|
348
328
|
<form.Field name="firstName" defaultMeta={{ isTouched: true }}>
|
@@ -396,8 +376,6 @@ describe('useForm', () => {
|
|
396
376
|
|
397
377
|
const errors = form.useStore((s) => s.errorMap)
|
398
378
|
|
399
|
-
form.provideFormContext()
|
400
|
-
|
401
379
|
return () => (
|
402
380
|
<div>
|
403
381
|
<form.Field name="firstName" defaultMeta={{ isTouched: true }}>
|
@@ -453,8 +431,6 @@ describe('useForm', () => {
|
|
453
431
|
})
|
454
432
|
const errors = form.useStore((s) => s.errorMap)
|
455
433
|
|
456
|
-
form.provideFormContext()
|
457
|
-
|
458
434
|
return () => (
|
459
435
|
<div>
|
460
436
|
<form.Field name="firstName" defaultMeta={{ isTouched: true }}>
|
@@ -513,8 +489,6 @@ describe('useForm', () => {
|
|
513
489
|
})
|
514
490
|
const errors = form.useStore((s) => s.errors)
|
515
491
|
|
516
|
-
form.provideFormContext()
|
517
|
-
|
518
492
|
return () => (
|
519
493
|
<div>
|
520
494
|
<form.Field name="firstName" defaultMeta={{ isTouched: true }}>
|
package/src/types.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import type {
|
2
2
|
DeepKeys,
|
3
3
|
DeepValue,
|
4
|
-
|
4
|
+
FieldApiOptions,
|
5
5
|
Validator,
|
6
6
|
} from '@tanstack/form-core'
|
7
7
|
|
@@ -15,6 +15,12 @@ export type UseFieldOptions<
|
|
15
15
|
| Validator<TParentData, unknown>
|
16
16
|
| undefined = undefined,
|
17
17
|
TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,
|
18
|
-
> =
|
18
|
+
> = FieldApiOptions<
|
19
|
+
TParentData,
|
20
|
+
TName,
|
21
|
+
TFieldValidator,
|
22
|
+
TFormValidator,
|
23
|
+
TData
|
24
|
+
> & {
|
19
25
|
mode?: 'value' | 'array'
|
20
26
|
}
|
package/src/useField.tsx
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import { FieldApi } from '@tanstack/form-core'
|
2
2
|
import { useStore } from '@tanstack/vue-store'
|
3
3
|
import { defineComponent, onMounted, onUnmounted, watch } from 'vue'
|
4
|
-
import { provideFormContext, useFormContext } from './formContext'
|
5
4
|
import type {
|
6
5
|
DeepKeys,
|
7
6
|
DeepValue,
|
@@ -38,21 +37,26 @@ export type UseField<
|
|
38
37
|
TFieldValidator extends
|
39
38
|
| Validator<DeepValue<TParentData, TName>, unknown>
|
40
39
|
| undefined = undefined,
|
40
|
+
TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,
|
41
41
|
>(
|
42
|
-
opts
|
43
|
-
TParentData,
|
44
|
-
|
45
|
-
TFieldValidator,
|
46
|
-
TFormValidator,
|
47
|
-
DeepValue<TParentData, TName>
|
42
|
+
opts: Omit<
|
43
|
+
UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,
|
44
|
+
'form'
|
48
45
|
>,
|
49
|
-
) =>
|
50
|
-
TParentData,
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
46
|
+
) => {
|
47
|
+
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>
|
48
|
+
state: Readonly<
|
49
|
+
Ref<
|
50
|
+
FieldApi<
|
51
|
+
TParentData,
|
52
|
+
TName,
|
53
|
+
TFieldValidator,
|
54
|
+
TFormValidator,
|
55
|
+
TData
|
56
|
+
>['state']
|
57
|
+
>
|
58
|
+
>
|
59
|
+
}
|
56
60
|
|
57
61
|
export function useField<
|
58
62
|
TParentData,
|
@@ -73,16 +77,7 @@ export function useField<
|
|
73
77
|
TData
|
74
78
|
>,
|
75
79
|
): {
|
76
|
-
api: FieldApi<
|
77
|
-
TParentData,
|
78
|
-
TName,
|
79
|
-
TFieldValidator,
|
80
|
-
TFormValidator,
|
81
|
-
TData
|
82
|
-
// Omit<typeof opts, 'onMount'> & {
|
83
|
-
// form: FormApi<TParentData>
|
84
|
-
// }
|
85
|
-
>
|
80
|
+
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>
|
86
81
|
state: Readonly<
|
87
82
|
Ref<
|
88
83
|
FieldApi<
|
@@ -95,13 +90,10 @@ export function useField<
|
|
95
90
|
>
|
96
91
|
>
|
97
92
|
} {
|
98
|
-
// Get the form API either manually or from context
|
99
|
-
const { formApi, parentFieldName } = useFormContext()
|
100
|
-
|
101
93
|
const fieldApi = (() => {
|
102
94
|
const api = new FieldApi({
|
103
95
|
...opts,
|
104
|
-
form:
|
96
|
+
form: opts.form,
|
105
97
|
name: opts.name,
|
106
98
|
} as never)
|
107
99
|
|
@@ -125,19 +117,13 @@ export function useField<
|
|
125
117
|
() => opts,
|
126
118
|
() => {
|
127
119
|
// Keep options up to date as they are rendered
|
128
|
-
fieldApi.update({ ...opts, form:
|
120
|
+
fieldApi.update({ ...opts, form: opts.form } as never)
|
129
121
|
},
|
130
122
|
)
|
131
123
|
|
132
124
|
return { api: fieldApi, state: fieldState } as never
|
133
125
|
}
|
134
126
|
|
135
|
-
export type FieldValue<TParentData, TName> = TParentData extends any[]
|
136
|
-
? unknown extends TName
|
137
|
-
? TParentData[number]
|
138
|
-
: DeepValue<TParentData[number], TName>
|
139
|
-
: DeepValue<TParentData, TName>
|
140
|
-
|
141
127
|
type FieldComponentProps<
|
142
128
|
TParentData,
|
143
129
|
TName extends DeepKeys<TParentData>,
|
@@ -147,19 +133,7 @@ type FieldComponentProps<
|
|
147
133
|
TFormValidator extends
|
148
134
|
| Validator<TParentData, unknown>
|
149
135
|
| undefined = undefined,
|
150
|
-
> =
|
151
|
-
? {
|
152
|
-
name?: TName
|
153
|
-
index: number
|
154
|
-
}
|
155
|
-
: {
|
156
|
-
name: TName
|
157
|
-
index?: never
|
158
|
-
}) &
|
159
|
-
Omit<
|
160
|
-
UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,
|
161
|
-
'name' | 'index'
|
162
|
-
>
|
136
|
+
> = UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>
|
163
137
|
|
164
138
|
export type FieldComponent<
|
165
139
|
TParentData,
|
@@ -173,11 +147,9 @@ export type FieldComponent<
|
|
173
147
|
| undefined = undefined,
|
174
148
|
TData extends DeepValue<TParentData, TName> = DeepValue<TParentData, TName>,
|
175
149
|
>(
|
176
|
-
fieldOptions:
|
177
|
-
TParentData,
|
178
|
-
|
179
|
-
TFieldValidator,
|
180
|
-
TFormValidator
|
150
|
+
fieldOptions: Omit<
|
151
|
+
FieldComponentProps<TParentData, TName, TFieldValidator, TFormValidator>,
|
152
|
+
'form'
|
181
153
|
>,
|
182
154
|
context: SetupContext<
|
183
155
|
{},
|
@@ -223,11 +195,6 @@ export const Field = defineComponent(
|
|
223
195
|
) => {
|
224
196
|
const fieldApi = useField({ ...fieldOptions, ...context.attrs } as any)
|
225
197
|
|
226
|
-
provideFormContext({
|
227
|
-
formApi: fieldApi.api.form,
|
228
|
-
parentFieldName: fieldApi.api.name,
|
229
|
-
} as never)
|
230
|
-
|
231
198
|
return () =>
|
232
199
|
context.slots.default!({
|
233
200
|
field: fieldApi.api,
|
package/src/useForm.tsx
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
import { FormApi } from '@tanstack/form-core'
|
2
2
|
import { useStore } from '@tanstack/vue-store'
|
3
|
-
import { defineComponent, onMounted } from 'vue'
|
3
|
+
import { defineComponent, h, onMounted } from 'vue'
|
4
4
|
import { Field, useField } from './useField'
|
5
|
-
import { provideFormContext } from './formContext'
|
6
5
|
import type { FormOptions, FormState, Validator } from '@tanstack/form-core'
|
7
6
|
import type { NoInfer } from '@tanstack/vue-store'
|
8
7
|
import type { EmitsOptions, Ref, SetupContext, SlotsType } from 'vue'
|
@@ -11,8 +10,6 @@ import type { FieldComponent, UseField } from './useField'
|
|
11
10
|
declare module '@tanstack/form-core' {
|
12
11
|
// eslint-disable-next-line no-shadow
|
13
12
|
interface FormApi<TFormData, TFormValidator> {
|
14
|
-
Provider: (props: Record<string, any> & {}) => any
|
15
|
-
provideFormContext: () => void
|
16
13
|
Field: FieldComponent<TFormData, TFormValidator>
|
17
14
|
useField: UseField<TFormData, TFormValidator>
|
18
15
|
useStore: <TSelected = NoInfer<FormState<TFormData>>>(
|
@@ -39,20 +36,21 @@ export function useForm<
|
|
39
36
|
const formApi = (() => {
|
40
37
|
const api = new FormApi<TFormData, TFormValidator>(opts)
|
41
38
|
|
42
|
-
api.
|
43
|
-
(
|
44
|
-
|
45
|
-
|
46
|
-
|
39
|
+
api.Field = defineComponent(
|
40
|
+
(props, context) => {
|
41
|
+
return () =>
|
42
|
+
h(
|
43
|
+
Field as never,
|
44
|
+
{ ...props, ...context.attrs, form: api },
|
45
|
+
context.slots,
|
46
|
+
)
|
47
|
+
},
|
48
|
+
{
|
49
|
+
name: 'APIField',
|
50
|
+
inheritAttrs: false,
|
47
51
|
},
|
48
|
-
{ name: 'Provider' },
|
49
52
|
)
|
50
|
-
api.
|
51
|
-
onMounted(api.mount)
|
52
|
-
provideFormContext({ formApi: formApi as never })
|
53
|
-
}
|
54
|
-
api.Field = Field as never
|
55
|
-
api.useField = useField as never
|
53
|
+
api.useField = (props) => useField({ ...props, form: api })
|
56
54
|
api.useStore = (selector) => {
|
57
55
|
return useStore(api.store as never, selector as never) as never
|
58
56
|
}
|
@@ -72,6 +70,8 @@ export function useForm<
|
|
72
70
|
return api
|
73
71
|
})()
|
74
72
|
|
73
|
+
onMounted(formApi.mount)
|
74
|
+
|
75
75
|
// formApi.useStore((state) => state.isSubmitting)
|
76
76
|
formApi.update(opts)
|
77
77
|
|
package/dist/cjs/formContext.cjs
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
3
|
-
const vue = require("vue");
|
4
|
-
const formContext = Symbol("FormContext");
|
5
|
-
function provideFormContext(val) {
|
6
|
-
vue.provide(formContext, val);
|
7
|
-
}
|
8
|
-
function useFormContext() {
|
9
|
-
const formApi = vue.inject(formContext);
|
10
|
-
if (!formApi) {
|
11
|
-
throw new Error(`You are trying to use the form API outside of a form!`);
|
12
|
-
}
|
13
|
-
return formApi;
|
14
|
-
}
|
15
|
-
exports.formContext = formContext;
|
16
|
-
exports.provideFormContext = provideFormContext;
|
17
|
-
exports.useFormContext = useFormContext;
|
18
|
-
//# sourceMappingURL=formContext.cjs.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"formContext.cjs","sources":["../../src/formContext.ts"],"sourcesContent":["import { inject, provide } from 'vue'\nimport type { FormApi, Validator } from '@tanstack/form-core'\n\nexport type FormContext<\n TFormData = any,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n formApi: FormApi<TFormData, TFormValidator>\n parentFieldName?: string\n} | null\n\nexport const formContext = Symbol('FormContext')\n\nexport function provideFormContext<\n TFormData = any,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(val: FormContext<TFormData, TFormValidator>) {\n provide(formContext, val)\n}\n\nexport function useFormContext() {\n const formApi = inject(formContext) as FormContext\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n"],"names":["provide","inject"],"mappings":";;;AAWa,MAAA,cAAc,OAAO,aAAa;AAExC,SAAS,mBAGd,KAA6C;AAC7CA,cAAQ,aAAa,GAAG;AAC1B;AAEO,SAAS,iBAAiB;AACzB,QAAA,UAAUC,WAAO,WAAW;AAElC,MAAI,CAAC,SAAS;AACN,UAAA,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEO,SAAA;AACT;;;;"}
|
@@ -1,11 +0,0 @@
|
|
1
|
-
import type { FormApi, Validator } from '@tanstack/form-core';
|
2
|
-
export type FormContext<TFormData = any, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = {
|
3
|
-
formApi: FormApi<TFormData, TFormValidator>;
|
4
|
-
parentFieldName?: string;
|
5
|
-
} | null;
|
6
|
-
export declare const formContext: unique symbol;
|
7
|
-
export declare function provideFormContext<TFormData = any, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(val: FormContext<TFormData, TFormValidator>): void;
|
8
|
-
export declare function useFormContext(): {
|
9
|
-
formApi: FormApi<any, undefined>;
|
10
|
-
parentFieldName?: string | undefined;
|
11
|
-
};
|
@@ -1,11 +0,0 @@
|
|
1
|
-
import type { FormApi, Validator } from '@tanstack/form-core';
|
2
|
-
export type FormContext<TFormData = any, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined> = {
|
3
|
-
formApi: FormApi<TFormData, TFormValidator>;
|
4
|
-
parentFieldName?: string;
|
5
|
-
} | null;
|
6
|
-
export declare const formContext: unique symbol;
|
7
|
-
export declare function provideFormContext<TFormData = any, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(val: FormContext<TFormData, TFormValidator>): void;
|
8
|
-
export declare function useFormContext(): {
|
9
|
-
formApi: FormApi<any, undefined>;
|
10
|
-
parentFieldName?: string | undefined;
|
11
|
-
};
|
package/dist/esm/formContext.js
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
import { provide, inject } from "vue";
|
2
|
-
const formContext = Symbol("FormContext");
|
3
|
-
function provideFormContext(val) {
|
4
|
-
provide(formContext, val);
|
5
|
-
}
|
6
|
-
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
|
-
}
|
13
|
-
export {
|
14
|
-
formContext,
|
15
|
-
provideFormContext,
|
16
|
-
useFormContext
|
17
|
-
};
|
18
|
-
//# sourceMappingURL=formContext.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"formContext.js","sources":["../../src/formContext.ts"],"sourcesContent":["import { inject, provide } from 'vue'\nimport type { FormApi, Validator } from '@tanstack/form-core'\n\nexport type FormContext<\n TFormData = any,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n> = {\n formApi: FormApi<TFormData, TFormValidator>\n parentFieldName?: string\n} | null\n\nexport const formContext = Symbol('FormContext')\n\nexport function provideFormContext<\n TFormData = any,\n TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,\n>(val: FormContext<TFormData, TFormValidator>) {\n provide(formContext, val)\n}\n\nexport function useFormContext() {\n const formApi = inject(formContext) as FormContext\n\n if (!formApi) {\n throw new Error(`You are trying to use the form API outside of a form!`)\n }\n\n return formApi\n}\n"],"names":[],"mappings":";AAWa,MAAA,cAAc,OAAO,aAAa;AAExC,SAAS,mBAGd,KAA6C;AAC7C,UAAQ,aAAa,GAAG;AAC1B;AAEO,SAAS,iBAAiB;AACzB,QAAA,UAAU,OAAO,WAAW;AAElC,MAAI,CAAC,SAAS;AACN,UAAA,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEO,SAAA;AACT;"}
|
package/src/formContext.ts
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
import { inject, provide } from 'vue'
|
2
|
-
import type { FormApi, Validator } from '@tanstack/form-core'
|
3
|
-
|
4
|
-
export type FormContext<
|
5
|
-
TFormData = any,
|
6
|
-
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
7
|
-
> = {
|
8
|
-
formApi: FormApi<TFormData, TFormValidator>
|
9
|
-
parentFieldName?: string
|
10
|
-
} | null
|
11
|
-
|
12
|
-
export const formContext = Symbol('FormContext')
|
13
|
-
|
14
|
-
export function provideFormContext<
|
15
|
-
TFormData = any,
|
16
|
-
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
17
|
-
>(val: FormContext<TFormData, TFormValidator>) {
|
18
|
-
provide(formContext, val)
|
19
|
-
}
|
20
|
-
|
21
|
-
export function useFormContext() {
|
22
|
-
const formApi = inject(formContext) as FormContext
|
23
|
-
|
24
|
-
if (!formApi) {
|
25
|
-
throw new Error(`You are trying to use the form API outside of a form!`)
|
26
|
-
}
|
27
|
-
|
28
|
-
return formApi
|
29
|
-
}
|