Skip to content

Commit

Permalink
Merge pull request #14 from stevent-team/fix/partial-arrays
Browse files Browse the repository at this point in the history
Don't apply Partial to custom field if type is an array
  • Loading branch information
GRA0007 authored Jul 8, 2023
2 parents 3f5a5bb + f889c10 commit c46ba25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-camels-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stevent-team/react-zoom-form": patch
---

Don't apply Partial to custom field if type is an array
21 changes: 8 additions & 13 deletions lib/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type FieldControls<Schema extends z.ZodType> = {
formErrors: z.ZodError<z.infer<Schema>> | undefined
}

type PartialObject<T> = T extends any[] ? T : Partial<T>

export type Field<T> = {
/**
* The name of this field.
Expand All @@ -20,9 +22,9 @@ export type Field<T> = {
/** The zod schema for this field. */
schema: z.ZodType<T>
/** Reactive value of this field. */
value: Partial<T> | undefined
value: PartialObject<T> | undefined
/** Takes a new value to set `value` of this field. */
onChange: (value: Partial<T> | undefined) => void
onChange: (value: PartialObject<T> | undefined) => void
/** Array of ZodIssues for this field. */
errors: z.ZodIssue[]
}
Expand All @@ -35,18 +37,11 @@ export type Field<T> = {
export const controlled = <T>({ _field }: { _field: FieldControls<z.ZodType<T>> }): Field<T> => {
const { schema, path, formValue, setFormValue, formErrors } = _field

const name = path.map(p => p.key).join('.')

const value = getDeepProp(formValue, path) as Partial<T> | undefined
const onChange = (value: Partial<T> | undefined) => setFormValue(v => setDeepProp(v, path, value) as typeof v)

const errors = formErrors?.issues?.filter(issue => arrayStartsWith(issue.path, path.map(p => p.key))) ?? []

return {
name,
schema,
value,
onChange,
errors,
name: path.map(p => p.key).join('.'),
value: getDeepProp(formValue, path) as PartialObject<T> | undefined,
onChange: value => setFormValue(v => setDeepProp(v, path, value) as typeof v),
errors: formErrors?.issues?.filter(issue => arrayStartsWith(issue.path, path.map(p => p.key))) ?? [],
}
}

0 comments on commit c46ba25

Please sign in to comment.