Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript support for custom fieldConfig props #115

Open
michaelbrusegard opened this issue Oct 19, 2024 · 8 comments
Open

Typescript support for custom fieldConfig props #115

michaelbrusegard opened this issue Oct 19, 2024 · 8 comments

Comments

@michaelbrusegard
Copy link

I created a FieldWrapper, with a custom topDescription prop on the fieldConfig as seen below. But I get type errors that it does not exist on fieldConfig, and I was wondering how to fix it. I tried adding it to the fieldconfig type that is used in the fieldconfig function but that did not fix it, probably because it is imported from autoform/react. How can I fix this issue?

import type { FieldWrapperProps } from '@autoform/react';

import { Label } from '@/components/ui/Label';

const DISABLED_LABELS = ['boolean', 'date', 'object', 'array'];

const FieldWrapper: React.FC<FieldWrapperProps> = ({
  label,
  children,
  id,
  field,
}) => {
  const isDisabled = DISABLED_LABELS.includes(field.type);

  return (
    <div className='relative space-y-2'>
      {!isDisabled &&
        (field.fieldConfig?.topDescription ? (
          <div className='flex items-center justify-between'>
            <Label htmlFor={id}>
              {label}
              {field.required && <span className='text-destructive'> *</span>}
            </Label>
            {field.fieldConfig?.topDescription}
          </div>
        ) : (
          <Label htmlFor={id}>
            {label}
            {field.required && <span className='text-destructive'> *</span>}
          </Label>
        ))}
      {children}
      {field.fieldConfig?.description && (
        <p className='pt-2 text-[0.8rem] text-muted-foreground'>
          {field.fieldConfig.description}
        </p>
      )}
    </div>
  );
};

export { FieldWrapper };
@michaelbrusegard
Copy link
Author

michaelbrusegard commented Oct 19, 2024

In other words I got it working for the fieldconfig function used in the schema with the below type, but not with the fieldwrapper type.

type FieldConfig<T, U> = BaseFieldConfig<T, U> & {
  topDescription?: React.ReactNode;
};

@vantezzen
Copy link
Owner

I'll add support for a customData attribute in the next release, like this:

import { buildZodFieldConfig } from "@autoform/react";
import { FieldTypes } from "./AutoForm";

export const fieldConfig = buildZodFieldConfig<
  FieldTypes,
  {
     isImportant?: boolean
  }
>();

const zodFormSchema = z.object({
  username: z
    .string()
    .superRefine(
      fieldConfig({
        customData: {
          isImportant: true,
        },
      })
    ),

@michaelbrusegard
Copy link
Author

It did work to just add it to the default field props, but typescript is complaining. Also a random question, since you are using react-hook-form under the hood, is this something you want to move away from or do you want to use it? I think it is smart to use it

@vantezzen
Copy link
Owner

In the rewrite I wanted to remove the dependency on react-hook-form so it's currently not used. But I see now that it doesn't make sense to implement that functionality manually, so I will switch to using react-hook-form again in the next release!

@michaelbrusegard
Copy link
Author

@vantezzen I think in general it is a bad idea to try to circumvent react-hook-form because it have been developed for a long time and is very mature (it would probably be hard to match its efficiency). Im glad you are incorporating it back in!

@michaelbrusegard
Copy link
Author

How do I get this working now that I have updated to V2, v2.1 is not available on npm yet

@leomerida15
Copy link

I think this problem can be addressed with this issues #124

@vantezzen
Copy link
Owner

How do I get this working now that I have updated to V2, v2.1 is not available on npm yet

I think there was a problem generating the new version - it should now be live

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants