feat(TypeaheadInput): enhance external update handling and suggestion management
This commit is contained in:
parent
b0a8271040
commit
e1992af634
1 changed files with 32 additions and 8 deletions
|
@ -1,4 +1,4 @@
|
||||||
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
import React, { useState, useEffect, useCallback, useRef } from 'react';
|
||||||
import { WikidataSuggestion } from '@/types/database';
|
import { WikidataSuggestion } from '@/types/database';
|
||||||
|
|
||||||
interface TypeaheadInputProps {
|
interface TypeaheadInputProps {
|
||||||
|
@ -27,15 +27,33 @@ export function TypeaheadInput({
|
||||||
const [showSuggestions, setShowSuggestions] = useState(false);
|
const [showSuggestions, setShowSuggestions] = useState(false);
|
||||||
const [selectedIndex, setSelectedIndex] = useState(-1);
|
const [selectedIndex, setSelectedIndex] = useState(-1);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [isExternalUpdate, setIsExternalUpdate] = useState(false);
|
||||||
|
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
const suggestionsRef = useRef<HTMLDivElement>(null);
|
const suggestionsRef = useRef<HTMLDivElement>(null);
|
||||||
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
|
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
|
||||||
|
|
||||||
// Update local value when prop changes
|
// Update local value when prop changes (including external updates)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLocalValue(value);
|
if (value !== localValue) {
|
||||||
}, [value]);
|
setLocalValue(value);
|
||||||
|
setIsExternalUpdate(true);
|
||||||
|
// Clear suggestions when externally updated
|
||||||
|
setSuggestions([]);
|
||||||
|
setShowSuggestions(false);
|
||||||
|
setSelectedIndex(-1);
|
||||||
|
}
|
||||||
|
}, [value, localValue]);
|
||||||
|
|
||||||
|
// Reset external update flag after a short delay
|
||||||
|
useEffect(() => {
|
||||||
|
if (isExternalUpdate) {
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
setIsExternalUpdate(false);
|
||||||
|
}, 100);
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}
|
||||||
|
}, [isExternalUpdate]);
|
||||||
|
|
||||||
// Debounced suggestion fetching
|
// Debounced suggestion fetching
|
||||||
const debouncedFetchSuggestions = useCallback(async (query: string) => {
|
const debouncedFetchSuggestions = useCallback(async (query: string) => {
|
||||||
|
@ -73,8 +91,12 @@ export function TypeaheadInput({
|
||||||
const newValue = e.target.value;
|
const newValue = e.target.value;
|
||||||
setLocalValue(newValue);
|
setLocalValue(newValue);
|
||||||
onInput(newValue);
|
onInput(newValue);
|
||||||
debouncedFetchSuggestions(newValue);
|
|
||||||
}, [onInput, debouncedFetchSuggestions]);
|
// Only fetch suggestions if this is user input, not external update
|
||||||
|
if (!isExternalUpdate) {
|
||||||
|
debouncedFetchSuggestions(newValue);
|
||||||
|
}
|
||||||
|
}, [onInput, debouncedFetchSuggestions, isExternalUpdate]);
|
||||||
|
|
||||||
// Handle keyboard navigation
|
// Handle keyboard navigation
|
||||||
const handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {
|
const handleKeyDown = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||||
|
@ -136,11 +158,13 @@ export function TypeaheadInput({
|
||||||
// Update local value with the selected suggestion
|
// Update local value with the selected suggestion
|
||||||
const newValue = suggestion.label || '';
|
const newValue = suggestion.label || '';
|
||||||
setLocalValue(newValue);
|
setLocalValue(newValue);
|
||||||
onInput(newValue);
|
|
||||||
|
|
||||||
// Call the selection handler
|
// Call the selection handler first (this will update the parent)
|
||||||
onSelect(suggestion);
|
onSelect(suggestion);
|
||||||
|
|
||||||
|
// Then update the input value
|
||||||
|
onInput(newValue);
|
||||||
|
|
||||||
// Hide suggestions
|
// Hide suggestions
|
||||||
setShowSuggestions(false);
|
setShowSuggestions(false);
|
||||||
setSelectedIndex(-1);
|
setSelectedIndex(-1);
|
||||||
|
|
Loading…
Add table
Reference in a new issue