Changeset 79a0317 for imaps-frontend/src/components/SearchBar
- Timestamp:
- 01/21/25 03:08:24 (3 days ago)
- Branches:
- main
- Parents:
- 0c6b92a
- Location:
- imaps-frontend/src/components/SearchBar
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/src/components/SearchBar/SearchBar.jsx
r0c6b92a r79a0317 6 6 import styles from "./SearchBar.module.css"; 7 7 8 function SearchBar({ map, handleDirectionsSubmit, setIsPanelOpen, setSelectedRoom, availableShapes,handleFloorChange }) { 8 function SearchBar({ 9 map, 10 handleDirectionsSubmit, 11 setIsPanelOpen, 12 setSelectedRoom, 13 availableShapes, 14 handleFloorChange, 15 }) { 9 16 const [isExpanded, setIsExpanded] = useState(false); 10 17 const [from, setFrom] = useState(""); … … 14 21 const [dropdownVisible, setDropdownVisible] = useState(false); 15 22 const [inputFieldType, setInputFieldType] = useState(""); 16 const dropdownRef = useRef(null); 17 18 const toggleExpanded = () => { 19 setIsExpanded(!isExpanded); 20 }; 21 22 function searchRoom() { 23 let foundRoom = availableShapes.find(sh => sh.info.name === from) 24 console.log("map fnum",map.floorNum) 25 if(foundRoom.floorNum !== map.floorNum){ 23 24 const wrapperRef = useRef(null); 25 const dropdownContainerRef = useRef(null); 26 const activeInputRef = useRef(null); // Track the currently focused input field 27 28 const toggleExpanded = () => setIsExpanded(!isExpanded); 29 30 const handleClickOutside = (event) => { 31 if ( 32 wrapperRef.current && 33 !wrapperRef.current.contains(event.target) && 34 dropdownContainerRef.current && 35 !dropdownContainerRef.current.contains(event.target) 36 ) { 37 setDropdownVisible(false); 38 } 39 }; 40 41 useEffect(() => { 42 document.addEventListener("mousedown", handleClickOutside); 43 return () => { 44 document.removeEventListener("mousedown", handleClickOutside); 45 }; 46 }, []); 47 48 const searchRoom = () => { 49 const foundRoom = availableShapes.find((sh) => sh.info.name === from); 50 if (foundRoom && foundRoom.floorNum !== map.floorNum) { 26 51 handleFloorChange(foundRoom.floorNum); 27 52 } 28 29 30 console.log("FOUND ROOM: " + foundRoom)31 53 map.highlightShape(from); 32 54 setSelectedRoom(foundRoom); 33 55 setIsPanelOpen(true); 34 } 35 36 const handleInputFocus = (field ) => {56 }; 57 58 const handleInputFocus = (field, inputRef) => { 37 59 if (availableOptions.length === 0 && map) { 38 60 setAvailableOptions( … … 42 64 ); 43 65 } 66 setInputFieldType(field); 44 67 setDropdownVisible(true); 45 setInputFieldType(field);68 activeInputRef.current = inputRef; // Set the active input ref 46 69 }; 47 70 … … 69 92 if (!dropdownVisible || filteredOptions.length === 0) return null; 70 93 71 const position = dropdownRef.current?.getBoundingClientRect() || { top: 0, left: 0, width: 0 }; 94 const position = activeInputRef.current?.getBoundingClientRect() || { 95 top: 0, 96 left: 0, 97 width: 0, 98 }; 72 99 73 100 return ReactDOM.createPortal( 74 101 <ul 102 ref={dropdownContainerRef} 75 103 className={styles.dropdown} 76 104 style={{ 77 105 position: "absolute", 78 top: position.top + position.height ,79 left: position.left ,106 top: position.top + position.height + window.scrollY, 107 left: position.left + window.scrollX, 80 108 width: position.width, 81 109 }} … … 91 119 ))} 92 120 </ul>, 93 document.body // Portal renders outside the parent hierarchy121 document.body 94 122 ); 95 123 }; 96 124 97 125 return ( 98 <div className={styles.wrapper} >126 <div className={styles.wrapper} ref={wrapperRef}> 99 127 {!isExpanded ? ( 100 128 <div className={styles.searchBar}> … … 104 132 placeholder="Search location" 105 133 aria-label="Search" 106 ref={dropdownRef} // Attach the input to calculate dropdown position 107 onFocus={() => handleInputFocus("from")} 134 onFocus={(e) => handleInputFocus("from", e.target)} 108 135 onChange={handleInputChange(setFrom)} 109 136 value={from} … … 111 138 {renderDropdown()} 112 139 <div className={styles.buttons}> 113 <button type="button" className={styles.iconButton} onClick={searchRoom}> 140 <button 141 type="button" 142 className={styles.iconButton} 143 onClick={searchRoom} 144 > 114 145 <img src={searchIcon} alt="Search Icon" /> 115 146 </button> 116 <button type="button" className={styles.iconButton} onClick={toggleExpanded}> 147 <button 148 type="button" 149 className={styles.iconButton} 150 onClick={toggleExpanded} 151 > 117 152 <img src={routeIcon} alt="Route Icon" /> 118 153 </button> … … 127 162 aria-label="From" 128 163 value={from} 129 onFocus={( ) => handleInputFocus("from")}164 onFocus={(e) => handleInputFocus("from", e.target)} 130 165 onChange={handleInputChange(setFrom)} 131 166 className={styles.inputField} 132 ref={inputFieldType === "from" ? dropdownRef : null}133 167 /> 134 168 <input … … 137 171 aria-label="To" 138 172 value={to} 139 onFocus={( ) => handleInputFocus("to")}173 onFocus={(e) => handleInputFocus("to", e.target)} 140 174 onChange={handleInputChange(setTo)} 141 175 className={styles.inputField} 142 ref={inputFieldType === "to" ? dropdownRef : null}143 176 /> 144 177 {renderDropdown()} … … 152 185 <img src={searchIcon} alt="Submit Directions" /> 153 186 </button> 154 <button type="button" className={styles.iconButton} onClick={toggleExpanded}> 187 <button 188 type="button" 189 className={styles.iconButton} 190 onClick={toggleExpanded} 191 > 155 192 <img src={closeIcon} alt="Close Icon" /> 156 193 </button> -
imaps-frontend/src/components/SearchBar/SearchBar.module.css
r0c6b92a r79a0317 26 26 .inputField { 27 27 flex-grow: 1; 28 padding: 10px 15px;28 /*padding: 10px 15px;*/ 29 29 font-size: 16px; 30 30 border: 1px solid #ddd; … … 33 33 margin-right: 10px; 34 34 min-width: 0; 35 35 margin-top: 12px; 36 36 } 37 37
Note:
See TracChangeset
for help on using the changeset viewer.