@jcoreio/zod-forms 2.0.0-beta.4 → 2.0.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/ConditionalValidator.d.ts.map +1 -1
- package/ConditionalValidator.js +4 -2
- package/ConditionalValidator.js.map +1 -1
- package/ConditionalValidator.mjs +3 -1
- package/ConditionalValidator.mjs.map +1 -1
- package/FieldPath.d.ts.map +1 -1
- package/FieldPath.js +1 -0
- package/FieldPath.js.map +1 -1
- package/FieldPath.mjs.map +1 -1
- package/FormState.d.ts +6 -4
- package/FormState.d.ts.map +1 -1
- package/FormState.js.map +1 -1
- package/FormState.mjs.map +1 -1
- package/actions/arrayActions.js.map +1 -1
- package/actions/arrayActions.mjs.map +1 -1
- package/actions/setSubmitStatus.d.ts +1 -1
- package/createFormMiddleware.js +3 -3
- package/createFormMiddleware.js.map +1 -1
- package/createFormMiddleware.mjs.map +1 -1
- package/createFormProvider.d.ts +9 -3
- package/createFormProvider.d.ts.map +1 -1
- package/createFormProvider.js +10 -3
- package/createFormProvider.js.map +1 -1
- package/createFormProvider.mjs +53 -44
- package/createFormProvider.mjs.map +1 -1
- package/createFormReducer.js +16 -16
- package/createFormReducer.js.map +1 -1
- package/createSelectFieldErrorMap.d.ts +3 -3
- package/createSelectFieldErrorMap.js.map +1 -1
- package/createSelectFieldErrorMap.mjs.map +1 -1
- package/createSelectFormStatus.d.ts +7 -7
- package/createSelectFormStatus.d.ts.map +1 -1
- package/createSelectFormStatus.js.map +1 -1
- package/createSelectFormValues.js.map +1 -1
- package/createZodForm.d.ts +10 -5
- package/createZodForm.d.ts.map +1 -1
- package/package.json +5 -4
- package/reducers/addHandlers.d.ts +2 -2
- package/reducers/addHandlers.d.ts.map +1 -1
- package/reducers/arrayMove.js.map +1 -1
- package/reducers/arrayMove.mjs.map +1 -1
- package/reducers/initialize.d.ts +4 -4
- package/reducers/initialize.d.ts.map +1 -1
- package/reducers/initialize.js.map +1 -1
- package/reducers/initialize.mjs.map +1 -1
- package/reducers/removeHandlers.d.ts +2 -2
- package/reducers/removeHandlers.d.ts.map +1 -1
- package/reducers/setMeta.d.ts.map +1 -1
- package/reducers/setMeta.js +4 -1
- package/reducers/setMeta.js.map +1 -1
- package/reducers/setMeta.mjs +4 -1
- package/reducers/setMeta.mjs.map +1 -1
- package/reducers/setParsedValue.d.ts +2 -2
- package/reducers/setParsedValue.d.ts.map +1 -1
- package/reducers/setParsedValue.js +1 -1
- package/reducers/setParsedValue.js.map +1 -1
- package/reducers/setParsedValue.mjs +1 -1
- package/reducers/setParsedValue.mjs.map +1 -1
- package/reducers/setSubmitStatus.d.ts +2 -2
- package/reducers/setSubmitStatus.d.ts.map +1 -1
- package/reducers/setValue.d.ts +1 -1
- package/reducers/setValue.d.ts.map +1 -1
- package/reducers/submitSucceeded.d.ts +1 -1
- package/reducers/submitSucceeded.d.ts.map +1 -1
- package/reducers/util/updateRawArray.js.map +1 -1
- package/reducers/util/updateRawArray.mjs.map +1 -1
- package/src/ConditionalValidator.ts +11 -12
- package/src/FieldPath.ts +34 -54
- package/src/FormState.ts +4 -4
- package/src/actions/arrayActions.ts +4 -4
- package/src/createFormMiddleware.ts +2 -2
- package/src/createFormProvider.tsx +14 -2
- package/src/createSelectFieldErrorMap.ts +12 -10
- package/src/reducers/arrayMove.ts +2 -2
- package/src/reducers/initialize.ts +3 -3
- package/src/reducers/setMeta.ts +4 -1
- package/src/reducers/setParsedValue.ts +10 -9
- package/src/reducers/util/updateRawArray.ts +4 -4
- package/src/useArrayField.ts +26 -26
- package/src/useField.ts +15 -13
- package/src/useFormContext.ts +2 -2
- package/src/useFormDispatch.ts +3 -7
- package/src/useHtmlField.ts +18 -20
- package/src/useInitialize.ts +1 -1
- package/src/util/DeepPartial.ts +2 -3
- package/src/util/PathInType.ts +16 -16
- package/src/util/SchemaAt.ts +38 -49
- package/src/util/bindActionsToField.ts +6 -7
- package/src/util/parsePathstring.ts +47 -41
- package/src/util/pathstring.ts +9 -13
- package/src/util/set.ts +4 -1
- package/useArrayField.d.ts +2 -2
- package/useArrayField.d.ts.map +1 -1
- package/useArrayField.js.map +1 -1
- package/useArrayField.mjs.map +1 -1
- package/useField.d.ts.map +1 -1
- package/useField.js.map +1 -1
- package/useField.mjs.map +1 -1
- package/useFormContext.js.map +1 -1
- package/useFormContext.mjs.map +1 -1
- package/useFormDispatch.d.ts +1 -3
- package/useFormDispatch.d.ts.map +1 -1
- package/useFormDispatch.js.map +1 -1
- package/useFormDispatch.mjs.map +1 -1
- package/useFormStatus.d.ts +1 -1
- package/useHtmlField.d.ts +1 -1
- package/useHtmlField.d.ts.map +1 -1
- package/useHtmlField.js +1 -1
- package/useHtmlField.js.map +1 -1
- package/useHtmlField.mjs +1 -1
- package/useHtmlField.mjs.map +1 -1
- package/useInitialize.js.map +1 -1
- package/useInitialize.mjs.map +1 -1
- package/useSubmit.js.map +1 -1
- package/util/DeepPartial.d.ts.map +1 -1
- package/util/DeepPartial.js.map +1 -1
- package/util/DeepPartial.mjs.map +1 -1
- package/util/PathInType.d.ts +2 -1
- package/util/PathInType.d.ts.map +1 -1
- package/util/PathInType.js.map +1 -1
- package/util/PathInType.mjs.map +1 -1
- package/util/SchemaAt.d.ts +1 -1
- package/util/SchemaAt.d.ts.map +1 -1
- package/util/SchemaAt.js.map +1 -1
- package/util/SchemaAt.mjs.map +1 -1
- package/util/bindActionsToField.d.ts +1 -1
- package/util/bindActionsToField.d.ts.map +1 -1
- package/util/bindActionsToField.js.map +1 -1
- package/util/bindActionsToField.mjs.map +1 -1
- package/util/parsePathstring.d.ts +23 -6
- package/util/parsePathstring.d.ts.map +1 -1
- package/util/parsePathstring.js.map +1 -1
- package/util/parsePathstring.mjs.map +1 -1
- package/util/pathstring.d.ts +2 -8
- package/util/pathstring.d.ts.map +1 -1
- package/util/pathstring.js.map +1 -1
- package/util/pathstring.mjs.map +1 -1
- package/util/set.d.ts.map +1 -1
- package/util/set.js +1 -4
- package/util/set.js.map +1 -1
- package/util/set.mjs +1 -3
- package/util/set.mjs.map +1 -1
package/src/FormState.ts
CHANGED
|
@@ -14,14 +14,14 @@ export type SubmitHandler<T extends z.ZodTypeAny> = (
|
|
|
14
14
|
}
|
|
15
15
|
) => void | Promise<void>
|
|
16
16
|
|
|
17
|
-
export type SubmitSuccededHandler = () => void
|
|
17
|
+
export type SubmitSuccededHandler = () => void | Promise<void>
|
|
18
18
|
|
|
19
|
-
export type SubmitFailedHandler = (error:
|
|
19
|
+
export type SubmitFailedHandler = (error: unknown) => void | Promise<void>
|
|
20
20
|
|
|
21
21
|
export type FormState<T extends z.ZodTypeAny> = {
|
|
22
22
|
mounted: boolean
|
|
23
23
|
initialized: boolean
|
|
24
|
-
fieldMeta:
|
|
24
|
+
fieldMeta: { [K in string]?: FieldMeta }
|
|
25
25
|
values?: DeepPartial<z.input<T>>
|
|
26
26
|
parsedValues?: z.output<T>
|
|
27
27
|
submittedParsedValues?: z.output<T>
|
|
@@ -36,5 +36,5 @@ export type FormState<T extends z.ZodTypeAny> = {
|
|
|
36
36
|
submitting: boolean
|
|
37
37
|
submitSucceeded: boolean
|
|
38
38
|
submitFailed: boolean
|
|
39
|
-
submitError?:
|
|
39
|
+
submitError?: unknown
|
|
40
40
|
}
|
|
@@ -16,7 +16,7 @@ type ValueFor<Field extends ArrayFieldPath> = DeepPartial<
|
|
|
16
16
|
>
|
|
17
17
|
|
|
18
18
|
export type ArrayInsertParsedAction<
|
|
19
|
-
Field extends ArrayFieldPath = ArrayFieldPath
|
|
19
|
+
Field extends ArrayFieldPath = ArrayFieldPath,
|
|
20
20
|
> = ReturnType<typeof arrayInsertParsed<Field>>
|
|
21
21
|
|
|
22
22
|
export function arrayInsertParsed<Field extends ArrayFieldPath>(
|
|
@@ -51,7 +51,7 @@ export function arrayPop(field: ArrayFieldPath) {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export type ArrayPushParsedAction<
|
|
54
|
-
Field extends ArrayFieldPath = ArrayFieldPath
|
|
54
|
+
Field extends ArrayFieldPath = ArrayFieldPath,
|
|
55
55
|
> = ReturnType<typeof arrayPushParsed<Field>>
|
|
56
56
|
|
|
57
57
|
export function arrayPushParsed<Field extends ArrayFieldPath>(
|
|
@@ -90,7 +90,7 @@ export function arrayShift(field: ArrayFieldPath) {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
export type ArraySpliceParsedAction<
|
|
93
|
-
Field extends ArrayFieldPath = ArrayFieldPath
|
|
93
|
+
Field extends ArrayFieldPath = ArrayFieldPath,
|
|
94
94
|
> = ReturnType<typeof arraySpliceParsed<Field>>
|
|
95
95
|
|
|
96
96
|
export function arraySpliceParsed<Field extends ArrayFieldPath>(
|
|
@@ -137,7 +137,7 @@ export function arraySwap(
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
export type ArrayUnshiftParsedAction<
|
|
140
|
-
Field extends ArrayFieldPath = ArrayFieldPath
|
|
140
|
+
Field extends ArrayFieldPath = ArrayFieldPath,
|
|
141
141
|
> = ReturnType<typeof arrayUnshiftParsed<Field>>
|
|
142
142
|
|
|
143
143
|
export function arrayUnshiftParsed<Field extends ArrayFieldPath>(
|
|
@@ -7,7 +7,7 @@ import { setSubmitStatus } from './actions/setSubmitStatus'
|
|
|
7
7
|
import { submitSucceeded } from './actions/submitSucceeded'
|
|
8
8
|
|
|
9
9
|
export function createFormMiddleware<T extends z.ZodTypeAny>(): Middleware<
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
11
11
|
{},
|
|
12
12
|
FormState<T>,
|
|
13
13
|
Dispatch<FormAction<T>>
|
|
@@ -49,7 +49,7 @@ export function createFormMiddleware<T extends z.ZodTypeAny>(): Middleware<
|
|
|
49
49
|
store.dispatch(submitSucceeded())
|
|
50
50
|
for (const fn of onSubmitSucceeded) await fn()
|
|
51
51
|
},
|
|
52
|
-
async (error) => {
|
|
52
|
+
async (error: unknown) => {
|
|
53
53
|
if (store.getState().submitPromise !== submitPromise) return
|
|
54
54
|
store.dispatch(
|
|
55
55
|
setSubmitStatus({
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
Store,
|
|
4
|
+
applyMiddleware,
|
|
5
|
+
bindActionCreators,
|
|
6
|
+
legacy_createStore as createStore,
|
|
7
|
+
} from 'redux'
|
|
3
8
|
import z from 'zod'
|
|
4
9
|
import { setMounted } from './actions/setMounted'
|
|
5
10
|
import { createFormReducer } from './createFormReducer'
|
|
@@ -18,6 +23,7 @@ import { setMeta } from './actions/setMeta'
|
|
|
18
23
|
import { addHandlers } from './actions/addHandlers'
|
|
19
24
|
import { removeHandlers } from './actions/removeHandlers'
|
|
20
25
|
import { arrayActions } from './actions/arrayActions'
|
|
26
|
+
import PropTypes from 'prop-types'
|
|
21
27
|
|
|
22
28
|
export const createFormProvider = <T extends z.ZodTypeAny>(
|
|
23
29
|
props: Pick<
|
|
@@ -29,7 +35,7 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
|
|
|
29
35
|
| 'selectFieldErrorMap'
|
|
30
36
|
| 'selectFormValues'
|
|
31
37
|
>
|
|
32
|
-
) =>
|
|
38
|
+
) => {
|
|
33
39
|
function FormProvider({ children }: { children: React.ReactElement }) {
|
|
34
40
|
const storeRef = React.useRef<Store<FormState<T>, FormAction<T>>>()
|
|
35
41
|
if (!storeRef.current) {
|
|
@@ -55,6 +61,7 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
|
|
|
55
61
|
[]
|
|
56
62
|
)
|
|
57
63
|
const getStatus = React.useCallback(
|
|
64
|
+
// eslint-disable-next-line react/prop-types
|
|
58
65
|
() => props.selectFormStatus(store.getState()),
|
|
59
66
|
[]
|
|
60
67
|
)
|
|
@@ -102,3 +109,8 @@ export const createFormProvider = <T extends z.ZodTypeAny>(
|
|
|
102
109
|
</FormContext.Provider>
|
|
103
110
|
)
|
|
104
111
|
}
|
|
112
|
+
FormProvider.propTypes = {
|
|
113
|
+
children: PropTypes.node,
|
|
114
|
+
}
|
|
115
|
+
return FormProvider
|
|
116
|
+
}
|
|
@@ -14,12 +14,12 @@ export function createSelectFieldErrorMap() {
|
|
|
14
14
|
(...errors: any[]): { [K in string]?: string } =>
|
|
15
15
|
Object.fromEntries(
|
|
16
16
|
errors.flatMap((e) =>
|
|
17
|
-
isZodError(e)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
isZodError(e) ?
|
|
18
|
+
e.issues.map((issue) => [
|
|
19
|
+
pathstring(issue.path),
|
|
20
|
+
messageForIssue(issue),
|
|
21
|
+
])
|
|
22
|
+
: []
|
|
23
23
|
)
|
|
24
24
|
)
|
|
25
25
|
)
|
|
@@ -34,14 +34,16 @@ function isZodError(error: any): error is z.ZodError {
|
|
|
34
34
|
*/
|
|
35
35
|
function messageForIssue(issue: z.ZodIssue): string {
|
|
36
36
|
if (issue.code === 'invalid_type') {
|
|
37
|
-
return
|
|
38
|
-
|
|
37
|
+
return (
|
|
38
|
+
issue.received === 'null' || issue.received === 'undefined' ?
|
|
39
|
+
// Without this, the error would say "Expected <type>, received null"
|
|
39
40
|
// or "Invalid number" as below
|
|
40
41
|
'Required'
|
|
41
|
-
: issue.expected === 'number' || issue.expected === 'bigint'
|
|
42
|
-
|
|
42
|
+
: issue.expected === 'number' || issue.expected === 'bigint' ?
|
|
43
|
+
// Without this, invalid text input for z.number() would say "Expected number, received string"
|
|
43
44
|
'Invalid number'
|
|
44
45
|
: issue.message
|
|
46
|
+
)
|
|
45
47
|
}
|
|
46
48
|
return issue.message
|
|
47
49
|
}
|
|
@@ -20,8 +20,8 @@ export function arrayMoveReducer<T extends z.ZodTypeAny>(
|
|
|
20
20
|
export function move<T>(array: T[], from: number, to: number): T[] {
|
|
21
21
|
if (from < 0 || from >= array.length) throw new Error(`from out of range`)
|
|
22
22
|
if (to < 0 || to >= array.length) throw new Error(`to out of range`)
|
|
23
|
-
return from < to
|
|
24
|
-
|
|
23
|
+
return from < to ?
|
|
24
|
+
[
|
|
25
25
|
...array.slice(0, from),
|
|
26
26
|
...array.slice(from + 1, to),
|
|
27
27
|
array[from],
|
|
@@ -14,9 +14,9 @@ export const createInitializeReducer = <T extends z.ZodTypeAny>({
|
|
|
14
14
|
try {
|
|
15
15
|
const values =
|
|
16
16
|
action.values ??
|
|
17
|
-
(action.parsedValues
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
(action.parsedValues ?
|
|
18
|
+
inverseSchema.parse(action.parsedValues)
|
|
19
|
+
: undefined)
|
|
20
20
|
const parsedValues =
|
|
21
21
|
action.parsedValues ??
|
|
22
22
|
(action.values ? schema.parse(action.values) : undefined)
|
package/src/reducers/setMeta.ts
CHANGED
|
@@ -20,7 +20,10 @@ export function setMetaReducer<T extends z.ZodTypeAny>(
|
|
|
20
20
|
...state,
|
|
21
21
|
fieldMeta: {
|
|
22
22
|
...state.fieldMeta,
|
|
23
|
-
[field.pathstring]: {
|
|
23
|
+
[field.pathstring]: {
|
|
24
|
+
...(oldMeta || { touched: false, visited: false }),
|
|
25
|
+
...meta,
|
|
26
|
+
},
|
|
24
27
|
},
|
|
25
28
|
}
|
|
26
29
|
}
|
|
@@ -31,12 +31,13 @@ export const createSetParsedValueReducer = <T extends z.ZodTypeAny>({
|
|
|
31
31
|
values: newValues,
|
|
32
32
|
parsedValues: newParsedValues,
|
|
33
33
|
}
|
|
34
|
-
} catch
|
|
34
|
+
} catch {
|
|
35
35
|
const newParsed = invert(action.field.schema).safeParse(
|
|
36
36
|
action.parsedValue
|
|
37
37
|
)
|
|
38
|
-
const values =
|
|
39
|
-
?
|
|
38
|
+
const values =
|
|
39
|
+
newParsed.success ?
|
|
40
|
+
(set(
|
|
40
41
|
state.values,
|
|
41
42
|
action.field.path,
|
|
42
43
|
newParsed.data
|
|
@@ -46,14 +47,14 @@ export const createSetParsedValueReducer = <T extends z.ZodTypeAny>({
|
|
|
46
47
|
const result = {
|
|
47
48
|
...state,
|
|
48
49
|
submitError: undefined,
|
|
49
|
-
validationError:
|
|
50
|
-
? newParsed.error
|
|
51
|
-
: newValidatedParsed.success
|
|
52
|
-
? undefined
|
|
50
|
+
validationError:
|
|
51
|
+
!newParsed.success ? newParsed.error
|
|
52
|
+
: newValidatedParsed.success ? undefined
|
|
53
53
|
: newValidatedParsed.error,
|
|
54
54
|
values,
|
|
55
|
-
parsedValues:
|
|
56
|
-
|
|
55
|
+
parsedValues:
|
|
56
|
+
newValidatedParsed.success ?
|
|
57
|
+
newValidatedParsed.data
|
|
57
58
|
: state.parsedValues,
|
|
58
59
|
}
|
|
59
60
|
return result
|
|
@@ -8,7 +8,7 @@ import { setValue } from '../../actions/setValue'
|
|
|
8
8
|
|
|
9
9
|
export function updateRawArray<
|
|
10
10
|
T extends z.ZodTypeAny,
|
|
11
|
-
Field extends FieldPathForValue<any[]
|
|
11
|
+
Field extends FieldPathForValue<any[]>,
|
|
12
12
|
>(
|
|
13
13
|
reducer: Reducer<FormState<T>, FormAction<T>>,
|
|
14
14
|
state: FormState<T>,
|
|
@@ -19,7 +19,7 @@ export function updateRawArray<
|
|
|
19
19
|
) {
|
|
20
20
|
const oldValue = get(state.values, field.path)
|
|
21
21
|
const newValue = updater(oldValue as any)
|
|
22
|
-
return newValue === oldValue
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
return newValue === oldValue ? state : (
|
|
23
|
+
reducer(state, setValue(field, newValue))
|
|
24
|
+
)
|
|
25
25
|
}
|
package/src/useArrayField.ts
CHANGED
|
@@ -21,35 +21,35 @@ import { setMeta } from './actions/setMeta'
|
|
|
21
21
|
import { FieldMeta } from './FormState'
|
|
22
22
|
import { DeepPartial } from './util/DeepPartial'
|
|
23
23
|
|
|
24
|
-
export type UseArrayFieldProps<Field extends
|
|
25
|
-
NonNullable<z.input<Field['schema']>> extends any[]
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
24
|
+
export type UseArrayFieldProps<Field extends FieldPath> =
|
|
25
|
+
NonNullable<z.input<Field['schema']>> extends any[] ?
|
|
26
|
+
FieldMeta &
|
|
27
|
+
ReturnType<
|
|
28
|
+
typeof bindActionsToField<
|
|
29
|
+
Field,
|
|
30
|
+
arrayActions<Field> & {
|
|
31
|
+
setParsedValue: typeof setParsedValue<Field>
|
|
32
|
+
setValue: typeof setValue<Field>
|
|
33
|
+
setMeta: typeof setMeta<Field>
|
|
34
|
+
}
|
|
35
|
+
>
|
|
36
|
+
> & {
|
|
37
|
+
elements: FieldPath<SchemaAt<Field['schema'], [number]>>[]
|
|
38
|
+
error?: string
|
|
39
|
+
dirty: boolean
|
|
40
|
+
pristine: boolean
|
|
41
|
+
valid: boolean
|
|
42
|
+
invalid: boolean
|
|
43
|
+
}
|
|
44
|
+
: { ERROR: 'not an array field' }
|
|
45
45
|
|
|
46
46
|
export interface TypedUseArrayField<T extends z.ZodTypeAny> {
|
|
47
47
|
<Field extends FieldPathForValue<any[] | null | undefined>>(
|
|
48
48
|
field: Field
|
|
49
49
|
): UseArrayFieldProps<Field>
|
|
50
|
-
<Path extends PathInSchema<T>>(
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
<Path extends PathInSchema<T>>(
|
|
51
|
+
path: Path
|
|
52
|
+
): UseArrayFieldProps<FieldPath<SchemaAt<T, Path>>>
|
|
53
53
|
<Pathstring extends PathstringInSchema<T>>(
|
|
54
54
|
path: Pathstring
|
|
55
55
|
): UseArrayFieldProps<FieldPath<SchemaAt<T, parsePathstring<Pathstring>>>>
|
|
@@ -154,14 +154,14 @@ export function useArrayField<
|
|
|
154
154
|
z.ZodNever,
|
|
155
155
|
'cast to TypedUseArrayField<T> to pass a path array'
|
|
156
156
|
>,
|
|
157
|
-
Path extends PathInSchema<T> = any
|
|
157
|
+
Path extends PathInSchema<T> = any,
|
|
158
158
|
>(field: Path): UseArrayFieldProps<FieldPath<SchemaAt<T, Path>>>
|
|
159
159
|
export function useArrayField<
|
|
160
160
|
T extends z.ZodTypeAny = z.ZodBranded<
|
|
161
161
|
z.ZodNever,
|
|
162
162
|
'cast to TypedUseArrayField<T> to pass a pathstring'
|
|
163
163
|
>,
|
|
164
|
-
Pathstring extends PathstringInSchema<T> = any
|
|
164
|
+
Pathstring extends PathstringInSchema<T> = any,
|
|
165
165
|
>(
|
|
166
166
|
field: Pathstring
|
|
167
167
|
): UseArrayFieldProps<FieldPath<SchemaAt<T, parsePathstring<Pathstring>>>>
|
package/src/useField.ts
CHANGED
|
@@ -45,17 +45,19 @@ export type UseFieldProps<Field extends FieldPath> = FieldMeta &
|
|
|
45
45
|
|
|
46
46
|
export interface TypedUseField<T extends z.ZodTypeAny> {
|
|
47
47
|
<Field extends FieldPath>(field: Field): UseFieldProps<Field>
|
|
48
|
-
<Path extends PathInSchema<T>>(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
<Pathstring extends PathstringInSchema<T>>(
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
<Path extends PathInSchema<T>>(
|
|
49
|
+
path: Path
|
|
50
|
+
): UseFieldProps<FieldPath<SchemaAt<T, Path>>>
|
|
51
|
+
<Pathstring extends PathstringInSchema<T>>(
|
|
52
|
+
path: Pathstring
|
|
53
|
+
): UseFieldProps<FieldPath<SchemaAt<T, parsePathstring<Pathstring>>>>
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
function useFieldBase<
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
function useFieldBase<
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
|
|
58
|
+
T extends z.ZodTypeAny,
|
|
59
|
+
Field extends FieldPath,
|
|
60
|
+
>(field: Field): UseFieldProps<Field> {
|
|
59
61
|
type Schema = Field['schema']
|
|
60
62
|
|
|
61
63
|
const {
|
|
@@ -77,13 +79,13 @@ function useFieldBase<T extends z.ZodTypeAny, Field extends FieldPath>(
|
|
|
77
79
|
createStructuredSelector({
|
|
78
80
|
parsedValue: ({ parsedValues }) =>
|
|
79
81
|
get(parsedValues, field.path) as z.output<Schema> | undefined,
|
|
80
|
-
value: ({ values }) => get(values, field.path)
|
|
82
|
+
value: ({ values }) => get(values, field.path),
|
|
81
83
|
initialParsedValue: ({ initialParsedValues }) =>
|
|
82
84
|
get(initialParsedValues, field.path) as
|
|
83
85
|
| z.output<Schema>
|
|
84
86
|
| undefined,
|
|
85
87
|
initialValue: ({ initialValues }) =>
|
|
86
|
-
get(initialValues, field.path)
|
|
88
|
+
get(initialValues, field.path),
|
|
87
89
|
}),
|
|
88
90
|
],
|
|
89
91
|
({
|
|
@@ -143,14 +145,14 @@ export function useField<
|
|
|
143
145
|
z.ZodNever,
|
|
144
146
|
'cast to TypedUseArray<T> to pass a path array'
|
|
145
147
|
>,
|
|
146
|
-
Path extends PathInSchema<T> = any
|
|
148
|
+
Path extends PathInSchema<T> = any,
|
|
147
149
|
>(field: Path): UseFieldProps<FieldPath<SchemaAt<T, Path>>>
|
|
148
150
|
export function useField<
|
|
149
151
|
T extends z.ZodTypeAny = z.ZodBranded<
|
|
150
152
|
z.ZodNever,
|
|
151
153
|
'cast to TypedUseArray<T> to pass a pathstring'
|
|
152
154
|
>,
|
|
153
|
-
Pathstring extends PathstringInSchema<T> = any
|
|
155
|
+
Pathstring extends PathstringInSchema<T> = any,
|
|
154
156
|
>(
|
|
155
157
|
field: Pathstring
|
|
156
158
|
): UseFieldProps<FieldPath<SchemaAt<T, parsePathstring<Pathstring>>>>
|
package/src/useFormContext.ts
CHANGED
|
@@ -6,9 +6,9 @@ export function useFormContext<
|
|
|
6
6
|
T extends z.ZodTypeAny = z.ZodBranded<
|
|
7
7
|
z.ZodNever,
|
|
8
8
|
'you must pass a schema type'
|
|
9
|
-
|
|
9
|
+
>,
|
|
10
10
|
>(): FormContextProps<T> {
|
|
11
|
-
const props: FormContextProps<T> = React.useContext(FormContext) as any
|
|
11
|
+
const props: FormContextProps<T> | null = React.useContext(FormContext) as any
|
|
12
12
|
if (!props) {
|
|
13
13
|
throw new Error(`must be used inside a <FormProvider>`)
|
|
14
14
|
}
|
package/src/useFormDispatch.ts
CHANGED
|
@@ -4,13 +4,9 @@ import z from 'zod'
|
|
|
4
4
|
import { FormAction } from './FormAction'
|
|
5
5
|
import { Dispatch } from 'redux'
|
|
6
6
|
|
|
7
|
-
export const useFormDispatch = createDispatchHook(
|
|
8
|
-
FormStateContext
|
|
9
|
-
) as typeof useFormDispatchType
|
|
10
|
-
|
|
11
|
-
declare function useFormDispatchType<
|
|
7
|
+
export const useFormDispatch = createDispatchHook(FormStateContext) as <
|
|
12
8
|
T extends z.ZodTypeAny = z.ZodBranded<
|
|
13
9
|
z.ZodNever,
|
|
14
10
|
'you must pass a schema type'
|
|
15
|
-
|
|
16
|
-
>()
|
|
11
|
+
>,
|
|
12
|
+
>() => Dispatch<FormAction<T>>
|
package/src/useHtmlField.ts
CHANGED
|
@@ -26,10 +26,11 @@ export type ValidUseHtmlFieldProps<Field extends FieldPath> = {
|
|
|
26
26
|
meta: UseFieldProps<Field>
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
export type UseHtmlFieldProps<Field extends FieldPath> =
|
|
30
|
-
Field['schema']
|
|
31
|
-
|
|
32
|
-
?
|
|
29
|
+
export type UseHtmlFieldProps<Field extends FieldPath> =
|
|
30
|
+
z.input<Field['schema']> extends (
|
|
31
|
+
string | number | bigint | boolean | null | undefined
|
|
32
|
+
) ?
|
|
33
|
+
{
|
|
33
34
|
input: HtmlFieldInputProps
|
|
34
35
|
meta: UseFieldProps<Field>
|
|
35
36
|
}
|
|
@@ -39,14 +40,12 @@ export type UseHtmlFieldProps<Field extends FieldPath> = z.input<
|
|
|
39
40
|
|
|
40
41
|
export type UseHtmlFieldOptions<
|
|
41
42
|
Field,
|
|
42
|
-
Schema extends z.ZodTypeAny = Field extends FieldPath<infer S>
|
|
43
|
-
|
|
44
|
-
: z.ZodTypeAny
|
|
43
|
+
Schema extends z.ZodTypeAny = Field extends FieldPath<infer S> ? S
|
|
44
|
+
: z.ZodTypeAny,
|
|
45
45
|
> = {
|
|
46
46
|
field: Field
|
|
47
|
-
type: z.input<Schema> extends boolean | null | undefined
|
|
48
|
-
|
|
49
|
-
: Exclude<HTMLInputTypeAttribute, 'checkbox'>
|
|
47
|
+
type: z.input<Schema> extends boolean | null | undefined ? 'checkbox'
|
|
48
|
+
: Exclude<HTMLInputTypeAttribute, 'checkbox'>
|
|
50
49
|
normalizeOnBlur?: boolean
|
|
51
50
|
}
|
|
52
51
|
|
|
@@ -121,8 +120,9 @@ function useHtmlFieldBase<Field extends FieldPath>(
|
|
|
121
120
|
})
|
|
122
121
|
if (normalizeOnBlur) {
|
|
123
122
|
const parsed = field.schema.safeParse(value)
|
|
124
|
-
const formatted =
|
|
125
|
-
|
|
123
|
+
const formatted =
|
|
124
|
+
parsed.success ?
|
|
125
|
+
invert(field.schema).safeParse(parsed.data)
|
|
126
126
|
: undefined
|
|
127
127
|
if (formatted?.success) value = formatted.data
|
|
128
128
|
setValue(value)
|
|
@@ -139,11 +139,9 @@ function useHtmlFieldBase<Field extends FieldPath>(
|
|
|
139
139
|
name: field.pathstring,
|
|
140
140
|
type,
|
|
141
141
|
value:
|
|
142
|
-
typeof value === 'boolean'
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
? value || tempValue || ''
|
|
146
|
-
: tempValue || (value == null ? '' : String(value) || ''),
|
|
142
|
+
typeof value === 'boolean' ? String(value)
|
|
143
|
+
: typeof value === 'string' ? value || tempValue || ''
|
|
144
|
+
: tempValue || (value == null ? '' : String(value) || ''),
|
|
147
145
|
...(type === 'checkbox' && { checked: Boolean(value) }),
|
|
148
146
|
onChange,
|
|
149
147
|
onFocus,
|
|
@@ -181,7 +179,7 @@ function normalizeBlank(schema: z.ZodTypeAny): any {
|
|
|
181
179
|
function safeBigInt(value: string): bigint | undefined {
|
|
182
180
|
try {
|
|
183
181
|
return BigInt(value)
|
|
184
|
-
} catch
|
|
182
|
+
} catch {
|
|
185
183
|
return undefined
|
|
186
184
|
}
|
|
187
185
|
}
|
|
@@ -219,7 +217,7 @@ export function useHtmlField<
|
|
|
219
217
|
z.ZodNever,
|
|
220
218
|
'cast to TypedUseHtmlField<T> to pass a path array'
|
|
221
219
|
>,
|
|
222
|
-
Path extends PathInSchema<T> = any
|
|
220
|
+
Path extends PathInSchema<T> = any,
|
|
223
221
|
>(
|
|
224
222
|
options: UseHtmlFieldOptions<Path, SchemaAt<T, Path>>
|
|
225
223
|
): UseHtmlFieldProps<FieldPath<SchemaAt<T, Path>>>
|
|
@@ -228,7 +226,7 @@ export function useHtmlField<
|
|
|
228
226
|
z.ZodNever,
|
|
229
227
|
'cast to TypedUseHtmlField<T> to pass a pathstring'
|
|
230
228
|
>,
|
|
231
|
-
Path extends PathstringInSchema<T> = any
|
|
229
|
+
Path extends PathstringInSchema<T> = any,
|
|
232
230
|
>(
|
|
233
231
|
options: UseHtmlFieldOptions<Path, SchemaAt<T, parsePathstring<Path>>>
|
|
234
232
|
): UseHtmlFieldProps<FieldPath<SchemaAt<T, parsePathstring<Path>>>>
|
package/src/useInitialize.ts
CHANGED
package/src/util/DeepPartial.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export type DeepPartial<T> =
|
|
2
|
-
? { [K in keyof T]?: DeepPartial<T[K]> }
|
|
3
|
-
: T
|
|
1
|
+
export type DeepPartial<T> =
|
|
2
|
+
T extends object | any[] ? { [K in keyof T]?: DeepPartial<T[K]> } : T
|
package/src/util/PathInType.ts
CHANGED
|
@@ -3,22 +3,22 @@ import { pathstring } from './pathstring'
|
|
|
3
3
|
|
|
4
4
|
export type PathInType<T> = [] | SubPathInType<T>
|
|
5
5
|
|
|
6
|
-
type SubPathInType<T> =
|
|
7
|
-
? BasePath
|
|
8
|
-
: T extends
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
: T extends readonly any[]
|
|
21
|
-
|
|
6
|
+
type SubPathInType<T> =
|
|
7
|
+
0 extends 1 & T ? BasePath
|
|
8
|
+
: T extends (
|
|
9
|
+
| string
|
|
10
|
+
| number
|
|
11
|
+
| boolean
|
|
12
|
+
| bigint
|
|
13
|
+
| symbol
|
|
14
|
+
| null
|
|
15
|
+
| undefined
|
|
16
|
+
| void
|
|
17
|
+
| ((...args: any[]) => any)
|
|
18
|
+
) ?
|
|
19
|
+
[]
|
|
20
|
+
: T extends readonly any[] ?
|
|
21
|
+
{ [K in keyof T & number]-?: [K, ...PathInType<T[K]>] }[keyof T & number]
|
|
22
22
|
: {
|
|
23
23
|
[K in keyof T]-?: K extends symbol ? never : [K, ...PathInType<T[K]>]
|
|
24
24
|
}[keyof T]
|