@tanstack/react-form 0.3.5 → 0.3.7
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/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 +11 -11
- package/build/legacy/useField.d.ts +11 -11
- package/build/legacy/useField.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 +11 -11
- package/build/modern/useField.d.ts +11 -11
- package/build/modern/useField.js.map +1 -1
- package/package.json +2 -2
- package/src/tests/useField.test.tsx +47 -2
- package/src/types.ts +3 -3
- package/src/useField.tsx +14 -41
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type { FieldOptions, DeepKeys } from '@tanstack/form-core'\n\nexport type UseFieldOptions<\n
|
|
1
|
+
{"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type { FieldOptions, DeepKeys, DeepValue } from '@tanstack/form-core'\n\nexport type UseFieldOptions<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n> = FieldOptions<TParentData, TName, TData> & {\n mode?: 'value' | 'array'\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
package/build/legacy/types.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DeepKeys, FieldOptions } from '@tanstack/form-core';
|
|
1
|
+
import { DeepKeys, DeepValue, FieldOptions } from '@tanstack/form-core';
|
|
2
2
|
|
|
3
|
-
type UseFieldOptions<
|
|
3
|
+
type UseFieldOptions<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> = FieldOptions<TParentData, TName, TData> & {
|
|
4
4
|
mode?: 'value' | 'array';
|
|
5
5
|
};
|
|
6
6
|
|
package/build/legacy/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DeepKeys, FieldOptions } from '@tanstack/form-core';
|
|
1
|
+
import { DeepKeys, DeepValue, FieldOptions } from '@tanstack/form-core';
|
|
2
2
|
|
|
3
|
-
type UseFieldOptions<
|
|
3
|
+
type UseFieldOptions<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> = FieldOptions<TParentData, TName, TData> & {
|
|
4
4
|
mode?: 'value' | 'array';
|
|
5
5
|
};
|
|
6
6
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'\nimport { FieldApi, functionalUpdate } from '@tanstack/form-core'\nimport { useFormContext, formContext } from './formContext'\nimport useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'\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 TData = DeepValue<TParentData, TName>,\n > {\n Field: FieldComponent<TData>\n }\n}\n\nexport type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(\n opts?: { name: Narrow<TName> } & UseFieldOptions<TParentData, TName>,\n) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>\n\nexport function useField<TParentData, TName extends DeepKeys<TParentData>>(\n opts: UseFieldOptions<TParentData, TName>,\n): FieldApi<\n TParentData,\n TName\n // Omit<typeof opts, 'onMount'> & {\n // form: FormApi<TParentData>\n // }\n> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState(() => {\n const name = (\n typeof opts.index === 'number'\n ? [parentFieldName, opts.index, opts.name]\n : [parentFieldName, opts.name]\n )\n .filter((d) => d !== undefined)\n .join('.')\n\n const api = new FieldApi({\n ...opts,\n form: formApi,\n name: name,\n } as never)\n\n api.Field = Field as never\n\n return api\n })\n\n /**\n * fieldApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n fieldApi.update({ ...opts, form: formApi } as never)\n })\n\n useStore(\n fieldApi.store,\n opts.mode === 'array'\n ? (state: any) => {\n return [state.meta, Object.keys(state.value || []).length]\n }\n : undefined,\n )\n // Instantiates field meta and removes it when unrendered\n useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi as never\n}\n\ntype FieldComponentProps<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n> = {\n children: (fieldApi: FieldApi<TParentData, TName, TData>) => any\n} & (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>\n\nexport type FieldComponent<TParentData> = <\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n>({\n children,\n ...fieldOptions\n}: FieldComponentProps<TParentData, TName, TData>) => any\n\nexport function Field<TParentData, TName extends DeepKeys<TParentData>>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<TParentData, TName>) => any\n} & UseFieldOptions<TParentData, TName>) {\n const fieldApi = useField(fieldOptions as any)\n\n return (\n <formContext.Provider\n value={{ formApi: fieldApi.form, parentFieldName: fieldApi.name }}\n children={functionalUpdate(children, fieldApi as any)}\n />\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAgC;AAChC,yBAAyB;AAEzB,uBAA2C;AAC3C,yBAA4C;AAC5C,0CAAsC;AAkB/B,SAAS,SACd,MAOA;AAEA,QAAM,EAAE,SAAS,gBAAgB,QAAI,mCAAe;AAEpD,QAAM,CAAC,QAAQ,QAAI,uBAAS,MAAM;AAChC,UAAM,QACJ,OAAO,KAAK,UAAU,WAClB,CAAC,iBAAiB,KAAK,OAAO,KAAK,IAAI,IACvC,CAAC,iBAAiB,KAAK,IAAI,GAE9B,OAAO,CAAC,MAAM,MAAM,MAAS,EAC7B,KAAK,GAAG;AAEX,UAAM,MAAM,IAAI,0BAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA,MACN;AAAA,IACF,CAAU;AAEV,QAAI,QAAQ;AAEZ,WAAO;AAAA,EACT,CAAC;AAMD,0CAAAA,SAA0B,MAAM;AAC9B,aAAS,OAAO,EAAE,GAAG,MAAM,MAAM,QAAQ,CAAU;AAAA,EACrD,CAAC;AAED;AAAA,IACE,SAAS;AAAA,IACT,KAAK,SAAS,UACV,CAAC,UAAe;AACd,aAAO,CAAC,MAAM,MAAM,OAAO,KAAK,MAAM,SAAS,CAAC,CAAC,EAAE,MAAM;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,0CAAAA,SAA0B,MAAM,SAAS,MAAM,GAAG,CAAC,QAAQ,CAAC;AAE5D,SAAO;AACT;AA2BO,SAAS,MAAwD;AAAA,EACtE;AAAA,EACA,GAAG;AACL,GAEyC;AACvC,QAAM,WAAW,SAAS,YAAmB;AAE7C,SACE,6BAAAC,QAAA;AAAA,IAAC,+BAAY;AAAA,IAAZ;AAAA,MACC,OAAO,EAAE,SAAS,SAAS,MAAM,iBAAiB,SAAS,KAAK;AAAA,MAChE,cAAU,mCAAiB,UAAU,QAAe;AAAA;AAAA,EACtD;AAEJ;","names":["useIsomorphicLayoutEffect","React"]}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { DeepKeys,
|
|
1
|
+
import { DeepKeys, DeepValue, Narrow, FieldApi } from '@tanstack/form-core';
|
|
2
2
|
import { UseFieldOptions } from './types.cjs';
|
|
3
3
|
|
|
4
4
|
declare module '@tanstack/form-core' {
|
|
5
|
-
interface FieldApi<
|
|
5
|
+
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> {
|
|
6
6
|
Field: FieldComponent<TData>;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(opts?: {
|
|
10
10
|
name: Narrow<TName>;
|
|
11
|
-
} & UseFieldOptions<
|
|
12
|
-
declare function useField<
|
|
13
|
-
type FieldComponentProps<
|
|
14
|
-
children: (fieldApi: FieldApi<
|
|
11
|
+
} & UseFieldOptions<TParentData, TName>) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>;
|
|
12
|
+
declare function useField<TParentData, TName extends DeepKeys<TParentData>>(opts: UseFieldOptions<TParentData, TName>): FieldApi<TParentData, TName>;
|
|
13
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> = {
|
|
14
|
+
children: (fieldApi: FieldApi<TParentData, TName, TData>) => any;
|
|
15
15
|
} & (TParentData extends any[] ? {
|
|
16
16
|
name?: TName;
|
|
17
17
|
index: number;
|
|
18
18
|
} : {
|
|
19
19
|
name: TName;
|
|
20
20
|
index?: never;
|
|
21
|
-
}) & Omit<UseFieldOptions<
|
|
22
|
-
type FieldComponent<TParentData> = <
|
|
23
|
-
declare function Field<
|
|
24
|
-
children: (fieldApi: FieldApi<
|
|
25
|
-
} & UseFieldOptions<
|
|
21
|
+
}) & Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>;
|
|
22
|
+
type FieldComponent<TParentData> = <TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TName, TData>) => any;
|
|
23
|
+
declare function Field<TParentData, TName extends DeepKeys<TParentData>>({ children, ...fieldOptions }: {
|
|
24
|
+
children: (fieldApi: FieldApi<TParentData, TName>) => any;
|
|
25
|
+
} & UseFieldOptions<TParentData, TName>): JSX.Element;
|
|
26
26
|
|
|
27
27
|
export { Field, FieldComponent, UseField, useField };
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { DeepKeys,
|
|
1
|
+
import { DeepKeys, DeepValue, Narrow, FieldApi } from '@tanstack/form-core';
|
|
2
2
|
import { UseFieldOptions } from './types.js';
|
|
3
3
|
|
|
4
4
|
declare module '@tanstack/form-core' {
|
|
5
|
-
interface FieldApi<
|
|
5
|
+
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> {
|
|
6
6
|
Field: FieldComponent<TData>;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(opts?: {
|
|
10
10
|
name: Narrow<TName>;
|
|
11
|
-
} & UseFieldOptions<
|
|
12
|
-
declare function useField<
|
|
13
|
-
type FieldComponentProps<
|
|
14
|
-
children: (fieldApi: FieldApi<
|
|
11
|
+
} & UseFieldOptions<TParentData, TName>) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>;
|
|
12
|
+
declare function useField<TParentData, TName extends DeepKeys<TParentData>>(opts: UseFieldOptions<TParentData, TName>): FieldApi<TParentData, TName>;
|
|
13
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> = {
|
|
14
|
+
children: (fieldApi: FieldApi<TParentData, TName, TData>) => any;
|
|
15
15
|
} & (TParentData extends any[] ? {
|
|
16
16
|
name?: TName;
|
|
17
17
|
index: number;
|
|
18
18
|
} : {
|
|
19
19
|
name: TName;
|
|
20
20
|
index?: never;
|
|
21
|
-
}) & Omit<UseFieldOptions<
|
|
22
|
-
type FieldComponent<TParentData> = <
|
|
23
|
-
declare function Field<
|
|
24
|
-
children: (fieldApi: FieldApi<
|
|
25
|
-
} & UseFieldOptions<
|
|
21
|
+
}) & Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>;
|
|
22
|
+
type FieldComponent<TParentData> = <TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TName, TData>) => any;
|
|
23
|
+
declare function Field<TParentData, TName extends DeepKeys<TParentData>>({ children, ...fieldOptions }: {
|
|
24
|
+
children: (fieldApi: FieldApi<TParentData, TName>) => any;
|
|
25
|
+
} & UseFieldOptions<TParentData, TName>): JSX.Element;
|
|
26
26
|
|
|
27
27
|
export { Field, FieldComponent, UseField, useField };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'\nimport { FieldApi, functionalUpdate } from '@tanstack/form-core'\nimport { useFormContext, formContext } from './formContext'\nimport useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'\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 TData = DeepValue<TParentData, TName>,\n > {\n Field: FieldComponent<TData>\n }\n}\n\nexport type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(\n opts?: { name: Narrow<TName> } & UseFieldOptions<TParentData, TName>,\n) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>\n\nexport function useField<TParentData, TName extends DeepKeys<TParentData>>(\n opts: UseFieldOptions<TParentData, TName>,\n): FieldApi<\n TParentData,\n TName\n // Omit<typeof opts, 'onMount'> & {\n // form: FormApi<TParentData>\n // }\n> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState(() => {\n const name = (\n typeof opts.index === 'number'\n ? [parentFieldName, opts.index, opts.name]\n : [parentFieldName, opts.name]\n )\n .filter((d) => d !== undefined)\n .join('.')\n\n const api = new FieldApi({\n ...opts,\n form: formApi,\n name: name,\n } as never)\n\n api.Field = Field as never\n\n return api\n })\n\n /**\n * fieldApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n fieldApi.update({ ...opts, form: formApi } as never)\n })\n\n useStore(\n fieldApi.store,\n opts.mode === 'array'\n ? (state: any) => {\n return [state.meta, Object.keys(state.value || []).length]\n }\n : undefined,\n )\n // Instantiates field meta and removes it when unrendered\n useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi as never\n}\n\ntype FieldComponentProps<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n> = {\n children: (fieldApi: FieldApi<TParentData, TName, TData>) => any\n} & (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>\n\nexport type FieldComponent<TParentData> = <\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n>({\n children,\n ...fieldOptions\n}: FieldComponentProps<TParentData, TName, TData>) => any\n\nexport function Field<TParentData, TName extends DeepKeys<TParentData>>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<TParentData, TName>) => any\n} & UseFieldOptions<TParentData, TName>) {\n const fieldApi = useField(fieldOptions as any)\n\n return (\n <formContext.Provider\n value={{ formApi: fieldApi.form, parentFieldName: fieldApi.name }}\n children={functionalUpdate(children, fieldApi as any)}\n />\n )\n}\n"],"mappings":";AAAA,OAAO,SAAS,gBAAgB;AAChC,SAAS,gBAAgB;AAEzB,SAAS,UAAU,wBAAwB;AAC3C,SAAS,gBAAgB,mBAAmB;AAC5C,OAAO,+BAA+B;AAkB/B,SAAS,SACd,MAOA;AAEA,QAAM,EAAE,SAAS,gBAAgB,IAAI,eAAe;AAEpD,QAAM,CAAC,QAAQ,IAAI,SAAS,MAAM;AAChC,UAAM,QACJ,OAAO,KAAK,UAAU,WAClB,CAAC,iBAAiB,KAAK,OAAO,KAAK,IAAI,IACvC,CAAC,iBAAiB,KAAK,IAAI,GAE9B,OAAO,CAAC,MAAM,MAAM,MAAS,EAC7B,KAAK,GAAG;AAEX,UAAM,MAAM,IAAI,SAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA,MACN;AAAA,IACF,CAAU;AAEV,QAAI,QAAQ;AAEZ,WAAO;AAAA,EACT,CAAC;AAMD,4BAA0B,MAAM;AAC9B,aAAS,OAAO,EAAE,GAAG,MAAM,MAAM,QAAQ,CAAU;AAAA,EACrD,CAAC;AAED;AAAA,IACE,SAAS;AAAA,IACT,KAAK,SAAS,UACV,CAAC,UAAe;AACd,aAAO,CAAC,MAAM,MAAM,OAAO,KAAK,MAAM,SAAS,CAAC,CAAC,EAAE,MAAM;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,4BAA0B,MAAM,SAAS,MAAM,GAAG,CAAC,QAAQ,CAAC;AAE5D,SAAO;AACT;AA2BO,SAAS,MAAwD;AAAA,EACtE;AAAA,EACA,GAAG;AACL,GAEyC;AACvC,QAAM,WAAW,SAAS,YAAmB;AAE7C,SACE;AAAA,IAAC,YAAY;AAAA,IAAZ;AAAA,MACC,OAAO,EAAE,SAAS,SAAS,MAAM,iBAAiB,SAAS,KAAK;AAAA,MAChE,UAAU,iBAAiB,UAAU,QAAe;AAAA;AAAA,EACtD;AAEJ;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type { FieldOptions, DeepKeys } from '@tanstack/form-core'\n\nexport type UseFieldOptions<\n
|
|
1
|
+
{"version":3,"sources":["../../src/types.ts"],"sourcesContent":["import type { FieldOptions, DeepKeys, DeepValue } from '@tanstack/form-core'\n\nexport type UseFieldOptions<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n> = FieldOptions<TParentData, TName, TData> & {\n mode?: 'value' | 'array'\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
package/build/modern/types.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DeepKeys, FieldOptions } from '@tanstack/form-core';
|
|
1
|
+
import { DeepKeys, DeepValue, FieldOptions } from '@tanstack/form-core';
|
|
2
2
|
|
|
3
|
-
type UseFieldOptions<
|
|
3
|
+
type UseFieldOptions<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> = FieldOptions<TParentData, TName, TData> & {
|
|
4
4
|
mode?: 'value' | 'array';
|
|
5
5
|
};
|
|
6
6
|
|
package/build/modern/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DeepKeys, FieldOptions } from '@tanstack/form-core';
|
|
1
|
+
import { DeepKeys, DeepValue, FieldOptions } from '@tanstack/form-core';
|
|
2
2
|
|
|
3
|
-
type UseFieldOptions<
|
|
3
|
+
type UseFieldOptions<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> = FieldOptions<TParentData, TName, TData> & {
|
|
4
4
|
mode?: 'value' | 'array';
|
|
5
5
|
};
|
|
6
6
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'\nimport { FieldApi, functionalUpdate } from '@tanstack/form-core'\nimport { useFormContext, formContext } from './formContext'\nimport useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'\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 TData = DeepValue<TParentData, TName>,\n > {\n Field: FieldComponent<TData>\n }\n}\n\nexport type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(\n opts?: { name: Narrow<TName> } & UseFieldOptions<TParentData, TName>,\n) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>\n\nexport function useField<TParentData, TName extends DeepKeys<TParentData>>(\n opts: UseFieldOptions<TParentData, TName>,\n): FieldApi<\n TParentData,\n TName\n // Omit<typeof opts, 'onMount'> & {\n // form: FormApi<TParentData>\n // }\n> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState(() => {\n const name = (\n typeof opts.index === 'number'\n ? [parentFieldName, opts.index, opts.name]\n : [parentFieldName, opts.name]\n )\n .filter((d) => d !== undefined)\n .join('.')\n\n const api = new FieldApi({\n ...opts,\n form: formApi,\n name: name,\n } as never)\n\n api.Field = Field as never\n\n return api\n })\n\n /**\n * fieldApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n fieldApi.update({ ...opts, form: formApi } as never)\n })\n\n useStore(\n fieldApi.store,\n opts.mode === 'array'\n ? (state: any) => {\n return [state.meta, Object.keys(state.value || []).length]\n }\n : undefined,\n )\n // Instantiates field meta and removes it when unrendered\n useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi as never\n}\n\ntype FieldComponentProps<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n> = {\n children: (fieldApi: FieldApi<TParentData, TName, TData>) => any\n} & (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>\n\nexport type FieldComponent<TParentData> = <\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n>({\n children,\n ...fieldOptions\n}: FieldComponentProps<TParentData, TName, TData>) => any\n\nexport function Field<TParentData, TName extends DeepKeys<TParentData>>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<TParentData, TName>) => any\n} & UseFieldOptions<TParentData, TName>) {\n const fieldApi = useField(fieldOptions as any)\n\n return (\n <formContext.Provider\n value={{ formApi: fieldApi.form, parentFieldName: fieldApi.name }}\n children={functionalUpdate(children, fieldApi as any)}\n />\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAgC;AAChC,yBAAyB;AAEzB,uBAA2C;AAC3C,yBAA4C;AAC5C,0CAAsC;AAkB/B,SAAS,SACd,MAOA;AAEA,QAAM,EAAE,SAAS,gBAAgB,QAAI,mCAAe;AAEpD,QAAM,CAAC,QAAQ,QAAI,uBAAS,MAAM;AAChC,UAAM,QACJ,OAAO,KAAK,UAAU,WAClB,CAAC,iBAAiB,KAAK,OAAO,KAAK,IAAI,IACvC,CAAC,iBAAiB,KAAK,IAAI,GAE9B,OAAO,CAAC,MAAM,MAAM,MAAS,EAC7B,KAAK,GAAG;AAEX,UAAM,MAAM,IAAI,0BAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA,MACN;AAAA,IACF,CAAU;AAEV,QAAI,QAAQ;AAEZ,WAAO;AAAA,EACT,CAAC;AAMD,0CAAAA,SAA0B,MAAM;AAC9B,aAAS,OAAO,EAAE,GAAG,MAAM,MAAM,QAAQ,CAAU;AAAA,EACrD,CAAC;AAED;AAAA,IACE,SAAS;AAAA,IACT,KAAK,SAAS,UACV,CAAC,UAAe;AACd,aAAO,CAAC,MAAM,MAAM,OAAO,KAAK,MAAM,SAAS,CAAC,CAAC,EAAE,MAAM;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,0CAAAA,SAA0B,MAAM,SAAS,MAAM,GAAG,CAAC,QAAQ,CAAC;AAE5D,SAAO;AACT;AA2BO,SAAS,MAAwD;AAAA,EACtE;AAAA,EACA,GAAG;AACL,GAEyC;AACvC,QAAM,WAAW,SAAS,YAAmB;AAE7C,SACE,6BAAAC,QAAA;AAAA,IAAC,+BAAY;AAAA,IAAZ;AAAA,MACC,OAAO,EAAE,SAAS,SAAS,MAAM,iBAAiB,SAAS,KAAK;AAAA,MAChE,cAAU,mCAAiB,UAAU,QAAe;AAAA;AAAA,EACtD;AAEJ;","names":["useIsomorphicLayoutEffect","React"]}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { DeepKeys,
|
|
1
|
+
import { DeepKeys, DeepValue, Narrow, FieldApi } from '@tanstack/form-core';
|
|
2
2
|
import { UseFieldOptions } from './types.cjs';
|
|
3
3
|
|
|
4
4
|
declare module '@tanstack/form-core' {
|
|
5
|
-
interface FieldApi<
|
|
5
|
+
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> {
|
|
6
6
|
Field: FieldComponent<TData>;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(opts?: {
|
|
10
10
|
name: Narrow<TName>;
|
|
11
|
-
} & UseFieldOptions<
|
|
12
|
-
declare function useField<
|
|
13
|
-
type FieldComponentProps<
|
|
14
|
-
children: (fieldApi: FieldApi<
|
|
11
|
+
} & UseFieldOptions<TParentData, TName>) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>;
|
|
12
|
+
declare function useField<TParentData, TName extends DeepKeys<TParentData>>(opts: UseFieldOptions<TParentData, TName>): FieldApi<TParentData, TName>;
|
|
13
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> = {
|
|
14
|
+
children: (fieldApi: FieldApi<TParentData, TName, TData>) => any;
|
|
15
15
|
} & (TParentData extends any[] ? {
|
|
16
16
|
name?: TName;
|
|
17
17
|
index: number;
|
|
18
18
|
} : {
|
|
19
19
|
name: TName;
|
|
20
20
|
index?: never;
|
|
21
|
-
}) & Omit<UseFieldOptions<
|
|
22
|
-
type FieldComponent<TParentData> = <
|
|
23
|
-
declare function Field<
|
|
24
|
-
children: (fieldApi: FieldApi<
|
|
25
|
-
} & UseFieldOptions<
|
|
21
|
+
}) & Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>;
|
|
22
|
+
type FieldComponent<TParentData> = <TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TName, TData>) => any;
|
|
23
|
+
declare function Field<TParentData, TName extends DeepKeys<TParentData>>({ children, ...fieldOptions }: {
|
|
24
|
+
children: (fieldApi: FieldApi<TParentData, TName>) => any;
|
|
25
|
+
} & UseFieldOptions<TParentData, TName>): JSX.Element;
|
|
26
26
|
|
|
27
27
|
export { Field, FieldComponent, UseField, useField };
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { DeepKeys,
|
|
1
|
+
import { DeepKeys, DeepValue, Narrow, FieldApi } from '@tanstack/form-core';
|
|
2
2
|
import { UseFieldOptions } from './types.js';
|
|
3
3
|
|
|
4
4
|
declare module '@tanstack/form-core' {
|
|
5
|
-
interface FieldApi<
|
|
5
|
+
interface FieldApi<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> {
|
|
6
6
|
Field: FieldComponent<TData>;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(opts?: {
|
|
10
10
|
name: Narrow<TName>;
|
|
11
|
-
} & UseFieldOptions<
|
|
12
|
-
declare function useField<
|
|
13
|
-
type FieldComponentProps<
|
|
14
|
-
children: (fieldApi: FieldApi<
|
|
11
|
+
} & UseFieldOptions<TParentData, TName>) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>;
|
|
12
|
+
declare function useField<TParentData, TName extends DeepKeys<TParentData>>(opts: UseFieldOptions<TParentData, TName>): FieldApi<TParentData, TName>;
|
|
13
|
+
type FieldComponentProps<TParentData, TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>> = {
|
|
14
|
+
children: (fieldApi: FieldApi<TParentData, TName, TData>) => any;
|
|
15
15
|
} & (TParentData extends any[] ? {
|
|
16
16
|
name?: TName;
|
|
17
17
|
index: number;
|
|
18
18
|
} : {
|
|
19
19
|
name: TName;
|
|
20
20
|
index?: never;
|
|
21
|
-
}) & Omit<UseFieldOptions<
|
|
22
|
-
type FieldComponent<TParentData> = <
|
|
23
|
-
declare function Field<
|
|
24
|
-
children: (fieldApi: FieldApi<
|
|
25
|
-
} & UseFieldOptions<
|
|
21
|
+
}) & Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>;
|
|
22
|
+
type FieldComponent<TParentData> = <TName extends DeepKeys<TParentData>, TData = DeepValue<TParentData, TName>>({ children, ...fieldOptions }: FieldComponentProps<TParentData, TName, TData>) => any;
|
|
23
|
+
declare function Field<TParentData, TName extends DeepKeys<TParentData>>({ children, ...fieldOptions }: {
|
|
24
|
+
children: (fieldApi: FieldApi<TParentData, TName>) => any;
|
|
25
|
+
} & UseFieldOptions<TParentData, TName>): JSX.Element;
|
|
26
26
|
|
|
27
27
|
export { Field, FieldComponent, UseField, useField };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../src/useField.tsx"],"sourcesContent":["import React, { useState } from 'react'\nimport { useStore } from '@tanstack/react-store'\nimport type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'\nimport { FieldApi, functionalUpdate } from '@tanstack/form-core'\nimport { useFormContext, formContext } from './formContext'\nimport useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'\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 TData = DeepValue<TParentData, TName>,\n > {\n Field: FieldComponent<TData>\n }\n}\n\nexport type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(\n opts?: { name: Narrow<TName> } & UseFieldOptions<TParentData, TName>,\n) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>\n\nexport function useField<TParentData, TName extends DeepKeys<TParentData>>(\n opts: UseFieldOptions<TParentData, TName>,\n): FieldApi<\n TParentData,\n TName\n // Omit<typeof opts, 'onMount'> & {\n // form: FormApi<TParentData>\n // }\n> {\n // Get the form API either manually or from context\n const { formApi, parentFieldName } = useFormContext()\n\n const [fieldApi] = useState(() => {\n const name = (\n typeof opts.index === 'number'\n ? [parentFieldName, opts.index, opts.name]\n : [parentFieldName, opts.name]\n )\n .filter((d) => d !== undefined)\n .join('.')\n\n const api = new FieldApi({\n ...opts,\n form: formApi,\n name: name,\n } as never)\n\n api.Field = Field as never\n\n return api\n })\n\n /**\n * fieldApi.update should not have any side effects. Think of it like a `useRef`\n * that we need to keep updated every render with the most up-to-date information.\n */\n useIsomorphicLayoutEffect(() => {\n fieldApi.update({ ...opts, form: formApi } as never)\n })\n\n useStore(\n fieldApi.store,\n opts.mode === 'array'\n ? (state: any) => {\n return [state.meta, Object.keys(state.value || []).length]\n }\n : undefined,\n )\n // Instantiates field meta and removes it when unrendered\n useIsomorphicLayoutEffect(() => fieldApi.mount(), [fieldApi])\n\n return fieldApi as never\n}\n\ntype FieldComponentProps<\n TParentData,\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n> = {\n children: (fieldApi: FieldApi<TParentData, TName, TData>) => any\n} & (TParentData extends any[]\n ? {\n name?: TName\n index: number\n }\n : {\n name: TName\n index?: never\n }) &\n Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>\n\nexport type FieldComponent<TParentData> = <\n TName extends DeepKeys<TParentData>,\n TData = DeepValue<TParentData, TName>,\n>({\n children,\n ...fieldOptions\n}: FieldComponentProps<TParentData, TName, TData>) => any\n\nexport function Field<TParentData, TName extends DeepKeys<TParentData>>({\n children,\n ...fieldOptions\n}: {\n children: (fieldApi: FieldApi<TParentData, TName>) => any\n} & UseFieldOptions<TParentData, TName>) {\n const fieldApi = useField(fieldOptions as any)\n\n return (\n <formContext.Provider\n value={{ formApi: fieldApi.form, parentFieldName: fieldApi.name }}\n children={functionalUpdate(children, fieldApi as any)}\n />\n )\n}\n"],"mappings":";AAAA,OAAO,SAAS,gBAAgB;AAChC,SAAS,gBAAgB;AAEzB,SAAS,UAAU,wBAAwB;AAC3C,SAAS,gBAAgB,mBAAmB;AAC5C,OAAO,+BAA+B;AAkB/B,SAAS,SACd,MAOA;AAEA,QAAM,EAAE,SAAS,gBAAgB,IAAI,eAAe;AAEpD,QAAM,CAAC,QAAQ,IAAI,SAAS,MAAM;AAChC,UAAM,QACJ,OAAO,KAAK,UAAU,WAClB,CAAC,iBAAiB,KAAK,OAAO,KAAK,IAAI,IACvC,CAAC,iBAAiB,KAAK,IAAI,GAE9B,OAAO,CAAC,MAAM,MAAM,MAAS,EAC7B,KAAK,GAAG;AAEX,UAAM,MAAM,IAAI,SAAS;AAAA,MACvB,GAAG;AAAA,MACH,MAAM;AAAA,MACN;AAAA,IACF,CAAU;AAEV,QAAI,QAAQ;AAEZ,WAAO;AAAA,EACT,CAAC;AAMD,4BAA0B,MAAM;AAC9B,aAAS,OAAO,EAAE,GAAG,MAAM,MAAM,QAAQ,CAAU;AAAA,EACrD,CAAC;AAED;AAAA,IACE,SAAS;AAAA,IACT,KAAK,SAAS,UACV,CAAC,UAAe;AACd,aAAO,CAAC,MAAM,MAAM,OAAO,KAAK,MAAM,SAAS,CAAC,CAAC,EAAE,MAAM;AAAA,IAC3D,IACA;AAAA,EACN;AAEA,4BAA0B,MAAM,SAAS,MAAM,GAAG,CAAC,QAAQ,CAAC;AAE5D,SAAO;AACT;AA2BO,SAAS,MAAwD;AAAA,EACtE;AAAA,EACA,GAAG;AACL,GAEyC;AACvC,QAAM,WAAW,SAAS,YAAmB;AAE7C,SACE;AAAA,IAAC,YAAY;AAAA,IAAZ;AAAA,MACC,OAAO,EAAE,SAAS,SAAS,MAAM,iBAAiB,SAAS,KAAK;AAAA,MAChE,UAAU,iBAAiB,UAAU,QAAe;AAAA;AAAA,EACtD;AAEJ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-form",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"description": "Powerful, type-safe forms for React.",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"@tanstack/react-store": "0.1.3",
|
|
55
55
|
"@tanstack/store": "0.1.3",
|
|
56
56
|
"use-isomorphic-layout-effect": "^1.1.2",
|
|
57
|
-
"@tanstack/form-core": "0.3.
|
|
57
|
+
"@tanstack/form-core": "0.3.7"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"react": "^17.0.0 || ^18.0.0",
|
|
@@ -18,13 +18,17 @@ describe('useField', () => {
|
|
|
18
18
|
const formFactory = createFormFactory<Person>()
|
|
19
19
|
|
|
20
20
|
function Comp() {
|
|
21
|
-
const form = formFactory.useForm(
|
|
21
|
+
const form = formFactory.useForm({
|
|
22
|
+
defaultValues: {
|
|
23
|
+
firstName: 'FirstName',
|
|
24
|
+
lastName: 'LastName',
|
|
25
|
+
},
|
|
26
|
+
})
|
|
22
27
|
|
|
23
28
|
return (
|
|
24
29
|
<form.Provider>
|
|
25
30
|
<form.Field
|
|
26
31
|
name="firstName"
|
|
27
|
-
defaultValue="FirstName"
|
|
28
32
|
children={(field) => {
|
|
29
33
|
return (
|
|
30
34
|
<input
|
|
@@ -45,6 +49,47 @@ describe('useField', () => {
|
|
|
45
49
|
expect(input).toHaveValue('FirstName')
|
|
46
50
|
})
|
|
47
51
|
|
|
52
|
+
it('should use field default value first', async () => {
|
|
53
|
+
type Person = {
|
|
54
|
+
firstName: string
|
|
55
|
+
lastName: string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const formFactory = createFormFactory<Person>()
|
|
59
|
+
|
|
60
|
+
function Comp() {
|
|
61
|
+
const form = formFactory.useForm({
|
|
62
|
+
defaultValues: {
|
|
63
|
+
firstName: 'FirstName',
|
|
64
|
+
lastName: 'LastName',
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<form.Provider>
|
|
70
|
+
<form.Field
|
|
71
|
+
name="firstName"
|
|
72
|
+
defaultValue="otherName"
|
|
73
|
+
children={(field) => {
|
|
74
|
+
return (
|
|
75
|
+
<input
|
|
76
|
+
data-testid="fieldinput"
|
|
77
|
+
value={field.state.value}
|
|
78
|
+
onBlur={field.handleBlur}
|
|
79
|
+
onChange={(e) => field.handleChange(e.target.value)}
|
|
80
|
+
/>
|
|
81
|
+
)
|
|
82
|
+
}}
|
|
83
|
+
/>
|
|
84
|
+
</form.Provider>
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const { getByTestId } = render(<Comp />)
|
|
89
|
+
const input = getByTestId('fieldinput')
|
|
90
|
+
expect(input).toHaveValue('otherName')
|
|
91
|
+
})
|
|
92
|
+
|
|
48
93
|
it('should not validate on change if isTouched is false', async () => {
|
|
49
94
|
type Person = {
|
|
50
95
|
firstName: string
|
package/src/types.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { FieldOptions, DeepKeys } from '@tanstack/form-core'
|
|
1
|
+
import type { FieldOptions, DeepKeys, DeepValue } from '@tanstack/form-core'
|
|
2
2
|
|
|
3
3
|
export type UseFieldOptions<
|
|
4
|
-
TData,
|
|
5
4
|
TParentData,
|
|
6
5
|
TName extends DeepKeys<TParentData>,
|
|
7
|
-
|
|
6
|
+
TData = DeepValue<TParentData, TName>,
|
|
7
|
+
> = FieldOptions<TParentData, TName, TData> & {
|
|
8
8
|
mode?: 'value' | 'array'
|
|
9
9
|
}
|
package/src/useField.tsx
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import React, { useState } from 'react'
|
|
2
2
|
import { useStore } from '@tanstack/react-store'
|
|
3
|
-
import type {
|
|
4
|
-
DeepKeys,
|
|
5
|
-
DeepValue,
|
|
6
|
-
Narrow,
|
|
7
|
-
ResolveData,
|
|
8
|
-
} from '@tanstack/form-core'
|
|
3
|
+
import type { DeepKeys, DeepValue, Narrow } from '@tanstack/form-core'
|
|
9
4
|
import { FieldApi, functionalUpdate } from '@tanstack/form-core'
|
|
10
5
|
import { useFormContext, formContext } from './formContext'
|
|
11
6
|
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'
|
|
@@ -14,35 +9,21 @@ import type { UseFieldOptions } from './types'
|
|
|
14
9
|
declare module '@tanstack/form-core' {
|
|
15
10
|
// eslint-disable-next-line no-shadow
|
|
16
11
|
interface FieldApi<
|
|
17
|
-
TData,
|
|
18
12
|
TParentData,
|
|
19
13
|
TName extends DeepKeys<TParentData>,
|
|
20
|
-
|
|
21
|
-
TData,
|
|
22
|
-
TParentData,
|
|
23
|
-
TName
|
|
24
|
-
>,
|
|
14
|
+
TData = DeepValue<TParentData, TName>,
|
|
25
15
|
> {
|
|
26
16
|
Field: FieldComponent<TData>
|
|
27
17
|
}
|
|
28
18
|
}
|
|
29
19
|
|
|
30
20
|
export type UseField<TParentData> = <TName extends DeepKeys<TParentData>>(
|
|
31
|
-
opts?: { name: Narrow<TName> } & UseFieldOptions<
|
|
32
|
-
|
|
33
|
-
TParentData,
|
|
34
|
-
TName
|
|
35
|
-
>,
|
|
36
|
-
) => FieldApi<DeepValue<TParentData, TName>, TParentData, TName>
|
|
21
|
+
opts?: { name: Narrow<TName> } & UseFieldOptions<TParentData, TName>,
|
|
22
|
+
) => FieldApi<TParentData, TName, DeepValue<TParentData, TName>>
|
|
37
23
|
|
|
38
|
-
export function useField<
|
|
39
|
-
|
|
40
|
-
TParentData,
|
|
41
|
-
TName extends DeepKeys<TParentData>,
|
|
42
|
-
>(
|
|
43
|
-
opts: UseFieldOptions<TData, TParentData, TName>,
|
|
24
|
+
export function useField<TParentData, TName extends DeepKeys<TParentData>>(
|
|
25
|
+
opts: UseFieldOptions<TParentData, TName>,
|
|
44
26
|
): FieldApi<
|
|
45
|
-
TData,
|
|
46
27
|
TParentData,
|
|
47
28
|
TName
|
|
48
29
|
// Omit<typeof opts, 'onMount'> & {
|
|
@@ -95,14 +76,11 @@ export function useField<
|
|
|
95
76
|
}
|
|
96
77
|
|
|
97
78
|
type FieldComponentProps<
|
|
98
|
-
TData,
|
|
99
79
|
TParentData,
|
|
100
80
|
TName extends DeepKeys<TParentData>,
|
|
101
|
-
|
|
81
|
+
TData = DeepValue<TParentData, TName>,
|
|
102
82
|
> = {
|
|
103
|
-
children: (
|
|
104
|
-
fieldApi: FieldApi<TData, TParentData, TName, TResolvedData>,
|
|
105
|
-
) => any
|
|
83
|
+
children: (fieldApi: FieldApi<TParentData, TName, TData>) => any
|
|
106
84
|
} & (TParentData extends any[]
|
|
107
85
|
? {
|
|
108
86
|
name?: TName
|
|
@@ -112,27 +90,22 @@ type FieldComponentProps<
|
|
|
112
90
|
name: TName
|
|
113
91
|
index?: never
|
|
114
92
|
}) &
|
|
115
|
-
Omit<UseFieldOptions<
|
|
93
|
+
Omit<UseFieldOptions<TParentData, TName>, 'name' | 'index'>
|
|
116
94
|
|
|
117
95
|
export type FieldComponent<TParentData> = <
|
|
118
|
-
TData,
|
|
119
96
|
TName extends DeepKeys<TParentData>,
|
|
120
|
-
|
|
121
|
-
TData,
|
|
122
|
-
TParentData,
|
|
123
|
-
TName
|
|
124
|
-
>,
|
|
97
|
+
TData = DeepValue<TParentData, TName>,
|
|
125
98
|
>({
|
|
126
99
|
children,
|
|
127
100
|
...fieldOptions
|
|
128
|
-
}: FieldComponentProps<
|
|
101
|
+
}: FieldComponentProps<TParentData, TName, TData>) => any
|
|
129
102
|
|
|
130
|
-
export function Field<
|
|
103
|
+
export function Field<TParentData, TName extends DeepKeys<TParentData>>({
|
|
131
104
|
children,
|
|
132
105
|
...fieldOptions
|
|
133
106
|
}: {
|
|
134
|
-
children: (fieldApi: FieldApi<
|
|
135
|
-
} & UseFieldOptions<
|
|
107
|
+
children: (fieldApi: FieldApi<TParentData, TName>) => any
|
|
108
|
+
} & UseFieldOptions<TParentData, TName>) {
|
|
136
109
|
const fieldApi = useField(fieldOptions as any)
|
|
137
110
|
|
|
138
111
|
return (
|