-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a7cb4d
commit 8209697
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
frontend/front/src/components/SurveyComponent/HeatPumpCheckbox.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |