@tanstack/react-form 0.42.1 → 0.43.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/nextjs/createServerValidate.cjs +18 -10
- package/dist/cjs/nextjs/createServerValidate.cjs.map +1 -1
- package/dist/cjs/nextjs/createServerValidate.d.cts +5 -9
- package/dist/cjs/nextjs/error.cjs.map +1 -1
- package/dist/cjs/nextjs/error.d.cts +6 -5
- package/dist/cjs/nextjs/types.d.cts +2 -2
- package/dist/cjs/remix/createServerValidate.cjs +18 -10
- package/dist/cjs/remix/createServerValidate.cjs.map +1 -1
- package/dist/cjs/remix/createServerValidate.d.cts +5 -9
- package/dist/cjs/remix/error.cjs.map +1 -1
- package/dist/cjs/remix/error.d.cts +6 -5
- package/dist/cjs/remix/types.d.cts +2 -2
- package/dist/cjs/start/createServerValidate.cjs +21 -12
- package/dist/cjs/start/createServerValidate.cjs.map +1 -1
- package/dist/cjs/start/createServerValidate.d.cts +4 -10
- package/dist/cjs/start/error.cjs.map +1 -1
- package/dist/cjs/start/error.d.cts +6 -5
- package/dist/cjs/start/getFormData.cjs +4 -5
- package/dist/cjs/start/getFormData.cjs.map +1 -1
- package/dist/cjs/start/getFormData.d.cts +2 -3
- package/dist/cjs/start/types.d.cts +2 -2
- package/dist/cjs/types.d.cts +2 -2
- package/dist/cjs/useField.cjs.map +1 -1
- package/dist/cjs/useField.d.cts +10 -10
- package/dist/cjs/useForm.cjs.map +1 -1
- package/dist/cjs/useForm.d.cts +7 -7
- package/dist/cjs/useTransform.cjs.map +1 -1
- package/dist/cjs/useTransform.d.cts +2 -2
- package/dist/esm/nextjs/createServerValidate.d.ts +5 -9
- package/dist/esm/nextjs/createServerValidate.js +18 -10
- package/dist/esm/nextjs/createServerValidate.js.map +1 -1
- package/dist/esm/nextjs/error.d.ts +6 -5
- package/dist/esm/nextjs/error.js.map +1 -1
- package/dist/esm/nextjs/types.d.ts +2 -2
- package/dist/esm/remix/createServerValidate.d.ts +5 -9
- package/dist/esm/remix/createServerValidate.js +18 -10
- package/dist/esm/remix/createServerValidate.js.map +1 -1
- package/dist/esm/remix/error.d.ts +6 -5
- package/dist/esm/remix/error.js.map +1 -1
- package/dist/esm/remix/types.d.ts +2 -2
- package/dist/esm/start/createServerValidate.d.ts +4 -10
- package/dist/esm/start/createServerValidate.js +21 -12
- package/dist/esm/start/createServerValidate.js.map +1 -1
- package/dist/esm/start/error.d.ts +6 -5
- package/dist/esm/start/error.js.map +1 -1
- package/dist/esm/start/getFormData.d.ts +2 -3
- package/dist/esm/start/getFormData.js +4 -5
- package/dist/esm/start/getFormData.js.map +1 -1
- package/dist/esm/start/types.d.ts +2 -2
- package/dist/esm/types.d.ts +2 -2
- package/dist/esm/useField.d.ts +10 -10
- package/dist/esm/useField.js.map +1 -1
- package/dist/esm/useForm.d.ts +7 -7
- package/dist/esm/useForm.js.map +1 -1
- package/dist/esm/useTransform.d.ts +2 -2
- package/dist/esm/useTransform.js.map +1 -1
- package/package.json +16 -11
- package/src/nextjs/createServerValidate.ts +71 -43
- package/src/nextjs/error.ts +13 -6
- package/src/nextjs/types.ts +16 -3
- package/src/remix/createServerValidate.ts +71 -43
- package/src/remix/error.ts +13 -6
- package/src/remix/types.ts +16 -3
- package/src/start/createServerValidate.tsx +73 -47
- package/src/start/error.ts +13 -6
- package/src/start/getFormData.tsx +6 -6
- package/src/start/types.ts +16 -3
- package/src/types.ts +42 -12
- package/src/useField.tsx +295 -58
- package/src/useForm.tsx +132 -14
- package/src/useTransform.ts +38 -4
|
@@ -1,83 +1,111 @@
|
|
|
1
1
|
import { decode } from 'decode-formdata'
|
|
2
|
+
import {
|
|
3
|
+
isGlobalFormValidationError,
|
|
4
|
+
isStandardSchemaValidator,
|
|
5
|
+
standardSchemaValidators,
|
|
6
|
+
} from '@tanstack/form-core'
|
|
2
7
|
import { ServerValidateError } from './error'
|
|
3
8
|
import type {
|
|
9
|
+
FormAsyncValidateOrFn,
|
|
4
10
|
FormOptions,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
FormValidateAsyncFn,
|
|
12
|
+
FormValidateOrFn,
|
|
13
|
+
UnwrapFormAsyncValidateOrFn,
|
|
8
14
|
} from '@tanstack/form-core'
|
|
9
15
|
import type { ServerFormState } from './types'
|
|
10
16
|
|
|
11
|
-
type OnServerValidateFn<TFormData> = (props: {
|
|
12
|
-
value: TFormData
|
|
13
|
-
}) => ValidationError | Promise<ValidationError>
|
|
14
|
-
|
|
15
|
-
type OnServerValidateOrFn<
|
|
16
|
-
TFormData,
|
|
17
|
-
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
18
|
-
> =
|
|
19
|
-
TFormValidator extends Validator<TFormData, infer FFN>
|
|
20
|
-
? FFN | OnServerValidateFn<TFormData>
|
|
21
|
-
: OnServerValidateFn<TFormData>
|
|
22
|
-
|
|
23
17
|
interface CreateServerValidateOptions<
|
|
24
18
|
TFormData,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
19
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
20
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
21
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
22
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
23
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
24
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
25
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
26
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
27
|
+
> extends FormOptions<
|
|
28
|
+
TFormData,
|
|
29
|
+
TOnMount,
|
|
30
|
+
TOnChange,
|
|
31
|
+
TOnChangeAsync,
|
|
32
|
+
TOnBlur,
|
|
33
|
+
TOnBlurAsync,
|
|
34
|
+
TOnSubmit,
|
|
35
|
+
TOnSubmitAsync,
|
|
36
|
+
TOnServer
|
|
37
|
+
> {
|
|
38
|
+
onServerValidate: TOnServer
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
export const createServerValidate =
|
|
37
42
|
<
|
|
38
43
|
TFormData,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
45
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
46
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
47
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
48
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
49
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
50
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
51
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
42
52
|
>(
|
|
43
|
-
defaultOpts: CreateServerValidateOptions<
|
|
53
|
+
defaultOpts: CreateServerValidateOptions<
|
|
54
|
+
TFormData,
|
|
55
|
+
TOnMount,
|
|
56
|
+
TOnChange,
|
|
57
|
+
TOnChangeAsync,
|
|
58
|
+
TOnBlur,
|
|
59
|
+
TOnBlurAsync,
|
|
60
|
+
TOnSubmit,
|
|
61
|
+
TOnSubmitAsync,
|
|
62
|
+
TOnServer
|
|
63
|
+
>,
|
|
44
64
|
) =>
|
|
45
65
|
async (formData: FormData, info?: Parameters<typeof decode>[1]) => {
|
|
46
|
-
const {
|
|
66
|
+
const { onServerValidate } = defaultOpts
|
|
47
67
|
|
|
48
|
-
const runValidator = async (
|
|
68
|
+
const runValidator = async ({
|
|
69
|
+
value,
|
|
70
|
+
validationSource,
|
|
71
|
+
}: {
|
|
49
72
|
value: TFormData
|
|
50
73
|
validationSource: 'form'
|
|
51
74
|
}) => {
|
|
52
|
-
if (
|
|
53
|
-
return
|
|
75
|
+
if (isStandardSchemaValidator(onServerValidate)) {
|
|
76
|
+
return await standardSchemaValidators.validateAsync(
|
|
77
|
+
{ value, validationSource },
|
|
78
|
+
onServerValidate,
|
|
79
|
+
)
|
|
54
80
|
}
|
|
55
|
-
|
|
56
|
-
|
|
81
|
+
return (onServerValidate as FormValidateAsyncFn<TFormData>)({
|
|
82
|
+
value,
|
|
83
|
+
signal: undefined as never,
|
|
84
|
+
formApi: undefined as never,
|
|
85
|
+
})
|
|
57
86
|
}
|
|
58
87
|
|
|
59
88
|
const values = decode(formData, info) as never as TFormData
|
|
60
89
|
|
|
61
|
-
const onServerError = await runValidator({
|
|
90
|
+
const onServerError = (await runValidator({
|
|
62
91
|
value: values,
|
|
63
92
|
validationSource: 'form',
|
|
64
|
-
})
|
|
93
|
+
})) as UnwrapFormAsyncValidateOrFn<TOnServer> | undefined
|
|
65
94
|
|
|
66
95
|
if (!onServerError) return
|
|
67
96
|
|
|
68
|
-
const
|
|
69
|
-
onServerError
|
|
70
|
-
typeof onServerError !== 'string' &&
|
|
71
|
-
isFormValidationError(onServerError)
|
|
97
|
+
const onServerErrorVal = (
|
|
98
|
+
isGlobalFormValidationError(onServerError)
|
|
72
99
|
? onServerError.form
|
|
73
100
|
: onServerError
|
|
101
|
+
) as UnwrapFormAsyncValidateOrFn<TOnServer>
|
|
74
102
|
|
|
75
|
-
const formState: ServerFormState<TFormData> = {
|
|
103
|
+
const formState: ServerFormState<TFormData, TOnServer> = {
|
|
76
104
|
errorMap: {
|
|
77
105
|
onServer: onServerError,
|
|
78
106
|
},
|
|
79
107
|
values,
|
|
80
|
-
errors:
|
|
108
|
+
errors: onServerErrorVal ? [onServerErrorVal] : [],
|
|
81
109
|
}
|
|
82
110
|
|
|
83
111
|
throw new ServerValidateError({
|
|
@@ -85,7 +113,7 @@ export const createServerValidate =
|
|
|
85
113
|
})
|
|
86
114
|
}
|
|
87
115
|
|
|
88
|
-
export const initialFormState: ServerFormState<any> = {
|
|
116
|
+
export const initialFormState: ServerFormState<any, undefined> = {
|
|
89
117
|
errorMap: {
|
|
90
118
|
onServer: undefined,
|
|
91
119
|
},
|
package/src/remix/error.ts
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
import type { ServerFormState } from './types'
|
|
2
|
+
import type { FormAsyncValidateOrFn } from '@tanstack/form-core'
|
|
2
3
|
|
|
3
|
-
interface ServerValidateErrorState<
|
|
4
|
-
|
|
4
|
+
interface ServerValidateErrorState<
|
|
5
|
+
TFormData,
|
|
6
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
7
|
+
> {
|
|
8
|
+
formState: ServerFormState<TFormData, TOnServer>
|
|
5
9
|
}
|
|
6
10
|
|
|
7
|
-
export class ServerValidateError<
|
|
11
|
+
export class ServerValidateError<
|
|
12
|
+
TFormData,
|
|
13
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
14
|
+
>
|
|
8
15
|
extends Error
|
|
9
|
-
implements ServerValidateErrorState<TFormData>
|
|
16
|
+
implements ServerValidateErrorState<TFormData, TOnServer>
|
|
10
17
|
{
|
|
11
|
-
formState: ServerFormState<TFormData>
|
|
18
|
+
formState: ServerFormState<TFormData, TOnServer>
|
|
12
19
|
|
|
13
|
-
constructor(options: ServerValidateErrorState<TFormData>) {
|
|
20
|
+
constructor(options: ServerValidateErrorState<TFormData, TOnServer>) {
|
|
14
21
|
super('Your form has errors. Please check the fields and try again.')
|
|
15
22
|
this.name = 'ServerValidateError'
|
|
16
23
|
this.formState = options.formState
|
package/src/remix/types.ts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
|
-
import type { FormState } from '@tanstack/form-core'
|
|
1
|
+
import type { FormAsyncValidateOrFn, FormState } from '@tanstack/form-core'
|
|
2
2
|
|
|
3
|
-
export type ServerFormState<
|
|
4
|
-
|
|
3
|
+
export type ServerFormState<
|
|
4
|
+
TFormData,
|
|
5
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
6
|
+
> = Pick<
|
|
7
|
+
FormState<
|
|
8
|
+
TFormData,
|
|
9
|
+
undefined,
|
|
10
|
+
undefined,
|
|
11
|
+
undefined,
|
|
12
|
+
undefined,
|
|
13
|
+
undefined,
|
|
14
|
+
undefined,
|
|
15
|
+
undefined,
|
|
16
|
+
TOnServer
|
|
17
|
+
>,
|
|
5
18
|
'values' | 'errors' | 'errorMap'
|
|
6
19
|
>
|
|
@@ -1,89 +1,115 @@
|
|
|
1
1
|
import { decode } from 'decode-formdata'
|
|
2
|
+
import {
|
|
3
|
+
isGlobalFormValidationError,
|
|
4
|
+
isStandardSchemaValidator,
|
|
5
|
+
standardSchemaValidators,
|
|
6
|
+
} from '@tanstack/form-core'
|
|
7
|
+
import { getHeader } from 'vinxi/http'
|
|
2
8
|
import { _tanstackInternalsCookie } from './utils'
|
|
3
9
|
import { ServerValidateError } from './error'
|
|
4
10
|
import type {
|
|
11
|
+
FormAsyncValidateOrFn,
|
|
5
12
|
FormOptions,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
FormValidateAsyncFn,
|
|
14
|
+
FormValidateOrFn,
|
|
15
|
+
UnwrapFormAsyncValidateOrFn,
|
|
9
16
|
} from '@tanstack/form-core'
|
|
10
|
-
import type { FetchFn } from '@tanstack/start'
|
|
11
17
|
import type { ServerFormState } from './types'
|
|
12
18
|
|
|
13
|
-
type Ctx = Parameters<FetchFn<FormData, unknown>>[1]
|
|
14
|
-
|
|
15
|
-
type OnServerValidateFn<TFormData> = (props: {
|
|
16
|
-
value: TFormData
|
|
17
|
-
}) => ValidationError | Promise<ValidationError>
|
|
18
|
-
|
|
19
|
-
type OnServerValidateOrFn<
|
|
20
|
-
TFormData,
|
|
21
|
-
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
|
|
22
|
-
> =
|
|
23
|
-
TFormValidator extends Validator<TFormData, infer FFN>
|
|
24
|
-
? FFN | OnServerValidateFn<TFormData>
|
|
25
|
-
: OnServerValidateFn<TFormData>
|
|
26
|
-
|
|
27
19
|
interface CreateServerValidateOptions<
|
|
28
20
|
TFormData,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
21
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
22
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
23
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
24
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
25
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
26
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
27
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
28
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
29
|
+
> extends FormOptions<
|
|
30
|
+
TFormData,
|
|
31
|
+
TOnMount,
|
|
32
|
+
TOnChange,
|
|
33
|
+
TOnChangeAsync,
|
|
34
|
+
TOnBlur,
|
|
35
|
+
TOnBlurAsync,
|
|
36
|
+
TOnSubmit,
|
|
37
|
+
TOnSubmitAsync,
|
|
38
|
+
TOnServer
|
|
39
|
+
> {
|
|
40
|
+
onServerValidate: TOnServer
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
export const createServerValidate =
|
|
41
44
|
<
|
|
42
45
|
TFormData,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
TOnMount extends undefined | FormValidateOrFn<TFormData>,
|
|
47
|
+
TOnChange extends undefined | FormValidateOrFn<TFormData>,
|
|
48
|
+
TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
49
|
+
TOnBlur extends undefined | FormValidateOrFn<TFormData>,
|
|
50
|
+
TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
51
|
+
TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
|
|
52
|
+
TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
53
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
46
54
|
>(
|
|
47
|
-
defaultOpts: CreateServerValidateOptions<
|
|
55
|
+
defaultOpts: CreateServerValidateOptions<
|
|
56
|
+
TFormData,
|
|
57
|
+
TOnMount,
|
|
58
|
+
TOnChange,
|
|
59
|
+
TOnChangeAsync,
|
|
60
|
+
TOnBlur,
|
|
61
|
+
TOnBlurAsync,
|
|
62
|
+
TOnSubmit,
|
|
63
|
+
TOnSubmitAsync,
|
|
64
|
+
TOnServer
|
|
65
|
+
>,
|
|
48
66
|
) =>
|
|
49
|
-
async (
|
|
50
|
-
const {
|
|
67
|
+
async (formData: FormData, info?: Parameters<typeof decode>[1]) => {
|
|
68
|
+
const { onServerValidate } = defaultOpts
|
|
51
69
|
|
|
52
|
-
const runValidator = async (
|
|
70
|
+
const runValidator = async ({
|
|
71
|
+
value,
|
|
72
|
+
validationSource,
|
|
73
|
+
}: {
|
|
53
74
|
value: TFormData
|
|
54
75
|
validationSource: 'form'
|
|
55
76
|
}) => {
|
|
56
|
-
if (
|
|
57
|
-
return
|
|
77
|
+
if (isStandardSchemaValidator(onServerValidate)) {
|
|
78
|
+
return await standardSchemaValidators.validateAsync(
|
|
79
|
+
{ value, validationSource },
|
|
80
|
+
onServerValidate,
|
|
81
|
+
)
|
|
58
82
|
}
|
|
59
|
-
|
|
60
|
-
|
|
83
|
+
return (onServerValidate as FormValidateAsyncFn<TFormData>)({
|
|
84
|
+
value,
|
|
85
|
+
signal: undefined as never,
|
|
86
|
+
formApi: undefined as never,
|
|
87
|
+
})
|
|
61
88
|
}
|
|
62
89
|
|
|
63
|
-
const referer =
|
|
90
|
+
const referer = getHeader('referer')!
|
|
64
91
|
|
|
65
92
|
const data = decode(formData, info) as never as TFormData
|
|
66
93
|
|
|
67
|
-
const onServerError = await runValidator({
|
|
94
|
+
const onServerError = (await runValidator({
|
|
68
95
|
value: data,
|
|
69
96
|
validationSource: 'form',
|
|
70
|
-
})
|
|
97
|
+
})) as UnwrapFormAsyncValidateOrFn<TOnServer> | undefined
|
|
71
98
|
|
|
72
99
|
if (!onServerError) return
|
|
73
100
|
|
|
74
|
-
const
|
|
75
|
-
onServerError
|
|
76
|
-
typeof onServerError !== 'string' &&
|
|
77
|
-
isFormValidationError(onServerError)
|
|
101
|
+
const onServerErrorVal = (
|
|
102
|
+
isGlobalFormValidationError(onServerError)
|
|
78
103
|
? onServerError.form
|
|
79
104
|
: onServerError
|
|
105
|
+
) as UnwrapFormAsyncValidateOrFn<TOnServer>
|
|
80
106
|
|
|
81
|
-
const formState: ServerFormState<TFormData> = {
|
|
107
|
+
const formState: ServerFormState<TFormData, TOnServer> = {
|
|
82
108
|
errorMap: {
|
|
83
109
|
onServer: onServerError,
|
|
84
110
|
},
|
|
85
111
|
values: data,
|
|
86
|
-
errors:
|
|
112
|
+
errors: onServerErrorVal ? [onServerErrorVal] : [],
|
|
87
113
|
}
|
|
88
114
|
|
|
89
115
|
const cookie = await _tanstackInternalsCookie.serialize(formState)
|
package/src/start/error.ts
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
1
|
import type { ServerFormState } from './types'
|
|
2
|
+
import type { FormAsyncValidateOrFn } from '@tanstack/form-core'
|
|
2
3
|
|
|
3
|
-
interface ServerValidateErrorState<
|
|
4
|
-
|
|
4
|
+
interface ServerValidateErrorState<
|
|
5
|
+
TFormData,
|
|
6
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
7
|
+
> {
|
|
8
|
+
formState: ServerFormState<TFormData, TOnServer>
|
|
5
9
|
response: Response
|
|
6
10
|
}
|
|
7
11
|
|
|
8
|
-
export class ServerValidateError<
|
|
12
|
+
export class ServerValidateError<
|
|
13
|
+
TFormData,
|
|
14
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
15
|
+
>
|
|
9
16
|
extends Error
|
|
10
|
-
implements ServerValidateErrorState<TFormData>
|
|
17
|
+
implements ServerValidateErrorState<TFormData, TOnServer>
|
|
11
18
|
{
|
|
19
|
+
formState: ServerFormState<TFormData, TOnServer>
|
|
12
20
|
response: Response
|
|
13
|
-
formState: ServerFormState<TFormData>
|
|
14
21
|
|
|
15
|
-
constructor(options: ServerValidateErrorState<TFormData>) {
|
|
22
|
+
constructor(options: ServerValidateErrorState<TFormData, TOnServer>) {
|
|
16
23
|
super('Your form has errors. Please check the fields and try again.')
|
|
17
24
|
this.name = 'ServerValidateError'
|
|
18
25
|
this.response = options.response
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { getHeader, removeResponseHeader } from 'vinxi/http'
|
|
1
2
|
import { _tanstackInternalsCookie } from './utils'
|
|
2
|
-
import type { FetchFnCtx } from '@tanstack/start'
|
|
3
3
|
import type { ServerFormState } from './types'
|
|
4
4
|
|
|
5
5
|
export const initialFormState = {
|
|
@@ -9,12 +9,12 @@ export const initialFormState = {
|
|
|
9
9
|
errors: [],
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export const getFormData = async (
|
|
13
|
-
const data = (await _tanstackInternalsCookie.parse(
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
export const getFormData = async () => {
|
|
13
|
+
const data = (await _tanstackInternalsCookie.parse(getHeader('Cookie')!)) as
|
|
14
|
+
| undefined
|
|
15
|
+
| ServerFormState<any, undefined>
|
|
16
16
|
// Delete the cookie before it hits the client again¸
|
|
17
|
-
|
|
17
|
+
removeResponseHeader('Cookie')
|
|
18
18
|
if (!data) return initialFormState
|
|
19
19
|
return data
|
|
20
20
|
}
|
package/src/start/types.ts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
|
-
import type { FormState } from '@tanstack/form-core'
|
|
1
|
+
import type { FormAsyncValidateOrFn, FormState } from '@tanstack/form-core'
|
|
2
2
|
|
|
3
|
-
export type ServerFormState<
|
|
4
|
-
|
|
3
|
+
export type ServerFormState<
|
|
4
|
+
TFormData,
|
|
5
|
+
TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
|
|
6
|
+
> = Pick<
|
|
7
|
+
FormState<
|
|
8
|
+
TFormData,
|
|
9
|
+
undefined,
|
|
10
|
+
undefined,
|
|
11
|
+
undefined,
|
|
12
|
+
undefined,
|
|
13
|
+
undefined,
|
|
14
|
+
undefined,
|
|
15
|
+
undefined,
|
|
16
|
+
TOnServer
|
|
17
|
+
>,
|
|
5
18
|
'values' | 'errors' | 'errorMap'
|
|
6
19
|
>
|
package/src/types.ts
CHANGED
|
@@ -2,9 +2,11 @@ import type {
|
|
|
2
2
|
DeepKeys,
|
|
3
3
|
DeepValue,
|
|
4
4
|
FieldApiOptions,
|
|
5
|
-
|
|
5
|
+
FieldAsyncValidateOrFn,
|
|
6
|
+
FieldValidateOrFn,
|
|
7
|
+
FormAsyncValidateOrFn,
|
|
8
|
+
FormValidateOrFn,
|
|
6
9
|
} from '@tanstack/form-core'
|
|
7
|
-
import type { FunctionComponent } from 'react'
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
12
|
* The field options.
|
|
@@ -12,19 +14,47 @@ import type { FunctionComponent } from 'react'
|
|
|
12
14
|
export type UseFieldOptions<
|
|
13
15
|
TParentData,
|
|
14
16
|
TName extends DeepKeys<TParentData>,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
|
20
|
-
|
|
|
21
|
-
|
|
17
|
+
TData extends DeepValue<TParentData, TName>,
|
|
18
|
+
TOnMount extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
19
|
+
TOnChange extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
20
|
+
TOnChangeAsync extends
|
|
21
|
+
| undefined
|
|
22
|
+
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
23
|
+
TOnBlur extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
24
|
+
TOnBlurAsync extends
|
|
25
|
+
| undefined
|
|
26
|
+
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
27
|
+
TOnSubmit extends undefined | FieldValidateOrFn<TParentData, TName, TData>,
|
|
28
|
+
TOnSubmitAsync extends
|
|
29
|
+
| undefined
|
|
30
|
+
| FieldAsyncValidateOrFn<TParentData, TName, TData>,
|
|
31
|
+
TFormOnMount extends undefined | FormValidateOrFn<TParentData>,
|
|
32
|
+
TFormOnChange extends undefined | FormValidateOrFn<TParentData>,
|
|
33
|
+
TFormOnChangeAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
34
|
+
TFormOnBlur extends undefined | FormValidateOrFn<TParentData>,
|
|
35
|
+
TFormOnBlurAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
36
|
+
TFormOnSubmit extends undefined | FormValidateOrFn<TParentData>,
|
|
37
|
+
TFormOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
38
|
+
TFormOnServer extends undefined | FormAsyncValidateOrFn<TParentData>,
|
|
22
39
|
> = FieldApiOptions<
|
|
23
40
|
TParentData,
|
|
24
41
|
TName,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
42
|
+
TData,
|
|
43
|
+
TOnMount,
|
|
44
|
+
TOnChange,
|
|
45
|
+
TOnChangeAsync,
|
|
46
|
+
TOnBlur,
|
|
47
|
+
TOnBlurAsync,
|
|
48
|
+
TOnSubmit,
|
|
49
|
+
TOnSubmitAsync,
|
|
50
|
+
TFormOnMount,
|
|
51
|
+
TFormOnChange,
|
|
52
|
+
TFormOnChangeAsync,
|
|
53
|
+
TFormOnBlur,
|
|
54
|
+
TFormOnBlurAsync,
|
|
55
|
+
TFormOnSubmit,
|
|
56
|
+
TFormOnSubmitAsync,
|
|
57
|
+
TFormOnServer
|
|
28
58
|
> & {
|
|
29
59
|
mode?: 'value' | 'array'
|
|
30
60
|
}
|