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.27k stars 81 forks source link

Cannot load array default value #62

Closed tyghaykal closed 3 months ago

tyghaykal commented 3 months ago

Hi, i have a problem to load array default value. My code like this:

datevisits: z
      .array(
        z.object({
          date: z.date().optional(),
          time: z
            .string()
            .regex(
              /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/,
              "Time must be in format HH:MM (24 hours format)"
            )
            .optional(),
        })
      )
      .describe("Date Visits")
      .default(data.date_visits || []),

the data.date_visits will have value like this:

data.data.date_visits = data.data.date_visits.map((item: any) => {
            return {
              date: new Date(item.date),
              time: moment(item.date).format("HH:mm"),
            };
          });

The page loaded, but the date visits value are not set, and when i try to add date visits, date and string field are vanished.

vantezzen commented 3 months ago

It's probably best to use controlled values here instead of the zod .default attribute because arrays are a bit more tricky behind the scenes

tyghaykal commented 3 months ago

Already added as a feature on PR #65