import React, { useState, useEffect, useRef } from "react"; import ReactDOM from "react-dom"; import searchIcon from "../../assets/search_icon.png"; import routeIcon from "../../assets/route_icon.png"; import closeIcon from "../../assets/close_icon.png"; import styles from "./SearchBar.module.css"; function SearchBar({ map, handleDirectionsSubmit, setIsPanelOpen, setSelectedRoom, availableShapes, handleFloorChange, }) { const [isExpanded, setIsExpanded] = useState(false); const [from, setFrom] = useState(""); const [to, setTo] = useState(""); const [availableOptions, setAvailableOptions] = useState([]); const [filteredOptions, setFilteredOptions] = useState([]); const [dropdownVisible, setDropdownVisible] = useState(false); const [inputFieldType, setInputFieldType] = useState(""); const wrapperRef = useRef(null); const dropdownContainerRef = useRef(null); const activeInputRef = useRef(null); // Track the currently focused input field const toggleExpanded = () => setIsExpanded(!isExpanded); const handleClickOutside = (event) => { if ( wrapperRef.current && !wrapperRef.current.contains(event.target) && dropdownContainerRef.current && !dropdownContainerRef.current.contains(event.target) ) { setDropdownVisible(false); } }; useEffect(() => { document.addEventListener("mousedown", handleClickOutside); return () => { document.removeEventListener("mousedown", handleClickOutside); }; }, []); const searchRoom = () => { const foundRoom = availableShapes.find((sh) => sh.info.name === from); if (foundRoom && foundRoom.floorNum !== map.floorNum) { handleFloorChange(foundRoom.floorNum); } map.highlightShape(from); setSelectedRoom(foundRoom); setIsPanelOpen(true); }; const handleInputFocus = (field, inputRef) => { if (availableOptions.length === 0 && map) { setAvailableOptions( availableShapes .filter((sh) => sh.className === "RenderedRoom") .map((shape) => shape.info.name) ); } setInputFieldType(field); setDropdownVisible(true); activeInputRef.current = inputRef; // Set the active input ref }; const handleInputChange = (setter) => (event) => { const value = event.target.value; setter(value); setDropdownVisible(true); const filtered = availableOptions.filter((option) => option.toLowerCase().includes(value.toLowerCase()) ); setFilteredOptions(filtered); }; const handleOptionClick = (option) => { if (inputFieldType === "from") { setFrom(option); } else if (inputFieldType === "to") { setTo(option); } setDropdownVisible(false); }; const renderDropdown = () => { if (!dropdownVisible || filteredOptions.length === 0) return null; const position = activeInputRef.current?.getBoundingClientRect() || { top: 0, left: 0, width: 0, }; return ReactDOM.createPortal( , document.body ); }; return (
{!isExpanded ? (
handleInputFocus("from", e.target)} onChange={handleInputChange(setFrom)} value={from} /> {renderDropdown()}
) : (
handleInputFocus("from", e.target)} onChange={handleInputChange(setFrom)} className={styles.inputField} /> handleInputFocus("to", e.target)} onChange={handleInputChange(setTo)} className={styles.inputField} /> {renderDropdown()}
)}
); } export default SearchBar;