frontPanel/src/components/CustomSelect/CustomSelect.tsx

127 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-04-08 14:05:38 +00:00
import {
Avatar,
MenuItem,
Select,
SelectChangeEvent,
Typography,
useMediaQuery,
useTheme,
} from "@mui/material";
import Box from "@mui/material/Box";
2024-04-09 13:07:42 +00:00
import { FC, useCallback, useRef, useState } from "react";
2024-04-08 14:05:38 +00:00
import "./CustomSelect.css";
import arrow_down from "../../assets/icons/arrow_down.svg";
type CustomSelectProps = {
items: string[];
2024-04-09 13:07:42 +00:00
selectedItem: string | null;
setSelectedItem: (value: string | null) => void;
2024-04-08 14:05:38 +00:00
};
2024-04-09 13:07:42 +00:00
export const CustomSelect: FC<CustomSelectProps> = ({
2024-04-08 14:05:38 +00:00
items,
selectedItem,
setSelectedItem,
2024-04-09 13:07:42 +00:00
}) => {
2024-04-08 14:05:38 +00:00
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down(600));
const [opened, setOpened] = useState<boolean>(false);
2024-04-09 13:07:42 +00:00
const [currentValue, setCurrentValue] = useState<string | null>(selectedItem);
2024-04-08 14:05:38 +00:00
const ref = useRef<HTMLDivElement | null>(null);
2024-04-09 13:07:42 +00:00
const onSelectItem = useCallback(
2024-04-08 14:05:38 +00:00
(event: SelectChangeEvent<HTMLDivElement>) => {
2024-04-09 13:07:42 +00:00
const newValue = event.target.value.toString();
setCurrentValue(newValue);
setSelectedItem(newValue);
2024-04-08 14:05:38 +00:00
},
2024-04-09 13:07:42 +00:00
[setSelectedItem, setCurrentValue],
2024-04-08 14:05:38 +00:00
);
const toggleOpened = useCallback(() => {
setOpened((isOpened) => !isOpened);
}, []);
return (
<Box>
<Box
sx={{
2024-04-09 13:07:42 +00:00
zIndex: 3,
2024-04-08 14:05:38 +00:00
position: "relative",
width: "100%",
height: "56px",
padding: "5px",
2024-04-09 13:07:42 +00:00
color: currentValue === null ? "#9A9AAF" : "#7E2AEA",
2024-04-08 14:05:38 +00:00
border: "2px solid #ffffff",
borderRadius: "30px",
background: "#EFF0F5",
display: "flex",
alignItems: "center",
cursor: "pointer",
}}
onClick={() => ref.current?.click()}
>
<Avatar sx={{ width: 46, height: 46, marginRight: 1 }} />
<Typography
sx={{
marginLeft: isMobile ? "10px" : "20px",
2024-04-09 13:07:42 +00:00
fontWeight: currentValue === null ? "400" : "500",
2024-04-08 14:05:38 +00:00
fontSize: isMobile ? "14px" : "16px",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
overflow: "hidden",
flexGrow: 1,
}}
>
2024-04-09 13:07:42 +00:00
{currentValue || "Выберите ответственного за сделку"}
2024-04-08 14:05:38 +00:00
</Typography>
<img
src={arrow_down}
alt="check"
style={{
position: "absolute",
top: "50%",
right: "10px",
transform: `translateY(-50%) rotate(${opened ? "180deg" : "0deg"}`,
height: "36px",
width: "36px",
}}
className={`select-icon ${opened ? "opened" : ""}`}
/>
</Box>
<Select
ref={ref}
className="select"
2024-04-09 13:07:42 +00:00
value={""}
2024-04-08 14:05:38 +00:00
open={opened}
MenuProps={{
disablePortal: true,
PaperProps: {
style: {
2024-04-09 13:07:42 +00:00
zIndex: 2,
2024-04-08 14:05:38 +00:00
maxHeight: "300px",
overflow: "auto",
},
},
}}
sx={{ width: "100%" }}
2024-04-09 13:07:42 +00:00
onChange={onSelectItem}
2024-04-08 14:05:38 +00:00
onClick={toggleOpened}
>
2024-04-09 13:07:42 +00:00
{items.map((item) => {
const uniqueKey = `${item}-${Date.now()}`;
return (
<MenuItem
key={uniqueKey}
value={item}
sx={{ padding: "12px", zIndex: 2 }}
>
{item}
</MenuItem>
);
})}
2024-04-08 14:05:38 +00:00
</Select>
</Box>
);
};