import type { BaseFormBuilderProps, GenericFormElementProps, } from "@components/Form/DynamicForm.tsx"; import { Input } from "@components/UI/Input.tsx"; import type { LucideIcon } from "lucide-react"; import { Eye, EyeOff } from "lucide-react"; import type { ChangeEventHandler } from "react"; import { useState } from "react"; import { useController, type FieldValues } from "react-hook-form"; export interface InputFieldProps extends BaseFormBuilderProps { type: "text" | "number" | "password"; inputChange?: ChangeEventHandler; properties?: { value?: string; prefix?: string; suffix?: string; step?: number; fieldLength?: { min?: number; max?: number; currentValueLength?: number; showCharacterCount?: boolean; }, action?: { icon: LucideIcon; onClick: () => void; }; }; } export function GenericInput({ control, disabled, field, }: GenericFormElementProps>) { const { fieldLength, ...restProperties } = field.properties || {}; const [passwordShown, setPasswordShown] = useState(false); const [currentLength, setCurrentLength] = useState(fieldLength?.currentValueLength || 0); const { field: controllerField } = useController({ name: field.name, control, }); const togglePasswordVisiblity = () => { setPasswordShown(!passwordShown); }; const handleInputChange = (e: React.ChangeEvent) => { const newValue = e.target.value; if (field.properties?.fieldLength?.max && newValue.length > field.properties?.fieldLength?.max) { return; } setCurrentLength(newValue.length); if (field.inputChange) field.inputChange(e); controllerField.onChange(field.type === "number" ? Number.parseFloat(newValue).toString() : newValue); }; return (
{fieldLength?.showCharacterCount && fieldLength?.max && (
{currentLength ?? fieldLength?.currentValueLength}/{fieldLength?.max}
)}
); }