All files / src/components/common/BusinessProfileInputs/CustomIdentificationInput CustomIdentificationInput.tsx

10% Statements 2/20
0% Branches 0/27
0% Functions 0/16
10.52% Lines 2/19

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216                          19x                                                                                                                                                                                                                                                                                                                                                                                                             19x      
import { memo } from "react";
import Box from "@mui/material/Box";
import FormControl from "@mui/material/FormControl";
import Select from "@mui/material/Select";
import MenuItem from "@mui/material/MenuItem";
import { Text } from "@common/Text";
import DropdownIcon from "@assets/icons/dropDownIcon";
import { TextField } from "@mui/material";
import { isEmpty } from "lodash";
import { palette } from "@palette";
import { WithIdentificationLogic } from "./WithIdentificationLogic";
import { CustomIdentificationInputProps } from "./types";
 
const CustomIdentificationInput = ({
  identificationName = "documentNumber",
  disabled = false,
  helperText,
  customError,
  isCustomErrorArray,
}: CustomIdentificationInputProps) => {
  return (
    <WithIdentificationLogic
      identificationName={identificationName}
      render={({
        open,
        setOpen,
        isFocused,
        setIsFocused,
        setIsHovered,
        isHovered,
        handleChange,
        selectValue,
        options,
        handleKeyDown,
      }) =>
        function render({ field: { ref, ...rest }, fieldState: { error } }) {
          const borderColor = error
            ? palette.filled.red
            : isFocused
            ? `${palette.gray[300]}`
            : isHovered && !disabled
            ? `${palette.gray.main}`
            : `${palette?.liftedWhite[200]}`;
          const errorMessage = error?.message || helperText || "";
          return (
            <>
              <Box
                width="100%"
                sx={{
                  display: "flex",
                  alignItems: "center",
                  borderRight: "none",
                }}
                onMouseOver={() => setIsHovered(true)}
                onMouseOut={() => setIsHovered(false)}
              >
                <FormControl
                  sx={{
                    minWidth: "100px",
                    borderRadius: 0,
                    "& fieldset": { border: "none" },
                  }}
                >
                  <Select
                    open={open}
                    onClose={() => setOpen(false)}
                    onOpen={() => setOpen(true)}
                    value={selectValue}
                    onChange={handleChange}
                    disabled={disabled}
                    label="Select"
                    sx={{
                      bgcolor: "#F8F8F6",
                      ...(!disabled && {
                        "&:hover": {
                          bgcolor: `${palette.neutral[20]} !important`,
                        },
                      }),
                      border: `2px solid ${borderColor}`,
                      height: "54.13px",
                      borderRightWidth: "1px !important",
                      padding: "8px",
                      borderRadius: "8px 0 0 8px",
                      "& .MuiSelect-select": {
                        display: "flex",
                        flexDirection: "column",
                        justifyContent: "center",
                        paddingRight: "8px !important",
                        paddingLeft: "8px !important",
                        width: "auto",
                        minWidth: "80px",
                      },
                    }}
                    IconComponent={() => (
                      <Box
                        sx={{
                          cursor: disabled ? "default" : "pointer",
                          width: "100%",
                          height: "100%",
                          display: "flex",
                          alignItems: "center",
                        }}
                        onClick={() => !disabled && setOpen((prev) => !prev)}
                      >
                        <DropdownIcon
                          width={11.5}
                          height={8.5}
                          fill={palette.gray[300]}
                        />
                      </Box>
                    )}
                  >
                    {Object.values(options).map((text) => (
                      <MenuItem
                        key={text}
                        value={text}
                        sx={{
                          display: text === selectValue ? "none" : "block",
                        }}
                      >
                        <Text
                          color={palette.black[100]}
                          fontWeight="regular"
                          textAlign="left"
                          fontSize="14px"
                        >
                          {text}
                        </Text>
                      </MenuItem>
                    ))}
                  </Select>
                </FormControl>
 
                <Box flex={4} sx={{ height: "54.13px" }}>
                  <TextField
                    {...rest}
                    fullWidth
                    ref={(inputRef) => {
                      ref({
                        ...inputRef,
                        focus: () => setIsFocused(true),
                      });
                    }}
                    error={!!error}
                    disabled={disabled}
                    onFocus={() => setIsFocused(true)}
                    onBlur={() => setIsFocused(false)}
                    onKeyDown={(event) => handleKeyDown(event, rest.value)}
                    placeholder="XXXXXXXXX"
                    inputProps={{
                      style: { textTransform: "uppercase" },
                      maxLength: 20,
                    }}
                    sx={{
                      height: "100%",
                      letterSpacing: "1px",
                      "& .MuiFormHelperText-root": {
                        letterSpacing: "0px",
                      },
                      "&  .MuiInputBase-root": {
                        borderRadius: "0 8px 8px 0",
                        borderLeft: "none",
                      },
                      "& .css-1a7prda-MuiInputBase-root-MuiOutlinedInput-root":
                        {
                          padding: 0,
                          paddingLeft: "5px",
                        },
                      "& .MuiInputBase-root": {
                        borderColor: borderColor,
                        height: "100%",
                      },
                      "& .MuiInputBase-root input ": {
                        marginTop: "0 !important",
                        height: "100%",
                      },
                    }}
                    InputProps={{
                      sx: {
                        borderColor,
                      },
                    }}
                  />
                </Box>
              </Box>
 
              {!isEmpty(errorMessage) && (
                <>
                  {isCustomErrorArray?.includes(errorMessage) && customError ? (
                    customError(errorMessage)
                  ) : (
                    <Text
                      fontWeight="regular"
                      paddingInline="12px"
                      fontSize="12px"
                      color={palette.gray[300]}
                      marginTop="4px"
                      textAlign="left"
                    >
                      {errorMessage}
                    </Text>
                  )}
                </>
              )}
            </>
          );
        }
      }
    />
  );
};
 
CustomIdentificationInput.displayName = "CustomIdentificationInput";
 
export default memo(CustomIdentificationInput);