frontAnswerer/lib/components/ViewPublicationPage/ContactForm/CustomInput/CustomInput.tsx

100 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-05-31 16:41:18 +00:00
import { Box, InputAdornment, TextField as MuiTextField, TextFieldProps, Typography, useTheme } from "@mui/material";
import { useRootContainerSize } from "@contexts/RootContainerWidthContext.ts";
import { useIMask, IMask } from "react-imask";
2024-05-31 16:41:18 +00:00
import { quizThemes } from "@utils/themes/Publication/themePublication.ts";
import { ChangeEvent, FC, HTMLInputTypeAttribute, useEffect, useState } from "react";
2024-05-31 16:41:18 +00:00
import { CountrySelector } from "@/components/ViewPublicationPage/ContactForm/CustomInput/CountrySelector/CountrySelector.tsx";
import { phoneMasksByCountry } from "@utils/phoneMasksByCountry.tsx";
2025-05-01 13:15:54 +00:00
import { useQuizStore } from "@/stores/useQuizStore";
type InputProps = {
2024-05-31 16:41:18 +00:00
title: string;
desc: string;
Icon: FC<{ color: string; backgroundColor: string }>;
onChange: TextFieldProps["onChange"];
2024-07-11 20:12:13 +00:00
onChangePhone?: (phone: string) => void;
2024-05-31 16:41:18 +00:00
id: string;
isPhone?: boolean;
type?: HTMLInputTypeAttribute;
2024-07-11 20:12:13 +00:00
value?: string;
};
const TextField = MuiTextField as unknown as FC<TextFieldProps>;
let first = true;
function phoneChange(e: ChangeEvent<HTMLInputElement>, mask: string) {
const masked = IMask.createMask({
mask: "+7 (000) 000-00-00",
// ...and other options
});
masked.value = e.target.value;
const a = IMask.pipe(e.target.value, {
mask,
});
return a || "";
}
2024-07-11 20:12:13 +00:00
export const CustomInput = ({ title, desc, Icon, onChange, onChangePhone, isPhone, type, value }: InputProps) => {
2024-05-31 16:41:18 +00:00
const theme = useTheme();
const isMobile = useRootContainerSize() < 600;
2025-05-01 13:15:54 +00:00
const { settings } = useQuizStore();
2024-05-31 16:41:18 +00:00
const [mask, setMask] = useState(phoneMasksByCountry["RU"][1]);
// const { ref } = useIMask({ mask });
2024-05-31 16:41:18 +00:00
return (
<Box m="10px 0">
<Typography
mb="7px"
color={theme.palette.text.primary}
fontSize={"16px"}
>
2024-05-31 16:41:18 +00:00
{title}
</Typography>
2024-05-31 16:41:18 +00:00
<TextField
// inputRef={isPhone ? ref : null}
//@ts-ignore
2024-07-15 22:06:38 +00:00
onChange={(e: ChangeEvent<HTMLInputElement>) =>
isPhone ? onChangePhone?.(phoneChange(e, mask)) : onChange?.(e)
}
type={isPhone ? "tel" : type}
value={value}
2024-05-31 16:41:18 +00:00
sx={{
width: isMobile ? "100%" : "390px",
backgroundColor: theme.palette.background.default,
fontSize: "16px",
"& .MuiOutlinedInput-notchedOutline": {
borderColor: "#9A9AAF80",
borderRadius: "12px",
},
"& .MuiInputBase-root": {
paddingLeft: 0,
},
"& .MuiOutlinedInput-input": {
paddingLeft: "10px",
},
"& .MuiOutlinedInput-root": {
"&:hover fieldset": {
borderColor: theme.palette.primary.main,
},
},
}}
placeholder={desc}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<Icon
color="gray"
backgroundColor={quizThemes[settings.cfg.theme].isLight ? "#F2F3F7" : "#F2F3F71A"}
/>
2024-05-31 16:41:18 +00:00
</InputAdornment>
),
endAdornment: (
<InputAdornment position="end">{isPhone && <CountrySelector setMask={setMask} />}</InputAdornment>
),
}}
/>
</Box>
);
};