diff --git a/src/components/contactForm/ContactForm.js b/src/components/contactForm/ContactForm.js index a82fb60..7114822 100644 --- a/src/components/contactForm/ContactForm.js +++ b/src/components/contactForm/ContactForm.js @@ -3,8 +3,17 @@ import "./ContactForm.css"; import { useState, useRef } from "react"; const ContactForm = () => { - const form = useRef(); + const nameInputRef = useRef(null); + const phoneInputRef = useRef(null); + const emailInputRef = useRef(null); + const messageInputRef = useRef(null); + const form = useRef(null); const [sentT, setSentT] = useState("Submit"); + const focusInput = (inputRef) => { + if (inputRef && inputRef.current) { + inputRef.current.focus(); + } + }; const handleButtonClick = (e) => { e.preventDefault(); var emailData = { @@ -29,13 +38,32 @@ const ContactForm = () => { (err) => {} ); setSentT("Whoosh! Done"); - console.log(form.current); + console.log(form.current.name); }; + + const handleClick = (e) => { + const clickedElement = e.target; + + if (clickedElement.tagName === "INPUT" || clickedElement.tagName === "TEXTAREA") { + // If the clicked element is an element, focus on it. + let inputRef = null; + if(clickedElement === nameInputRef.current){ + inputRef = nameInputRef; + }else if(clickedElement === phoneInputRef.current){ + inputRef = phoneInputRef; + }else if(clickedElement===emailInputRef.current){ + inputRef = emailInputRef; + }else if(clickedElement===messageInputRef.current){ + inputRef = messageInputRef; + } + focusInput(inputRef); + } + } return (