Skip to content

Commit e566517

Browse files
hook(useRefHook): completed the useRef form project
1 parent cd4fc5c commit e566517

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

src/App.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
// COMPONENTS
12
import Greeting from "./components/Basics/Greeting";
23
// import CounterEffect from "./components/Hooks/useEffectHook/Counter1";
34
import Counter from "./components/Hooks/useStateHook/Counter";
45
import Form from "./project/Form";
56
import ThemeToggler from "./project/ThemeToggler";
7+
import HandlingForm from "./components/Hooks/useRefHook/HandlingForm";
68

79
// TRY IT YOURSELF COMPONENTS
810
// import ModifiedCounter from "./tryityourself/tushit/ModifiedCounter";
911

1012
const App = () => {
11-
return <Form/>;
13+
return <HandlingForm />;
1214
};
1315

1416
export default App;
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.field {
2+
@apply border-1 border-white text-white p-1 rounded-md;
3+
}

src/index.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
@import "tailwindcss";
1+
@import "tailwindcss";
2+
@import "./components/Hooks/useRefHook/HandlingForm.style.css";

0 commit comments

Comments
 (0)