diff --git a/components/ui/TagInput.tsx b/components/ui/TagInput.tsx index 899c14d..b9b4b5e 100644 --- a/components/ui/TagInput.tsx +++ b/components/ui/TagInput.tsx @@ -26,7 +26,7 @@ export function TagInput({ const inputRef = useRef(null); const suggestionsRef = useRef(null); - const { suggestions, loading, searchTags, clearSuggestions } = useTagsAutocomplete(); + const { suggestions, loading, searchTags, clearSuggestions, loadPopularTags } = useTagsAutocomplete(); // Rechercher des suggestions quand l'input change useEffect(() => { @@ -93,6 +93,18 @@ export function TagInput({ }, 150); }; + const handleFocus = () => { + if (inputValue.trim()) { + // Si il y a du texte, afficher les suggestions existantes + setShowSuggestions(true); + } else { + // Si l'input est vide, charger les tags populaires + loadPopularTags(20); + setShowSuggestions(true); + } + setSelectedIndex(-1); + }; + return (
{/* Container des tags et input */} @@ -126,7 +138,7 @@ export function TagInput({ onChange={(e) => setInputValue(e.target.value)} onKeyDown={handleKeyDown} onBlur={handleBlur} - onFocus={() => inputValue && setShowSuggestions(true)} + onFocus={handleFocus} placeholder={tags.length === 0 ? placeholder : ""} className="flex-1 min-w-[120px] bg-transparent border-none outline-none text-slate-100 placeholder-slate-400 text-sm" /> @@ -138,37 +150,38 @@ export function TagInput({ {showSuggestions && (suggestions.length > 0 || loading) && (
{loading ? (
Recherche...
) : ( - suggestions.map((tag, index) => ( - - )) + + ))} +
)}
)} diff --git a/hooks/useTags.ts b/hooks/useTags.ts index 6bf001e..69ac84e 100644 --- a/hooks/useTags.ts +++ b/hooks/useTags.ts @@ -214,10 +214,24 @@ export function useTagsAutocomplete() { setSuggestions([]); }, []); + const loadPopularTags = useCallback(async (limit: number = 20) => { + setLoading(true); + try { + const response = await tagsClient.getPopularTags(limit); + setSuggestions(response.data); + } catch (error) { + console.error('Erreur lors du chargement des tags populaires:', error); + setSuggestions([]); + } finally { + setLoading(false); + } + }, []); + return { suggestions, loading, searchTags, - clearSuggestions + clearSuggestions, + loadPopularTags }; }