Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/components/PropertySearch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import React, { useState, useRef, useEffect, useId } from 'react';
import React, { memo, useCallback, useState, useRef, useEffect, useId } from 'react';
import { useDebounce } from '@/hooks/useDebounce';
import { usePropertyAutocompleteQuery } from '@/hooks/usePropertySearchQuery';
import { useSearchHistory } from '@/hooks/useSearchHistory';
Expand Down Expand Up @@ -51,35 +51,35 @@ const PropertySearchInner = ({
return () => document.removeEventListener('mousedown', handleClickOutside);
}, []);

const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
onChange(e.target.value);
setSelectedIndex(-1);
if (e.target.value.length > 2) {
setShowHistory(false);
}
};
}, [onChange]);

const handleSuggestionClick = (suggestion: AutocompleteResult) => {
const handleSuggestionClick = useCallback((suggestion: AutocompleteResult) => {
onChange(suggestion.value);
setIsFocused(false);
saveToHistory(suggestion.value, suggestion.type === 'property' ? 'property_type' : 'location');
};
}, [onChange, saveToHistory]);

const handleSearchSelect = (query: string) => {
const handleSearchSelect = useCallback((query: string) => {
onChange(query);
setIsFocused(false);
setShowHistory(false);
saveToHistory(query);
};
}, [onChange, saveToHistory]);

const handleFocus = () => {
const handleFocus = useCallback(() => {
setIsFocused(true);
if (value.length <= 2) {
setShowHistory(true);
}
};
}, [value]);

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
const handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {
if (!suggestions.length) return;

switch (e.key) {
Expand All @@ -106,12 +106,12 @@ const PropertySearchInner = ({
setIsFocused(false);
break;
}
};
}, [suggestions, selectedIndex, handleSuggestionClick]);

const handleClear = () => {
const handleClear = useCallback(() => {
onChange('');
inputRef.current?.focus();
};
}, [onChange]);

const showDropdown = isFocused && (suggestions.length > 0 || isLoading || showHistory);
const activeOptionId = selectedIndex >= 0 ? `${listboxId}-option-${selectedIndex}` : undefined;
Expand Down
Loading