Files
Checkmate/Client/src/Components/Inputs/Checkbox/index.jsx
T

96 lines
2.7 KiB
React
Raw Normal View History

2024-07-27 14:32:54 -04:00
import PropTypes from "prop-types";
import { FormControlLabel, Checkbox as MuiCheckbox } from "@mui/material";
2024-08-27 17:01:28 -04:00
import { useTheme } from "@emotion/react";
2024-07-27 14:32:54 -04:00
import CheckboxOutline from "../../../assets/icons/checkbox-outline.svg?react";
import CheckboxFilled from "../../../assets/icons/checkbox-filled.svg?react";
import "./index.css";
/**
* @param {Object} props
* @param {string} props.id - The id attribute for the checkbox input.
* @param {string} props.label - The label to display next to the checkbox.
* @param {('small' | 'medium' | 'large')} - The size of the checkbox.
* @param {boolean} props.isChecked - Whether the checkbox is checked or not.
* @param {string} [props.value] - The value of the checkbox input.
* @param {function} [props.onChange] - The function to call when the checkbox value changes.
* @param {boolean} [props.isDisabled] - Whether the checkbox is disabled or not.
*
* @returns {JSX.Element}
2024-08-27 17:01:28 -04:00
*
2024-07-27 14:32:54 -04:00
* @example
* <Checkbox
* id="checkbox-id"
* label="Ping monitoring"
* isChecked={checks.type === "ping"}
* value="ping"
* onChange={handleChange}
* />
*/
const Checkbox = ({
id,
label,
size = "medium",
isChecked,
value,
onChange,
isDisabled,
}) => {
const sizes = { small: "14px", medium: "16px", large: "18px" };
2024-08-27 17:01:28 -04:00
const theme = useTheme();
2024-07-27 14:32:54 -04:00
return (
<FormControlLabel
className="checkbox-wrapper"
control={
<MuiCheckbox
checked={isDisabled ? false : isChecked}
value={value}
onChange={onChange}
icon={<CheckboxOutline />}
checkedIcon={<CheckboxFilled />}
inputProps={{
"aria-label": "controlled checkbox",
id: id,
}}
sx={{
"&:hover": { backgroundColor: "transparent" },
"& svg": { width: sizes[size], height: sizes[size] },
}}
/>
}
label={label}
disabled={isDisabled}
sx={{
2024-08-27 17:01:28 -04:00
borderRadius: theme.shape.borderRadius,
p: theme.spacing(2.5),
m: theme.spacing(-2.5),
2024-07-27 14:32:54 -04:00
"& .MuiButtonBase-root": {
2024-08-27 17:01:28 -04:00
width: theme.spacing(10),
p: 0,
mr: theme.spacing(6),
},
"&:not(:has(.Mui-disabled)):hover": {
2024-08-28 22:14:55 -04:00
backgroundColor: theme.palette.background.accent,
2024-08-27 17:01:28 -04:00
},
"& span.MuiTypography-root": {
fontSize: 13,
color: theme.palette.text.tertiary,
2024-07-27 14:32:54 -04:00
},
}}
/>
);
};
Checkbox.propTypes = {
id: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
size: PropTypes.oneOf(["small", "medium", "large"]),
isChecked: PropTypes.bool.isRequired,
value: PropTypes.string,
onChange: PropTypes.func,
isDisabled: PropTypes.bool,
};
export default Checkbox;