All files / src/components/common/SearchBar_V2 SearchBar.tsx

92.59% Statements 25/27
75% Branches 12/16
90% Functions 9/10
92.3% Lines 24/26

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                                                    10x                   18x 18x   18x 18x   18x 1x 1x       18x 12x 4x                       8x       18x 12x 12x     18x 6x 6x     18x                                 18x                                   1x             4x                       68x                               68x                        
import React, { FocusEvent, useState } from "react";
import ExpandedSearchBar from "@common/SearchBar/SearchBar";
import { IconButton, styled, SxProps } from "@mui/material";
import { SearchIcon } from "@assets/rebrandIcons";
import { palette } from "@palette";
import { useAppDispatch } from "@redux/hooks";
import { updateSearchQuery } from "@redux/slices/fundraisers";
import { setSearchQueryByKey } from "@redux/slices/search";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import ErrorCatcher from "@common/Error/ErrorCatcher";
 
type VoidFunc = () => void;
 
interface SearchBarProps {
  onChange?: (value: string) => void;
  queryKey?: string;
  transparent?: boolean;
  onMouseEnter?: VoidFunc | boolean;
  onMouseLeave?: VoidFunc | boolean;
  isExpandedByDefault?: boolean;
  formSx?: SxProps;
  initialValue?: string;
  customPlaceholder?: string;
  tab?: string;
}
 
export const SearchBar = ({
  onChange,
  queryKey,
  transparent,
  isExpandedByDefault,
  formSx,
  initialValue,
  customPlaceholder,
  tab,
}: SearchBarProps) => {
  const dispatch = useAppDispatch();
  const { isDesktopView } = useCustomTheme();
 
  const [expanded, setExpanded] = useState(false);
  const [queryString, setQueryString] = useState<string>("");
 
  const onBlur = (event: FocusEvent) => {
    Eif (!queryString) {
      setExpanded(false);
    }
  };
 
  const searchQueryDispatcher = (value: string) => {
    if (queryKey) {
      dispatch(
        setSearchQueryByKey({
          queryKey,
          params: {
            value,
          },
        }),
      );
    } else {
      // else we store the search query in the default state
      // (in the future we need to change the default state.
      // now it's in the fundraiser state should be in more generic state)
      dispatch(updateSearchQuery(value));
    }
  };
 
  const resetSearch = () => {
    setQueryString("");
    searchQueryDispatcher("");
  };
 
  React.useEffect(() => {
    resetSearch();
    return resetSearch;
  }, [tab]);
 
  Iif (!isDesktopView) {
    //latest requirement: it shoudl be opened by default on mobile
    return (
      <ExpandedSearchBar
        handleChange={onChange}
        queryKey={queryKey}
        transparent={transparent}
        initialValue={initialValue}
        customPlaceholder={customPlaceholder}
        formSx={{
          width: "100%",
          ...formSx,
        }}
      />
    );
  }
 
  return isExpandedByDefault || expanded ? (
    <ErrorCatcher errorID="ExpandedSearchBar">
      <ExpandedSearchBar
        IOCsetQueryString={setQueryString}
        IOCqueryString={queryString}
        onBlur={onBlur}
        handleChange={onChange}
        queryKey={queryKey}
        transparent={transparent}
        initialValue={initialValue}
        customPlaceholder={customPlaceholder}
        isExpandedByDefault={isExpandedByDefault}
        formSx={{
          width: isExpandedByDefault ? "100%" : "fit-content",
          ...formSx,
          height: "32px",
        }}
        collapse={() => {
          setExpanded(false);
        }}
        autoFocus={!isExpandedByDefault}
      />
    </ErrorCatcher>
  ) : (
    <ErrorCatcher errorID="DefaultSearchbar">
      <DefaultSearchbar onClick={() => setExpanded(true)} />
    </ErrorCatcher>
  );
};
 
export function DefaultSearchbar({
  onClick,
  onMouseEnter = () => {
    return;
  },
  disabled = false,
}: any) {
  return (
    <StyledIconButton
      onClick={onClick}
      onMouseEnter={onMouseEnter}
      disabled={disabled}
      disableRipple
      data-testid="default-searchbar-icon"
    >
      <SearchIcon
        width={24}
        height={24}
        stroke={disabled ? palette.liftedWhite[300] : palette.gray[300]}
      />
    </StyledIconButton>
  );
}
const StyledIconButton = styled(IconButton)(() => ({
  background: `${palette.background.dimmedWhite} !important`,
  boxShadow: "none !important",
  padding: "4px 12px",
  borderRadius: "90px",
  border: `1px solid ${palette.neutral[10]}`,
  userSelect: "none",
  height: "32px",
  "&:disabled": {
    background: palette.liftedWhite[100],
  },
}));