@jcoreio/zod-forms 1.2.1 → 2.0.0-beta.2
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/FieldPath.d.ts +2 -2
- package/FieldPath.d.ts.map +1 -1
- package/FieldPath.js.map +1 -1
- package/FieldPath.mjs.map +1 -1
- package/FormAction.d.ts +2 -2
- package/FormAction.d.ts.map +1 -1
- package/FormAction.js.map +1 -1
- package/FormAction.mjs.map +1 -1
- package/FormContext.d.ts +7 -6
- package/FormContext.d.ts.map +1 -1
- package/FormContext.js.map +1 -1
- package/FormContext.mjs.map +1 -1
- package/FormState.d.ts +10 -8
- package/FormState.d.ts.map +1 -1
- package/FormState.js.map +1 -1
- package/FormState.mjs.map +1 -1
- package/README.md +2 -2
- package/actions/arrayActions.d.ts +40 -39
- package/actions/arrayActions.d.ts.map +1 -1
- package/actions/arrayActions.js +37 -37
- package/actions/arrayActions.js.map +1 -1
- package/actions/arrayActions.mjs +29 -29
- package/actions/arrayActions.mjs.map +1 -1
- package/actions/initialize.d.ts +5 -4
- package/actions/initialize.d.ts.map +1 -1
- package/actions/initialize.js.map +1 -1
- package/actions/initialize.mjs.map +1 -1
- package/actions/setParsedValue.d.ts +12 -0
- package/actions/setParsedValue.d.ts.map +1 -0
- package/{reducers/setRawValue.js → actions/setParsedValue.js} +9 -26
- package/actions/setParsedValue.js.map +1 -0
- package/actions/setParsedValue.mjs +9 -0
- package/actions/setParsedValue.mjs.map +1 -0
- package/actions/setSubmitStatus.d.ts +3 -3
- package/actions/setSubmitStatus.d.ts.map +1 -1
- package/actions/setSubmitStatus.js.map +1 -1
- package/actions/setSubmitStatus.mjs.map +1 -1
- package/actions/setValue.d.ts +3 -5
- package/actions/setValue.d.ts.map +1 -1
- package/actions/setValue.js +3 -7
- package/actions/setValue.js.map +1 -1
- package/actions/setValue.mjs +2 -3
- package/actions/setValue.mjs.map +1 -1
- package/createFormMiddleware.d.ts.map +1 -1
- package/createFormMiddleware.js +7 -5
- package/createFormMiddleware.js.map +1 -1
- package/createFormMiddleware.mjs +7 -5
- package/createFormMiddleware.mjs.map +1 -1
- package/createFormProvider.d.ts.map +1 -1
- package/createFormProvider.js +10 -10
- package/createFormProvider.js.map +1 -1
- package/createFormProvider.mjs +7 -7
- package/createFormProvider.mjs.map +1 -1
- package/createFormReducer.js +17 -17
- package/createFormReducer.js.map +1 -1
- package/createFormReducer.mjs +17 -17
- package/createFormReducer.mjs.map +1 -1
- package/createSelectFormStatus.d.ts +1 -1
- package/createSelectFormStatus.d.ts.map +1 -1
- package/createSelectFormStatus.js +6 -6
- package/createSelectFormStatus.js.map +1 -1
- package/createSelectFormStatus.mjs +1 -1
- package/createSelectFormStatus.mjs.map +1 -1
- package/createSelectFormValues.d.ts +19 -19
- package/createSelectFormValues.d.ts.map +1 -1
- package/createSelectFormValues.js +5 -5
- package/createSelectFormValues.js.map +1 -1
- package/createSelectFormValues.mjs +3 -3
- package/createSelectFormValues.mjs.map +1 -1
- package/createZodForm.d.ts +4 -4
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js.map +1 -1
- package/index.mjs.map +1 -1
- package/initFormState.js +1 -1
- package/initFormState.js.map +1 -1
- package/initFormState.mjs +1 -1
- package/initFormState.mjs.map +1 -1
- package/package.json +4 -1
- package/reducers/addHandlers.d.ts +6 -6
- package/reducers/arrayInsert.d.ts.map +1 -1
- package/reducers/arrayInsert.js +10 -4
- package/reducers/arrayInsert.js.map +1 -1
- package/reducers/arrayInsert.mjs +6 -4
- package/reducers/arrayInsert.mjs.map +1 -1
- package/reducers/arrayInsertParsed.d.ts +7 -0
- package/reducers/arrayInsertParsed.d.ts.map +1 -0
- package/reducers/arrayInsertParsed.js +16 -0
- package/reducers/arrayInsertParsed.js.map +1 -0
- package/reducers/arrayInsertParsed.mjs +12 -0
- package/reducers/arrayInsertParsed.mjs.map +1 -0
- package/reducers/arrayPush.d.ts.map +1 -1
- package/reducers/arrayPush.js +6 -4
- package/reducers/arrayPush.js.map +1 -1
- package/reducers/arrayPush.mjs +2 -4
- package/reducers/arrayPush.mjs.map +1 -1
- package/reducers/arrayPushParsed.d.ts +7 -0
- package/reducers/{arrayUnshiftRaw.d.ts.map → arrayPushParsed.d.ts.map} +1 -1
- package/reducers/arrayPushParsed.js +15 -0
- package/reducers/arrayPushParsed.js.map +1 -0
- package/reducers/arrayPushParsed.mjs +11 -0
- package/reducers/arrayPushParsed.mjs.map +1 -0
- package/reducers/arraySplice.d.ts.map +1 -1
- package/reducers/arraySplice.js +12 -6
- package/reducers/arraySplice.js.map +1 -1
- package/reducers/arraySplice.mjs +9 -5
- package/reducers/arraySplice.mjs.map +1 -1
- package/reducers/arraySpliceParsed.d.ts +7 -0
- package/reducers/arraySpliceParsed.d.ts.map +1 -0
- package/reducers/arraySpliceParsed.js +20 -0
- package/reducers/arraySpliceParsed.js.map +1 -0
- package/reducers/arraySpliceParsed.mjs +14 -0
- package/reducers/arraySpliceParsed.mjs.map +1 -0
- package/reducers/arrayUnshift.d.ts.map +1 -1
- package/reducers/arrayUnshift.js +6 -4
- package/reducers/arrayUnshift.js.map +1 -1
- package/reducers/arrayUnshift.mjs +2 -4
- package/reducers/arrayUnshift.mjs.map +1 -1
- package/reducers/arrayUnshiftParsed.d.ts +7 -0
- package/reducers/arrayUnshiftParsed.d.ts.map +1 -0
- package/reducers/arrayUnshiftParsed.js +15 -0
- package/reducers/arrayUnshiftParsed.js.map +1 -0
- package/reducers/arrayUnshiftParsed.mjs +11 -0
- package/reducers/arrayUnshiftParsed.mjs.map +1 -0
- package/reducers/initialize.d.ts +12 -12
- package/reducers/initialize.d.ts.map +1 -1
- package/reducers/initialize.js +9 -9
- package/reducers/initialize.js.map +1 -1
- package/reducers/initialize.mjs +8 -8
- package/reducers/initialize.mjs.map +1 -1
- package/reducers/removeHandlers.d.ts +6 -6
- package/reducers/setParsedValue.d.ts +47 -0
- package/reducers/setParsedValue.d.ts.map +1 -0
- package/reducers/setParsedValue.js +41 -0
- package/reducers/setParsedValue.js.map +1 -0
- package/reducers/setParsedValue.mjs +32 -0
- package/reducers/setParsedValue.mjs.map +1 -0
- package/reducers/setSubmitStatus.d.ts +6 -6
- package/reducers/setValue.d.ts +9 -10
- package/reducers/setValue.d.ts.map +1 -1
- package/reducers/setValue.js +9 -15
- package/reducers/setValue.js.map +1 -1
- package/reducers/setValue.mjs +9 -15
- package/reducers/setValue.mjs.map +1 -1
- package/reducers/submitSucceeded.d.ts +6 -6
- package/reducers/submitSucceeded.js +2 -2
- package/reducers/submitSucceeded.js.map +1 -1
- package/reducers/submitSucceeded.mjs +2 -2
- package/reducers/submitSucceeded.mjs.map +1 -1
- package/reducers/util/updateRawArray.d.ts +2 -2
- package/reducers/util/updateRawArray.d.ts.map +1 -1
- package/reducers/util/updateRawArray.js +3 -3
- package/reducers/util/updateRawArray.js.map +1 -1
- package/reducers/util/updateRawArray.mjs +3 -3
- package/reducers/util/updateRawArray.mjs.map +1 -1
- package/src/FieldPath.ts +3 -5
- package/src/FormAction.ts +2 -2
- package/src/FormContext.ts +7 -6
- package/src/FormState.ts +12 -8
- package/src/actions/arrayActions.ts +66 -58
- package/src/actions/initialize.ts +3 -2
- package/src/actions/setParsedValue.ts +21 -0
- package/src/actions/setSubmitStatus.ts +1 -1
- package/src/actions/setValue.ts +2 -5
- package/src/createFormMiddleware.ts +5 -3
- package/src/createFormProvider.tsx +12 -9
- package/src/createFormReducer.ts +17 -17
- package/src/createSelectFormStatus.ts +5 -5
- package/src/createSelectFormValues.ts +2 -2
- package/src/index.ts +1 -1
- package/src/initFormState.ts +1 -1
- package/src/reducers/arrayInsert.ts +9 -6
- package/src/reducers/arrayInsertParsed.ts +19 -0
- package/src/reducers/arrayPush.ts +5 -7
- package/src/reducers/arrayPushParsed.ts +23 -0
- package/src/reducers/arraySplice.ts +18 -5
- package/src/reducers/arraySpliceParsed.ts +17 -0
- package/src/reducers/arrayUnshift.ts +6 -4
- package/src/reducers/arrayUnshiftParsed.ts +16 -0
- package/src/reducers/initialize.ts +10 -8
- package/src/reducers/setParsedValue.ts +61 -0
- package/src/reducers/setValue.ts +12 -21
- package/src/reducers/submitSucceeded.ts +1 -1
- package/src/reducers/util/updateRawArray.ts +5 -5
- package/src/useArrayField.ts +13 -12
- package/src/useField.ts +26 -24
- package/src/useHtmlField.ts +42 -42
- package/src/useInitialize.ts +1 -1
- package/src/util/maybeParse.ts +2 -2
- package/useArrayField.d.ts +4 -4
- package/useArrayField.d.ts.map +1 -1
- package/useArrayField.js +8 -13
- package/useArrayField.js.map +1 -1
- package/useArrayField.mjs +3 -7
- package/useArrayField.mjs.map +1 -1
- package/useField.d.ts +7 -6
- package/useField.d.ts.map +1 -1
- package/useField.js +24 -24
- package/useField.js.map +1 -1
- package/useField.mjs +18 -18
- package/useField.mjs.map +1 -1
- package/useFormValues.d.ts +4 -4
- package/useHtmlField.js +40 -40
- package/useHtmlField.js.map +1 -1
- package/useHtmlField.mjs +36 -36
- package/useHtmlField.mjs.map +1 -1
- package/useInitialize.d.ts.map +1 -1
- package/useInitialize.js +1 -1
- package/useInitialize.js.map +1 -1
- package/useInitialize.mjs +1 -1
- package/useInitialize.mjs.map +1 -1
- package/util/maybeParse.d.ts +1 -1
- package/util/maybeParse.d.ts.map +1 -1
- package/util/maybeParse.js +2 -2
- package/util/maybeParse.js.map +1 -1
- package/util/maybeParse.mjs +2 -2
- package/util/maybeParse.mjs.map +1 -1
- package/actions/setRawValue.d.ts +0 -9
- package/actions/setRawValue.d.ts.map +0 -1
- package/actions/setRawValue.js +0 -14
- package/actions/setRawValue.js.map +0 -1
- package/actions/setRawValue.mjs +0 -8
- package/actions/setRawValue.mjs.map +0 -1
- package/reducers/arrayInsertRaw.d.ts +0 -7
- package/reducers/arrayInsertRaw.d.ts.map +0 -1
- package/reducers/arrayInsertRaw.js +0 -22
- package/reducers/arrayInsertRaw.js.map +0 -1
- package/reducers/arrayInsertRaw.mjs +0 -14
- package/reducers/arrayInsertRaw.mjs.map +0 -1
- package/reducers/arrayPushRaw.d.ts +0 -7
- package/reducers/arrayPushRaw.d.ts.map +0 -1
- package/reducers/arrayPushRaw.js +0 -17
- package/reducers/arrayPushRaw.js.map +0 -1
- package/reducers/arrayPushRaw.mjs +0 -9
- package/reducers/arrayPushRaw.mjs.map +0 -1
- package/reducers/arraySpliceRaw.d.ts +0 -7
- package/reducers/arraySpliceRaw.d.ts.map +0 -1
- package/reducers/arraySpliceRaw.js +0 -26
- package/reducers/arraySpliceRaw.js.map +0 -1
- package/reducers/arraySpliceRaw.mjs +0 -18
- package/reducers/arraySpliceRaw.mjs.map +0 -1
- package/reducers/arrayUnshiftRaw.d.ts +0 -7
- package/reducers/arrayUnshiftRaw.js +0 -17
- package/reducers/arrayUnshiftRaw.js.map +0 -1
- package/reducers/arrayUnshiftRaw.mjs +0 -9
- package/reducers/arrayUnshiftRaw.mjs.map +0 -1
- package/reducers/setRawValue.d.ts +0 -27
- package/reducers/setRawValue.d.ts.map +0 -1
- package/reducers/setRawValue.js.map +0 -1
- package/reducers/setRawValue.mjs +0 -26
- package/reducers/setRawValue.mjs.map +0 -1
- package/src/actions/setRawValue.ts +0 -17
- package/src/reducers/arrayInsertRaw.ts +0 -22
- package/src/reducers/arrayPushRaw.ts +0 -18
- package/src/reducers/arraySpliceRaw.ts +0 -30
- package/src/reducers/arrayUnshiftRaw.ts +0 -18
- package/src/reducers/setRawValue.ts +0 -40
package/src/FormState.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import z from 'zod'
|
|
2
|
+
import { DeepPartial } from './util/DeepPartial'
|
|
2
3
|
|
|
3
4
|
export type FieldMeta = {
|
|
4
5
|
touched: boolean
|
|
@@ -6,8 +7,11 @@ export type FieldMeta = {
|
|
|
6
7
|
}
|
|
7
8
|
|
|
8
9
|
export type SubmitHandler<T extends z.ZodTypeAny> = (
|
|
9
|
-
|
|
10
|
-
options: {
|
|
10
|
+
parsedValues: z.output<T>,
|
|
11
|
+
options: {
|
|
12
|
+
initialValues?: DeepPartial<z.input<T>>
|
|
13
|
+
initialParsedValues?: z.output<T>
|
|
14
|
+
}
|
|
11
15
|
) => void | Promise<void>
|
|
12
16
|
|
|
13
17
|
export type SubmitSuccededHandler = () => void
|
|
@@ -18,12 +22,12 @@ export type FormState<T extends z.ZodTypeAny> = {
|
|
|
18
22
|
mounted: boolean
|
|
19
23
|
initialized: boolean
|
|
20
24
|
fieldMeta: Record<string, FieldMeta>
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
values?: DeepPartial<z.input<T>>
|
|
26
|
+
parsedValues?: z.output<T>
|
|
27
|
+
submittedParsedValues?: z.output<T>
|
|
28
|
+
submittedValues?: z.input<T>
|
|
29
|
+
initialValues?: DeepPartial<z.input<T>>
|
|
30
|
+
initialParsedValues?: z.output<T>
|
|
27
31
|
validationError?: any
|
|
28
32
|
submitPromise?: Promise<void>
|
|
29
33
|
onSubmit: Set<SubmitHandler<T>>
|
|
@@ -1,40 +1,41 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FieldPathForParsedValue } from '../FieldPath'
|
|
2
2
|
import z from 'zod'
|
|
3
|
+
import { DeepPartial } from '../util/DeepPartial'
|
|
3
4
|
|
|
4
|
-
export type ArrayFieldPath<V = any, R = any> =
|
|
5
|
+
export type ArrayFieldPath<V = any, R = any> = FieldPathForParsedValue<
|
|
5
6
|
V[] | null | undefined,
|
|
6
7
|
R[] | null | undefined
|
|
7
8
|
>
|
|
8
9
|
|
|
9
|
-
type
|
|
10
|
+
type ParsedValueFor<Field extends ArrayFieldPath> = NonNullable<
|
|
10
11
|
z.output<Field['schema']>
|
|
11
12
|
>[number]
|
|
12
13
|
|
|
13
|
-
type
|
|
14
|
-
z.input<Field['schema']
|
|
15
|
-
>
|
|
14
|
+
type ValueFor<Field extends ArrayFieldPath> = DeepPartial<
|
|
15
|
+
NonNullable<z.input<Field['schema']>>[number]
|
|
16
|
+
>
|
|
16
17
|
|
|
17
|
-
export type
|
|
18
|
-
|
|
18
|
+
export type ArrayInsertParsedAction<
|
|
19
|
+
Field extends ArrayFieldPath = ArrayFieldPath
|
|
20
|
+
> = ReturnType<typeof arrayInsertParsed<Field>>
|
|
19
21
|
|
|
20
|
-
export function
|
|
22
|
+
export function arrayInsertParsed<Field extends ArrayFieldPath>(
|
|
21
23
|
field: Field,
|
|
22
24
|
index: number,
|
|
23
|
-
|
|
25
|
+
parsedValue: ParsedValueFor<Field>
|
|
24
26
|
) {
|
|
25
|
-
return { type: '
|
|
27
|
+
return { type: 'arrayInsertParsed', field, index, parsedValue } as const
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
export type
|
|
29
|
-
Field
|
|
30
|
-
> = ReturnType<typeof arrayInsertRaw<Field>>
|
|
30
|
+
export type ArrayInsertAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
31
|
+
ReturnType<typeof arrayInsert<Field>>
|
|
31
32
|
|
|
32
|
-
export function
|
|
33
|
+
export function arrayInsert<Field extends ArrayFieldPath>(
|
|
33
34
|
field: Field,
|
|
34
35
|
index: number,
|
|
35
|
-
|
|
36
|
+
value: ValueFor<Field>
|
|
36
37
|
) {
|
|
37
|
-
return { type: '
|
|
38
|
+
return { type: 'arrayInsert', field, index, value } as const
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
export type ArrayMoveAction = ReturnType<typeof arrayMove>
|
|
@@ -49,24 +50,25 @@ export function arrayPop(field: ArrayFieldPath) {
|
|
|
49
50
|
return { type: 'arrayPop', field } as const
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
export type
|
|
53
|
-
|
|
53
|
+
export type ArrayPushParsedAction<
|
|
54
|
+
Field extends ArrayFieldPath = ArrayFieldPath
|
|
55
|
+
> = ReturnType<typeof arrayPushParsed<Field>>
|
|
54
56
|
|
|
55
|
-
export function
|
|
57
|
+
export function arrayPushParsed<Field extends ArrayFieldPath>(
|
|
56
58
|
field: Field,
|
|
57
|
-
|
|
59
|
+
parsedValue: ParsedValueFor<Field>
|
|
58
60
|
) {
|
|
59
|
-
return { type: '
|
|
61
|
+
return { type: 'arrayPushParsed', field, parsedValue } as const
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
export type
|
|
63
|
-
ReturnType<typeof
|
|
64
|
+
export type ArrayPushAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
65
|
+
ReturnType<typeof arrayPush<Field>>
|
|
64
66
|
|
|
65
|
-
export function
|
|
67
|
+
export function arrayPush<Field extends ArrayFieldPath>(
|
|
66
68
|
field: Field,
|
|
67
|
-
|
|
69
|
+
value: ValueFor<Field>
|
|
68
70
|
) {
|
|
69
|
-
return { type: '
|
|
71
|
+
return { type: 'arrayPush', field, value } as const
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
export type ArrayRemoveAction = ReturnType<typeof arrayRemove>
|
|
@@ -87,34 +89,40 @@ export function arrayShift(field: ArrayFieldPath) {
|
|
|
87
89
|
return { type: 'arrayShift', field } as const
|
|
88
90
|
}
|
|
89
91
|
|
|
90
|
-
export type
|
|
91
|
-
|
|
92
|
+
export type ArraySpliceParsedAction<
|
|
93
|
+
Field extends ArrayFieldPath = ArrayFieldPath
|
|
94
|
+
> = ReturnType<typeof arraySpliceParsed<Field>>
|
|
92
95
|
|
|
93
|
-
export function
|
|
96
|
+
export function arraySpliceParsed<Field extends ArrayFieldPath>(
|
|
94
97
|
field: Field,
|
|
95
98
|
index: number,
|
|
96
99
|
deleteCount: number,
|
|
97
|
-
...
|
|
100
|
+
...parsedValues: ParsedValueFor<Field>[]
|
|
98
101
|
) {
|
|
99
|
-
return {
|
|
102
|
+
return {
|
|
103
|
+
type: 'arraySpliceParsed',
|
|
104
|
+
field,
|
|
105
|
+
index,
|
|
106
|
+
deleteCount,
|
|
107
|
+
parsedValues,
|
|
108
|
+
} as const
|
|
100
109
|
}
|
|
101
110
|
|
|
102
|
-
export type
|
|
103
|
-
Field
|
|
104
|
-
> = ReturnType<typeof arraySpliceRaw<Field>>
|
|
111
|
+
export type ArraySpliceAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
112
|
+
ReturnType<typeof arraySplice<Field>>
|
|
105
113
|
|
|
106
|
-
export function
|
|
114
|
+
export function arraySplice<Field extends ArrayFieldPath>(
|
|
107
115
|
field: Field,
|
|
108
116
|
index: number,
|
|
109
117
|
deleteCount: number,
|
|
110
|
-
...
|
|
118
|
+
...values: ValueFor<Field>[]
|
|
111
119
|
) {
|
|
112
120
|
return {
|
|
113
|
-
type: '
|
|
121
|
+
type: 'arraySplice',
|
|
114
122
|
field,
|
|
115
123
|
index,
|
|
116
124
|
deleteCount,
|
|
117
|
-
|
|
125
|
+
values,
|
|
118
126
|
} as const
|
|
119
127
|
}
|
|
120
128
|
|
|
@@ -128,56 +136,56 @@ export function arraySwap(
|
|
|
128
136
|
return { type: 'arraySwap', field, indexA, indexB } as const
|
|
129
137
|
}
|
|
130
138
|
|
|
131
|
-
export type
|
|
132
|
-
|
|
139
|
+
export type ArrayUnshiftParsedAction<
|
|
140
|
+
Field extends ArrayFieldPath = ArrayFieldPath
|
|
141
|
+
> = ReturnType<typeof arrayUnshiftParsed<Field>>
|
|
133
142
|
|
|
134
|
-
export function
|
|
143
|
+
export function arrayUnshiftParsed<Field extends ArrayFieldPath>(
|
|
135
144
|
field: Field,
|
|
136
|
-
|
|
145
|
+
parsedValue: ParsedValueFor<Field>
|
|
137
146
|
) {
|
|
138
|
-
return { type: '
|
|
147
|
+
return { type: 'arrayUnshiftParsed', field, parsedValue } as const
|
|
139
148
|
}
|
|
140
149
|
|
|
141
|
-
export type
|
|
142
|
-
Field
|
|
143
|
-
> = ReturnType<typeof arrayUnshiftRaw<Field>>
|
|
150
|
+
export type ArrayUnshiftAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
151
|
+
ReturnType<typeof arrayUnshift<Field>>
|
|
144
152
|
|
|
145
|
-
export function
|
|
153
|
+
export function arrayUnshift<Field extends ArrayFieldPath>(
|
|
146
154
|
field: Field,
|
|
147
|
-
|
|
155
|
+
value: ValueFor<Field>
|
|
148
156
|
) {
|
|
149
|
-
return { type: '
|
|
157
|
+
return { type: 'arrayUnshift', field, value } as const
|
|
150
158
|
}
|
|
151
159
|
|
|
152
160
|
export type ArrayAction<Field extends ArrayFieldPath = ArrayFieldPath> =
|
|
161
|
+
| ArrayInsertParsedAction<Field>
|
|
153
162
|
| ArrayInsertAction<Field>
|
|
154
|
-
| ArrayInsertRawAction<Field>
|
|
155
163
|
| ArrayMoveAction
|
|
156
164
|
| ArrayPopAction
|
|
165
|
+
| ArrayPushParsedAction<Field>
|
|
157
166
|
| ArrayPushAction<Field>
|
|
158
|
-
| ArrayPushRawAction<Field>
|
|
159
167
|
| ArrayRemoveAction
|
|
160
168
|
| ArrayRemoveAllAction
|
|
161
169
|
| ArrayShiftAction
|
|
170
|
+
| ArraySpliceParsedAction<Field>
|
|
162
171
|
| ArraySpliceAction<Field>
|
|
163
|
-
| ArraySpliceRawAction<Field>
|
|
164
172
|
| ArraySwapAction
|
|
173
|
+
| ArrayUnshiftParsedAction<Field>
|
|
165
174
|
| ArrayUnshiftAction<Field>
|
|
166
|
-
| ArrayUnshiftRawAction<Field>
|
|
167
175
|
|
|
168
176
|
export const arrayActions = {
|
|
177
|
+
insertParsed: arrayInsertParsed,
|
|
169
178
|
insert: arrayInsert,
|
|
170
|
-
insertRaw: arrayInsertRaw,
|
|
171
179
|
move: arrayMove,
|
|
172
180
|
pop: arrayPop,
|
|
181
|
+
pushParsed: arrayPushParsed,
|
|
173
182
|
push: arrayPush,
|
|
174
|
-
pushRaw: arrayPushRaw,
|
|
175
183
|
remove: arrayRemove,
|
|
176
184
|
removeAll: arrayRemoveAll,
|
|
177
185
|
shift: arrayShift,
|
|
186
|
+
spliceParsed: arraySpliceParsed,
|
|
178
187
|
splice: arraySplice,
|
|
179
|
-
spliceRaw: arraySpliceRaw,
|
|
180
188
|
swap: arraySwap,
|
|
189
|
+
unshiftParsed: arrayUnshiftParsed,
|
|
181
190
|
unshift: arrayUnshift,
|
|
182
|
-
unshiftRaw: arrayUnshiftRaw,
|
|
183
191
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import z from 'zod'
|
|
2
|
+
import { DeepPartial } from '../util/DeepPartial'
|
|
2
3
|
|
|
3
4
|
export type InitializeAction<T extends z.ZodTypeAny> = ReturnType<
|
|
4
5
|
typeof initialize<T>
|
|
5
6
|
>
|
|
6
7
|
|
|
7
8
|
export function initialize<T extends z.ZodTypeAny>(props: {
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
values?: DeepPartial<z.input<T>>
|
|
10
|
+
parsedValues?: z.output<T>
|
|
10
11
|
keepSubmitSucceeded?: boolean
|
|
11
12
|
}) {
|
|
12
13
|
return {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import z from 'zod'
|
|
2
|
+
import { FieldPath } from '../FieldPath'
|
|
3
|
+
|
|
4
|
+
export type SetParsedValueAction<Field extends FieldPath> = ReturnType<
|
|
5
|
+
typeof setParsedValue<Field>
|
|
6
|
+
>
|
|
7
|
+
|
|
8
|
+
export function setParsedValue<Field extends FieldPath>(
|
|
9
|
+
field: Field,
|
|
10
|
+
parsedValue: z.output<Field['schema']>,
|
|
11
|
+
options?: {
|
|
12
|
+
normalize?: boolean
|
|
13
|
+
}
|
|
14
|
+
) {
|
|
15
|
+
return {
|
|
16
|
+
type: 'setParsedValue',
|
|
17
|
+
field,
|
|
18
|
+
parsedValue,
|
|
19
|
+
...options,
|
|
20
|
+
} as const
|
|
21
|
+
}
|
package/src/actions/setValue.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import z from 'zod'
|
|
2
2
|
import { FieldPath } from '../FieldPath'
|
|
3
|
+
import { DeepPartial } from '../util/DeepPartial'
|
|
3
4
|
|
|
4
5
|
export type SetValueAction<Field extends FieldPath> = ReturnType<
|
|
5
6
|
typeof setValue<Field>
|
|
@@ -7,15 +8,11 @@ export type SetValueAction<Field extends FieldPath> = ReturnType<
|
|
|
7
8
|
|
|
8
9
|
export function setValue<Field extends FieldPath>(
|
|
9
10
|
field: Field,
|
|
10
|
-
value: z.
|
|
11
|
-
options?: {
|
|
12
|
-
normalize?: boolean
|
|
13
|
-
}
|
|
11
|
+
value: DeepPartial<z.input<Field['schema']>>
|
|
14
12
|
) {
|
|
15
13
|
return {
|
|
16
14
|
type: 'setValue',
|
|
17
15
|
field,
|
|
18
16
|
value,
|
|
19
|
-
...options,
|
|
20
17
|
} as const
|
|
21
18
|
}
|
|
@@ -22,19 +22,21 @@ export function createFormMiddleware<T extends z.ZodTypeAny>(): Middleware<
|
|
|
22
22
|
onSubmit,
|
|
23
23
|
onSubmitSucceeded,
|
|
24
24
|
onSubmitFailed,
|
|
25
|
+
parsedValues,
|
|
25
26
|
values,
|
|
26
|
-
rawValues,
|
|
27
27
|
initialValues,
|
|
28
|
+
initialParsedValues,
|
|
28
29
|
} = nextState
|
|
29
30
|
const submitPromise = (async () => {
|
|
30
31
|
if (nextState.validationError) throw nextState.validationError
|
|
31
|
-
for (const fn of onSubmit)
|
|
32
|
+
for (const fn of onSubmit)
|
|
33
|
+
await fn(parsedValues, { initialValues, initialParsedValues })
|
|
32
34
|
})()
|
|
33
35
|
store.dispatch(
|
|
34
36
|
setSubmitStatus({
|
|
35
37
|
submitting: true,
|
|
38
|
+
submittedParsedValues: parsedValues,
|
|
36
39
|
submittedValues: values,
|
|
37
|
-
rawSubmittedValues: rawValues,
|
|
38
40
|
submitError: undefined,
|
|
39
41
|
submitSucceeded: false,
|
|
40
42
|
submitFailed: false,
|
|
@@ -7,8 +7,8 @@ import { FormAction } from './FormAction'
|
|
|
7
7
|
import { FormState } from './FormState'
|
|
8
8
|
import { Provider } from 'react-redux'
|
|
9
9
|
import { initialize } from './actions/initialize'
|
|
10
|
-
import { setRawValue } from './actions/setRawValue'
|
|
11
10
|
import { setValue } from './actions/setValue'
|
|
11
|
+
import { setParsedValue } from './actions/setParsedValue'
|
|
12
12
|
import { FormContext, FormContextProps } from './FormContext'
|
|
13
13
|
import { FormStateContext } from './FormStateContext'
|
|
14
14
|
import { createFormMiddleware } from './createFormMiddleware'
|
|
@@ -41,14 +41,17 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
|
|
|
41
41
|
const store = storeRef.current
|
|
42
42
|
const { dispatch } = store
|
|
43
43
|
|
|
44
|
+
const getParsedValues = React.useCallback(
|
|
45
|
+
() => store.getState().parsedValues,
|
|
46
|
+
[]
|
|
47
|
+
)
|
|
44
48
|
const getValues = React.useCallback(() => store.getState().values, [])
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
() => store.getState().initialValues,
|
|
49
|
+
const getInitialParsedValues = React.useCallback(
|
|
50
|
+
() => store.getState().initialParsedValues,
|
|
48
51
|
[]
|
|
49
52
|
)
|
|
50
|
-
const
|
|
51
|
-
() => store.getState().
|
|
53
|
+
const getInitialValues = React.useCallback(
|
|
54
|
+
() => store.getState().initialValues,
|
|
52
55
|
[]
|
|
53
56
|
)
|
|
54
57
|
const getStatus = React.useCallback(
|
|
@@ -65,10 +68,10 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
|
|
|
65
68
|
const formContext = React.useMemo(
|
|
66
69
|
(): FormContextProps<T> => ({
|
|
67
70
|
...props,
|
|
71
|
+
getParsedValues,
|
|
68
72
|
getValues,
|
|
69
|
-
|
|
73
|
+
getInitialParsedValues,
|
|
70
74
|
getInitialValues,
|
|
71
|
-
getRawInitialValues,
|
|
72
75
|
getStatus,
|
|
73
76
|
...bindActionCreators(
|
|
74
77
|
{
|
|
@@ -78,8 +81,8 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
|
|
|
78
81
|
submit,
|
|
79
82
|
setSubmitStatus: setSubmitStatus<T>,
|
|
80
83
|
setMeta: setMeta as any,
|
|
81
|
-
setRawValue: setRawValue as any,
|
|
82
84
|
setValue: setValue as any,
|
|
85
|
+
setParsedValue: setParsedValue as any,
|
|
83
86
|
},
|
|
84
87
|
dispatch
|
|
85
88
|
),
|
package/src/createFormReducer.ts
CHANGED
|
@@ -5,8 +5,8 @@ import { FormState } from './FormState'
|
|
|
5
5
|
import { initFormState } from './initFormState'
|
|
6
6
|
import { addHandlersReducer } from './reducers/addHandlers'
|
|
7
7
|
import { removeHandlersReducer } from './reducers/removeHandlers'
|
|
8
|
+
import { createSetParsedValueReducer } from './reducers/setParsedValue'
|
|
8
9
|
import { createSetValueReducer } from './reducers/setValue'
|
|
9
|
-
import { createSetRawValueReducer } from './reducers/setRawValue'
|
|
10
10
|
import { setMetaReducer } from './reducers/setMeta'
|
|
11
11
|
import { submitSucceededReducer } from './reducers/submitSucceeded'
|
|
12
12
|
import { setSubmitStatusReducer } from './reducers/setSubmitStatus'
|
|
@@ -17,22 +17,22 @@ import { arrayRemoveReducer } from './reducers/arrayRemove'
|
|
|
17
17
|
import { arrayRemoveAllReducer } from './reducers/arrayRemoveAll'
|
|
18
18
|
import { arrayShiftReducer } from './reducers/arrayShift'
|
|
19
19
|
import { arraySwapReducer } from './reducers/arraySwap'
|
|
20
|
-
import { arrayPushRawReducer } from './reducers/arrayPushRaw'
|
|
21
|
-
import { arrayInsertRawReducer } from './reducers/arrayInsertRaw'
|
|
22
|
-
import { arraySpliceRawReducer } from './reducers/arraySpliceRaw'
|
|
23
|
-
import { arrayUnshiftRawReducer } from './reducers/arrayUnshiftRaw'
|
|
24
20
|
import { arrayPushReducer } from './reducers/arrayPush'
|
|
25
|
-
import { arrayUnshiftReducer } from './reducers/arrayUnshift'
|
|
26
21
|
import { arrayInsertReducer } from './reducers/arrayInsert'
|
|
27
22
|
import { arraySpliceReducer } from './reducers/arraySplice'
|
|
23
|
+
import { arrayUnshiftReducer } from './reducers/arrayUnshift'
|
|
24
|
+
import { arrayPushParsedReducer } from './reducers/arrayPushParsed'
|
|
25
|
+
import { arrayUnshiftParsedReducer } from './reducers/arrayUnshiftParsed'
|
|
26
|
+
import { arrayInsertParsedReducer } from './reducers/arrayInsertParsed'
|
|
27
|
+
import { arraySpliceParsedReducer } from './reducers/arraySpliceParsed'
|
|
28
28
|
|
|
29
29
|
export function createFormReducer<T extends z.ZodTypeAny>(options: {
|
|
30
30
|
schema: T
|
|
31
31
|
inverseSchema: z.ZodType<z.input<T>, any, z.output<T>>
|
|
32
32
|
}): Reducer<FormState<T>, FormAction<T>> {
|
|
33
33
|
const initializeReducer = createInitializeReducer(options)
|
|
34
|
+
const setParsedValueReducer = createSetParsedValueReducer(options)
|
|
34
35
|
const setValueReducer = createSetValueReducer(options)
|
|
35
|
-
const setRawValueReducer = createSetRawValueReducer(options)
|
|
36
36
|
const formReducer = (
|
|
37
37
|
state: FormState<T> = initFormState(),
|
|
38
38
|
action: FormAction<T>
|
|
@@ -50,40 +50,40 @@ export function createFormReducer<T extends z.ZodTypeAny>(options: {
|
|
|
50
50
|
return setSubmitStatusReducer(state, action)
|
|
51
51
|
case 'submitSucceeded':
|
|
52
52
|
return submitSucceededReducer(state)
|
|
53
|
+
case 'setParsedValue':
|
|
54
|
+
return setParsedValueReducer(state, action)
|
|
53
55
|
case 'setValue':
|
|
54
56
|
return setValueReducer(state, action)
|
|
55
|
-
case 'setRawValue':
|
|
56
|
-
return setRawValueReducer(state, action)
|
|
57
57
|
case 'setMeta':
|
|
58
58
|
return setMetaReducer(state, action)
|
|
59
|
+
case 'arrayInsertParsed':
|
|
60
|
+
return arrayInsertParsedReducer(formReducer, state, action)
|
|
59
61
|
case 'arrayInsert':
|
|
60
62
|
return arrayInsertReducer(formReducer, state, action)
|
|
61
|
-
case 'arrayInsertRaw':
|
|
62
|
-
return arrayInsertRawReducer(formReducer, state, action)
|
|
63
63
|
case 'arrayMove':
|
|
64
64
|
return arrayMoveReducer(formReducer, state, action)
|
|
65
65
|
case 'arrayPop':
|
|
66
66
|
return arrayPopReducer(formReducer, state, action)
|
|
67
|
+
case 'arrayPushParsed':
|
|
68
|
+
return arrayPushParsedReducer(formReducer, state, action)
|
|
67
69
|
case 'arrayPush':
|
|
68
70
|
return arrayPushReducer(formReducer, state, action)
|
|
69
|
-
case 'arrayPushRaw':
|
|
70
|
-
return arrayPushRawReducer(formReducer, state, action)
|
|
71
71
|
case 'arrayRemove':
|
|
72
72
|
return arrayRemoveReducer(formReducer, state, action)
|
|
73
73
|
case 'arrayRemoveAll':
|
|
74
74
|
return arrayRemoveAllReducer(formReducer, state, action)
|
|
75
75
|
case 'arrayShift':
|
|
76
76
|
return arrayShiftReducer(formReducer, state, action)
|
|
77
|
+
case 'arraySpliceParsed':
|
|
78
|
+
return arraySpliceParsedReducer(formReducer, state, action)
|
|
77
79
|
case 'arraySplice':
|
|
78
80
|
return arraySpliceReducer(formReducer, state, action)
|
|
79
|
-
case 'arraySpliceRaw':
|
|
80
|
-
return arraySpliceRawReducer(formReducer, state, action)
|
|
81
81
|
case 'arraySwap':
|
|
82
82
|
return arraySwapReducer(formReducer, state, action)
|
|
83
|
+
case 'arrayUnshiftParsed':
|
|
84
|
+
return arrayUnshiftParsedReducer(formReducer, state, action)
|
|
83
85
|
case 'arrayUnshift':
|
|
84
86
|
return arrayUnshiftReducer(formReducer, state, action)
|
|
85
|
-
case 'arrayUnshiftRaw':
|
|
86
|
-
return arrayUnshiftRawReducer(formReducer, state, action)
|
|
87
87
|
}
|
|
88
88
|
return state
|
|
89
89
|
}
|
|
@@ -22,20 +22,20 @@ export function createSelectFormStatus() {
|
|
|
22
22
|
[
|
|
23
23
|
createSelector(
|
|
24
24
|
[
|
|
25
|
-
(state: FormState<any>) => state.
|
|
26
|
-
(state: FormState<any>) => state.
|
|
25
|
+
(state: FormState<any>) => state.parsedValues,
|
|
26
|
+
(state: FormState<any>) => state.initialParsedValues,
|
|
27
27
|
],
|
|
28
28
|
isEqual
|
|
29
29
|
),
|
|
30
30
|
createSelector(
|
|
31
31
|
[
|
|
32
|
-
(state: FormState<any>) => state.
|
|
33
|
-
(state: FormState<any>) => state.
|
|
32
|
+
(state: FormState<any>) => state.values,
|
|
33
|
+
(state: FormState<any>) => state.initialValues,
|
|
34
34
|
],
|
|
35
35
|
isEqual
|
|
36
36
|
),
|
|
37
37
|
],
|
|
38
|
-
(
|
|
38
|
+
(parsedPristine, pristine) => parsedPristine && pristine
|
|
39
39
|
)
|
|
40
40
|
|
|
41
41
|
return createStructuredSelector({
|
|
@@ -8,9 +8,9 @@ export type SelectFormValues<T extends z.ZodTypeAny> = ReturnType<
|
|
|
8
8
|
|
|
9
9
|
export function createSelectFormValues<T extends z.ZodTypeAny>() {
|
|
10
10
|
return createStructuredSelector({
|
|
11
|
+
parsedValues: (state: FormState<T>) => state.parsedValues,
|
|
11
12
|
values: (state: FormState<T>) => state.values,
|
|
12
|
-
|
|
13
|
+
initialParsedValues: (state: FormState<T>) => state.initialParsedValues,
|
|
13
14
|
initialValues: (state: FormState<T>) => state.initialValues,
|
|
14
|
-
rawInitialValues: (state: FormState<T>) => state.rawInitialValues,
|
|
15
15
|
})
|
|
16
16
|
}
|
package/src/index.ts
CHANGED
|
@@ -7,8 +7,8 @@ export {
|
|
|
7
7
|
} from './FormState'
|
|
8
8
|
export {
|
|
9
9
|
FieldPath,
|
|
10
|
+
type FieldPathForParsedValue,
|
|
10
11
|
type FieldPathForValue,
|
|
11
|
-
type FieldPathForRawValue,
|
|
12
12
|
} from './FieldPath'
|
|
13
13
|
export { useField, type UseFieldProps, type TypedUseField } from './useField'
|
|
14
14
|
export {
|
package/src/initFormState.ts
CHANGED
|
@@ -6,8 +6,8 @@ export function initFormState<T extends z.ZodTypeAny>(): FormState<T> {
|
|
|
6
6
|
mounted: true,
|
|
7
7
|
initialized: false,
|
|
8
8
|
fieldMeta: {},
|
|
9
|
-
rawInitialValues: undefined,
|
|
10
9
|
initialValues: undefined,
|
|
10
|
+
initialParsedValues: undefined,
|
|
11
11
|
submitting: false,
|
|
12
12
|
submitFailed: false,
|
|
13
13
|
submitSucceeded: false,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import z from 'zod'
|
|
2
2
|
import { FormState } from '../FormState'
|
|
3
|
-
import {
|
|
3
|
+
import { ArrayInsertAction } from '../actions/arrayActions'
|
|
4
4
|
import { Reducer } from 'redux'
|
|
5
5
|
import { FormAction } from '../FormAction'
|
|
6
|
-
import {
|
|
6
|
+
import { updateRawArray } from './util/updateRawArray'
|
|
7
7
|
|
|
8
8
|
export function arrayInsertReducer<T extends z.ZodTypeAny>(
|
|
9
9
|
reducer: Reducer<FormState<T>, FormAction<T>>,
|
|
@@ -11,9 +11,12 @@ export function arrayInsertReducer<T extends z.ZodTypeAny>(
|
|
|
11
11
|
action: ArrayInsertAction
|
|
12
12
|
) {
|
|
13
13
|
const { field, index, value } = action
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
state,
|
|
17
|
-
arrayInsertRaw(field, index, inverseSchema.parse(value))
|
|
14
|
+
return updateRawArray(reducer, state, field, (array) =>
|
|
15
|
+
insert(array ?? [], index, value)
|
|
18
16
|
)
|
|
19
17
|
}
|
|
18
|
+
|
|
19
|
+
function insert<T>(array: T[], index: number, parsedValue: T) {
|
|
20
|
+
if (index < 0 || index > array.length) throw new Error(`index out of range`)
|
|
21
|
+
return [...array.slice(0, index), parsedValue, ...array.slice(index)]
|
|
22
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import z from 'zod'
|
|
2
|
+
import { FormState } from '../FormState'
|
|
3
|
+
import { arrayInsert, ArrayInsertParsedAction } from '../actions/arrayActions'
|
|
4
|
+
import { Reducer } from 'redux'
|
|
5
|
+
import { FormAction } from '../FormAction'
|
|
6
|
+
import { getInverseArrayElementSchema } from './util/getInverseArrayElementSchema'
|
|
7
|
+
|
|
8
|
+
export function arrayInsertParsedReducer<T extends z.ZodTypeAny>(
|
|
9
|
+
reducer: Reducer<FormState<T>, FormAction<T>>,
|
|
10
|
+
state: FormState<T>,
|
|
11
|
+
action: ArrayInsertParsedAction
|
|
12
|
+
) {
|
|
13
|
+
const { field, index, parsedValue } = action
|
|
14
|
+
const inverseSchema = getInverseArrayElementSchema(field.schema)
|
|
15
|
+
return reducer(
|
|
16
|
+
state,
|
|
17
|
+
arrayInsert(field, index, inverseSchema.parse(parsedValue))
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -3,8 +3,7 @@ import { FormState } from '../FormState'
|
|
|
3
3
|
import { ArrayPushAction } from '../actions/arrayActions'
|
|
4
4
|
import { Reducer } from 'redux'
|
|
5
5
|
import { FormAction } from '../FormAction'
|
|
6
|
-
import {
|
|
7
|
-
import { get } from '../util/get'
|
|
6
|
+
import { updateRawArray } from './util/updateRawArray'
|
|
8
7
|
|
|
9
8
|
export function arrayPushReducer<T extends z.ZodTypeAny>(
|
|
10
9
|
reducer: Reducer<FormState<T>, FormAction<T>>,
|
|
@@ -12,9 +11,8 @@ export function arrayPushReducer<T extends z.ZodTypeAny>(
|
|
|
12
11
|
action: ArrayPushAction
|
|
13
12
|
) {
|
|
14
13
|
const { field, value } = action
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
)
|
|
14
|
+
return updateRawArray(reducer, state, field, (array) => [
|
|
15
|
+
...(array ?? []),
|
|
16
|
+
value,
|
|
17
|
+
])
|
|
20
18
|
}
|