Added focus on render for keyboard navigation
This commit is contained in:
1 parent
6c0d024f2a
commit
cac19d3130
2 files changed
+150
-123
No files matched your search
@@ -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,140 +30,151 @@ 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",
|
(
|
||||||
id,
|
{
|
||||||
label,
|
type = "text",
|
||||||
https,
|
id,
|
||||||
isRequired,
|
label,
|
||||||
isOptional,
|
https,
|
||||||
optionalLabel,
|
isRequired,
|
||||||
hasCopy,
|
isOptional,
|
||||||
autoComplete,
|
optionalLabel,
|
||||||
placeholder,
|
hasCopy,
|
||||||
value,
|
autoComplete,
|
||||||
onChange,
|
placeholder,
|
||||||
error,
|
value,
|
||||||
disabled,
|
onChange,
|
||||||
}) => {
|
error,
|
||||||
const theme = useTheme();
|
disabled,
|
||||||
|
},
|
||||||
|
ref
|
||||||
|
) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
|
||||||
// TODO - are we using this feature anywhere ?
|
// TODO - are we using this feature anywhere ?
|
||||||
const [copy, setCopy] = useState(false);
|
const [copy, setCopy] = useState(false);
|
||||||
const handleCopy = () => {
|
const handleCopy = () => {
|
||||||
setCopy(true);
|
setCopy(true);
|
||||||
setTimeout(() => setCopy(false), 1000);
|
setTimeout(() => setCopy(false), 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
const [isVisible, setVisible] = useState(false);
|
const [isVisible, setVisible] = useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack gap={theme.gap.xs} className={`field field-${type}`}>
|
<Stack gap={theme.gap.xs} className={`field field-${type}`}>
|
||||||
{label && (
|
{label && (
|
||||||
<Typography component="h3">
|
<Typography component="h3">
|
||||||
{label}
|
{label}
|
||||||
{isRequired ? <span className="field-required">*</span> : ""}
|
{isRequired ? <span className="field-required">*</span> : ""}
|
||||||
{isOptional ? (
|
{isOptional ? (
|
||||||
<span className="field-optional">
|
<span className="field-optional">
|
||||||
{optionalLabel || "(optional)"}
|
{optionalLabel || "(optional)"}
|
||||||
</span>
|
</span>
|
||||||
) : (
|
|
||||||
""
|
|
||||||
)}
|
|
||||||
</Typography>
|
|
||||||
)}
|
|
||||||
<TextField
|
|
||||||
type={type === "password" ? (isVisible ? "text" : type) : type}
|
|
||||||
id={id}
|
|
||||||
autoComplete={autoComplete}
|
|
||||||
placeholder={placeholder}
|
|
||||||
multiline={type === "description"}
|
|
||||||
rows={type === "description" ? 4 : 1}
|
|
||||||
value={value}
|
|
||||||
onChange={onChange}
|
|
||||||
disabled={disabled}
|
|
||||||
sx={
|
|
||||||
type === "url"
|
|
||||||
? {
|
|
||||||
"& .MuiInputBase-root": { padding: 0 },
|
|
||||||
"& .MuiStack-root": {
|
|
||||||
borderTopLeftRadius: `${theme.shape.borderRadius}px`,
|
|
||||||
borderBottomLeftRadius: `${theme.shape.borderRadius}px`,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
: {}
|
|
||||||
}
|
|
||||||
InputProps={{
|
|
||||||
startAdornment: type === "url" && (
|
|
||||||
<Stack
|
|
||||||
direction="row"
|
|
||||||
alignItems="center"
|
|
||||||
height="100%"
|
|
||||||
sx={{
|
|
||||||
borderRight: `solid 1px ${theme.palette.section.borderColor}`,
|
|
||||||
backgroundColor: "#f9f9fa",
|
|
||||||
pl: theme.gap.medium,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Typography component="h5" sx={{ lineHeight: 1 }}>
|
|
||||||
{https ? "https" : "http"}://
|
|
||||||
</Typography>
|
|
||||||
</Stack>
|
|
||||||
),
|
|
||||||
endAdornment:
|
|
||||||
type === "password" ? (
|
|
||||||
<InputAdornment position="end">
|
|
||||||
<IconButton
|
|
||||||
aria-label="toggle password visibility"
|
|
||||||
onClick={() => setVisible((show) => !show)}
|
|
||||||
tabIndex={-1}
|
|
||||||
sx={{
|
|
||||||
color: theme.palette.section.borderColor,
|
|
||||||
padding: `calc(${theme.gap.xs} / 2)`,
|
|
||||||
"&:focus": {
|
|
||||||
outline: "none",
|
|
||||||
},
|
|
||||||
"& .MuiTouchRipple-root": {
|
|
||||||
pointerEvents: "none",
|
|
||||||
display: "none",
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{!isVisible ? <VisibilityOff /> : <Visibility />}
|
|
||||||
</IconButton>
|
|
||||||
</InputAdornment>
|
|
||||||
) : (
|
) : (
|
||||||
hasCopy && (
|
""
|
||||||
<InputAdornment className="copy" position="end">
|
)}
|
||||||
<Button
|
</Typography>
|
||||||
level="tertiary"
|
)}
|
||||||
label={copy ? "Copied" : "Copy"}
|
<TextField
|
||||||
img={<ContentCopyIcon />}
|
type={type === "password" ? (isVisible ? "text" : type) : type}
|
||||||
onClick={handleCopy}
|
id={id}
|
||||||
|
autoComplete={autoComplete}
|
||||||
|
placeholder={placeholder}
|
||||||
|
multiline={type === "description"}
|
||||||
|
rows={type === "description" ? 4 : 1}
|
||||||
|
value={value}
|
||||||
|
onChange={onChange}
|
||||||
|
disabled={disabled}
|
||||||
|
inputRef={ref}
|
||||||
|
sx={
|
||||||
|
type === "url"
|
||||||
|
? {
|
||||||
|
"& .MuiInputBase-root": { padding: 0 },
|
||||||
|
"& .MuiStack-root": {
|
||||||
|
borderTopLeftRadius: `${theme.shape.borderRadius}px`,
|
||||||
|
borderBottomLeftRadius: `${theme.shape.borderRadius}px`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}
|
||||||
|
}
|
||||||
|
InputProps={{
|
||||||
|
startAdornment: type === "url" && (
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
alignItems="center"
|
||||||
|
height="100%"
|
||||||
|
sx={{
|
||||||
|
borderRight: `solid 1px ${theme.palette.section.borderColor}`,
|
||||||
|
backgroundColor: "#f9f9fa",
|
||||||
|
pl: theme.gap.medium,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Typography component="h5" sx={{ lineHeight: 1 }}>
|
||||||
|
{https ? "https" : "http"}://
|
||||||
|
</Typography>
|
||||||
|
</Stack>
|
||||||
|
),
|
||||||
|
endAdornment:
|
||||||
|
type === "password" ? (
|
||||||
|
<InputAdornment position="end">
|
||||||
|
<IconButton
|
||||||
|
aria-label="toggle password visibility"
|
||||||
|
onClick={() => setVisible((show) => !show)}
|
||||||
|
tabIndex={-1}
|
||||||
sx={{
|
sx={{
|
||||||
borderLeft: `solid 1px ${theme.palette.section.borderColor}`,
|
color: theme.palette.section.borderColor,
|
||||||
lineHeight: 0,
|
padding: `calc(${theme.gap.xs} / 2)`,
|
||||||
|
"&:focus": {
|
||||||
|
outline: "none",
|
||||||
|
},
|
||||||
"& .MuiTouchRipple-root": {
|
"& .MuiTouchRipple-root": {
|
||||||
pointerEvents: "none",
|
pointerEvents: "none",
|
||||||
display: "none",
|
display: "none",
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
/>
|
>
|
||||||
|
{!isVisible ? <VisibilityOff /> : <Visibility />}
|
||||||
|
</IconButton>
|
||||||
</InputAdornment>
|
</InputAdornment>
|
||||||
)
|
) : (
|
||||||
),
|
hasCopy && (
|
||||||
}}
|
<InputAdornment className="copy" position="end">
|
||||||
/>
|
<Button
|
||||||
{error && (
|
level="tertiary"
|
||||||
<Typography component="span" className="input-error" mt={theme.gap.xs}>
|
label={copy ? "Copied" : "Copy"}
|
||||||
{error}
|
img={<ContentCopyIcon />}
|
||||||
</Typography>
|
onClick={handleCopy}
|
||||||
)}
|
sx={{
|
||||||
</Stack>
|
borderLeft: `solid 1px ${theme.palette.section.borderColor}`,
|
||||||
);
|
lineHeight: 0,
|
||||||
};
|
"& .MuiTouchRipple-root": {
|
||||||
|
pointerEvents: "none",
|
||||||
|
display: "none",
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</InputAdornment>
|
||||||
|
)
|
||||||
|
),
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
{error && (
|
||||||
|
<Typography
|
||||||
|
component="span"
|
||||||
|
className="input-error"
|
||||||
|
mt={theme.gap.xs}
|
||||||
|
>
|
||||||
|
{error}
|
||||||
|
</Typography>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
Field.propTypes = {
|
Field.propTypes = {
|
||||||
type: PropTypes.oneOf(["text", "password", "url", "email", "description"]),
|
type: PropTypes.oneOf(["text", "password", "url", "email", "description"]),
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in new issue
Block a user