customSearchSelect.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React from "react";
  2. import "./customSearchSelect.scss";
  3. const mock = [{ id: 1, optionName: "ONE" }, { id: 2, optionName: "TWO" }, { id: 3, optionName: "THREE" }];
  4. export const CustomSearchSelect = ({ label, options = mock, emptyLine = false, searchInput = true, reset }) => {
  5. const [copyOption, setCopyOption] = React.useState([]);
  6. const [show, toggleShow] = React.useState(false);
  7. const [value, toggleValue] = React.useState("");
  8. const [inputValue, toggleInputValue] = React.useState("");
  9. const list = React.createRef();
  10. React.useEffect(() => {
  11. document.addEventListener("mousedown", handleClickOutSide);
  12. return () => document.removeEventListener("mousedown", handleClickOutSide);
  13. });
  14. React.useEffect(() => {
  15. if (reset) {
  16. toggleValue("");
  17. toggleInputValue("");
  18. setCopyOption(options);
  19. toggleShow(false);
  20. }
  21. }, [options, reset]);
  22. React.useEffect(() => {
  23. setCopyOption(options);
  24. }, [options]);
  25. const handleClickOutSide = e => {
  26. if (!show) return;
  27. if (list.current && !list.current.contains(e.target)) {
  28. toggleShow(false);
  29. }
  30. };
  31. const toggleEvent = optionName => {
  32. toggleValue(optionName);
  33. toggleInputValue(optionName);
  34. toggleShow(false);
  35. console.log('toggleValue', optionName)
  36. };
  37. const clickOnEptyLine = () => {
  38. toggleValue("");
  39. toggleInputValue("");
  40. toggleShow(false);
  41. };
  42. const chahgeValueEvent = e => {
  43. const { value } = e.target;
  44. toggleInputValue(e.target.value);
  45. if (!value) {
  46. setCopyOption(options);
  47. toggleInputValue(value);
  48. } else {
  49. const filtered = copyOption.filter(el => el.optionName.toLowerCase().indexOf(value.toLowerCase()) >= 0);
  50. setCopyOption(filtered);
  51. toggleInputValue(value);
  52. }
  53. console.log('selectValue', value)
  54. };
  55. return (
  56. <div className="select">
  57. {label && (
  58. <label htmlFor="select" className="select__lable">
  59. {label}
  60. </label>
  61. )}
  62. <div className="select__value-box" onClick={() => toggleShow(true)}>
  63. {searchInput ? null : <span>{value} &nbsp;</span>}
  64. <input
  65. value={inputValue}
  66. autoComplete="off"
  67. type={searchInput ? "text" : "hidden"}
  68. onChange={chahgeValueEvent}
  69. id="select"
  70. className="select__input"
  71. />
  72. </div>
  73. {show && (
  74. <ul className="select__list" ref={list}>
  75. {emptyLine && (
  76. <li className="select__item" onClick={clickOnEptyLine}>
  77. &nbsp;
  78. </li>
  79. )}
  80. {copyOption.map(el => (
  81. <li className="select__item" key={el.id} onClick={toggleEvent.bind(null, el.optionName)}>
  82. {el.optionName}
  83. </li>
  84. ))}
  85. </ul>
  86. )}
  87. </div>
  88. );
  89. };