2024-07-12 18:45:23 -04:00
|
|
|
import { useTheme } from "@emotion/react";
|
|
|
|
|
import {
|
|
|
|
|
IconButton,
|
|
|
|
|
InputAdornment,
|
|
|
|
|
Stack,
|
|
|
|
|
TextField,
|
|
|
|
|
Typography,
|
|
|
|
|
} from "@mui/material";
|
|
|
|
|
import VisibilityOff from "@mui/icons-material/VisibilityOff";
|
|
|
|
|
import Visibility from "@mui/icons-material/Visibility";
|
2024-07-13 14:17:04 -04:00
|
|
|
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
|
|
|
|
|
import Button from "../../Button";
|
2024-07-12 18:45:23 -04:00
|
|
|
import "./index.css";
|
2024-07-13 14:17:04 -04:00
|
|
|
import { useState } from "react";
|
2024-07-12 18:45:23 -04:00
|
|
|
|
|
|
|
|
const Field = ({
|
2024-07-12 19:27:47 -04:00
|
|
|
type = "text",
|
2024-07-12 18:45:23 -04:00
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
isRequired,
|
2024-07-12 20:01:41 -04:00
|
|
|
isOptional,
|
2024-07-12 18:45:23 -04:00
|
|
|
isVisible,
|
|
|
|
|
setVisibility,
|
2024-07-13 14:17:04 -04:00
|
|
|
hasCopy,
|
2024-07-12 18:45:23 -04:00
|
|
|
autoComplete,
|
|
|
|
|
placeholder,
|
|
|
|
|
value,
|
|
|
|
|
onChange,
|
|
|
|
|
error,
|
|
|
|
|
disabled,
|
|
|
|
|
}) => {
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
|
2024-07-13 14:17:04 -04:00
|
|
|
// TODO - are we using this feature anywhere ?
|
|
|
|
|
const [copy, setCopy] = useState(false);
|
|
|
|
|
const handleCopy = () => {
|
|
|
|
|
setCopy(true);
|
|
|
|
|
setTimeout(() => setCopy(false), 1000);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-12 18:45:23 -04:00
|
|
|
return (
|
|
|
|
|
<Stack gap={theme.gap.xs} className={`field field-${type}`}>
|
|
|
|
|
{label && (
|
2024-07-12 19:27:47 -04:00
|
|
|
<Typography component="h3">
|
2024-07-12 18:45:23 -04:00
|
|
|
{label}
|
2024-07-12 20:01:41 -04:00
|
|
|
{isRequired ? <span className="field-required">*</span> : ""}
|
|
|
|
|
{isOptional ? <span className="field-optional">(optional)</span> : ""}
|
2024-07-12 18:45:23 -04:00
|
|
|
</Typography>
|
|
|
|
|
)}
|
|
|
|
|
<TextField
|
2024-07-12 19:27:47 -04:00
|
|
|
type={type === "password" ? (isVisible ? "text" : type) : type}
|
2024-07-12 18:45:23 -04:00
|
|
|
id={id}
|
|
|
|
|
autoComplete={autoComplete}
|
|
|
|
|
placeholder={placeholder}
|
2024-07-13 14:17:04 -04:00
|
|
|
multiline={type === "description"}
|
|
|
|
|
rows={type === "description" ? 4 : 1}
|
2024-07-12 18:45:23 -04:00
|
|
|
value={value}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
disabled={disabled}
|
2024-07-12 19:27:47 -04:00
|
|
|
InputProps={{
|
|
|
|
|
startAdornment: type === "url" && (
|
2024-07-12 20:01:41 -04:00
|
|
|
<Stack
|
|
|
|
|
direction="row"
|
|
|
|
|
alignItems="center"
|
|
|
|
|
height="100%"
|
|
|
|
|
sx={{
|
|
|
|
|
borderRight: `solid 1px ${theme.palette.section.borderColor}`,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography component="h5">https://</Typography>
|
|
|
|
|
</Stack>
|
2024-07-12 19:27:47 -04:00
|
|
|
),
|
2024-07-13 14:17:04 -04:00
|
|
|
endAdornment:
|
|
|
|
|
type === "password" ? (
|
|
|
|
|
<InputAdornment position="end">
|
|
|
|
|
<IconButton
|
|
|
|
|
aria-label="toggle password visibility"
|
|
|
|
|
onClick={() => setVisibility((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 position="end">
|
|
|
|
|
<Button
|
|
|
|
|
level="tertiary"
|
|
|
|
|
label={copy ? "Copied" : "Copy"}
|
|
|
|
|
img={<ContentCopyIcon />}
|
|
|
|
|
onClick={handleCopy}
|
|
|
|
|
sx={{
|
|
|
|
|
borderLeft: `solid 1px ${theme.palette.section.borderColor}`,
|
|
|
|
|
lineHeight: 0,
|
|
|
|
|
"& .MuiTouchRipple-root": {
|
|
|
|
|
pointerEvents: "none",
|
|
|
|
|
display: "none",
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
)
|
|
|
|
|
),
|
2024-07-12 19:27:47 -04:00
|
|
|
}}
|
2024-07-12 18:45:23 -04:00
|
|
|
/>
|
|
|
|
|
{error && (
|
2024-07-12 19:27:47 -04:00
|
|
|
<Typography component="span" className="input-error" mt={theme.gap.xs}>
|
2024-07-12 18:45:23 -04:00
|
|
|
{error}
|
|
|
|
|
</Typography>
|
|
|
|
|
)}
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Field;
|