1
- import React , { useContext , useEffect , useReducer , useState } from 'react' ;
1
+ import React , { useContext , useEffect , useReducer , useRef , useState } from 'react' ;
2
2
3
3
import Card from '../UI/Card/Card' ;
4
4
import classes from './Login.module.css' ;
@@ -48,6 +48,8 @@ const Login = () => {
48
48
const { isValid : passwordIsValid } = passwordState
49
49
50
50
const authCtx = useContext ( AuthContext )
51
+ const emailInputRef = useRef ( ) ;
52
+ const passwordInputRef = useRef ( ) ;
51
53
52
54
useEffect ( ( ) => {
53
55
const identifier = setTimeout ( ( ) => {
@@ -93,13 +95,22 @@ const Login = () => {
93
95
94
96
const submitHandler = ( event ) => {
95
97
event . preventDefault ( ) ;
96
- authCtx . onLogin ( emailState . value , passwordState . value )
98
+ if ( formIsValid ) {
99
+ authCtx . onLogin ( emailState . value , passwordState . value )
100
+ }
101
+ else if ( ! emailIsValid ) {
102
+ emailInputRef . current . focus ( )
103
+ }
104
+ else {
105
+ passwordInputRef . current . focus ( )
106
+ }
97
107
} ;
98
108
99
109
return (
100
110
< Card className = { classes . login } >
101
111
< form onSubmit = { submitHandler } >
102
112
< Input
113
+ ref = { emailInputRef }
103
114
type = "email"
104
115
label = "Email"
105
116
id = "email"
@@ -109,6 +120,7 @@ const Login = () => {
109
120
onBlurHandler = { validateEmailHandler }
110
121
/>
111
122
< Input
123
+ ref = { passwordInputRef }
112
124
type = "password"
113
125
label = "Password"
114
126
id = "password"
@@ -118,7 +130,7 @@ const Login = () => {
118
130
onBlurHandler = { validatePasswordHandler }
119
131
/>
120
132
< div className = { classes . actions } >
121
- < Button type = "submit" className = { classes . btn } disabled = { ! formIsValid } >
133
+ < Button type = "submit" className = { classes . btn } >
122
134
Login
123
135
</ Button >
124
136
</ div >
0 commit comments