Skip to content

Commit 77e97f7

Browse files
committed
Forward Ref example
1 parent d94e843 commit 77e97f7

File tree

2 files changed

+29
-5
lines changed
  • Sections/Section 10/01-starting-project/src/components

2 files changed

+29
-5
lines changed

Sections/Section 10/01-starting-project/src/components/Login/Login.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext, useEffect, useReducer, useState } from 'react';
1+
import React, { useContext, useEffect, useReducer, useRef, useState } from 'react';
22

33
import Card from '../UI/Card/Card';
44
import classes from './Login.module.css';
@@ -48,6 +48,8 @@ const Login = () => {
4848
const { isValid: passwordIsValid } = passwordState
4949

5050
const authCtx = useContext(AuthContext)
51+
const emailInputRef = useRef();
52+
const passwordInputRef = useRef();
5153

5254
useEffect(()=>{
5355
const identifier = setTimeout(() => {
@@ -93,13 +95,22 @@ const Login = () => {
9395

9496
const submitHandler = (event) => {
9597
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+
}
97107
};
98108

99109
return (
100110
<Card className={classes.login}>
101111
<form onSubmit={submitHandler}>
102112
<Input
113+
ref={emailInputRef}
103114
type="email"
104115
label="Email"
105116
id="email"
@@ -109,6 +120,7 @@ const Login = () => {
109120
onBlurHandler={validateEmailHandler}
110121
/>
111122
<Input
123+
ref={passwordInputRef}
112124
type="password"
113125
label="Password"
114126
id="password"
@@ -118,7 +130,7 @@ const Login = () => {
118130
onBlurHandler={validatePasswordHandler}
119131
/>
120132
<div className={classes.actions}>
121-
<Button type="submit" className={classes.btn} disabled={!formIsValid}>
133+
<Button type="submit" className={classes.btn}>
122134
Login
123135
</Button>
124136
</div>
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
import React, { useRef, useImperativeHandle } from 'react'
12
import classes from './Input.module.css'
23

3-
const Input = (props) => {
4+
const Input = React.forwardRef((props, ref) => {
5+
const inputRef = useRef();
6+
7+
const activate = () => {
8+
inputRef.current.focus();
9+
}
10+
useImperativeHandle(ref ,() => {
11+
return {
12+
focus: activate
13+
}})
14+
415
return (
516
<div
617
className={`${classes.control} ${
@@ -9,6 +20,7 @@ const Input = (props) => {
920
>
1021
<label htmlFor={props.id}>{props.label}</label>
1122
<input
23+
ref={inputRef}
1224
type={props.type}
1325
id={props.id}
1426
value={props.value}
@@ -17,6 +29,6 @@ const Input = (props) => {
1729
/>
1830
</div>
1931
)
20-
}
32+
})
2133

2234
export default Input

0 commit comments

Comments
 (0)