'use client'; import type { BaseFormInputProps, WithPostfixProps, WithTextOnlyProps, } from '@/components/blocks/asset-form/asset-form-types'; import { getAriaAttributes } from '@/components/blocks/asset-form/asset-form-types'; import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { cn } from '@/lib/utils'; import type { ChangeEvent, ComponentPropsWithoutRef } from 'react'; import type { FieldValues } from 'react-hook-form'; const EMAIL_PATTERN = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i; const TEXT_ONLY_PATTERN = /^[A-Za-z]+$/; type InputProps = ComponentPropsWithoutRef; type AssetFormInputProps = Omit> & BaseFormInputProps & WithPostfixProps & WithTextOnlyProps; /** * A form input component that wraps shadcn's Input component with form field functionality. * Supports various input types including text, number, and email with built-in validation. * * @example * ```tsx * * ``` */ export function AssetFormInput({ label, rules, description, postfix, className, textOnly, ...props }: AssetFormInputProps) { return ( { const handleChange = (e: ChangeEvent) => { const value = e.target.value; if (props.type === 'number') { // Ensure we always pass a number or empty string, never undefined field.onChange(value === '' ? 0 : Number(value)); } else { // Ensure we always pass a string, never undefined field.onChange(value ?? ''); } }; const ariaAttrs = getAriaAttributes(field.name, !!fieldState.error, props.disabled); return ( {label && ( {label} {props.required && *} )}
{postfix && ( {postfix} )}
{description && {description}}
); }} /> ); }