|
| 1 | +import React, { useEffect } from "react"; |
| 2 | +import { useState, useRef } from "react"; |
| 3 | + |
| 4 | +const HandlingForm = () => { |
| 5 | + // STATE |
| 6 | + const [formData, setFormData] = useState({ |
| 7 | + name: "", |
| 8 | + email: "", |
| 9 | + }); |
| 10 | + |
| 11 | + const [isLoading, setIsLoading] = useState(false); |
| 12 | + const [isDisabled, setIsDisabled] = useState(false); |
| 13 | + // const [counter, setCounter] = useState(0); |
| 14 | + const counterCount = useRef(0); |
| 15 | + |
| 16 | + const guideMeName = useRef(); |
| 17 | + const guideMeEmail = useRef(); |
| 18 | + |
| 19 | + // STATE HANDLER FUNCTION |
| 20 | + const handleChange = (e) => { |
| 21 | + setFormData({ |
| 22 | + ...formData, |
| 23 | + [e.target.name]: e.target.value, |
| 24 | + }); |
| 25 | + console.log(formData.name); |
| 26 | + }; |
| 27 | + |
| 28 | + const handleSubmit = (e) => { |
| 29 | + e.preventDefault(); |
| 30 | + console.log(formData.name); |
| 31 | + console.log(formData.email); |
| 32 | + setIsLoading(true); |
| 33 | + setIsDisabled(true); |
| 34 | + setTimeout(() => { |
| 35 | + setIsLoading(false); |
| 36 | + setIsDisabled(false); |
| 37 | + }, 2000); |
| 38 | + clearForm(); |
| 39 | + }; |
| 40 | + |
| 41 | + const clearForm = () => { |
| 42 | + setFormData({ |
| 43 | + name: "", |
| 44 | + email: "", |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + setTimeout(() => { |
| 50 | + counterCount.current = counterCount.current + 1; |
| 51 | + // setCounter((previous) => previous + 1); |
| 52 | + }, 1000); |
| 53 | + }); |
| 54 | + |
| 55 | + const handleGuideMeName = () => { |
| 56 | + guideMeName.current.focus(); |
| 57 | + console.log(guideMeName.current); |
| 58 | + }; |
| 59 | + |
| 60 | + const handleGuideMeEmail = () => { |
| 61 | + guideMeEmail.current.focus(); |
| 62 | + console.log(guideMeEmail.current); |
| 63 | + }; |
| 64 | + |
| 65 | + return ( |
| 66 | + <div className="w-full h-screen flex items-center justify-center flex-col bg-slate-950"> |
| 67 | + <form className="flex flex-col gap-2"> |
| 68 | + <label |
| 69 | + className="text-white hover:cursor-pointer" |
| 70 | + onClick={handleGuideMeName} |
| 71 | + > |
| 72 | + FirstName : |
| 73 | + </label> |
| 74 | + <input |
| 75 | + ref={guideMeName} |
| 76 | + type="text" |
| 77 | + placeholder="name" |
| 78 | + name="name" |
| 79 | + value={formData.name} |
| 80 | + onChange={handleChange} |
| 81 | + className="field" |
| 82 | + ></input> |
| 83 | + <br></br> |
| 84 | + <label |
| 85 | + className="text-white hover:cursor-pointer" |
| 86 | + onClick={handleGuideMeEmail} |
| 87 | + > |
| 88 | + Email : |
| 89 | + </label> |
| 90 | + <input |
| 91 | + ref={guideMeEmail} |
| 92 | + type="text" |
| 93 | + placeholder="email" |
| 94 | + name="email" |
| 95 | + value={formData.email} |
| 96 | + onChange={handleChange} |
| 97 | + className="field" |
| 98 | + ></input> |
| 99 | + <br></br> |
| 100 | + <button |
| 101 | + disabled={isDisabled} |
| 102 | + onClick={handleSubmit} |
| 103 | + className="border-1 border-white rounded-md bg-white text-black hover:cursor-pointer p-1" |
| 104 | + > |
| 105 | + {isLoading ? "Submitting" : "Submit"} |
| 106 | + </button> |
| 107 | + </form> |
| 108 | + <div className="text-2xl text-white w-full mt-5 text-center"> |
| 109 | + {counterCount.current} |
| 110 | + {/* {counter} */} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + ); |
| 114 | +}; |
| 115 | + |
| 116 | +export default HandlingForm; |
0 commit comments