import { useState, useRef, useEffect, useMemo } from "react"; import * as CollapsiblePrimitive from "@radix-ui/react-collapsible"; import { Check, ChevronsUpDown, Search, X, Eye, EyeOff, Upload, Hash, CreditCard } from "lucide-react"; import { cn, cleanPhoneNumber } from "@/lib/utils"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Button } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Calendar } from "@/components/ui/calendar"; import { format, parse, isValid } from "date-fns"; import { CalendarIcon } from "lucide-react"; // ─── Collapsible aliases ────────────────────────────────────────────────────── const Collapsible = CollapsiblePrimitive.Root; const CollapsibleTrigger = CollapsiblePrimitive.Trigger; const CollapsibleContent = CollapsiblePrimitive.Content; // ─── Types ──────────────────────────────────────────────────────────────────── export type FormInputType = | "text" | "number" | "phone" | "email" | "password" | "date" | "select" | "searchable-select" | "file" | "textarea" | "card-number" | "card-name" | "passport-id" | "digits"; export interface SelectOption { value: string; label: string; } export interface FormInputProps { type?: FormInputType; label?: string; value?: string; onChange?: (value: string) => void; placeholder?: string; error?: string; required?: boolean; disabled?: boolean; options?: SelectOption[]; accept?: string; onFileChange?: (file: File | null) => void; fileValue?: File | null; className?: string; phonePrefix?: string; rows?: number; name?: string; readOnly?: boolean; maxLength?: number; } // ─── FieldLabel ─────────────────────────────────────────────────────────────── function FieldLabel({ label, required }: { label?: string; required?: boolean }) { if (!label) return null; return ( ); } // ─── ErrorMsg ───────────────────────────────────────────────────────────────── function ErrorMsg({ error }: { error?: string }) { if (!error) return null; return (

{error}

); } // ─── error border helper ────────────────────────────────────────────────────── const withError = (error?: string) => (error ? "border-destructive focus-visible:ring-destructive/30" : ""); // ─── TextInput ──────────────────────────────────────────────────────────────── // ─── formatNumber helpers ───────────────────────────────────────────────────── /** "1000000.50" → "1 000 000.50" (integer part only gets spaces) */ function formatNumberDisplay(raw: string): string { if (!raw) return ""; const [intPart, decPart] = raw.split("."); const formatted = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, " "); return decPart !== undefined ? `${formatted}.${decPart}` : formatted; } /** "1 000 000.50" → "1000000.50" */ function stripNumberFormat(display: string): string { return display.replace(/ /g, ""); } // ─── NumberInput ────────────────────────────────────────────────────────────── function NumberInput({ value = "", onChange, placeholder, error, disabled, required, label, className, name, readOnly }: FormInputProps) { // Display state — formatted for the user to see const [display, setDisplay] = useState(() => formatNumberDisplay(value)); // Sync display when value changes externally (e.g. form reset) useEffect(() => { const current = stripNumberFormat(display); if (current !== value) { setDisplay(formatNumberDisplay(value)); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [value]); const handleChange = (e: React.ChangeEvent) => { const raw = e.target.value; // Allow only digits, dots, and spaces (we manage spaces ourselves) const stripped = raw.replace(/ /g, ""); if (stripped !== "" && !/^\d*\.?\d*$/.test(stripped)) return; if (stripped !== "" && Number(stripped) < 0) return; setDisplay(formatNumberDisplay(stripped)); onChange?.(stripped); }; return (
); } // ─── TextInput ──────────────────────────────────────────────────────────────── function TextInput({ type = "text", value = "", onChange, placeholder, error, disabled, required, label, className, name, readOnly, rows = 3, }: FormInputProps) { const [showPass, setShowPass] = useState(false); const isPassword = type === "password"; const isTextarea = type === "textarea"; return (
{isTextarea ? (