@tanstack/vue-form 0.10.2 → 0.11.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/build/legacy/createFormFactory.cjs.map +1 -1
- package/build/legacy/createFormFactory.d.cts +6 -6
- package/build/legacy/createFormFactory.d.ts +6 -6
- package/build/legacy/createFormFactory.js.map +1 -1
- package/build/legacy/formContext.cjs.map +1 -1
- package/build/legacy/formContext.d.cts +5 -5
- package/build/legacy/formContext.d.ts +5 -5
- package/build/legacy/formContext.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +2 -2
- package/build/legacy/types.d.ts +2 -2
- package/build/legacy/useField.cjs.map +1 -1
- package/build/legacy/useField.d.cts +14 -14
- package/build/legacy/useField.d.ts +14 -14
- package/build/legacy/useField.js.map +1 -1
- package/build/legacy/useForm.cjs.map +1 -1
- package/build/legacy/useForm.d.cts +5 -5
- package/build/legacy/useForm.d.ts +5 -5
- package/build/legacy/useForm.js +3 -1
- package/build/legacy/useForm.js.map +1 -1
- package/build/modern/createFormFactory.cjs.map +1 -1
- package/build/modern/createFormFactory.d.cts +6 -6
- package/build/modern/createFormFactory.d.ts +6 -6
- package/build/modern/createFormFactory.js.map +1 -1
- package/build/modern/formContext.cjs.map +1 -1
- package/build/modern/formContext.d.cts +5 -5
- package/build/modern/formContext.d.ts +5 -5
- package/build/modern/formContext.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +2 -2
- package/build/modern/types.d.ts +2 -2
- package/build/modern/useField.cjs.map +1 -1
- package/build/modern/useField.d.cts +14 -14
- package/build/modern/useField.d.ts +14 -14
- package/build/modern/useField.js.map +1 -1
- package/build/modern/useForm.cjs.map +1 -1
- package/build/modern/useForm.d.cts +5 -5
- package/build/modern/useForm.d.ts +5 -5
- package/build/modern/useForm.js +3 -1
- package/build/modern/useForm.js.map +1 -1
- package/package.json +2 -2
- package/src/createFormFactory.ts +16 -10
- package/src/formContext.ts +10 -4
- package/src/tests/useField.test.tsx +23 -15
- package/src/tests/useForm.test.tsx +57 -39
- package/src/types.ts +13 -4
- package/src/useField.tsx +68 -35
- package/src/useForm.tsx +16 -8
package/build/modern/types.d.cts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { DeepKeys, DeepValue, FieldOptions } from '@tanstack/form-core';
|
1
|
+
import { DeepKeys, Validator, DeepValue, FieldOptions } from '@tanstack/form-core';
|
2
2
|
|
3
|
-
type UseFieldOptions<TParentData, TName extends DeepKeys<TParentData>,
|
3
|
+
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>> = FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> & {
|
4
4
|
mode?: 'value' | 'array';
|
5
5
|
};
|
6
6
|
|
package/build/modern/types.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { DeepKeys, DeepValue, FieldOptions } from '@tanstack/form-core';
|
1
|
+
import { DeepKeys, Validator, DeepValue, FieldOptions } from '@tanstack/form-core';
|
2
2
|
|
3
|
-
type UseFieldOptions<TParentData, TName extends DeepKeys<TParentData>,
|
3
|
+
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>> = FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, TData> & {
|
4
4
|
mode?: 'value' | 'array';
|
5
5
|
};
|
6
6
|
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import { FieldApi } from '@tanstack/form-core'\nimport type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted, onUnmounted, watch } from 'vue'\nimport type { SlotsType, SetupContext, Ref } from 'vue'\nimport { provideFormContext, useFormContext } from './formContext'\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
|
1
|
+
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import { FieldApi, type Validator } from '@tanstack/form-core'\nimport type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted, onUnmounted, watch } from 'vue'\nimport type { SlotsType, SetupContext, Ref } from 'vue'\nimport { provideFormContext, useFormContext } from './formContext'\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>(\n opts?: { name: Narrow<TName> } & UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n DeepValue<TParentData, TName>\n >,\n) => FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n DeepValue<TParentData, TName>\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<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n // Omit<typeof opts, 'onMount'> & {\n // form: FormApi<TParentData>\n // }\n >\n state: Readonly<\n Ref<\n FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >['state']\n >\n >\n} {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const fieldApi = (() => {\n const api = new FieldApi({\n ...opts,\n form: formApi,\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: formApi } as never)\n },\n )\n\n return { api: fieldApi, state: fieldState } as never\n}\n\nexport type FieldValue<TParentData, TName> = TParentData extends any[]\n ? unknown extends TName\n ? TParentData[number]\n : DeepValue<TParentData[number], TName>\n : DeepValue<TParentData, TName>\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> = (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<\n UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,\n 'name' | 'index'\n >\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: FieldComponentProps<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator\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 provideFormContext({\n formApi: fieldApi.api.form,\n parentFieldName: fieldApi.api.name,\n } as never)\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAyC;AAEzC,uBAAyB;AACzB,iBAA+D;AAE/D,yBAAmD;AA8C5C,SAAS,SAWd,MA6BA;AAEA,QAAM,EAAE,SAAS,gBAAgB,QAAI,mCAAe;AAEpD,QAAM,YAAY,MAAM;AACtB,UAAM,MAAM,IAAI,0BAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA,MACN,MAAM,KAAK;AAAA,IACb,CAAU;AAEV,QAAI,QAAQ;AAEZ,WAAO;AAAA,EACT,GAAG;AAEH,QAAM,iBAAa,2BAAS,SAAS,OAAO,CAAC,UAAU,KAAK;AAE5D,MAAI;AACJ,4BAAU,MAAM;AACd,cAAU,SAAS,MAAM;AAAA,EAC3B,CAAC;AAED,8BAAY,MAAM;AAChB,YAAQ;AAAA,EACV,CAAC;AAED;AAAA,IACE,MAAM;AAAA,IACN,MAAM;AAEJ,eAAS,OAAO,EAAE,GAAG,MAAM,MAAM,QAAQ,CAAU;AAAA,IACrD;AAAA,EACF;AAEA,SAAO,EAAE,KAAK,UAAU,OAAO,WAAW;AAC5C;AAwEO,IAAM,YAAQ;AAAA,EACnB,CAUE,cAMA,YACG;AACH,UAAM,WAAW,SAAS,EAAE,GAAG,cAAc,GAAG,QAAQ,MAAM,CAAQ;AAEtE,+CAAmB;AAAA,MACjB,SAAS,SAAS,IAAI;AAAA,MACtB,iBAAiB,SAAS,IAAI;AAAA,IAChC,CAAU;AAEV,WAAO,MACL,QAAQ,MAAM,QAAS;AAAA,MACrB,OAAO,SAAS;AAAA,MAChB,OAAO,SAAS,MAAM;AAAA,IACxB,CAAC;AAAA,EACL;AAAA,EACA,EAAE,MAAM,SAAS,cAAc,MAAM;AACvC;","names":[]}
|
@@ -1,35 +1,35 @@
|
|
1
1
|
import * as _tanstack_form_core from '@tanstack/form-core';
|
2
|
-
import { DeepKeys, DeepValue, FieldApi, Narrow } from '@tanstack/form-core';
|
2
|
+
import { DeepKeys, Validator, DeepValue, FieldApi, Narrow } from '@tanstack/form-core';
|
3
3
|
import { SetupContext, SlotsType, Ref } from 'vue';
|
4
4
|
import { UseFieldOptions } from './types.cjs';
|
5
5
|
|
6
6
|
declare module '@tanstack/form-core' {
|
7
|
-
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
8
|
-
Field: FieldComponent<
|
7
|
+
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined, TData = DeepValue<TParentData, TName>> {
|
8
|
+
Field: FieldComponent<TParentData, TFormValidator>;
|
9
9
|
}
|
10
10
|
}
|
11
|
-
type UseField<TParentData,
|
11
|
+
type UseField<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined>(opts?: {
|
12
12
|
name: Narrow<TName>;
|
13
|
-
} & UseFieldOptions<TParentData, TName,
|
14
|
-
declare function useField<TParentData, TName extends DeepKeys<TParentData>,
|
15
|
-
api: FieldApi<TParentData, TName,
|
16
|
-
state: Readonly<Ref<FieldApi<TParentData, TName,
|
13
|
+
} & UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, DeepValue<TParentData, TName>>) => FieldApi<TParentData, TName, TFieldValidator, TFormValidator, DeepValue<TParentData, TName>>;
|
14
|
+
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>): {
|
15
|
+
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
16
|
+
state: Readonly<Ref<FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state']>>;
|
17
17
|
};
|
18
18
|
type FieldValue<TParentData, TName> = TParentData extends any[] ? unknown extends TName ? TParentData[number] : DeepValue<TParentData[number], TName> : DeepValue<TParentData, TName>;
|
19
|
-
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>,
|
19
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = (TParentData extends any[] ? {
|
20
20
|
name?: TName;
|
21
21
|
index: number;
|
22
22
|
} : {
|
23
23
|
name: TName;
|
24
24
|
index?: never;
|
25
|
-
}) & Omit<UseFieldOptions<TParentData, TName,
|
26
|
-
type FieldComponent<TParentData,
|
25
|
+
}) & Omit<UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>, 'name' | 'index'>;
|
26
|
+
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<{
|
27
27
|
default: {
|
28
|
-
field: FieldApi<TParentData, TName,
|
29
|
-
state: FieldApi<TParentData, TName,
|
28
|
+
field: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
29
|
+
state: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state'];
|
30
30
|
};
|
31
31
|
}>>) => any;
|
32
|
-
declare const Field: <TParentData, TName extends DeepKeys<TParentData>,
|
32
|
+
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: _tanstack_form_core.FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, DeepValue<TParentData, TName>> & {
|
33
33
|
mode?: "value" | "array" | undefined;
|
34
34
|
} & ({
|
35
35
|
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
@@ -1,35 +1,35 @@
|
|
1
1
|
import * as _tanstack_form_core from '@tanstack/form-core';
|
2
|
-
import { DeepKeys, DeepValue, FieldApi, Narrow } from '@tanstack/form-core';
|
2
|
+
import { DeepKeys, Validator, DeepValue, FieldApi, Narrow } from '@tanstack/form-core';
|
3
3
|
import { SetupContext, SlotsType, Ref } from 'vue';
|
4
4
|
import { UseFieldOptions } from './types.js';
|
5
5
|
|
6
6
|
declare module '@tanstack/form-core' {
|
7
|
-
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>,
|
8
|
-
Field: FieldComponent<
|
7
|
+
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined, TData = DeepValue<TParentData, TName>> {
|
8
|
+
Field: FieldComponent<TParentData, TFormValidator>;
|
9
9
|
}
|
10
10
|
}
|
11
|
-
type UseField<TParentData,
|
11
|
+
type UseField<TParentData, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = <TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined>(opts?: {
|
12
12
|
name: Narrow<TName>;
|
13
|
-
} & UseFieldOptions<TParentData, TName,
|
14
|
-
declare function useField<TParentData, TName extends DeepKeys<TParentData>,
|
15
|
-
api: FieldApi<TParentData, TName,
|
16
|
-
state: Readonly<Ref<FieldApi<TParentData, TName,
|
13
|
+
} & UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator, DeepValue<TParentData, TName>>) => FieldApi<TParentData, TName, TFieldValidator, TFormValidator, DeepValue<TParentData, TName>>;
|
14
|
+
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>): {
|
15
|
+
api: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
16
|
+
state: Readonly<Ref<FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state']>>;
|
17
17
|
};
|
18
18
|
type FieldValue<TParentData, TName> = TParentData extends any[] ? unknown extends TName ? TParentData[number] : DeepValue<TParentData[number], TName> : DeepValue<TParentData, TName>;
|
19
|
-
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>,
|
19
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TFieldValidator extends Validator<DeepValue<TParentData, TName>, unknown> | undefined = undefined, TFormValidator extends Validator<TParentData, unknown> | undefined = undefined> = (TParentData extends any[] ? {
|
20
20
|
name?: TName;
|
21
21
|
index: number;
|
22
22
|
} : {
|
23
23
|
name: TName;
|
24
24
|
index?: never;
|
25
|
-
}) & Omit<UseFieldOptions<TParentData, TName,
|
26
|
-
type FieldComponent<TParentData,
|
25
|
+
}) & Omit<UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>, 'name' | 'index'>;
|
26
|
+
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<{
|
27
27
|
default: {
|
28
|
-
field: FieldApi<TParentData, TName,
|
29
|
-
state: FieldApi<TParentData, TName,
|
28
|
+
field: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>;
|
29
|
+
state: FieldApi<TParentData, TName, TFieldValidator, TFormValidator, TData>['state'];
|
30
30
|
};
|
31
31
|
}>>) => any;
|
32
|
-
declare const Field: <TParentData, TName extends DeepKeys<TParentData>,
|
32
|
+
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: _tanstack_form_core.FieldOptions<TParentData, TName, TFieldValidator, TFormValidator, DeepValue<TParentData, TName>> & {
|
33
33
|
mode?: "value" | "array" | undefined;
|
34
34
|
} & ({
|
35
35
|
[x: `on${Capitalize<string>}`]: ((...args: never) => any) | undefined;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import { FieldApi } from '@tanstack/form-core'\nimport type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted, onUnmounted, watch } from 'vue'\nimport type { SlotsType, SetupContext, Ref } from 'vue'\nimport { provideFormContext, useFormContext } from './formContext'\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
|
1
|
+
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import { FieldApi, type Validator } from '@tanstack/form-core'\nimport type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'\nimport { useStore } from '@tanstack/vue-store'\nimport { defineComponent, onMounted, onUnmounted, watch } from 'vue'\nimport type { SlotsType, SetupContext, Ref } from 'vue'\nimport { provideFormContext, useFormContext } from './formContext'\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>(\n opts?: { name: Narrow<TName> } & UseFieldOptions<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n DeepValue<TParentData, TName>\n >,\n) => FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n DeepValue<TParentData, TName>\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<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n // Omit<typeof opts, 'onMount'> & {\n // form: FormApi<TParentData>\n // }\n >\n state: Readonly<\n Ref<\n FieldApi<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator,\n TData\n >['state']\n >\n >\n} {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const fieldApi = (() => {\n const api = new FieldApi({\n ...opts,\n form: formApi,\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: formApi } as never)\n },\n )\n\n return { api: fieldApi, state: fieldState } as never\n}\n\nexport type FieldValue<TParentData, TName> = TParentData extends any[]\n ? unknown extends TName\n ? TParentData[number]\n : DeepValue<TParentData[number], TName>\n : DeepValue<TParentData, TName>\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> = (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<\n UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,\n 'name' | 'index'\n >\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: FieldComponentProps<\n TParentData,\n TName,\n TFieldValidator,\n TFormValidator\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 provideFormContext({\n formApi: fieldApi.api.form,\n parentFieldName: fieldApi.api.name,\n } as never)\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"],"mappings":";AAAA,SAAS,gBAAgC;AAEzC,SAAS,gBAAgB;AACzB,SAAS,iBAAiB,WAAW,aAAa,aAAa;AAE/D,SAAS,oBAAoB,sBAAsB;AA8C5C,SAAS,SAWd,MA6BA;AAEA,QAAM,EAAE,SAAS,gBAAgB,IAAI,eAAe;AAEpD,QAAM,YAAY,MAAM;AACtB,UAAM,MAAM,IAAI,SAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA,MACN,MAAM,KAAK;AAAA,IACb,CAAU;AAEV,QAAI,QAAQ;AAEZ,WAAO;AAAA,EACT,GAAG;AAEH,QAAM,aAAa,SAAS,SAAS,OAAO,CAAC,UAAU,KAAK;AAE5D,MAAI;AACJ,YAAU,MAAM;AACd,cAAU,SAAS,MAAM;AAAA,EAC3B,CAAC;AAED,cAAY,MAAM;AAChB,YAAQ;AAAA,EACV,CAAC;AAED;AAAA,IACE,MAAM;AAAA,IACN,MAAM;AAEJ,eAAS,OAAO,EAAE,GAAG,MAAM,MAAM,QAAQ,CAAU;AAAA,IACrD;AAAA,EACF;AAEA,SAAO,EAAE,KAAK,UAAU,OAAO,WAAW;AAC5C;AAwEO,IAAM,QAAQ;AAAA,EACnB,CAUE,cAMA,YACG;AACH,UAAM,WAAW,SAAS,EAAE,GAAG,cAAc,GAAG,QAAQ,MAAM,CAAQ;AAEtE,uBAAmB;AAAA,MACjB,SAAS,SAAS,IAAI;AAAA,MACtB,iBAAiB,SAAS,IAAI;AAAA,IAChC,CAAU;AAEV,WAAO,MACL,QAAQ,MAAM,QAAS;AAAA,MACrB,OAAO,SAAS;AAAA,MAChB,OAAO,SAAS,MAAM;AAAA,IACxB,CAAC;AAAA,EACL;AAAA,EACA,EAAE,MAAM,SAAS,cAAc,MAAM;AACvC;","names":[]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/useForm.tsx"],"sourcesContent":["import {
|
1
|
+
{"version":3,"sources":["../../src/useForm.tsx"],"sourcesContent":["import {\n FormApi,\n type FormState,\n type FormOptions,\n type Validator,\n} 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 type Ref,\n defineComponent,\n onMounted,\n} from 'vue'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData, TFormValidator> {\n Provider: (props: Record<string, any> & {}) => any\n provideFormContext: () => void\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.Provider = defineComponent(\n (_, context) => {\n onMounted(api.mount)\n provideFormContext({ formApi: formApi as never })\n return () => context.slots.default!()\n },\n { name: 'Provider' },\n )\n api.provideFormContext = () => {\n onMounted(api.mount)\n provideFormContext({ formApi: formApi as never })\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,uBAKO;AACP,uBAAuC;AACvC,sBAAoE;AACpE,yBAAmC;AACnC,iBAOO;AAwBA,SAAS,QAId,MACoC;AACpC,QAAM,WAAW,MAAM;AACrB,UAAM,MAAM,IAAI,yBAAmC,IAAI;AAEvD,QAAI,eAAW;AAAA,MACb,CAAC,GAAG,YAAY;AACd,kCAAU,IAAI,KAAK;AACnB,mDAAmB,EAAE,QAA0B,CAAC;AAChD,eAAO,MAAM,QAAQ,MAAM,QAAS;AAAA,MACtC;AAAA,MACA,EAAE,MAAM,WAAW;AAAA,IACrB;AACA,QAAI,qBAAqB,MAAM;AAC7B,gCAAU,IAAI,KAAK;AACnB,iDAAmB,EAAE,QAA0B,CAAC;AAAA,IAClD;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":[]}
|
@@ -1,15 +1,15 @@
|
|
1
|
-
import { FormState, FormOptions, FormApi } from '@tanstack/form-core';
|
1
|
+
import { FormState, Validator, FormOptions, FormApi } from '@tanstack/form-core';
|
2
2
|
import { NoInfer } from '@tanstack/vue-store';
|
3
3
|
import { FieldComponent, UseField } from './useField.cjs';
|
4
4
|
import { Ref, SetupContext, EmitsOptions, SlotsType } from 'vue';
|
5
5
|
import './types.cjs';
|
6
6
|
|
7
7
|
declare module '@tanstack/form-core' {
|
8
|
-
interface FormApi<TFormData,
|
8
|
+
interface FormApi<TFormData, TFormValidator> {
|
9
9
|
Provider: (props: Record<string, any> & {}) => any;
|
10
10
|
provideFormContext: () => void;
|
11
|
-
Field: FieldComponent<TFormData,
|
12
|
-
useField: UseField<TFormData,
|
11
|
+
Field: FieldComponent<TFormData, TFormValidator>;
|
12
|
+
useField: UseField<TFormData, TFormValidator>;
|
13
13
|
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => Readonly<Ref<TSelected>>;
|
14
14
|
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
15
15
|
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
@@ -18,6 +18,6 @@ declare module '@tanstack/form-core' {
|
|
18
18
|
}>>) => any;
|
19
19
|
}
|
20
20
|
}
|
21
|
-
declare function useForm<
|
21
|
+
declare function useForm<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(opts?: FormOptions<TFormData, TFormValidator>): FormApi<TFormData, TFormValidator>;
|
22
22
|
|
23
23
|
export { useForm };
|
@@ -1,15 +1,15 @@
|
|
1
|
-
import { FormState, FormOptions, FormApi } from '@tanstack/form-core';
|
1
|
+
import { FormState, Validator, FormOptions, FormApi } from '@tanstack/form-core';
|
2
2
|
import { NoInfer } from '@tanstack/vue-store';
|
3
3
|
import { FieldComponent, UseField } from './useField.js';
|
4
4
|
import { Ref, SetupContext, EmitsOptions, SlotsType } from 'vue';
|
5
5
|
import './types.js';
|
6
6
|
|
7
7
|
declare module '@tanstack/form-core' {
|
8
|
-
interface FormApi<TFormData,
|
8
|
+
interface FormApi<TFormData, TFormValidator> {
|
9
9
|
Provider: (props: Record<string, any> & {}) => any;
|
10
10
|
provideFormContext: () => void;
|
11
|
-
Field: FieldComponent<TFormData,
|
12
|
-
useField: UseField<TFormData,
|
11
|
+
Field: FieldComponent<TFormData, TFormValidator>;
|
12
|
+
useField: UseField<TFormData, TFormValidator>;
|
13
13
|
useStore: <TSelected = NoInfer<FormState<TFormData>>>(selector?: (state: NoInfer<FormState<TFormData>>) => TSelected) => Readonly<Ref<TSelected>>;
|
14
14
|
Subscribe: <TSelected = NoInfer<FormState<TFormData>>>(props: {
|
15
15
|
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected;
|
@@ -18,6 +18,6 @@ declare module '@tanstack/form-core' {
|
|
18
18
|
}>>) => any;
|
19
19
|
}
|
20
20
|
}
|
21
|
-
declare function useForm<
|
21
|
+
declare function useForm<TFormData, TFormValidator extends Validator<TFormData, unknown> | undefined = undefined>(opts?: FormOptions<TFormData, TFormValidator>): FormApi<TFormData, TFormValidator>;
|
22
22
|
|
23
23
|
export { useForm };
|
package/build/modern/useForm.js
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
// src/useForm.tsx
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
FormApi
|
4
|
+
} from "@tanstack/form-core";
|
3
5
|
import { useStore } from "@tanstack/vue-store";
|
4
6
|
import { Field, useField } from "./useField.js";
|
5
7
|
import { provideFormContext } from "./formContext.js";
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/useForm.tsx"],"sourcesContent":["import {
|
1
|
+
{"version":3,"sources":["../../src/useForm.tsx"],"sourcesContent":["import {\n FormApi,\n type FormState,\n type FormOptions,\n type Validator,\n} 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 type Ref,\n defineComponent,\n onMounted,\n} from 'vue'\n\ndeclare module '@tanstack/form-core' {\n // eslint-disable-next-line no-shadow\n interface FormApi<TFormData, TFormValidator> {\n Provider: (props: Record<string, any> & {}) => any\n provideFormContext: () => void\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.Provider = defineComponent(\n (_, context) => {\n onMounted(api.mount)\n provideFormContext({ formApi: formApi as never })\n return () => context.slots.default!()\n },\n { name: 'Provider' },\n )\n api.provideFormContext = () => {\n onMounted(api.mount)\n provideFormContext({ formApi: formApi as never })\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,EACE;AAAA,OAIK;AACP,SAAuB,gBAAgB;AACvC,SAA6C,OAAO,gBAAgB;AACpE,SAAS,0BAA0B;AACnC;AAAA,EAKE;AAAA,EACA;AAAA,OACK;AAwBA,SAAS,QAId,MACoC;AACpC,QAAM,WAAW,MAAM;AACrB,UAAM,MAAM,IAAI,QAAmC,IAAI;AAEvD,QAAI,WAAW;AAAA,MACb,CAAC,GAAG,YAAY;AACd,kBAAU,IAAI,KAAK;AACnB,2BAAmB,EAAE,QAA0B,CAAC;AAChD,eAAO,MAAM,QAAQ,MAAM,QAAS;AAAA,MACtC;AAAA,MACA,EAAE,MAAM,WAAW;AAAA,IACrB;AACA,QAAI,qBAAqB,MAAM;AAC7B,gBAAU,IAAI,KAAK;AACnB,yBAAmB,EAAE,QAA0B,CAAC;AAAA,IAClD;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
|
+
"version": "0.11.0",
|
4
4
|
"description": "Powerful, type-safe forms for Vue.",
|
5
5
|
"author": "tannerlinsley",
|
6
6
|
"license": "MIT",
|
@@ -44,7 +44,7 @@
|
|
44
44
|
"dependencies": {
|
45
45
|
"@tanstack/store": "0.1.3",
|
46
46
|
"@tanstack/vue-store": "0.1.3",
|
47
|
-
"@tanstack/form-core": "0.
|
47
|
+
"@tanstack/form-core": "0.11.0"
|
48
48
|
},
|
49
49
|
"devDependencies": {
|
50
50
|
"vue": "^3.3.4"
|
package/src/createFormFactory.ts
CHANGED
@@ -1,23 +1,29 @@
|
|
1
|
-
import type { FormApi, FormOptions } from '@tanstack/form-core'
|
1
|
+
import type { FormApi, FormOptions, Validator } from '@tanstack/form-core'
|
2
2
|
|
3
3
|
import { type UseField, type FieldComponent, Field, useField } from './useField'
|
4
4
|
import { useForm } from './useForm'
|
5
5
|
|
6
|
-
export type FormFactory<
|
6
|
+
export type FormFactory<
|
7
|
+
TFormData,
|
8
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
9
|
+
> = {
|
7
10
|
useForm: (
|
8
|
-
opts?: FormOptions<TFormData,
|
9
|
-
) => FormApi<TFormData,
|
10
|
-
useField: UseField<TFormData,
|
11
|
-
Field: FieldComponent<TFormData,
|
11
|
+
opts?: FormOptions<TFormData, TFormValidator>,
|
12
|
+
) => FormApi<TFormData, TFormValidator>
|
13
|
+
useField: UseField<TFormData, TFormValidator>
|
14
|
+
Field: FieldComponent<TFormData, TFormValidator>
|
12
15
|
}
|
13
16
|
|
14
|
-
export function createFormFactory<
|
15
|
-
|
16
|
-
|
17
|
+
export function createFormFactory<
|
18
|
+
TFormData,
|
19
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
20
|
+
>(
|
21
|
+
defaultOpts?: FormOptions<TFormData, TFormValidator>,
|
22
|
+
): FormFactory<TFormData, TFormValidator> {
|
17
23
|
return {
|
18
24
|
useForm: (opts) => {
|
19
25
|
const formOptions = Object.assign({}, defaultOpts, opts)
|
20
|
-
return useForm<TFormData,
|
26
|
+
return useForm<TFormData, TFormValidator>(formOptions)
|
21
27
|
},
|
22
28
|
useField: useField as any,
|
23
29
|
Field: Field as any,
|
package/src/formContext.ts
CHANGED
@@ -1,14 +1,20 @@
|
|
1
|
-
import type { FormApi } from '@tanstack/form-core'
|
1
|
+
import type { FormApi, Validator } from '@tanstack/form-core'
|
2
2
|
import { inject, provide } from 'vue'
|
3
3
|
|
4
|
-
export type FormContext
|
5
|
-
|
4
|
+
export type FormContext<
|
5
|
+
TFormData = any,
|
6
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
7
|
+
> = {
|
8
|
+
formApi: FormApi<TFormData, TFormValidator>
|
6
9
|
parentFieldName?: string
|
7
10
|
} | null
|
8
11
|
|
9
12
|
export const formContext = Symbol('FormContext')
|
10
13
|
|
11
|
-
export function provideFormContext
|
14
|
+
export function provideFormContext<
|
15
|
+
TFormData = any,
|
16
|
+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
17
|
+
>(val: FormContext<TFormData, TFormValidator>) {
|
12
18
|
provide(formContext, val)
|
13
19
|
}
|
14
20
|
|
@@ -15,7 +15,7 @@ describe('useField', () => {
|
|
15
15
|
lastName: string
|
16
16
|
}
|
17
17
|
|
18
|
-
const formFactory = createFormFactory<Person
|
18
|
+
const formFactory = createFormFactory<Person>()
|
19
19
|
|
20
20
|
const Comp = defineComponent(() => {
|
21
21
|
const form = formFactory.useForm()
|
@@ -54,7 +54,7 @@ describe('useField', () => {
|
|
54
54
|
}
|
55
55
|
const error = 'Please enter a different value'
|
56
56
|
|
57
|
-
const formFactory = createFormFactory<Person
|
57
|
+
const formFactory = createFormFactory<Person>()
|
58
58
|
|
59
59
|
const Comp = defineComponent(() => {
|
60
60
|
const form = formFactory.useForm()
|
@@ -64,7 +64,9 @@ describe('useField', () => {
|
|
64
64
|
return () => (
|
65
65
|
<form.Field
|
66
66
|
name="firstName"
|
67
|
-
|
67
|
+
validators={{
|
68
|
+
onChange: ({ value }) => (value === 'other' ? error : undefined),
|
69
|
+
}}
|
68
70
|
>
|
69
71
|
{({
|
70
72
|
field,
|
@@ -101,7 +103,7 @@ describe('useField', () => {
|
|
101
103
|
}
|
102
104
|
const error = 'Please enter a different value'
|
103
105
|
|
104
|
-
const formFactory = createFormFactory<Person
|
106
|
+
const formFactory = createFormFactory<Person>()
|
105
107
|
|
106
108
|
const Comp = defineComponent(() => {
|
107
109
|
const form = formFactory.useForm()
|
@@ -111,7 +113,9 @@ describe('useField', () => {
|
|
111
113
|
return () => (
|
112
114
|
<form.Field
|
113
115
|
name="firstName"
|
114
|
-
|
116
|
+
validators={{
|
117
|
+
onChange: ({ value }) => (value === 'other' ? error : undefined),
|
118
|
+
}}
|
115
119
|
>
|
116
120
|
{({
|
117
121
|
field,
|
@@ -149,7 +153,7 @@ describe('useField', () => {
|
|
149
153
|
}
|
150
154
|
const error = 'Please enter a different value'
|
151
155
|
|
152
|
-
const formFactory = createFormFactory<Person
|
156
|
+
const formFactory = createFormFactory<Person>()
|
153
157
|
|
154
158
|
const Comp = defineComponent(() => {
|
155
159
|
const form = formFactory.useForm()
|
@@ -160,9 +164,11 @@ describe('useField', () => {
|
|
160
164
|
<form.Field
|
161
165
|
name="firstName"
|
162
166
|
defaultMeta={{ isTouched: true }}
|
163
|
-
|
164
|
-
|
165
|
-
|
167
|
+
validators={{
|
168
|
+
onChangeAsync: async () => {
|
169
|
+
await sleep(10)
|
170
|
+
return error
|
171
|
+
},
|
166
172
|
}}
|
167
173
|
>
|
168
174
|
{({
|
@@ -203,7 +209,7 @@ describe('useField', () => {
|
|
203
209
|
|
204
210
|
const mockFn = vi.fn()
|
205
211
|
const error = 'Please enter a different value'
|
206
|
-
const formFactory = createFormFactory<Person
|
212
|
+
const formFactory = createFormFactory<Person>()
|
207
213
|
|
208
214
|
const Comp = defineComponent(() => {
|
209
215
|
const form = formFactory.useForm()
|
@@ -214,11 +220,13 @@ describe('useField', () => {
|
|
214
220
|
<form.Field
|
215
221
|
name="firstName"
|
216
222
|
defaultMeta={{ isTouched: true }}
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
223
|
+
validators={{
|
224
|
+
onChangeAsyncDebounceMs: 100,
|
225
|
+
onChangeAsync: async () => {
|
226
|
+
mockFn()
|
227
|
+
await sleep(10)
|
228
|
+
return error
|
229
|
+
},
|
222
230
|
}}
|
223
231
|
>
|
224
232
|
{({
|