2023-12-16 14:55:56 +00:00
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
import {
|
|
|
|
Select as MuiSelect,
|
|
|
|
MenuItem,
|
|
|
|
FormControl,
|
|
|
|
Typography,
|
|
|
|
useTheme,
|
|
|
|
} from "@mui/material";
|
|
|
|
|
|
|
|
import ArrowDown from "@icons/ArrowDownIcon";
|
|
|
|
|
|
|
|
import type { SelectChangeEvent, SxProps } from "@mui/material";
|
|
|
|
|
|
|
|
type SelectProps = {
|
|
|
|
items: string[];
|
|
|
|
activeItemIndex?: number;
|
|
|
|
empty?: boolean;
|
|
|
|
onChange?: (item: string, num: number) => void;
|
|
|
|
sx?: SxProps;
|
2023-12-29 00:58:19 +00:00
|
|
|
colorMain?: string;
|
|
|
|
colorPlaceholder?: string;
|
2023-12-16 14:55:56 +00:00
|
|
|
placeholder?: string;
|
2024-02-19 14:09:27 +00:00
|
|
|
disabled?: boolean;
|
2023-12-16 14:55:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const Select = ({
|
|
|
|
items,
|
|
|
|
activeItemIndex = 0,
|
|
|
|
empty,
|
|
|
|
onChange,
|
|
|
|
sx,
|
|
|
|
placeholder = "",
|
2023-12-29 00:58:19 +00:00
|
|
|
colorMain = "#7E2AEA",
|
|
|
|
colorPlaceholder = "#9A9AAF",
|
2024-02-19 14:09:27 +00:00
|
|
|
disabled = false,
|
2023-12-16 14:55:56 +00:00
|
|
|
}: SelectProps) => {
|
|
|
|
const [activeItem, setActiveItem] = useState<number>(
|
|
|
|
empty ? -1 : activeItemIndex
|
|
|
|
);
|
|
|
|
const theme = useTheme();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setActiveItem(activeItemIndex);
|
|
|
|
}, [activeItemIndex]);
|
|
|
|
|
|
|
|
const handleChange = (event: SelectChangeEvent) => {
|
|
|
|
const newItemIndex = Number(event.target.value);
|
|
|
|
|
|
|
|
if (newItemIndex === activeItem) {
|
|
|
|
setActiveItem(-1);
|
|
|
|
onChange?.("", -1);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setActiveItem(newItemIndex);
|
|
|
|
onChange?.(items[newItemIndex], newItemIndex);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControl
|
2024-02-19 14:09:27 +00:00
|
|
|
disabled={disabled}
|
2023-12-16 14:55:56 +00:00
|
|
|
fullWidth
|
|
|
|
size="small"
|
|
|
|
sx={{ width: "100%", height: "48px", ...sx }}
|
|
|
|
>
|
|
|
|
<MuiSelect
|
|
|
|
displayEmpty
|
|
|
|
renderValue={(value) =>
|
|
|
|
value ? (
|
|
|
|
items[Number(value)]
|
|
|
|
) : (
|
2023-12-29 00:58:19 +00:00
|
|
|
<Typography sx={{ color: colorPlaceholder }}>
|
2023-12-16 14:55:56 +00:00
|
|
|
{placeholder}
|
|
|
|
</Typography>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
id="display-select"
|
|
|
|
variant="outlined"
|
|
|
|
value={activeItem === -1 ? "" : String(activeItem)}
|
|
|
|
onChange={handleChange}
|
|
|
|
sx={{
|
|
|
|
width: "100%",
|
|
|
|
height: "48px",
|
|
|
|
borderRadius: "8px",
|
|
|
|
"& .MuiOutlinedInput-notchedOutline": {
|
2023-12-29 00:58:19 +00:00
|
|
|
border: `1px solid ${colorMain} !important`,
|
2023-12-16 14:55:56 +00:00
|
|
|
height: "48px",
|
|
|
|
borderRadius: "10px",
|
|
|
|
},
|
2024-03-13 00:10:08 +00:00
|
|
|
"& .MuiSelect-icon": {
|
|
|
|
color: theme.palette.primary.main
|
|
|
|
}
|
2023-12-16 14:55:56 +00:00
|
|
|
}}
|
|
|
|
MenuProps={{
|
|
|
|
PaperProps: {
|
|
|
|
sx: {
|
|
|
|
mt: "8px",
|
|
|
|
p: "4px",
|
|
|
|
borderRadius: "8px",
|
|
|
|
border: "1px solid #EEE4FC",
|
|
|
|
boxShadow: "0px 8px 24px rgba(210, 208, 225, 0.4)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MenuListProps: {
|
|
|
|
sx: {
|
|
|
|
py: 0,
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
gap: "8px",
|
|
|
|
"& .Mui-selected": {
|
2024-03-13 00:10:08 +00:00
|
|
|
backgroundColor: "#F2F3F7",
|
2023-12-29 00:58:19 +00:00
|
|
|
color: colorMain,
|
2023-12-16 14:55:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
inputProps={{
|
|
|
|
sx: {
|
2024-03-13 00:10:08 +00:00
|
|
|
color: theme.palette.text.primary,
|
2024-01-09 15:36:48 +00:00
|
|
|
display: "block",
|
2023-12-16 14:55:56 +00:00
|
|
|
px: "9px",
|
|
|
|
gap: "20px",
|
2024-01-09 15:36:48 +00:00
|
|
|
"& .MuiTypography-root": {
|
|
|
|
overflow: "hidden",
|
|
|
|
textOverflow: "ellipsis",
|
2024-03-04 16:40:53 +00:00
|
|
|
},
|
2023-12-16 14:55:56 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
IconComponent={(props) => <ArrowDown {...props} />}
|
|
|
|
>
|
|
|
|
{items.map((item, index) => (
|
|
|
|
<MenuItem
|
|
|
|
key={item + index}
|
|
|
|
value={index}
|
|
|
|
sx={{
|
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
|
|
|
gap: "20px",
|
|
|
|
padding: "10px",
|
|
|
|
borderRadius: "5px",
|
2023-12-29 00:58:19 +00:00
|
|
|
color: colorPlaceholder,
|
2024-02-15 01:20:35 +00:00
|
|
|
whiteSpace: "normal",
|
2024-03-04 16:40:53 +00:00
|
|
|
wordBreak: "break-word",
|
2023-12-16 14:55:56 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{item}
|
|
|
|
</MenuItem>
|
|
|
|
))}
|
|
|
|
</MuiSelect>
|
|
|
|
</FormControl>
|
|
|
|
);
|
|
|
|
};
|