|
1 |
| -import { useEffect, useRef, useState } from "react"; |
| 1 | +import {useState } from "react"; |
2 | 2 |
|
3 | 3 | const SimpleInput = (props) => {
|
4 |
| - const inputRef = useRef(); |
5 |
| - // for using once |
6 |
| - const [enteredName,setEnteredName] = useState(''); |
| 4 | + const [enteredName, setEnteredName] = useState(''); |
7 | 5 | // for storing values after every key stroke
|
| 6 | + const [enteredEmail, setEnteredEmail] = useState(''); |
| 7 | + const [enteredEmailTouched, setEnteredEmailTouched] = useState(false); |
8 | 8 |
|
9 | 9 | //VALIDATION
|
10 |
| - const [isNameValid, setIsNameValid] = useState(false);//if set to true initially, might create a problem like in useEffect |
11 | 10 | const [inputTouched, setInputTouched] = useState(false);
|
12 | 11 |
|
13 |
| - useEffect(() => { |
14 |
| - if (isNameValid) |
15 |
| - console.log("VALID NAME");//in case we might need to send a http request whenever name is valid |
16 |
| - },[isNameValid]) |
| 12 | + const isNameValid = !(enteredName.trim() === "");//since component will be re-rendered after every keystroke |
| 13 | + const nameInputInvalid = !isNameValid && inputTouched; |
| 14 | + |
| 15 | + const isEmailValid = enteredEmail.includes('@'); |
| 16 | + const emailInputInvalid = !isEmailValid && enteredEmailTouched; |
17 | 17 |
|
18 |
| - const nameInputHandler = event=> { |
| 18 | + let isFormValid = false; |
| 19 | + if(isNameValid && isEmailValid) |
| 20 | + isFormValid = true; |
| 21 | + |
| 22 | + const nameInputHandler = event => { |
19 | 23 | setEnteredName(event.target.value);
|
20 |
| - // Remove the error when input becomes valid (validationg after every keystroke) |
21 |
| - //we will use event.target.value instead of entered name as state updtaes aren't scheduled by React |
22 |
| - //not processed immediately |
23 |
| - if (event.target.value.trim() !== "") { |
24 |
| - setIsNameValid(true); |
25 |
| - } |
26 | 24 | }
|
| 25 | + const emailInputChangeHandler = (event) => { |
| 26 | + setEnteredEmail(event.target.value); |
| 27 | + }; |
27 | 28 | // gets event object by default when binded to the input
|
28 | 29 |
|
29 | 30 | // validate OR to show the error when input loses it's focus
|
30 | 31 | const onBlurHandler = () => {
|
31 | 32 | setInputTouched(true);
|
32 |
| - if (enteredName.trim() === "") { |
33 |
| - setIsNameValid(false); |
34 |
| - } |
35 | 33 | }
|
| 34 | + const emailInputBlurHandler = (event) => { |
| 35 | + setEnteredEmailTouched(true); |
| 36 | + }; |
36 | 37 |
|
37 |
| - const submitHandler =event => { |
| 38 | + const submitHandler = event => { |
38 | 39 | event.preventDefault();
|
39 | 40 | // by default http request is sent to the server serving this website
|
40 | 41 | // and page will be relaoded, current states will be losed
|
41 | 42 | setInputTouched(true);
|
42 |
| - if (enteredName.trim() === "") { |
43 |
| - setIsNameValid(false); |
| 43 | + if (!isNameValid) |
44 | 44 | return;
|
45 |
| - } |
46 |
| - setIsNameValid(true); |
47 | 45 | console.log(enteredName);
|
48 |
| - const inputValue = inputRef.current.value; |
49 |
| - console.log(inputValue); |
50 | 46 | // current property of the refs holds the value assigned to ref
|
51 | 47 | // pointer to the input element holded in current
|
52 | 48 |
|
53 | 49 | //Resetting the value
|
54 | 50 | setEnteredName('');
|
| 51 | + setInputTouched(false); |
55 | 52 | // inputRef.current.value = ''; WITH refs NOT IDEAL || DONOT MANIPULATE THE REAL DOM
|
| 53 | + setEnteredEmail(''); |
| 54 | + setEnteredEmailTouched(false); |
56 | 55 | }
|
57 |
| - |
58 |
| - const nameInputInvalid = !isNameValid && inputTouched; |
59 |
| - const formClass = nameInputInvalid ? "form-control invalid" : "form-control"; |
60 |
| - return ( |
61 |
| - <form onSubmit={submitHandler}> |
62 |
| - <div className={formClass}> |
63 |
| - <label htmlFor='name'>Your Name</label> |
64 |
| - <input ref={inputRef} type='text' id='name' onChange={nameInputHandler} value={enteredName} onBlur={onBlurHandler}/> |
65 |
| - {nameInputInvalid && <p className="error-text">Enter a valid name</p>} |
66 |
| - </div> |
67 |
| - <div className="form-actions"> |
68 |
| - <button>Submit</button> |
69 |
| - </div> |
70 |
| - </form> |
71 |
| - ); |
| 56 | + |
| 57 | + const nameInputClass = nameInputInvalid ? "form-control invalid" : "form-control"; |
| 58 | + const emailInputClasses = emailInputInvalid? 'form-control invalid': 'form-control'; |
| 59 | + |
| 60 | + return ( |
| 61 | + <form onSubmit={submitHandler}> |
| 62 | + <div className={nameInputClass}> |
| 63 | + <label htmlFor='name'>Your Name</label> |
| 64 | + <input type='text' id='name' onChange={nameInputHandler} value={enteredName} onBlur={onBlurHandler}/> |
| 65 | + {nameInputInvalid && <p className="error-text">Enter a valid name</p>} |
| 66 | + </div> |
| 67 | + <div className={emailInputClasses}> |
| 68 | + <label htmlFor='email'>Your E-Mail</label> |
| 69 | + <input |
| 70 | + type='email' |
| 71 | + id='email' |
| 72 | + onChange={emailInputChangeHandler} |
| 73 | + onBlur={emailInputBlurHandler} |
| 74 | + value={enteredEmail} |
| 75 | + /> |
| 76 | + {emailInputInvalid && ( |
| 77 | + <p className='error-text'>Please enter a valid email.</p> |
| 78 | + )} |
| 79 | + </div> |
| 80 | + <div className="form-actions"> |
| 81 | + <button disabled={!isFormValid}>Submit</button> |
| 82 | + </div> |
| 83 | + </form> |
| 84 | + ); |
72 | 85 | };
|
73 | 86 |
|
74 | 87 | export default SimpleInput;
|
0 commit comments