All files / src/shared/InputTags GiveInputWithTags.tsx

87.5% Statements 7/8
36.36% Branches 4/11
83.33% Functions 5/6
87.5% Lines 7/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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                        4x                     47x                     21x 3x       56x                         9x 9x                                                                      
import React from "react";
import { Autocomplete } from "@mui/material";
import { FieldError } from "react-hook-form";
import { GiveInput, InputProps } from "@shared/GiveInputs/GiveInput";
import GiveChip, { IChipProps } from "@shared/Chip/GiveChip";
import { isArray } from "lodash";
import GiveText from "@shared/Text/GiveText";
 
type TGiveInputWithTags = {
  chipProps?: IChipProps;
  inputProps?: InputProps;
  onKeydown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
  handleInputChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
  handleDelete?: (index: number) => void;
  inputValue?: string;
  setInputValue?: (value: string) => void;
  error?: FieldError;
  value: any;
};
 
const GiveInputWithTags: React.FC<TGiveInputWithTags> = ({
  chipProps,
  inputProps,
  onKeydown,
  handleInputChange,
  handleDelete,
  inputValue,
  setInputValue,
  value,
  error,
}) => {
  return (
    <>
      <Autocomplete
        multiple
        disableClearable
        freeSolo
        limitTags={-1}
        options={[]}
        value={value || []}
        inputValue={inputValue}
        onInputChange={(event, newInputValue, reason) => {
          if (reason === "reset") {
            setInputValue?.("");
          }
        }}
        renderInput={(params) => (
          <GiveInput
            {...params}
            label="Enter tag"
            placeholder="Tag"
            onKeyDown={onKeydown}
            onChange={handleInputChange}
            error={!!error}
            helperText={error?.message}
            height="fit-content"
            {...inputProps}
          />
        )}
        renderTags={(value: readonly string[], getTagProps) =>
          value?.map((option: string, index: number) => (
            <GiveChip
              variant="light"
              color="default"
              size="large"
              label={option}
              {...getTagProps({ index })}
              key={index}
              {...chipProps}
              onDelete={() => handleDelete && handleDelete(index)}
            />
          ))
        }
        sx={{
          "& .MuiFormControl-root": {
            border: "none",
          },
        }}
      />
 
      {isArray(error) && error?.length > 0 && (
        <GiveText
          fontWeight="regular"
          paddingInline="10px"
          fontSize="12px"
          color="error"
          textAlign="left"
        >
          {isArray(error) && error[error?.length - 1]?.message}
        </GiveText>
      )}
    </>
  );
};
 
export default GiveInputWithTags;