All files / src/sections/PayBuilder/Checkout/components CheckoutDetailsSectionItem.tsx

88.88% Statements 8/9
87.5% Branches 14/16
66.66% Functions 2/3
88.88% Lines 8/9

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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                  47x                                     134x 134x 134x 134x 134x 134x                                 134x                                                                                              
import { Stack } from "@mui/material";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import { FormDataType } from "@sections/PayBuilder/utils";
import GiveCheckbox from "@shared/GiveCheckbox/GiveCheckbox";
import GiveSwitch from "@shared/Switch/GiveSwitch";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { Controller } from "react-hook-form";
 
const CheckoutDetailsSectionItem = ({
  label,
  showRequiredLabel = false,
  secondaryText,
  name,
  isLocked,
  showRequiredSwitch,
  optionalInput,
  isLastItem = false,
}: {
  label: string;
  showRequiredLabel?: boolean;
  secondaryText?: string;
  name: keyof FormDataType["Checkout"];
  isLocked?: boolean;
  showRequiredSwitch?: boolean;
  optionalInput?: React.ReactElement;
  isLastItem?: boolean;
}) => {
  const { palette } = useAppTheme();
  const { methods } = usePayBuilderForm();
  const { watch, control } = methods;
  const values = watch();
  const isCheckboxSelected = values[`Checkout`][name].render;
  return (
    <Stack
      borderBottom={
        isLastItem ? "none" : `1px solid ${palette.border?.primary}`
      }
      padding="12px 0"
      sx={{
        overflowX: "hidden",
      }}
      spacing={2}
    >
      <Stack direction="row" justifyContent="space-between" alignItems="center">
        <Stack direction="row" spacing={1.5} flexGrow={1}>
          <Controller
            control={control}
            name={`Checkout.${name}.render`}
            render={({ field: { onChange, value } }) => {
              return (
                <GiveCheckbox
                  data-testid={`Checkout.${name}.render`}
                  checked={value}
                  disabled={isLocked}
                  onChange={onChange}
                />
              );
            }}
          />
          <Stack spacing={1} maxWidth="90%">
            <GiveText variant="bodyS">{label}</GiveText>
            {secondaryText && (
              <GiveText variant="bodyS" color="secondary">
                {secondaryText}
              </GiveText>
            )}
          </Stack>
        </Stack>
 
        <Stack direction="row" spacing={1.5}>
          {showRequiredLabel && isCheckboxSelected ? (
            <GiveText variant="bodyS" color={isLocked ? "secondary" : "primary"}>
              Required
            </GiveText>
          ) : (
            <></>
          )}
          {showRequiredSwitch && isCheckboxSelected ? (
            <Controller
              control={control}
              name={`Checkout.${name}.required` as any}
              render={({ field: { onChange, value } }) => {
                return <GiveSwitch checked={value} onChange={onChange} />;
              }}
            />
          ) : (
            <></>
          )}
        </Stack>
      </Stack>
      {optionalInput}
    </Stack>
  );
};
 
export default CheckoutDetailsSectionItem;