From 3040695e3135b5b9b293b2e5fe521f3eef7a81fc Mon Sep 17 00:00:00 2001 From: SKairinos Date: Wed, 26 Jun 2024 15:55:27 +0000 Subject: [PATCH] fix checkbox field --- src/components/form/CheckboxField.tsx | 94 +++++++++------------------ src/theme/components/MuiCheckbox.ts | 5 +- 2 files changed, 33 insertions(+), 66 deletions(-) diff --git a/src/components/form/CheckboxField.tsx b/src/components/form/CheckboxField.tsx index 6fc6cf38..1fecea27 100644 --- a/src/components/form/CheckboxField.tsx +++ b/src/components/form/CheckboxField.tsx @@ -1,105 +1,69 @@ -import { Error as ErrorIcon } from "@mui/icons-material" import { Checkbox, + FormControl, FormControlLabel, - Stack, + FormHelperText, type CheckboxProps, type FormControlLabelProps, } from "@mui/material" -import { - Field, - type FieldConfig, - type FieldProps, - type FieldValidator, -} from "formik" -import React from "react" -import { BooleanSchema, ValidationError, bool as YupBool } from "yup" +import { Field, type FieldConfig, type FieldProps } from "formik" +import { type FC } from "react" +import { bool as YupBool } from "yup" -import { form as formTypography } from "../../theme/typography" -import { wrap } from "../../utils/general" -import ClickableTooltip from "../ClickableTooltip" +import { schemaToFieldValidator } from "../../utils/form" export interface CheckboxFieldProps - extends Omit { - formControlLabelProps: Omit - validate?: FieldValidator | BooleanSchema - marginBottom?: number | string + extends Omit< + CheckboxProps, + "defaultChecked" | "id" | "value" | "onChange" | "onBlur" + > { name: string + formControlLabelProps: Omit + errorMessage?: string } -const CheckboxField: React.FC = ({ +const CheckboxField: FC = ({ + name, formControlLabelProps, - validate = YupBool(), required = false, - marginBottom = formTypography.marginBottom, - name, - onChange, + errorMessage = "this is a required field", ...otherCheckboxProps }) => { - if (required && validate instanceof BooleanSchema) { - validate = validate.oneOf([true], "this is a required field") - } + let validate = YupBool() + if (required) validate = validate.oneOf([true], errorMessage) const fieldConfig: FieldConfig = { name, type: "checkbox", - validate: async value => { - if (validate instanceof BooleanSchema) { - try { - await validate.validate(value) - } catch (error) { - if (error instanceof ValidationError) { - return error.errors[0] - } - throw error - } - } else { - return await validate(value) - } - }, + validate: schemaToFieldValidator(validate), } return ( {({ form, meta }: FieldProps) => { - // TODO: simplify this component and remove this state. - // eslint-disable-next-line react-hooks/rules-of-hooks - const [showError, setShowError] = React.useState(false) - - onChange = wrap( - { - after: (_, checked) => { - setShowError(true) - form.setFieldValue(name, checked, true) - }, - }, - onChange, - ) + const error = form.touched[name] && Boolean(form.errors[name]) + // https://mui.com/material-ui/react-checkbox/#formgroup return ( - + } {...formControlLabelProps} /> - {showError && ![undefined, ""].includes(meta.error) && ( - - - + {error && ( + {form.errors[name] as string} )} - + ) }} diff --git a/src/theme/components/MuiCheckbox.ts b/src/theme/components/MuiCheckbox.ts index 9e149031..cf6aef6e 100644 --- a/src/theme/components/MuiCheckbox.ts +++ b/src/theme/components/MuiCheckbox.ts @@ -2,7 +2,10 @@ import type Components from "./_components" const MuiCheckbox: Components["MuiCheckbox"] = { styleOverrides: { - root: {}, + root: { + paddingLeft: "0px", + marginLeft: "-2px", + }, }, }