Skip to content

Commit

Permalink
Add new checkbox component (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewy-gh authored Oct 16, 2024
1 parent 1a7cb4d commit 8209697
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions frontend/front/src/components/SurveyComponent/HeatPumpCheckbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { useController, Controller } from "react-hook-form";
import {
Checkbox,
FormControl,
FormControlLabel,
FormGroup,
FormLabel,
FormHelperText,
Stack,
Typography,
} from "@mui/material";

export const HeatPumpCheckbox = ({
name,
control,
options,
label,
disabled,
styles = {},
required,
}) => {
const { formState } = useController({ name, control });
const fieldError = formState.errors[name];
return (
<FormControl component="fieldset" disabled={disabled} error={!!fieldError}>
<FormLabel
component="legend"
sx={{
...styles?.label,
marginBottom: "1rem",
}}
>
{label}
</FormLabel>
<FormGroup>
<Stack spacing={2}>
{options.map((option) => (
<Controller
key={`option-${option.value}`}
name={name}
control={control}
rules={required && { required: "This field is required" }}
render={({ field }) => (
<FormControlLabel
control={
<Checkbox
id={option.label}
checked={field.value?.includes(option.value) || false}
onChange={(e) => {
const newValue = e.target.checked
? [...(field.value || []), option.value]
: field.value.filter((v) => v !== option.value);
field.onChange(newValue);
}}
value={option.value}
inputProps={{ "aria-label": "controlled" }}
/>
}
label={
<Typography
sx={{
...styles?.label,
}}
>
{option.label}
</Typography>
}
/>
)}
/>
))}
</Stack>
</FormGroup>
{!!fieldError && required && (
<FormHelperText>{fieldError?.message}</FormHelperText>
)}
</FormControl>
);
};

0 comments on commit 8209697

Please sign in to comment.