All files / src/shared/HFInputs/HFGiveCheckbox HFGiveCheckbox.tsx

88.88% Statements 8/9
54.54% Branches 6/11
100% Functions 4/4
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                          112x               1254x   1254x               1343x               5x 5x                                                               112x           347x    
import { Controller, useFormContext } from "react-hook-form";
import { Stack } from "@mui/material";
import GiveCheckbox from "@shared/GiveCheckbox/GiveCheckbox";
import GiveText from "@shared/Text/GiveText";
import { CheckboxProps } from "@shared/GiveCheckbox/type";
 
type HFCheckboxProps = CheckboxProps & {
  name: string;
  label?: string | React.ReactNode;
  helperText?: string;
  hideError?: boolean;
  inverse?: boolean;
};
export const HFCheckbox = ({
  label,
  name,
  helperText,
  hideError,
  inverse,
  ...props
}: HFCheckboxProps) => {
  const { control } = useFormContext();
 
  return (
    <Controller
      control={control}
      name={name}
      render={({
        field: { onChange, value, ref, ...rest },
        fieldState: { error },
      }) => {
        return (
          <Stack direction="column" spacing={1}>
            <Stack direction="row" alignItems="center" spacing="12px">
              <GiveCheckbox
                error={!!error}
                inputRef={ref}
                checked={inverse ? !value : value}
                onChange={(e, checked) => {
                  Iif (inverse) onChange(!checked);
                  else onChange(e);
                }}
                {...rest}
                {...props}
              />
              <Stack gap="4px">
                <GiveText variant="bodyS">{label}</GiveText>
                {helperText && typeof helperText === "string" ? (
                  <GiveText variant="bodyXS" color="secondary">
                    {helperText}
                  </GiveText>
                ) : (
                  <>{helperText}</>
                )}
              </Stack>
            </Stack>
            {error && !hideError && (
              <GiveText
                sx={{ paddingLeft: "28px" }}
                variant="bodyXS"
                color="error"
              >
                {error.message}
              </GiveText>
            )}
          </Stack>
        );
      }}
    />
  );
};
 
export const InverseHFCheckbox = ({
  name,
  ...props
}: Omit<HFCheckboxProps, "inverse">) => {
  //using watch to get the value is not a reliable method, it can have delays causing UI bugs, and flickering checkboxes
  //better to rely on the controller and use a simple prop check to inverse the value
  return <HFCheckbox {...props} name={name} inverse />;
};