I am trying to build an input with a dropdown that provides suggestions. I have an input element that uses onBlur, which technically when click outside will remove the ul element. When I click on the ul element, it also removes the ul element, which fires the onBlur event on the input. Is there a way to implement onBlur that prevents the ul element to fire the onBlur event?
import React, { useState, useEffect, useRef } from "react";
import "./styles.css";
const Autocomplete = (props) => {
const [userInput, setUserInput] = useState("");
const [listResults, setListResults] = useState([]);
const [openModal, setOpenModal] = useState(false);
useEffect(() => {
const filteredResults = props.options.filter((option) =>
option.includes(userInput)
);
setListResults(filteredResults);
}, [userInput, props.options]);
const handleInput = (event) => {
setUserInput(event.target.value);
};
const handleInputClick = () => {
setOpenModal(!openModal);
};
const handleClickOption = (data) => {
setUserInput(data);
setOpenModal(false);
ulEl.current.focus();
};
return (
<div className="container">
<input
name="search-input"
value={userInput}
onChange={handleInput}
autoComplete="off"
onFocus={handleInputClick}
onBlur={handleInputClick}
/>
<ul className="search-list">
{openModal &&
listResults &&
listResults.map((data, key) => (
<li
key={key}
onClick={() => {
handleClickOption(data);
}}
>
{data}
</li>
))}
</ul>
</div>
);
};
export default Autocomplete;
Any help will be greatly appreciated