|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useRef, useEffect } from "react"; |
| 4 | +import { X } from "lucide-react"; |
| 5 | +import type { AddressResult, ValidatedAddress } from "@/lib/types"; |
| 6 | +import { useAddressAutocomplete } from "@/hooks/useAddressAutocomplete"; |
| 7 | +import { Input } from "@/components/ui/input"; |
| 8 | +import { AddressDropdown } from "@/components/listings/address/AddressDropdown"; |
| 9 | +import { cn } from "@/lib/utils"; |
| 10 | + |
| 11 | +interface AddressAutocompleteProps { |
| 12 | + value: string; |
| 13 | + onChange: (value: string) => void; |
| 14 | + onValidatedAddressChange: (address: ValidatedAddress | null) => void; |
| 15 | + disabled?: boolean; |
| 16 | + error?: boolean; |
| 17 | + placeholder?: string; |
| 18 | +} |
| 19 | + |
| 20 | +export function AddressAutocomplete({ |
| 21 | + value, |
| 22 | + onChange, |
| 23 | + onValidatedAddressChange, |
| 24 | + disabled = false, |
| 25 | + error = false, |
| 26 | + placeholder = "123 Main St, Philadelphia, PA 19104", |
| 27 | +}: AddressAutocompleteProps) { |
| 28 | + const [isOpen, setIsOpen] = useState(false); |
| 29 | + const [highlightedIndex, setHighlightedIndex] = useState(-1); |
| 30 | + const [hasValidatedAddress, setHasValidatedAddress] = useState(false); |
| 31 | + const inputRef = useRef<HTMLInputElement>(null); |
| 32 | + const containerRef = useRef<HTMLDivElement>(null); |
| 33 | + const { |
| 34 | + suggestions, |
| 35 | + isLoading, |
| 36 | + error: apiError, |
| 37 | + search, |
| 38 | + clearSuggestions, |
| 39 | + } = useAddressAutocomplete(); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (value.trim().length >= 3 && !hasValidatedAddress) { |
| 43 | + search(value); |
| 44 | + setIsOpen(true); |
| 45 | + setHighlightedIndex(-1); |
| 46 | + } else if (!hasValidatedAddress) { |
| 47 | + clearSuggestions(); |
| 48 | + setIsOpen(false); |
| 49 | + } |
| 50 | + }, [value, hasValidatedAddress, search, clearSuggestions]); |
| 51 | + |
| 52 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 53 | + const newValue = e.target.value; |
| 54 | + onChange(newValue); |
| 55 | + |
| 56 | + if (hasValidatedAddress) { |
| 57 | + onValidatedAddressChange(null); |
| 58 | + setHasValidatedAddress(false); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const handleSelect = (address: AddressResult) => { |
| 63 | + const validatedAddress: ValidatedAddress = { |
| 64 | + displayName: address.displayName, |
| 65 | + lat: address.lat, |
| 66 | + lon: address.lon, |
| 67 | + placeId: address.placeId, |
| 68 | + }; |
| 69 | + |
| 70 | + onChange(address.displayName); |
| 71 | + onValidatedAddressChange(validatedAddress); |
| 72 | + setHasValidatedAddress(true); |
| 73 | + setIsOpen(false); |
| 74 | + clearSuggestions(); |
| 75 | + inputRef.current?.blur(); |
| 76 | + }; |
| 77 | + |
| 78 | + const handleClear = () => { |
| 79 | + onChange(""); |
| 80 | + onValidatedAddressChange(null); |
| 81 | + setHasValidatedAddress(false); |
| 82 | + setIsOpen(false); |
| 83 | + clearSuggestions(); |
| 84 | + inputRef.current?.focus(); |
| 85 | + }; |
| 86 | + |
| 87 | + const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 88 | + if (!isOpen || suggestions.length === 0) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + switch (e.key) { |
| 93 | + case "ArrowDown": |
| 94 | + e.preventDefault(); |
| 95 | + setHighlightedIndex((prev) => (prev < suggestions.length - 1 ? prev + 1 : 0)); |
| 96 | + break; |
| 97 | + |
| 98 | + case "ArrowUp": |
| 99 | + e.preventDefault(); |
| 100 | + setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : suggestions.length - 1)); |
| 101 | + break; |
| 102 | + |
| 103 | + case "Enter": |
| 104 | + e.preventDefault(); |
| 105 | + if (highlightedIndex >= 0 && highlightedIndex < suggestions.length) { |
| 106 | + handleSelect(suggestions[highlightedIndex]); |
| 107 | + } |
| 108 | + break; |
| 109 | + |
| 110 | + case "Escape": |
| 111 | + e.preventDefault(); |
| 112 | + setIsOpen(false); |
| 113 | + clearSuggestions(); |
| 114 | + inputRef.current?.blur(); |
| 115 | + break; |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + useEffect(() => { |
| 120 | + const handleClickOutside = (event: MouseEvent) => { |
| 121 | + if (containerRef.current && !containerRef.current.contains(event.target as Node)) { |
| 122 | + setIsOpen(false); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + document.addEventListener("mousedown", handleClickOutside); |
| 127 | + return () => document.removeEventListener("mousedown", handleClickOutside); |
| 128 | + }, []); |
| 129 | + |
| 130 | + return ( |
| 131 | + <div className={"relative"} ref={containerRef}> |
| 132 | + <div className={"relative"}> |
| 133 | + <Input |
| 134 | + ref={inputRef} |
| 135 | + type={"text"} |
| 136 | + value={value} |
| 137 | + onChange={handleInputChange} |
| 138 | + onKeyDown={handleKeyDown} |
| 139 | + onFocus={() => { |
| 140 | + if (value.trim().length >= 3) { |
| 141 | + setIsOpen(true); |
| 142 | + } |
| 143 | + }} |
| 144 | + disabled={disabled} |
| 145 | + placeholder={placeholder} |
| 146 | + className={cn( |
| 147 | + hasValidatedAddress && "pr-9", |
| 148 | + error && "border-destructive ring-destructive/20" |
| 149 | + )} |
| 150 | + role={"combobox"} |
| 151 | + aria-expanded={isOpen} |
| 152 | + aria-autocomplete={"list"} |
| 153 | + aria-controls={"address-dropdown"} |
| 154 | + aria-activedescendant={ |
| 155 | + highlightedIndex >= 0 ? `address-option-${highlightedIndex}` : undefined |
| 156 | + } |
| 157 | + /> |
| 158 | + |
| 159 | + {hasValidatedAddress && !disabled && ( |
| 160 | + <button |
| 161 | + type={"button"} |
| 162 | + onClick={handleClear} |
| 163 | + className={cn( |
| 164 | + "absolute top-1/2 right-2 -translate-y-1/2", |
| 165 | + "flex size-5 items-center justify-center rounded-sm", |
| 166 | + "text-muted-foreground hover:text-foreground", |
| 167 | + "transition-colors" |
| 168 | + )} |
| 169 | + aria-label={"Clear address"} |
| 170 | + > |
| 171 | + <X className={"size-4"} /> |
| 172 | + </button> |
| 173 | + )} |
| 174 | + </div> |
| 175 | + |
| 176 | + <AddressDropdown |
| 177 | + suggestions={suggestions} |
| 178 | + isLoading={isLoading} |
| 179 | + error={apiError} |
| 180 | + isOpen={isOpen} |
| 181 | + onSelect={handleSelect} |
| 182 | + highlightedIndex={highlightedIndex} |
| 183 | + /> |
| 184 | + </div> |
| 185 | + ); |
| 186 | +} |
0 commit comments