@tanstack/react-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/types.d.cts +2 -2
- package/dist/cjs/useField.cjs +4 -17
- package/dist/cjs/useField.cjs.map +1 -1
- package/dist/cjs/useField.d.cts +4 -12
- package/dist/cjs/useForm.cjs +4 -6
- package/dist/cjs/useForm.cjs.map +1 -1
- package/dist/cjs/useForm.d.cts +3 -4
- package/dist/esm/createFormFactory.d.ts +3 -3
- package/dist/esm/createFormFactory.js.map +1 -1
- package/dist/esm/types.d.ts +2 -2
- package/dist/esm/useField.d.ts +4 -12
- package/dist/esm/useField.js +5 -18
- package/dist/esm/useField.js.map +1 -1
- package/dist/esm/useForm.d.ts +3 -4
- package/dist/esm/useForm.js +4 -6
- package/dist/esm/useForm.js.map +1 -1
- package/package.json +2 -2
- package/src/createFormFactory.ts +4 -4
- package/src/tests/createFormFactory.test.tsx +4 -5
- package/src/tests/useField.test-d.tsx +5 -5
- package/src/tests/useField.test.tsx +160 -86
- package/src/tests/useForm.test-d.tsx +1 -1
- package/src/tests/useForm.test.tsx +24 -25
- package/src/types.ts +8 -2
- package/src/useField.tsx +23 -53
- package/src/useForm.tsx +8 -13
- package/dist/cjs/formContext.cjs +0 -14
- package/dist/cjs/formContext.cjs.map +0 -1
- package/dist/cjs/formContext.d.cts +0 -10
- package/dist/esm/formContext.d.ts +0 -10
- package/dist/esm/formContext.js +0 -14
- package/dist/esm/formContext.js.map +0 -1
- package/src/formContext.ts +0 -17
package/src/useField.tsx
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import React, { useState } from 'rehackt'
|
|
2
2
|
import { useStore } from '@tanstack/react-store'
|
|
3
3
|
import { FieldApi, functionalUpdate } from '@tanstack/form-core'
|
|
4
|
-
import { formContext, useFormContext } from './formContext'
|
|
5
4
|
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
|
|
6
5
|
import type { UseFieldOptions } from './types'
|
|
7
6
|
import type {
|
|
@@ -28,20 +27,20 @@ declare module '@tanstack/form-core' {
|
|
|
28
27
|
}
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
export type UseField<
|
|
30
|
+
export type UseField<
|
|
31
|
+
TParentData,
|
|
32
|
+
TFormValidator extends
|
|
33
|
+
| Validator<TParentData, unknown>
|
|
34
|
+
| undefined = undefined,
|
|
35
|
+
> = <
|
|
32
36
|
TName extends DeepKeys<TParentData>,
|
|
33
37
|
TFieldValidator extends
|
|
34
38
|
| Validator<DeepValue<TParentData, TName>, unknown>
|
|
35
39
|
| undefined = undefined,
|
|
36
|
-
TFormValidator extends
|
|
37
|
-
| Validator<TParentData, unknown>
|
|
38
|
-
| undefined = undefined,
|
|
39
40
|
>(
|
|
40
|
-
opts
|
|
41
|
-
TParentData,
|
|
42
|
-
|
|
43
|
-
TFieldValidator,
|
|
44
|
-
TFormValidator
|
|
41
|
+
opts: Omit<
|
|
42
|
+
UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,
|
|
43
|
+
'form'
|
|
45
44
|
>,
|
|
46
45
|
) => FieldApi<
|
|
47
46
|
TParentData,
|
|
@@ -63,23 +62,11 @@ export function useField<
|
|
|
63
62
|
>(
|
|
64
63
|
opts: UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,
|
|
65
64
|
): FieldApi<TParentData, TName, TFieldValidator, TFormValidator> {
|
|
66
|
-
// Get the form API either manually or from context
|
|
67
|
-
const { formApi, parentFieldName } = useFormContext()
|
|
68
|
-
|
|
69
65
|
const [fieldApi] = useState(() => {
|
|
70
|
-
const name = (
|
|
71
|
-
typeof opts.index === 'number'
|
|
72
|
-
? [parentFieldName, opts.index, opts.name]
|
|
73
|
-
: [parentFieldName, opts.name]
|
|
74
|
-
)
|
|
75
|
-
.filter((d) => d !== undefined)
|
|
76
|
-
.join('.')
|
|
77
|
-
|
|
78
66
|
const api = new FieldApi({
|
|
79
67
|
...opts,
|
|
80
|
-
form:
|
|
81
|
-
|
|
82
|
-
name: name as typeof opts.name as never,
|
|
68
|
+
form: opts.form,
|
|
69
|
+
name: opts.name,
|
|
83
70
|
})
|
|
84
71
|
|
|
85
72
|
api.Field = Field as never
|
|
@@ -94,7 +81,7 @@ export function useField<
|
|
|
94
81
|
* that we need to keep updated every render with the most up-to-date information.
|
|
95
82
|
*/
|
|
96
83
|
useIsomorphicLayoutEffect(() => {
|
|
97
|
-
fieldApi.update(
|
|
84
|
+
fieldApi.update(opts)
|
|
98
85
|
})
|
|
99
86
|
|
|
100
87
|
useStore(
|
|
@@ -129,19 +116,7 @@ type FieldComponentProps<
|
|
|
129
116
|
TData
|
|
130
117
|
>,
|
|
131
118
|
) => any
|
|
132
|
-
} &
|
|
133
|
-
? {
|
|
134
|
-
name?: TName
|
|
135
|
-
index: number
|
|
136
|
-
}
|
|
137
|
-
: {
|
|
138
|
-
name: TName
|
|
139
|
-
index?: never
|
|
140
|
-
}) &
|
|
141
|
-
Omit<
|
|
142
|
-
UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>,
|
|
143
|
-
'name' | 'index'
|
|
144
|
-
>
|
|
119
|
+
} & UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>
|
|
145
120
|
|
|
146
121
|
export type FieldComponent<
|
|
147
122
|
TParentData,
|
|
@@ -157,12 +132,15 @@ export type FieldComponent<
|
|
|
157
132
|
>({
|
|
158
133
|
children,
|
|
159
134
|
...fieldOptions
|
|
160
|
-
}:
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
135
|
+
}: Omit<
|
|
136
|
+
FieldComponentProps<
|
|
137
|
+
TParentData,
|
|
138
|
+
TName,
|
|
139
|
+
TFieldValidator,
|
|
140
|
+
TFormValidator,
|
|
141
|
+
TData
|
|
142
|
+
>,
|
|
143
|
+
'form'
|
|
166
144
|
>) => any
|
|
167
145
|
|
|
168
146
|
export function Field<
|
|
@@ -184,13 +162,5 @@ export function Field<
|
|
|
184
162
|
} & UseFieldOptions<TParentData, TName, TFieldValidator, TFormValidator>) {
|
|
185
163
|
const fieldApi = useField(fieldOptions as any)
|
|
186
164
|
|
|
187
|
-
return (
|
|
188
|
-
<formContext.Provider
|
|
189
|
-
value={{
|
|
190
|
-
formApi: fieldApi.form as never,
|
|
191
|
-
parentFieldName: fieldApi.name,
|
|
192
|
-
}}
|
|
193
|
-
children={functionalUpdate(children, fieldApi as any)}
|
|
194
|
-
/>
|
|
195
|
-
)
|
|
165
|
+
return <>{functionalUpdate(children, fieldApi as any)}</>
|
|
196
166
|
}
|
package/src/useForm.tsx
CHANGED
|
@@ -5,19 +5,16 @@ import React, {
|
|
|
5
5
|
type ReactNode,
|
|
6
6
|
useState,
|
|
7
7
|
} from 'rehackt'
|
|
8
|
-
import { Field, useField } from './useField'
|
|
9
|
-
import { formContext } from './formContext'
|
|
8
|
+
import { Field, type FieldComponent, type UseField, useField } from './useField'
|
|
10
9
|
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
|
|
11
10
|
import type { NoInfer } from '@tanstack/react-store'
|
|
12
11
|
import type { FormOptions, FormState, Validator } from '@tanstack/form-core'
|
|
13
|
-
import type { FieldComponent, UseField } from './useField'
|
|
14
12
|
|
|
15
13
|
declare module '@tanstack/form-core' {
|
|
16
14
|
// eslint-disable-next-line no-shadow
|
|
17
15
|
interface FormApi<TFormData, TFormValidator> {
|
|
18
|
-
Provider: (props: PropsWithChildren) => JSX.Element
|
|
19
16
|
Field: FieldComponent<TFormData, TFormValidator>
|
|
20
|
-
useField: UseField<TFormData>
|
|
17
|
+
useField: UseField<TFormData, TFormValidator>
|
|
21
18
|
useStore: <TSelected = NoInfer<FormState<TFormData>>>(
|
|
22
19
|
selector?: (state: NoInfer<FormState<TFormData>>) => TSelected,
|
|
23
20
|
) => TSelected
|
|
@@ -47,15 +44,11 @@ export function useForm<
|
|
|
47
44
|
): FormApi<TFormData, TFormValidator> {
|
|
48
45
|
const [formApi] = useState(() => {
|
|
49
46
|
const api = new FormApi<TFormData, TFormValidator>(opts)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
useIsomorphicLayoutEffect(api.mount, [])
|
|
53
|
-
return (
|
|
54
|
-
<formContext.Provider {...props} value={{ formApi: api as never }} />
|
|
55
|
-
)
|
|
47
|
+
api.Field = function APIField(props) {
|
|
48
|
+
return <Field {...props} form={api} />
|
|
56
49
|
}
|
|
57
|
-
|
|
58
|
-
api.useField = useField
|
|
50
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
51
|
+
api.useField = (props) => useField({ ...props, form: api })
|
|
59
52
|
api.useStore = (
|
|
60
53
|
// @ts-ignore
|
|
61
54
|
selector,
|
|
@@ -77,6 +70,8 @@ export function useForm<
|
|
|
77
70
|
return api
|
|
78
71
|
})
|
|
79
72
|
|
|
73
|
+
useIsomorphicLayoutEffect(formApi.mount, [])
|
|
74
|
+
|
|
80
75
|
formApi.useStore((state) => state.isSubmitting)
|
|
81
76
|
|
|
82
77
|
/**
|
package/dist/cjs/formContext.cjs
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const rehackt = require("rehackt");
|
|
4
|
-
const formContext = rehackt.createContext(null);
|
|
5
|
-
function useFormContext() {
|
|
6
|
-
const formApi = rehackt.useContext(formContext);
|
|
7
|
-
if (!formApi) {
|
|
8
|
-
throw new Error(`You are trying to use the form API outside of a form!`);
|
|
9
|
-
}
|
|
10
|
-
return formApi;
|
|
11
|
-
}
|
|
12
|
-
exports.formContext = formContext;
|
|
13
|
-
exports.useFormContext = useFormContext;
|
|
14
|
-
//# sourceMappingURL=formContext.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formContext.cjs","sources":["../../src/formContext.ts"],"sourcesContent":["import { createContext, useContext } from 'rehackt'\nimport type { FormApi, Validator } from '@tanstack/form-core'\n\nexport const formContext = createContext<{\n formApi: FormApi<any, Validator<any, unknown> | undefined>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = useContext(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":["createContext","useContext"],"mappings":";;;AAGa,MAAA,cAAcA,sBAGjB,IAAK;AAER,SAAS,iBAAiB;AACzB,QAAA,UAAUC,mBAAW,WAAW;AAEtC,MAAI,CAAC,SAAS;AACN,UAAA,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEO,SAAA;AACT;;;"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
import type { FormApi, Validator } from '@tanstack/form-core';
|
|
3
|
-
export declare const formContext: import("react").Context<{
|
|
4
|
-
formApi: FormApi<any, Validator<any, unknown> | undefined>;
|
|
5
|
-
parentFieldName?: string | undefined;
|
|
6
|
-
} | null>;
|
|
7
|
-
export declare function useFormContext(): {
|
|
8
|
-
formApi: FormApi<any, Validator<any, unknown> | undefined>;
|
|
9
|
-
parentFieldName?: string | undefined;
|
|
10
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
import type { FormApi, Validator } from '@tanstack/form-core';
|
|
3
|
-
export declare const formContext: import("react").Context<{
|
|
4
|
-
formApi: FormApi<any, Validator<any, unknown> | undefined>;
|
|
5
|
-
parentFieldName?: string | undefined;
|
|
6
|
-
} | null>;
|
|
7
|
-
export declare function useFormContext(): {
|
|
8
|
-
formApi: FormApi<any, Validator<any, unknown> | undefined>;
|
|
9
|
-
parentFieldName?: string | undefined;
|
|
10
|
-
};
|
package/dist/esm/formContext.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { createContext, useContext } from "rehackt";
|
|
2
|
-
const formContext = createContext(null);
|
|
3
|
-
function useFormContext() {
|
|
4
|
-
const formApi = useContext(formContext);
|
|
5
|
-
if (!formApi) {
|
|
6
|
-
throw new Error(`You are trying to use the form API outside of a form!`);
|
|
7
|
-
}
|
|
8
|
-
return formApi;
|
|
9
|
-
}
|
|
10
|
-
export {
|
|
11
|
-
formContext,
|
|
12
|
-
useFormContext
|
|
13
|
-
};
|
|
14
|
-
//# sourceMappingURL=formContext.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"formContext.js","sources":["../../src/formContext.ts"],"sourcesContent":["import { createContext, useContext } from 'rehackt'\nimport type { FormApi, Validator } from '@tanstack/form-core'\n\nexport const formContext = createContext<{\n formApi: FormApi<any, Validator<any, unknown> | undefined>\n parentFieldName?: string\n} | null>(null!)\n\nexport function useFormContext() {\n const formApi = useContext(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":";AAGa,MAAA,cAAc,cAGjB,IAAK;AAER,SAAS,iBAAiB;AACzB,QAAA,UAAU,WAAW,WAAW;AAEtC,MAAI,CAAC,SAAS;AACN,UAAA,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEO,SAAA;AACT;"}
|
package/src/formContext.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { createContext, useContext } from 'rehackt'
|
|
2
|
-
import type { FormApi, Validator } from '@tanstack/form-core'
|
|
3
|
-
|
|
4
|
-
export const formContext = createContext<{
|
|
5
|
-
formApi: FormApi<any, Validator<any, unknown> | undefined>
|
|
6
|
-
parentFieldName?: string
|
|
7
|
-
} | null>(null!)
|
|
8
|
-
|
|
9
|
-
export function useFormContext() {
|
|
10
|
-
const formApi = useContext(formContext)
|
|
11
|
-
|
|
12
|
-
if (!formApi) {
|
|
13
|
-
throw new Error(`You are trying to use the form API outside of a form!`)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return formApi
|
|
17
|
-
}
|