frontPanel/src/pages/Questions/Select.tsx

119 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-08-24 20:53:27 +00:00
import { useState, useEffect } from "react";
2023-08-15 14:04:01 +00:00
import {
Select as MuiSelect,
MenuItem,
FormControl,
useTheme,
} from "@mui/material";
import ArrowDown from "@icons/ArrowDownIcon";
import type { SelectChangeEvent, SxProps } from "@mui/material";
type SelectProps = {
items: string[];
2023-08-24 20:53:27 +00:00
activeItemIndex?: number;
2023-08-15 14:04:01 +00:00
empty?: boolean;
2023-08-16 12:00:35 +00:00
onChange?: (item: string, num: number) => void;
2023-08-15 14:04:01 +00:00
sx?: SxProps;
};
2023-08-24 20:53:27 +00:00
export const Select = ({
items,
activeItemIndex = 0,
empty,
onChange,
sx,
}: SelectProps) => {
const [activeItem, setActiveItem] = useState<number>(
empty ? -1 : activeItemIndex
);
2023-08-15 14:04:01 +00:00
const theme = useTheme();
2023-08-24 20:53:27 +00:00
useEffect(() => {
setActiveItem(activeItemIndex);
}, [activeItemIndex]);
2023-08-15 14:04:01 +00:00
const handleChange = (event: SelectChangeEvent) => {
2023-08-16 12:00:35 +00:00
const activeItemIndex = Number(event.target.value);
setActiveItem(activeItemIndex);
onChange?.(items[activeItemIndex], activeItemIndex);
2023-08-15 14:04:01 +00:00
};
return (
<FormControl
fullWidth
size="small"
2023-08-16 12:00:35 +00:00
sx={{ width: "100%", height: "48px", ...sx }}
2023-08-15 14:04:01 +00:00
>
<MuiSelect
id="display-select"
variant="outlined"
2023-08-15 14:15:48 +00:00
value={activeItem === -1 ? "" : String(activeItem)}
2023-08-15 14:04:01 +00:00
displayEmpty
onChange={handleChange}
sx={{
width: "100%",
height: "48px",
borderRadius: "8px",
"& .MuiOutlinedInput-notchedOutline": {
border: `1px solid ${theme.palette.brightPurple.main} !important`,
height: "48px",
borderRadius: "10px",
},
}}
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": {
backgroundColor: theme.palette.background.default,
color: theme.palette.brightPurple.main,
},
},
},
}}
inputProps={{
sx: {
color: theme.palette.brightPurple.main,
display: "flex",
alignItems: "center",
px: "9px",
gap: "20px",
},
}}
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",
color: theme.palette.grey2.main,
}}
>
{item}
</MenuItem>
))}
</MuiSelect>
</FormControl>
);
};