import { forwardRef, useState, useEffect, useCallback } from 'react'; import { Input } from '@/components/ui/input'; function formatPhone(value: string): string { const digits = value.replace(/\D/g, '').slice(0, 20); return digits.replace(/(\d{4})(?=\d)/g, '$1 ').trim(); } function unformatPhone(value: string): string { return value.replace(/\s/g, ''); } const PhoneInput = forwardRef, 'value' | 'onChange'> & { value?: string; onChange?: (value: string) => void; }>(({ value, onChange, name, ...props }, ref) => { const [displayValue, setDisplayValue] = useState(() => formatPhone(value ?? '')); useEffect(() => { setDisplayValue(formatPhone(value ?? '')); }, [value]); const handleChange = useCallback( (e: React.ChangeEvent) => { const raw = unformatPhone(e.target.value); const formatted = formatPhone(raw); setDisplayValue(formatted); onChange?.(raw); }, [onChange], ); return ( <> {name && } ); }); PhoneInput.displayName = 'PhoneInput'; export { PhoneInput };