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

Default value priority #68

Open cipriancaba opened 3 months ago

cipriancaba commented 3 months ago

I've hit a situation where I define the values in the schema, which is stored in a static file, but I want to override the default values via fieldConfig.inputProps which I feel should get priority

I would suggest the following change here:

https://github.com/vantezzen/auto-form/blob/0143f43b42e0081b5f3dae728f2c9eb1d49964f3/src/components/ui/auto-form/utils.ts#L105

Instead of

if (
        (defaultValue === null || defaultValue === "") &&
        fieldConfig?.[key]?.inputProps
      ) {
        defaultValue = (fieldConfig?.[key]?.inputProps as unknown as any)
          .defaultValue;
      }

I would change this to:

if ((fieldConfig?.[key]?.inputProps as unknown as any)?.defaultValue) {
        defaultValue = (fieldConfig?.[key]?.inputProps as unknown as any)
          .defaultValue
      }

This way the inputProps will get the highest prio and override the schema default value. What do you think @vantezzen ?