import React, { useState, useEffect } from 'react'; import './Register.css'; const PhoneVerification = ({ phoneNumber, onVerify, onResend }) => { const [code, setCode] = useState(['', '', '', '', '', '']); const [timeLeft, setTimeLeft] = useState(60); const [isResending, setIsResending] = useState(false); useEffect(() => { if (timeLeft > 0) { const timer = setTimeout(() => setTimeLeft(timeLeft - 1), 1000); return () => clearTimeout(timer); } }, [timeLeft]); const handleChange = (index, value) => { if (/^\d$/.test(value) || value === '') { const newCode = [...code]; newCode[index] = value; setCode(newCode); // Auto-focus next input if (value && index < 5) { document.getElementById(`code-${index + 1}`).focus(); } // Submit if all fields are filled if (newCode.every((digit) => digit !== '') && index === 5) { handleSubmit(newCode.join('')); } } }; const handleKeyDown = (index, e) => { if (e.key === 'Backspace' && !code[index] && index > 0) { document.getElementById(`code-${index - 1}`).focus(); } }; const handleSubmit = (verificationCode) => { onVerify(verificationCode); }; const handleResend = async () => { setIsResending(true); await onResend(); setTimeLeft(60); setIsResending(false); }; return (
We've sent a 6-digit code to {phoneNumber}
Resend code in {timeLeft}s
) : ( )}