You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearchInput.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { useCallback } from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. EndIcon,
  5. SearchIcon,
  6. SearchInputContainer,
  7. } from "./SearchInput.styled";
  8. import { forwardRef } from "react";
  9. // import { useCallback } from "react";
  10. import { useTranslation } from "react-i18next";
  11. import { routeMatches } from "../../../util/helpers/routeHelpers";
  12. import { ABOUT_PAGE, BASE_PAGE, HOME_PAGE } from "../../../constants/pages";
  13. import { debounceHelper } from "../../../util/helpers/debounceHelper";
  14. const SearchInput = forwardRef((props, ref) => {
  15. const { t } = useTranslation();
  16. const handleSearch = () => {
  17. if (routeMatches(HOME_PAGE) || routeMatches(BASE_PAGE)) {
  18. debounceHelper(() => props.handleSearch(ref.current.value), 500);
  19. }
  20. };
  21. const handleManualSearch = () => {
  22. debounceHelper(() => {}, 500);
  23. props.handleSearch(ref.current.value);
  24. };
  25. const listener = useCallback(
  26. (event) => {
  27. if (event.keyCode === 13) {
  28. event.preventDefault();
  29. handleManualSearch();
  30. }
  31. },
  32. [ref.current]
  33. );
  34. const handleFocusSearch = () => {
  35. ref.current.addEventListener("keyup", listener);
  36. };
  37. const handleBlurSearch = () => {
  38. ref.current.removeEventListener("keyup", listener);
  39. };
  40. if (routeMatches(ABOUT_PAGE)) {
  41. return <></>;
  42. }
  43. return (
  44. <SearchInputContainer
  45. fullWidth
  46. wider={!props.user}
  47. InputProps={{
  48. endAdornment: (
  49. <EndIcon size="36px">
  50. <SearchIcon onClick={handleManualSearch} />
  51. </EndIcon>
  52. ),
  53. }}
  54. onChange={handleSearch}
  55. placeholder={t("header.searchOffers")}
  56. onFocus={handleFocusSearch}
  57. onBlur={handleBlurSearch}
  58. ref={ref}
  59. />
  60. );
  61. });
  62. SearchInput.displayName = "SearchInput";
  63. SearchInput.propTypes = {
  64. handleSearch: PropTypes.func,
  65. user: PropTypes.bool,
  66. };
  67. export default SearchInput;