|
| 1 | +// client/src/components/quickmed/HomePage/SearchBar.jsx |
| 2 | +import React from 'react'; |
| 3 | +import Icons from './Icons'; |
| 4 | + |
| 5 | +const SearchBar = ({ |
| 6 | + searchRef, |
| 7 | + searchQuery, |
| 8 | + setSearchQuery, |
| 9 | + isLoading, |
| 10 | + showSuggestions, |
| 11 | + setShowSuggestions, |
| 12 | + suggestions, |
| 13 | + selectedIndex, |
| 14 | + setSelectedIndex, |
| 15 | + handleSelectMedicine, |
| 16 | + handleKeyDown, |
| 17 | +}) => { |
| 18 | + const popularTags = ['Aspirin', 'Ibuprofen', 'Acetaminophen', 'Amoxicillin']; |
| 19 | + |
| 20 | + const handleTagClick = (tag) => { |
| 21 | + setSearchQuery(tag); |
| 22 | + setShowSuggestions(true); |
| 23 | + }; |
| 24 | + |
| 25 | + return ( |
| 26 | + <div ref={searchRef} className="relative max-w-2xl mx-auto"> |
| 27 | + {/* Search Box */} |
| 28 | + <div className="relative group"> |
| 29 | + <div className="absolute -inset-1 bg-gradient-to-r from-med-green-500 to-med-blue-500 rounded-2xl blur opacity-25 group-hover:opacity-40 transition duration-300" /> |
| 30 | + <div className="relative flex items-center bg-med-surface border-2 border-med-border rounded-xl overflow-hidden focus-within:border-med-green-500 transition-all duration-200"> |
| 31 | + <div className="pl-5 text-med-text-muted"> |
| 32 | + {isLoading ? <Icons.Spinner /> : <Icons.Search />} |
| 33 | + </div> |
| 34 | + <input |
| 35 | + type="text" |
| 36 | + value={searchQuery} |
| 37 | + onChange={(e) => { |
| 38 | + setSearchQuery(e.target.value); |
| 39 | + setShowSuggestions(true); |
| 40 | + setSelectedIndex(-1); |
| 41 | + }} |
| 42 | + onFocus={() => setShowSuggestions(true)} |
| 43 | + onKeyDown={handleKeyDown} |
| 44 | + placeholder="Search for medicines (e.g., Aspirin, Ibuprofen...)" |
| 45 | + className="w-full px-4 py-4 sm:py-5 bg-transparent text-med-text placeholder-med-text-muted focus:outline-none text-base sm:text-lg" |
| 46 | + /> |
| 47 | + {searchQuery && ( |
| 48 | + <button |
| 49 | + onClick={() => { |
| 50 | + setSearchQuery(''); |
| 51 | + }} |
| 52 | + className="pr-5 text-med-text-muted hover:text-med-text transition-colors" |
| 53 | + > |
| 54 | + <Icons.Close /> |
| 55 | + </button> |
| 56 | + )} |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + |
| 60 | + {/* Suggestions Dropdown */} |
| 61 | + {showSuggestions && suggestions.length > 0 && ( |
| 62 | + <div className="absolute w-full mt-2 bg-med-surface border border-med-border rounded-xl shadow-xl overflow-hidden z-50"> |
| 63 | + <ul className="max-h-80 overflow-y-auto"> |
| 64 | + {suggestions.map((suggestion, index) => { |
| 65 | + const name = suggestion.name || suggestion; |
| 66 | + return ( |
| 67 | + <li key={index}> |
| 68 | + <button |
| 69 | + onClick={() => handleSelectMedicine(name)} |
| 70 | + className={`w-full px-5 py-3 text-left flex items-center gap-3 transition-colors duration-150 ${ |
| 71 | + index === selectedIndex |
| 72 | + ? 'bg-med-green-100 dark:bg-med-green-900/30 text-med-green-700 dark:text-med-green-400' |
| 73 | + : 'hover:bg-med-green-50 dark:hover:bg-med-green-900/20' |
| 74 | + }`} |
| 75 | + > |
| 76 | + <Icons.Pill /> |
| 77 | + <span className="font-medium">{name}</span> |
| 78 | + </button> |
| 79 | + </li> |
| 80 | + ); |
| 81 | + })} |
| 82 | + </ul> |
| 83 | + </div> |
| 84 | + )} |
| 85 | + |
| 86 | + {/* No Results */} |
| 87 | + {showSuggestions && searchQuery.length >= 2 && !isLoading && suggestions.length === 0 && ( |
| 88 | + <div className="absolute w-full mt-2 bg-med-surface border border-med-border rounded-xl shadow-xl p-6 text-center text-med-text-muted"> |
| 89 | + No medicines found for "{searchQuery}" |
| 90 | + </div> |
| 91 | + )} |
| 92 | + |
| 93 | + {/* Quick Tags */} |
| 94 | + <div className="mt-8 flex flex-wrap justify-center gap-2"> |
| 95 | + <span className="text-sm text-med-text-muted">Popular searches:</span> |
| 96 | + {popularTags.map((tag) => ( |
| 97 | + <button |
| 98 | + key={tag} |
| 99 | + onClick={() => handleTagClick(tag)} |
| 100 | + className="px-3 py-1 text-sm rounded-full bg-med-surface border border-med-border hover:border-med-green-500 hover:text-med-green-600 transition-all duration-200" |
| 101 | + > |
| 102 | + {tag} |
| 103 | + </button> |
| 104 | + ))} |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +export default SearchBar; |
0 commit comments