screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useRef } = React; function App() { const otpLength = 6; // You can change this to your desired OTP length const otpInputs = Array(otpLength).fill(null); const inputRefs = otpInputs.map(() => useRef(null)); const [otp, setOtp] = useState(new Array(otpLength).fill('')); const handleInputChange = (e, index) => { const value = e.target.value; if (!/^\d*$/.test(value)) return; // Only allow numeric input const newOtp = [...otp]; newOtp[index] = value; setOtp(newOtp); if (index < otpLength - 1 && value !== '') { inputRefs[index + 1].current.focus(); } }; const handleBackspace = (e, index) => { if (e.key === 'Backspace' && index > 0 && otp[index] === '') { inputRefs[index - 1].current.focus(); } }; return ( <div className='container'> <h3>React Input Mask Google-style OTP</h3> <div className="otp-container"> <span>G-</span> {otpInputs.map((_, index) => ( <input key={index} type="text" maxLength="1" value={otp[index]} onChange={(e) => handleInputChange(e, index)} onKeyDown={(e) => handleBackspace(e, index)} ref={inputRefs[index]} /> ))} </div> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { display: flex; flex-direction: column; align-items: center; margin: 0 auto; padding: 20px; width: 600px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } .otp-container { display: flex; justify-content: center; align-items: center; gap: 10px; font-size: 24px; width: 100%; } /* Style individual input fields */ .otp-container input { width: 40px; height: 40px; text-align: center; font-size: 24px; border: 2px solid #ccc; border-radius: 5px; outline: none; transition: border-color 0.2s; } /* Style focused input field */ .otp-container input:focus { border-color: #007bff; /* Change the border color when the input is focused */ } /* Style filled input field */ .otp-container input:valid { border-color: #00cc00; /* Change the border color when the input is filled */ } </style> </body> </html>