Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Autofocus back to first on error

Manav Rathid9e6ff2fee14
diff --git a/web/apps/photos/src/styles/global.css b/web/apps/photos/src/styles/global.css
index e8cf798bce..c30221d644 100644
--- a/web/apps/photos/src/styles/global.css
+++ b/web/apps/photos/src/styles/global.css
@@ -124,22 +124,3 @@ body {
.pswp__caption--empty {
display: none;
}
-
-div.otp-input input {
- width: 36px !important;
- height: 36px;
- margin: 0 10px;
-}
-
-div.otp-input input::placeholder {
- opacity: 0;
-}
-
-div.otp-input input:not(:placeholder-shown),
-div.otp-input input:focus {
- border: 2px solid #51cd7c;
- border-radius: 1px;
- -webkit-transition: 0.5s;
- transition: 0.5s;
- outline: none;
-}
diff --git a/web/packages/accounts/components/two-factor/VerifyForm.tsx b/web/packages/accounts/components/two-factor/VerifyForm.tsx
index 2881397ef8..1561568f35 100644
--- a/web/packages/accounts/components/two-factor/VerifyForm.tsx
+++ b/web/packages/accounts/components/two-factor/VerifyForm.tsx
@@ -7,7 +7,7 @@ import {
import { Box, Typography, styled } from "@mui/material";
import { Formik, type FormikHelpers } from "formik";
import { t } from "i18next";
-import { useRef, useState } from "react";
+import { useState } from "react";
import OtpInput from "react-otp-input";
interface formValues {
@@ -25,7 +25,7 @@ export type VerifyTwoFactorCallback = (
export default function VerifyTwoFactor(props: Props) {
const [waiting, setWaiting] = useState(false);
- const otpInputRef = useRef<OtpInput>(null);
+ const [shouldAutoFocus, setShouldAutoFocus] = useState(true);
const markSuccessful = async () => {
setWaiting(false);
@@ -40,11 +40,13 @@ export default function VerifyTwoFactor(props: Props) {
await props.onSubmit(otp, markSuccessful);
} catch (e) {
resetForm();
- for (let i = 0; i < 6; i++) {
- otpInputRef.current?.focusPrevInput();
- }
const message = e instanceof Error ? e.message : "";
setFieldError("otp", `${t("generic_error_retry")} ${message}`);
+ // Workaround (toggling shouldAutoFocus) to reset the focus back to
+ // the first input field in case of errors.
+ // https://github.com/devfolioco/react-otp-input/issues/420
+ setShouldAutoFocus(false);
+ setTimeout(() => setShouldAutoFocus(true), 100);
}
setWaiting(false);
};
@@ -71,21 +73,17 @@ export default function VerifyTwoFactor(props: Props) {
</Typography>
<Box my={2}>
<OtpInput
+ shouldAutoFocus={shouldAutoFocus}
value={values.otp}
onChange={onChange(
handleChange("otp"),
submitForm,
)}
numInputs={6}
- // ref={otpInputRef}
renderSeparator={<span>-</span>}
renderInput={(props) => (
<IndividualInput {...props} />
)}
- shouldAutoFocus
- // separator={"-"}
- // isInputNum
- // className={"otp-input"}
/>
{errors.otp && (
<CenteredFlex sx={{ mt: 1 }}>
@@ -123,7 +121,7 @@ const IndividualInput = styled("input")(
outline-color: ${theme.colors.accent.A300};
transition: 0.5s;
- @media (width < 30em) {
+ ${theme.breakpoints.down("sm")} {
font-size: 1rem;
padding: 4px;
min-width: 2rem;