2024-07-24 13:16:43 -04:00
|
|
|
import PropTypes from "prop-types";
|
2024-07-24 13:11:38 -04:00
|
|
|
import { useTheme } from "@emotion/react";
|
2024-07-24 14:09:24 -04:00
|
|
|
import {
|
|
|
|
|
MenuItem,
|
|
|
|
|
Select as MuiSelect,
|
|
|
|
|
Stack,
|
|
|
|
|
Typography,
|
|
|
|
|
} from "@mui/material";
|
2024-07-24 13:11:38 -04:00
|
|
|
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
|
|
|
|
|
|
|
|
|
|
import "./index.css";
|
|
|
|
|
|
2024-07-24 13:16:43 -04:00
|
|
|
/**
|
|
|
|
|
* @component
|
|
|
|
|
* @param {object} props
|
|
|
|
|
* @param {string} props.id - The ID attribute for the select element.
|
2024-07-24 14:09:24 -04:00
|
|
|
* @param {string} props.placeholder - The label of the select element.
|
2024-07-24 13:16:43 -04:00
|
|
|
* @param {string} props.placeholder - The placeholder text when no option is selected.
|
|
|
|
|
* @param {boolean} props.isHidden - Whether the placeholder should be hidden.
|
|
|
|
|
* @param {string} props.value - The currently selected value.
|
|
|
|
|
* @param {object[]} props.items - The array of items to populate in the select dropdown.
|
|
|
|
|
* @param {string} props.items._id - The unique identifier of each item.
|
|
|
|
|
* @param {string} props.items.name - The display name of each item.
|
|
|
|
|
* @param {function} props.onChange - The function to handle onChange event.
|
|
|
|
|
* @param {object} props.sx - The custom styles object for MUI Select component.
|
|
|
|
|
* @returns {JSX.Element}
|
2024-07-24 14:28:32 -04:00
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* const frequencies = [
|
|
|
|
|
* { _id: 1, name: "1 minute" },
|
|
|
|
|
* { _id: 2, name: "2 minutes" },
|
|
|
|
|
* { _id: 3, name: "3 minutes" },
|
|
|
|
|
* ];
|
|
|
|
|
*
|
|
|
|
|
* <Select
|
|
|
|
|
* id="frequency-id"
|
|
|
|
|
* label="Check frequency"
|
|
|
|
|
* placeholder
|
|
|
|
|
* value={value}
|
|
|
|
|
* onChange={handleChange}
|
|
|
|
|
* items={frequencies}
|
|
|
|
|
* />
|
2024-07-24 13:16:43 -04:00
|
|
|
*/
|
|
|
|
|
|
2024-07-24 14:09:24 -04:00
|
|
|
const Select = ({
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
placeholder,
|
|
|
|
|
isHidden,
|
|
|
|
|
value,
|
|
|
|
|
items,
|
|
|
|
|
onChange,
|
|
|
|
|
sx,
|
|
|
|
|
}) => {
|
2024-07-24 13:11:38 -04:00
|
|
|
const theme = useTheme();
|
|
|
|
|
const itemStyles = {
|
|
|
|
|
fontSize: "var(--env-var-font-size-medium)",
|
|
|
|
|
color: theme.palette.otherColors.bluishGray,
|
|
|
|
|
textTransform: "capitalize",
|
|
|
|
|
borderRadius: `${theme.shape.borderRadius}px`,
|
|
|
|
|
margin: theme.gap.xs,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2024-07-24 14:09:24 -04:00
|
|
|
<Stack gap={theme.gap.xs} className="select-wrapper">
|
|
|
|
|
{label && <Typography component="h3">{label}</Typography>}
|
|
|
|
|
<MuiSelect
|
|
|
|
|
className="select-component"
|
|
|
|
|
value={value}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
displayEmpty
|
|
|
|
|
inputProps={{ id: id }}
|
|
|
|
|
MenuProps={{
|
|
|
|
|
PaperProps: {
|
|
|
|
|
className: "select-dropdown",
|
|
|
|
|
style: {
|
|
|
|
|
marginTop: theme.gap.xs,
|
|
|
|
|
},
|
2024-07-24 13:11:38 -04:00
|
|
|
},
|
2024-07-24 14:09:24 -04:00
|
|
|
MenuListProps: {
|
|
|
|
|
style: { padding: 0 },
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
IconComponent={KeyboardArrowDownIcon}
|
|
|
|
|
sx={{ ...sx }}
|
|
|
|
|
>
|
|
|
|
|
{placeholder && (
|
|
|
|
|
<MenuItem
|
|
|
|
|
className="select-placeholder"
|
|
|
|
|
value="0"
|
|
|
|
|
sx={{
|
|
|
|
|
display: isHidden ? "none" : "flex",
|
|
|
|
|
visibility: isHidden ? "none" : "visible",
|
|
|
|
|
...itemStyles,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{placeholder}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
)}
|
|
|
|
|
{items.map((item) => (
|
|
|
|
|
<MenuItem
|
|
|
|
|
value={item._id}
|
|
|
|
|
key={`${id}-${item._id}`}
|
|
|
|
|
sx={{
|
|
|
|
|
...itemStyles,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{item.name}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</MuiSelect>
|
|
|
|
|
</Stack>
|
2024-07-24 13:11:38 -04:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-24 13:16:43 -04:00
|
|
|
Select.propTypes = {
|
|
|
|
|
id: PropTypes.string.isRequired,
|
2024-07-24 14:09:24 -04:00
|
|
|
label: PropTypes.string,
|
2024-07-24 13:16:43 -04:00
|
|
|
placeholder: PropTypes.string,
|
|
|
|
|
isHidden: PropTypes.bool,
|
2024-07-24 14:09:24 -04:00
|
|
|
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
|
2024-07-24 13:16:43 -04:00
|
|
|
items: PropTypes.arrayOf(
|
|
|
|
|
PropTypes.shape({
|
2024-07-24 14:09:24 -04:00
|
|
|
_id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
|
2024-07-24 13:16:43 -04:00
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
|
})
|
|
|
|
|
).isRequired,
|
|
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
|
sx: PropTypes.object,
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-24 13:11:38 -04:00
|
|
|
export default Select;
|