Skip to content

Commit b9b5cc7

Browse files
committed
autofocus inputs
1 parent b616988 commit b9b5cc7

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

frontend/src/pages/Login/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React, { useState, ChangeEvent } from "react";
1+
import React, { useEffect, useRef, useState, ChangeEvent } from "react";
22

33
import { Alert, Button } from "components";
44
import { useAuthState } from "context";
55

66
import logo from "../../img/logo-text-vertical-grey.png";
77

88
function Login() {
9+
const emailRef = useRef(null);
910
const { login } = useAuthState();
1011
const [loading, setLoading] = useState(false);
1112
const [errorMessage, setErrorMessage] = useState("");
@@ -31,6 +32,11 @@ function Login() {
3132
setFormData({ ...formData, [target.name]: target.value });
3233
};
3334

35+
useEffect(() => {
36+
// @ts-expect-error ts-migrate(2531) FIXME: Object is possibly 'null'.
37+
emailRef.current.focus();
38+
}, []);
39+
3440
return (
3541
<div className="container-tight py-4">
3642
<div className="text-center mb-4">
@@ -46,6 +52,7 @@ function Login() {
4652
<div className="mb-3">
4753
<label className="form-label">Email address</label>
4854
<input
55+
ref={emailRef}
4956
type="email"
5057
onChange={onChange}
5158
className="form-control"

frontend/src/pages/Setup/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, ChangeEvent } from "react";
1+
import React, { useEffect, useRef, useState, ChangeEvent } from "react";
22

33
import { createUser } from "api/npm";
44
import { Alert, Button } from "components";
@@ -7,6 +7,7 @@ import { useAuthState, useHealthState } from "context";
77
import logo from "../../img/logo-text-vertical-grey.png";
88

99
function Setup() {
10+
const nameRef = useRef(null);
1011
const { refreshHealth } = useHealthState();
1112
const { login } = useAuthState();
1213
const [loading, setLoading] = useState(false);
@@ -62,6 +63,11 @@ function Setup() {
6263
setFormData({ ...formData, [target.name]: target.value });
6364
};
6465

66+
useEffect(() => {
67+
// @ts-expect-error ts-migrate(2531) FIXME: Object is possibly 'null'.
68+
nameRef.current.focus();
69+
}, []);
70+
6571
return (
6672
<div className="page page-center">
6773
<div className="container-tight py-4">
@@ -86,6 +92,7 @@ function Setup() {
8692
<div className="mb-3">
8793
<label className="form-label">Name</label>
8894
<input
95+
ref={nameRef}
8996
onChange={onChange}
9097
className="form-control"
9198
name="name"

0 commit comments

Comments
 (0)