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

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-04-24 21:44:05 +02:00
import { FC } from "react";
2024-04-25 12:31:03 +02:00
import {
TextField as InternalTextField,
InputAdornment,
IconButton,
} from "@mui/material";
import { Refresh as RefreshIcon } from "@mui/icons-material";
2024-05-09 07:42:44 +02:00
import { colors, fonts } from "../styles/theme";
2024-04-24 21:44:05 +02:00
type TextFieldProps = {
name: string;
label: string;
formState: Record<string, any>;
2024-04-25 12:31:03 +02:00
refreshable?: boolean;
disabled?: boolean;
2024-04-24 21:44:05 +02:00
required?: boolean;
lines?: number;
helperText?: string;
2024-08-07 15:25:53 +02:00
updateFormState?: (field: string, value: any) => void;
2024-04-24 21:44:05 +02:00
};
export const TextField: FC<TextFieldProps> = ({
name,
label,
formState,
2024-04-25 12:31:03 +02:00
refreshable = false,
disabled = false,
2024-04-24 21:44:05 +02:00
required = false,
lines = 1,
helperText,
2024-08-07 15:25:53 +02:00
updateFormState,
2024-04-25 12:31:03 +02:00
}) => {
2024-05-09 07:42:44 +02:00
const { darkMediumGray, white } = colors;
const { roboto } = fonts;
2024-04-25 12:31:03 +02:00
return (
<InternalTextField
fullWidth
name={name}
label={label}
size="small"
disabled={disabled}
multiline={lines > 1}
rows={lines}
required={required}
defaultValue={formState.values[name]}
2024-08-07 15:25:53 +02:00
onChange={(e: any) => updateFormState?.(name, e.target.value)}
2024-04-25 12:31:03 +02:00
error={Boolean(formState.errors[name])}
helperText={formState.errors[name] ?? helperText}
InputProps={{
endAdornment: refreshable ? (
<InputAdornment position="end">
<IconButton
onClick={() => {}}
size="small"
color="primary"
sx={{ p: 0, color: darkMediumGray }}
>
<RefreshIcon />
</IconButton>
</InputAdornment>
) : null,
sx: {
2024-05-09 07:42:44 +02:00
fontFamily: roboto.style.fontFamily,
backgroundColor: white,
2024-04-25 12:31:03 +02:00
},
}}
/>
);
};