Added focus on render for keyboard navigation

This commit is contained in:
Daniel Cojocea committed 2024-08-09 15:59:37 -04:00
1 parent 6c0d024f2a
commit cac19d3130
2 files changed
+33 -6

No files matched your search

+16 -5
View File
@@ -12,7 +12,7 @@ import Visibility from "@mui/icons-material/Visibility";
import ContentCopyIcon from "@mui/icons-material/ContentCopy"; import ContentCopyIcon from "@mui/icons-material/ContentCopy";
import Button from "../../Button"; import Button from "../../Button";
import "./index.css"; import "./index.css";
import { useState } from "react"; import { forwardRef, useState } from "react";
/** /**
* @param {Object} props * @param {Object} props
@@ -30,9 +30,12 @@ import { useState } from "react";
* @param {function} props.onChange - Function called on input change. * @param {function} props.onChange - Function called on input change.
* @param {string} [props.error] - Error message to display for the input field. * @param {string} [props.error] - Error message to display for the input field.
* @param {boolean} [props.disabled] - Indicates if the input field is disabled. * @param {boolean} [props.disabled] - Indicates if the input field is disabled.
* @param {React.Ref} [ref] - Ref forwarded to the underlying `TextField` component. Allows for direct interactions such as focusing.
*/ */
const Field = ({ const Field = forwardRef(
(
{
type = "text", type = "text",
id, id,
label, label,
@@ -47,7 +50,9 @@ const Field = ({
onChange, onChange,
error, error,
disabled, disabled,
}) => { },
ref
) => {
const theme = useTheme(); const theme = useTheme();
// TODO - are we using this feature anywhere ? // TODO - are we using this feature anywhere ?
@@ -84,6 +89,7 @@ const Field = ({
value={value} value={value}
onChange={onChange} onChange={onChange}
disabled={disabled} disabled={disabled}
inputRef={ref}
sx={ sx={
type === "url" type === "url"
? { ? {
@@ -157,13 +163,18 @@ const Field = ({
}} }}
/> />
{error && ( {error && (
<Typography component="span" className="input-error" mt={theme.gap.xs}> <Typography
component="span"
className="input-error"
mt={theme.gap.xs}
>
{error} {error}
</Typography> </Typography>
)} )}
</Stack> </Stack>
); );
}; }
);
Field.propTypes = { Field.propTypes = {
type: PropTypes.oneOf(["text", "password", "url", "email", "description"]), type: PropTypes.oneOf(["text", "password", "url", "email", "description"]),
+17 -1
View File
@@ -1,4 +1,4 @@
import { useState, useEffect } from "react"; import { useState, useEffect, useRef } from "react";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { Box, Stack, Typography } from "@mui/material"; import { Box, Stack, Typography } from "@mui/material";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
@@ -72,6 +72,13 @@ const LandingPage = ({ onContinue }) => {
*/ */
const StepOne = ({ form, errors, onSubmit, onChange, onBack }) => { const StepOne = ({ form, errors, onSubmit, onChange, onBack }) => {
const theme = useTheme(); const theme = useTheme();
const inputRef = useRef(null);
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);
return ( return (
<> <>
@@ -92,6 +99,7 @@ const StepOne = ({ form, errors, onSubmit, onChange, onBack }) => {
value={form.email} value={form.email}
onChange={onChange} onChange={onChange}
error={errors.email} error={errors.email}
ref={inputRef}
/> />
</form> </form>
</Box> </Box>
@@ -136,6 +144,13 @@ const StepOne = ({ form, errors, onSubmit, onChange, onBack }) => {
*/ */
const StepTwo = ({ form, errors, onSubmit, onChange, onBack }) => { const StepTwo = ({ form, errors, onSubmit, onChange, onBack }) => {
const theme = useTheme(); const theme = useTheme();
const inputRef = useRef(null);
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);
return ( return (
<> <>
@@ -156,6 +171,7 @@ const StepTwo = ({ form, errors, onSubmit, onChange, onBack }) => {
value={form.password} value={form.password}
onChange={onChange} onChange={onChange}
error={errors.password} error={errors.password}
ref={inputRef}
/> />
</form> </form>
</Box> </Box>