link-stack/packages/ui/components/DisplayTextField.tsx

78 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2024-04-24 21:44:05 +02:00
import { FC } from "react";
import {
TextField as InternalTextField,
InputAdornment,
IconButton,
} from "@mui/material";
import { ContentCopy as ContentCopyIcon } from "@mui/icons-material";
import { colors } from "../styles/theme";
type DisplayTextFieldProps = {
name: string;
label: string;
value: string | number | null;
lines?: number;
helperText?: string;
copyable?: boolean;
};
export const DisplayTextField: FC<DisplayTextFieldProps> = ({
name,
label,
value,
lines = 1,
helperText,
copyable = false,
}) => {
const { almostBlack, darkGray, darkMediumGray } = colors;
const copyToClipboard = (value: any) => {
navigator?.clipboard?.writeText(value.toString());
};
return (
<InternalTextField
variant="standard"
name={name}
label={label}
size="medium"
multiline={lines > 1}
rows={lines}
defaultValue={value === "" ? " " : value}
disabled
helperText={helperText}
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: almostBlack,
},
"& .MuiFormLabel-root": {
2024-04-25 12:31:03 +02:00
fontSize: 18,
2024-04-24 21:44:05 +02:00
color: darkGray,
},
2024-04-26 16:29:13 +02:00
width: "100%",
2024-04-24 21:44:05 +02:00
}}
InputProps={{
endAdornment: copyable ? (
2024-04-25 12:31:03 +02:00
<InputAdornment position="end">
2024-04-24 21:44:05 +02:00
<IconButton
onClick={() => copyToClipboard(value)}
size="small"
color="primary"
sx={{ p: 0, color: darkMediumGray }}
>
<ContentCopyIcon />
</IconButton>
</InputAdornment>
) : null,
disableUnderline: true,
sx: {
minWidth: 0,
2024-04-25 12:31:03 +02:00
fontSize: 16,
2024-04-24 21:44:05 +02:00
backgroundColor: "transparent",
pt: "1px",
},
}}
/>
);
};