1- import React , { useCallback } from "react" ;
1+ import React , { useMemo } from "react" ;
22import { AutoComplete , Button , Space , Input , Spin } from "antd" ;
33import { SearchOutlined , LoadingOutlined } from "@ant-design/icons" ;
4- import { NpmPackageSuggestion } from "../services/npmAutocompleteService" ;
54import { usePackageSuggestions } from "../hooks/usePackageSuggestions" ;
65import PackageSuggestionLabel from "./PackageSuggestionLabel" ;
76
@@ -15,6 +14,11 @@ interface PackageSearchInputProps {
1514 style ?: React . CSSProperties ;
1615}
1716
17+ interface AutoCompleteOption {
18+ value : string ;
19+ label : React . ReactNode ;
20+ }
21+
1822const PackageSearchInput : React . FC < PackageSearchInputProps > = ( {
1923 searchTerm,
2024 onSearchTermChange,
@@ -23,17 +27,20 @@ const PackageSearchInput: React.FC<PackageSearchInputProps> = ({
2327 size = "middle" ,
2428 style,
2529} ) => {
26- // Render function for suggestion items
27- const renderSuggestion = useCallback ( ( pkg : NpmPackageSuggestion ) => {
28- return < PackageSuggestionLabel package = { pkg } /> ;
29- } , [ ] ) ;
30-
3130 // Use our custom hook for package suggestions
3231 const {
33- suggestions,
32+ suggestions : packageSuggestions ,
3433 isLoading : fetchingSuggestions ,
3534 handleInputChange,
36- } = usePackageSuggestions ( searchTerm , renderSuggestion ) ;
35+ } = usePackageSuggestions ( searchTerm ) ;
36+
37+ // Transform raw package data into AutoComplete options
38+ const autoCompleteOptions = useMemo < AutoCompleteOption [ ] > ( ( ) => {
39+ return packageSuggestions . map ( ( pkg ) => ( {
40+ value : pkg . name ,
41+ label : < PackageSuggestionLabel package = { pkg } /> ,
42+ } ) ) ;
43+ } , [ packageSuggestions ] ) ;
3744
3845 const handleLocalInputChange = ( value : string ) => {
3946 onSearchTermChange ( value ) ;
@@ -63,7 +70,7 @@ const PackageSearchInput: React.FC<PackageSearchInputProps> = ({
6370 < AutoComplete
6471 style = { { width : "100%" } }
6572 value = { searchTerm }
66- options = { suggestions }
73+ options = { autoCompleteOptions }
6774 onSelect = { handleSelect }
6875 onChange = { handleLocalInputChange }
6976 notFoundContent = { notFoundContent }
0 commit comments