All files / Autocomplete Autocomplete.tsx

100% Statements 71/71
100% Branches 34/34
100% Functions 10/10
100% Lines 57/57

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 1562x   2x 2x 2x     2x   2x     2x 36x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x   35x 35x 35x     35x 35x 35x   35x   35x 35x 35x                     35x 35x 17x     35x 22x 22x 8x 7x   18x                                           35x   3x   3x 3x   3x 3x   35x   3x       3x   35x 1x   35x 5x 5x 5x     35x                                                           66x                         2x  
import React, { useEffect } from "react";
 
import Menu from "../Menu/Menu";
import { IconArrowDown } from "@tabler/icons-react";
import Input from "../Input/Input";
import { AutocompleteProps } from "./Autocomplete.types";
import { MenuItemKeyProps } from "../MenuItem/MenuItem.types";
import MenuItem from "../MenuItem/MenuItem";
// import { useTheme } from "../ThemeProvider/ThemeProvider";
import colorTokens from "../../tokens/colors.json";
// import { getColorPalette } from "../../helpers/helpers";
 
const Autocomplete: React.FC<AutocompleteProps> = ({
  id,
  options,
  placeholder,
  value,
  className,
  style,
  label,
  open,
  multiSelect,
  color = colorTokens.default.primary.main,
  width = "15rem",
  variant = "contained",
  size = "medium", 
  // Events
  onChange,
  onClick,
  onInputChange,
 
  // Test Props
  "data-testid": dataTestId,
  "data-testid-input": dataTestIdInput,
  "data-testid-menu": dataTestIdMenu,
 
  ...props
}) => {
  const inputRef = React.useRef<HTMLInputElement>(null);
  const [isOpen, setIsOpen] = React.useState(open ?? false);
  const [inputValue, setInputValue] = React.useState<string | string[]>(
    value
      ? // multiSelect
        // ? typeof value === "string"
        // ? [value]
        // : value :
        value
      : ""
  );
  // const theme = useTheme().theme;
  // const colorPalette = getColorPalette(theme,color);
  const [filteredOptions, setFilteredOptions] = React.useState(options);
  useEffect(() => {
    setInputValue(value ?? "");
  }, [value]);
 
  useEffect(() => {
    setFilteredOptions(() => {
      if (!inputValue) return options;
      if (!options) return;
      return options.filter(
        (option) =>
          option.title
            .toLowerCase()
            .includes(inputValue.toString().toLowerCase()) ||
          option.value
            .toLowerCase()
            .includes(inputValue.toString().toLowerCase())
      );
    });
  }, [options, inputValue]);
 
  // const _handleMultiSelectOnClick = (
  //   e: React.MouseEvent,
  //   { title, value }: MenuItemKeyProps
  // ) => {
  //   setInputValue(
  //     !inputValue
  //       ? title
  //       : Array.isArray(inputValue)
  //         ? [...inputValue, title]
  //         : [inputValue, title]
  //   );
  // };
  const _handleSingleSelectOnClick = (
    e: React.MouseEvent,
    { title, value }: MenuItemKeyProps
  ) => {
    setIsOpen(false);
    setInputValue(title);
 
    onClick && onClick(e, { title, value });
    onChange && onChange(e, { title, value });
  };
  const _onMenuClick = (
    e: React.MouseEvent,
    { title, value }: MenuItemKeyProps
  ) => {
    // if (multiSelect) return _handleMultiSelectOnClick(e, { title, value });
    // else
    return _handleSingleSelectOnClick(e, { title, value });
  };
  const _onInputClick = (e: React.MouseEvent) => {
    setIsOpen(!isOpen);
  };
  const _onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setIsOpen(true);
    setInputValue(e.target.value);
    onInputChange && onInputChange(e, e.target.value);
  };
 
  return (
    <div data-testid={dataTestId}>
      <Input
        size={size}
        variant={variant}
        color={color}
        width={width}
        placeholder={placeholder}
        value={inputValue}
        containerRef={inputRef}
        label={label}
        trailingIcon={<IconArrowDown />}
        className={className}
        style={style}
        onClick={_onInputClick}
        onChange={_onInputChange}
        // data-testid={dataTestIdInput}
        data-testid-input={dataTestIdInput}
        {...props}
      />
      <Menu
        color={color}
        variant={variant}
        open={isOpen}
        anchorElement={inputRef.current}
        minWidth={`${inputRef.current?.offsetWidth}px`}
        onClick={_onMenuClick}
        data-testid={dataTestIdMenu}
      >
        {filteredOptions &&
          filteredOptions.map((option, index) => (
            <MenuItem
              {...option}
              title={option.title}
              value={option.value}
              key={index}
            />
          ))}
      </Menu>
    </div>
  );
};
 
export default Autocomplete;