@@ -14,6 +14,7 @@ type NpmSearchResult = {
1414}
1515
1616const CREATE_ITEM_VALUE = '__create__'
17+ const MIN_SEARCH_QUERY_LENGTH = 2
1718
1819export type PackageSearchProps = {
1920 onSelect : ( packageName : string ) => void
@@ -50,14 +51,15 @@ export function PackageSearch({
5051 }
5152 } , [ ] )
5253
53- const hasUsableQuery = debouncedInputValue . length > 2
54+ const trimmedInput = debouncedInputValue . trim ( )
55+ const hasUsableQuery = trimmedInput . length >= MIN_SEARCH_QUERY_LENGTH
5456
5557 const searchQuery = useQuery ( {
56- queryKey : [ 'npm-search' , debouncedInputValue ] ,
58+ queryKey : [ 'npm-search' , trimmedInput ] ,
5759 queryFn : async ( ) => {
5860 const response = await fetch (
5961 `https://registry.npmjs.org/-/v1/search?text=${ encodeURIComponent (
60- debouncedInputValue ,
62+ trimmedInput ,
6163 ) } &size=10`,
6264 )
6365 const data = ( await response . json ( ) ) as {
@@ -70,7 +72,6 @@ export function PackageSearch({
7072 } )
7173
7274 const searchResults = hasUsableQuery ? ( searchQuery . data ?? [ ] ) : [ ]
73- const trimmedInput = debouncedInputValue . trim ( )
7475 const showCreateItem =
7576 hasUsableQuery &&
7677 trimmedInput . length > 0 &&
@@ -122,7 +123,7 @@ export function PackageSearch({
122123 ! showList && 'hidden' ,
123124 ) }
124125 >
125- { inputValue . length < 3 ? (
126+ { inputValue . trim ( ) . length < MIN_SEARCH_QUERY_LENGTH ? (
126127 < div className = "px-3 py-2 text-sm text-gray-500 dark:text-gray-400" >
127128 Keep typing to search...
128129 </ div >
0 commit comments