import type { BaseFormBuilderProps, GenericFormElementProps, } from "@components/Form/DynamicForm.tsx"; import { Input } from "@components/UI/Input.tsx"; import type { ChangeEventHandler } from "react"; import { type FieldValues, useController } from "react-hook-form"; export interface InputFieldProps extends BaseFormBuilderProps { type: "text" | "number" | "password"; inputChange?: ChangeEventHandler; prefix?: string; properties?: { id?: string; suffix?: string; step?: number; className?: string; fieldLength?: { min?: number; max?: number; currentValueLength?: number; showCharacterCount?: boolean; }; showPasswordToggle?: boolean; showCopyButton?: boolean; }; } export function GenericInput({ control, disabled, field, invalid, }: GenericFormElementProps>) { const { fieldLength, ...restProperties } = field.properties || {}; const { field: controllerField, fieldState: { error, isDirty }, } = useController({ name: field.name, control, rules: { minLength: field.properties?.fieldLength?.min, maxLength: field.properties?.fieldLength?.max, }, }); const isInvalid = invalid || Boolean(error?.message); const handleInputChange = (e: React.ChangeEvent) => { const newValue = e.target.value; if ( field.properties?.fieldLength?.max && newValue.length > field.properties.fieldLength.max ) { return; } if (field.inputChange) field.inputChange(e); controllerField.onChange( field.type === "number" ? Number.parseFloat(newValue).toString() : newValue, ); }; const currentLength = controllerField.value ? String(controllerField.value).length : 0; return (
{fieldLength?.showCharacterCount && fieldLength.max && (
{currentLength}/{fieldLength.max}
)} {isInvalid && (

{error?.message ?? ""}

)}
); }