import React from "react"; import "./customSearchSelect.scss"; const mock = [{ id: 1, optionName: "ONE" }, { id: 2, optionName: "TWO" }, { id: 3, optionName: "THREE" }]; export const CustomSearchSelect = ({ label, options = mock, emptyLine = false, searchInput = true, reset }) => { const [copyOption, setCopyOption] = React.useState([]); const [show, toggleShow] = React.useState(false); const [value, toggleValue] = React.useState(""); const [inputValue, toggleInputValue] = React.useState(""); const list = React.createRef(); React.useEffect(() => { document.addEventListener("mousedown", handleClickOutSide); return () => document.removeEventListener("mousedown", handleClickOutSide); }); React.useEffect(() => { if (reset) { toggleValue(""); toggleInputValue(""); setCopyOption(options); toggleShow(false); } }, [options, reset]); React.useEffect(() => { setCopyOption(options); }, [options]); const handleClickOutSide = e => { if (!show) return; if (list.current && !list.current.contains(e.target)) { toggleShow(false); } }; const toggleEvent = optionName => { toggleValue(optionName); toggleInputValue(optionName); toggleShow(false); console.log('toggleValue', optionName) }; const clickOnEptyLine = () => { toggleValue(""); toggleInputValue(""); toggleShow(false); }; const chahgeValueEvent = e => { const { value } = e.target; toggleInputValue(e.target.value); if (!value) { setCopyOption(options); toggleInputValue(value); } else { const filtered = copyOption.filter(el => el.optionName.toLowerCase().indexOf(value.toLowerCase()) >= 0); setCopyOption(filtered); toggleInputValue(value); } console.log('selectValue', value) }; return (