vantezzen / auto-form

🌟 A React component that automatically creates a @shadcn/ui form based on a zod schema.
https://vantezzen.github.io/auto-form/
2.29k stars 81 forks source link

onValuesChange not working properly for nested objects, and arrays. #20

Closed Nicopiwi closed 11 months ago

Nicopiwi commented 11 months ago

I noticed that onValuesChange is not getting called until the nested object is valid. Example:

On page.tsx

"use client"
import { useState } from "react"
import z from "zod"

import { AutoForm } from 'ui';

const schema = z.object({
  field: z.string(),
  objField: z.object({
      nested: z.string(),
      nested2: z.boolean()
   })
});

export default function Page() {
  const [values, setValues] = useState<Partial<z.infer<typeof schema>>>({})

  function updateValues(data: Partial<z.infer<typeof schema>>){
    console.log(values)
    setValues(data)
  }

  return (
    <>
      <AutoForm
        onValuesChange={updateValues}
        values={values}
        formSchema={schema}
      />
    </>
  );
}

When updating nested but not nested2, onValuesChange is not called since nested2 is required. Maybe it's not a bug, but I would like to get track of every change in the form.

vantezzen commented 11 months ago

Looks like the actual behavour didn't match the actual behaviour - I now fixed the onValuesChange function and added a new onParsedValuesChange prop for the previous behaviour you described