Skip to content

Commit

Permalink
improve checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Sworzen1 committed Feb 14, 2024
1 parent 85389d2 commit b5452eb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion govtool/frontend/src/components/atoms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ export type SpacerProps = {
y?: number;
};

export type CheckboxProps = MUICheckboxProps & {
export type CheckboxProps = Omit<MUICheckboxProps, "onChange" | "value"> & {
dataTestId?: string;
errorMessage?: string;
onChange: (newValue: boolean) => void;
value: boolean;
};

export type FormErrorMessageProps = {
Expand Down
18 changes: 15 additions & 3 deletions govtool/frontend/src/components/molecules/Field/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Box } from "@mui/material";

import { Checkbox as CheckboxBase, Typography } from "@atoms";
import { Checkbox as CheckboxBase, FormErrorMessage, Typography } from "@atoms";

import { CheckboxFieldProps } from "./types";
import { FormErrorMessage } from "@/components/atoms/FormErrorMessage";
import { useCallback } from "react";

export const Checkbox = ({
errorMessage,
Expand All @@ -12,18 +12,30 @@ export const Checkbox = ({
labelStyles,
layoutStyles,
onChange,
value,
...rest
}: CheckboxFieldProps) => {
const handleValue = useCallback(() => {
onChange(!value);
}, [value]);

return (
<Box sx={{ width: "100%", ...layoutStyles }}>
<Box
onClick={handleValue}
sx={{
alignItems: "center",
cursor: "pointer",
display: "flex",
flexDirection: "row",
width: "fit-content",
}}
>
<CheckboxBase errorMessage={errorMessage} {...rest} />
<CheckboxBase
{...{ onChange, value }}
errorMessage={errorMessage}
{...rest}
/>
{label && (
<Typography variant="caption" {...labelStyles}>
{label}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Checkbox = ({
checked={!!field.value}
errorMessage={errorMessage}
name={field.name}
onChange={(newValue) => field.onChange(newValue)}
onChange={field.onChange}
value={field.value}
{...props}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export type ControlledInputProps = InputFieldProps & {
rules?: Omit<RegisterOptions, "valueAsNumber" | "valueAsDate" | "setValueAs">;
};

export type ControlledCheckboxProps = CheckboxFieldProps & {
export type ControlledCheckboxProps = Omit<
CheckboxFieldProps,
"onChange" | "value"
> & {
control: Control<any>;
errors: FieldErrors<any>;
name: Path<any>;
Expand Down

0 comments on commit b5452eb

Please sign in to comment.