All files / src/shared/GiveInputs GiveInput.tsx

100% Statements 12/12
97.22% Branches 35/36
100% Functions 4/4
100% Lines 12/12

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                                                            498x                                           17211x 17211x   17211x                                                                               498x   498x                                         18396x   18396x                                                                                                                                               498x   498x 286313x 18396x                        
import * as React from "react";
import {
  TextFieldProps,
  TextField,
  FormControl,
  FormLabel,
  SxProps,
  Theme,
} from "@mui/material";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { InputAdornment } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
 
export type InputProps = TextFieldProps & {
  label?: string | React.ReactNode;
  error?: boolean;
  leftContent?: string | JSX.Element;
  rightContent?: string | JSX.Element;
  currency?: string;
  inputRef?: any;
  value?: any;
  displayCounter?: boolean;
  maxLength?: number;
  height?: string | number;
  hidePlaceholder?: boolean;
  isPicker?: boolean;
  scrollableHeight?: number | string;
  controlSx?: SxProps<Theme> | undefined;
};
 
export const GiveInput = React.forwardRef<
  HTMLInputElement | HTMLDivElement,
  InputProps
>(
  (
    {
      label,
      error,
      leftContent,
      rightContent,
      currency,
      inputRef,
      displayCounter,
      maxLength,
      height,
      hidePlaceholder,
      controlSx,
      scrollableHeight,
      ...rest
    },
    ref,
  ) => {
    const isStringValue = typeof rest?.value === "string";
    const showCounter = isStringValue && displayCounter && maxLength;
 
    return (
      <FormControl
        fullWidth
        sx={{
          "& .MuiInputBase-root": {
            ...(rest?.multiline
              ? {
                  maxHeight: scrollableHeight || 200,
                  overflowY: "auto",
                }
              : {
                  maxHeight: height || "48px",
                  height: height,
                }),
          },
          ...controlSx,
        }}
      >
        <GiveTextField
          error={error}
          inputRef={inputRef}
          leftContent={leftContent}
          rightContent={rightContent}
          currency={currency}
          maxLength={maxLength}
          label={label}
          hidePlaceholder={hidePlaceholder}
          {...rest}
          ref={ref}
        />
        {showCounter && (
          <GiveText fontWeight={400} color="secondary" fontSize="12px">
            {isStringValue ? rest.value.length : 0}/{maxLength}
          </GiveText>
        )}
      </FormControl>
    );
  },
);
 
GiveInput.displayName = "GiveInput";
 
export const GiveTextField = React.forwardRef<
  HTMLInputElement | HTMLDivElement,
  InputProps
>(
  (
    {
      error,
      inputRef,
      leftContent,
      rightContent,
      currency,
      maxLength,
      inputProps = {},
      label,
      helperText,
      hidePlaceholder,
      isPicker,
      ...rest
    },
    ref,
  ) => {
    const { palette } = useAppTheme();
 
    return (
      <>
        {label && (
          <FormLabel
            error={error}
            disabled={rest.disabled}
            sx={{
              ...(error && { color: palette.primitive?.error }),
              color: palette.text.primary,
              fontSize: "14px",
              marginBottom: "8px",
              // Long / unbreakable labels (e.g. merchant-authored custom-field
              // labels) must wrap within the field width instead of overflowing
              // the layout. break-word only kicks in on overflow, so short
              // labels are unaffected.
              overflowWrap: "break-word",
              wordBreak: "break-word",
            }}
          >
            {label}
          </FormLabel>
        )}
        <StyledTextField
          error={error}
          inputRef={inputRef}
          ref={ref}
          {...rest}
          helperText={helperText == null ? null : helperText}
          sx={{
            ...(hidePlaceholder && {
              "& .MuiInputBase-root input": {
                "&::placeholder": { opacity: 0 },
              },
            }),
            ...(isPicker && {
              "& .MuiInputBase-root": {
                padding: "12px",
              },
            }),
            "& .MuiOutlinedInput-root": {
              padding: "12.8px 16px !important",
            },
            ...(rest?.sx || {}),
          }}
          InputProps={{
            ...rest.InputProps,
            ...(leftContent && {
              startAdornment: (
                <InputAdornment position="start">{leftContent}</InputAdornment>
              ),
            }),
            ...((rightContent || currency) && {
              endAdornment: (
                <InputAdornment position="end">
                  {rightContent || currency}
                </InputAdornment>
              ),
            }),
          }}
          version="two"
          inputProps={{
            maxLength,
            ...inputProps,
            "data-testid": `custom-input-${rest.name}`,
          }}
          type={rest.type}
        />
      </>
    );
  },
);
 
GiveTextField.displayName = "GiveTextField";
 
const StyledTextField = styled(TextField, {
  shouldForwardProp: (prop) => prop !== "focusViewColor",
})(({ theme, placeholder, value }) => ({
  ...(!value &&
    !!placeholder && {
      "& .MuiSelect-select::before": {
        content: `'${placeholder}'`,
        color: theme.palette.text?.secondary,
        verticalAlign: "center",
        position: "absolute",
        top: "calc( 50% - 10px )",
      },
    }),
}));