({ isOpen, onClose, onSubmit, mode })
| 2 | import PropTypes from 'prop-types'; // Import PropTypes |
| 3 | |
| 4 | const InputModal = ({ isOpen, onClose, onSubmit, mode }) => { |
| 5 | const [name, setName] = useState(''); |
| 6 | const [modalMessage, setModalMessage] = useState(''); |
| 7 | |
| 8 | const handleJoin = () => { |
| 9 | if (!name) { |
| 10 | setModalMessage("Name cannot be empty. Please enter your name."); |
| 11 | } else if (name.length < 3 || name.length > 20) { |
| 12 | setModalMessage("Name must be between 3 and 20 characters long."); |
| 13 | } else if (!/^[a-zA-Z\s]+$/.test(name)) { |
| 14 | setModalMessage("Name can only contain letters and spaces."); |
| 15 | } else { |
| 16 | onSubmit(name); // Pass the valid name to the parent component |
| 17 | onClose(); // Close the modal |
| 18 | } |
| 19 | }; |
| 20 | |
| 21 | // Prevents keyboard accessibility issues by handling Enter key for submit |
| 22 | const handleKeyDown = (event) => { |
| 23 | if (event.key === 'Enter') { |
| 24 | handleJoin(); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | if (!isOpen) return null; |
| 29 | |
| 30 | return ( /*fixed the inputmodal overlay issue in dark mode*/ |
| 31 | <div className="fixed inset-0 flex items-center justify-center bg-black bg-opacity-50 z-50"> |
| 32 | <div className={`rounded-lg shadow-lg p-8 w-[24rem] h-auto ${mode === 'dark' ? 'bg-gray-800' : 'bg-white'}`}> |
| 33 | <h2 className="text-lg font-semibold mb-4 text-center" |
| 34 | style={{ |
| 35 | color: mode === "dark" ? "white" : "black", |
| 36 | }}> |
| 37 | Enter your name to join: |
| 38 | </h2> |
| 39 | <input |
| 40 | type="text" |
| 41 | value={name} |
| 42 | onChange={(e) => { |
| 43 | setName(e.target.value); |
| 44 | setModalMessage(''); // Clear error message on input change |
| 45 | }} |
| 46 | style={{ |
| 47 | color: mode === "dark" ? "white" : "black", |
| 48 | backgroundColor: mode === "dark" ? "#444" : "white", // Adjust input background color |
| 49 | }} |
| 50 | onKeyDown={handleKeyDown} // Allow Enter key to submit |
| 51 | placeholder="Enter your name" |
| 52 | className="border rounded py-2 px-2 mb-3 w-full focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 53 | /> |
| 54 | {modalMessage && <p className="text-red-500 mb-3 text-md text-center">{modalMessage}</p>} |
| 55 | <div className="flex justify-center"> |
| 56 | <button |
| 57 | onClick={handleJoin} |
| 58 | className="w-full px-4 py-2 bg-blue-500 text-white rounded-lg shadow hover:bg-blue-600 transition duration-200" |
| 59 | > |
| 60 | Join |
| 61 | </button> |
nothing calls this directly
no outgoing calls
no test coverage detected