Fix reslectiton problem by efactor graph search input box handling logic
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useState, useEffect, useCallback } from 'react'
|
import React, { useState, useEffect, useCallback, useRef } from 'react'
|
||||||
import { Loader2 } from 'lucide-react'
|
import { Loader2 } from 'lucide-react'
|
||||||
import { useDebounce } from '@/hooks/useDebounce'
|
import { useDebounce } from '@/hooks/useDebounce'
|
||||||
|
|
||||||
@@ -81,100 +81,97 @@ export function AsyncSearch<T>({
|
|||||||
const [options, setOptions] = useState<T[]>([])
|
const [options, setOptions] = useState<T[]>([])
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
const [error, setError] = useState<string | null>(null)
|
const [error, setError] = useState<string | null>(null)
|
||||||
const [selectedValue, setSelectedValue] = useState(value)
|
|
||||||
const [focusedValue, setFocusedValue] = useState<string | null>(null)
|
|
||||||
const [searchTerm, setSearchTerm] = useState('')
|
const [searchTerm, setSearchTerm] = useState('')
|
||||||
const debouncedSearchTerm = useDebounce(searchTerm, preload ? 0 : 150)
|
const debouncedSearchTerm = useDebounce(searchTerm, preload ? 0 : 150)
|
||||||
const [originalOptions, setOriginalOptions] = useState<T[]>([])
|
const containerRef = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setMounted(true)
|
setMounted(true)
|
||||||
setSelectedValue(value)
|
}, [])
|
||||||
}, [value])
|
|
||||||
|
|
||||||
// Effect for initial fetch
|
// Handle clicks outside of the component
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initializeOptions = async () => {
|
const handleClickOutside = (event: MouseEvent) => {
|
||||||
try {
|
if (
|
||||||
setLoading(true)
|
containerRef.current &&
|
||||||
setError(null)
|
!containerRef.current.contains(event.target as Node) &&
|
||||||
// If we have a value, use it for the initial search
|
open
|
||||||
const data = value !== null ? await fetcher(value) : []
|
) {
|
||||||
setOriginalOptions(data)
|
setOpen(false)
|
||||||
setOptions(data)
|
|
||||||
} catch (err) {
|
|
||||||
setError(err instanceof Error ? err.message : 'Failed to fetch options')
|
|
||||||
} finally {
|
|
||||||
setLoading(false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mounted) {
|
document.addEventListener('mousedown', handleClickOutside)
|
||||||
initializeOptions()
|
return () => {
|
||||||
|
document.removeEventListener('mousedown', handleClickOutside)
|
||||||
}
|
}
|
||||||
}, [mounted, fetcher, value])
|
}, [open])
|
||||||
|
|
||||||
|
const fetchOptions = useCallback(async (query: string) => {
|
||||||
|
try {
|
||||||
|
setLoading(true)
|
||||||
|
setError(null)
|
||||||
|
const data = await fetcher(query)
|
||||||
|
setOptions(data)
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'Failed to fetch options')
|
||||||
|
} finally {
|
||||||
|
setLoading(false)
|
||||||
|
}
|
||||||
|
}, [fetcher])
|
||||||
|
|
||||||
|
// Load options when search term changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchOptions = async () => {
|
if (!mounted) return
|
||||||
try {
|
|
||||||
setLoading(true)
|
|
||||||
setError(null)
|
|
||||||
const data = await fetcher(debouncedSearchTerm)
|
|
||||||
setOriginalOptions(data)
|
|
||||||
setOptions(data)
|
|
||||||
} catch (err) {
|
|
||||||
setError(err instanceof Error ? err.message : 'Failed to fetch options')
|
|
||||||
} finally {
|
|
||||||
setLoading(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!mounted) {
|
if (preload) {
|
||||||
fetchOptions()
|
|
||||||
} else if (!preload) {
|
|
||||||
fetchOptions()
|
|
||||||
} else if (preload) {
|
|
||||||
if (debouncedSearchTerm) {
|
if (debouncedSearchTerm) {
|
||||||
setOptions(
|
setOptions((prev) =>
|
||||||
originalOptions.filter((option) =>
|
prev.filter((option) =>
|
||||||
filterFn ? filterFn(option, debouncedSearchTerm) : true
|
filterFn ? filterFn(option, debouncedSearchTerm) : true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
setOptions(originalOptions)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
fetchOptions(debouncedSearchTerm)
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
}, [mounted, debouncedSearchTerm, preload, filterFn, fetchOptions])
|
||||||
}, [fetcher, debouncedSearchTerm, mounted, preload, filterFn])
|
|
||||||
|
|
||||||
const handleSelect = useCallback(
|
// Load initial value
|
||||||
(currentValue: string) => {
|
useEffect(() => {
|
||||||
if (currentValue !== selectedValue) {
|
if (!mounted || !value) return
|
||||||
setSelectedValue(currentValue)
|
fetchOptions(value)
|
||||||
onChange(currentValue)
|
}, [mounted, value, fetchOptions])
|
||||||
}
|
|
||||||
|
const handleSelect = useCallback((currentValue: string) => {
|
||||||
|
onChange(currentValue)
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
// Blur the input to ensure focus event triggers on next click
|
||||||
|
const input = document.activeElement as HTMLElement
|
||||||
|
input?.blur()
|
||||||
|
// Close the dropdown
|
||||||
setOpen(false)
|
setOpen(false)
|
||||||
},
|
})
|
||||||
[selectedValue, setSelectedValue, setOpen, onChange]
|
}, [onChange])
|
||||||
)
|
|
||||||
|
|
||||||
const handleFocus = useCallback(
|
const handleFocus = useCallback(() => {
|
||||||
(currentValue: string) => {
|
setOpen(true)
|
||||||
if (currentValue !== focusedValue) {
|
// Use current search term to fetch options
|
||||||
setFocusedValue(currentValue)
|
fetchOptions(searchTerm)
|
||||||
onFocus(currentValue)
|
}, [searchTerm, fetchOptions])
|
||||||
}
|
|
||||||
},
|
const handleMouseDown = useCallback((e: React.MouseEvent) => {
|
||||||
[focusedValue, setFocusedValue, onFocus]
|
const target = e.target as HTMLElement
|
||||||
)
|
if (target.closest('.cmd-item')) {
|
||||||
|
e.preventDefault()
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
ref={containerRef}
|
||||||
className={cn(disabled && 'cursor-not-allowed opacity-50', className)}
|
className={cn(disabled && 'cursor-not-allowed opacity-50', className)}
|
||||||
onFocus={() => {
|
onMouseDown={handleMouseDown}
|
||||||
setOpen(true)
|
|
||||||
}}
|
|
||||||
onBlur={() => setOpen(false)}
|
|
||||||
>
|
>
|
||||||
<Command shouldFilter={false} className="bg-transparent">
|
<Command shouldFilter={false} className="bg-transparent">
|
||||||
<div>
|
<div>
|
||||||
@@ -182,12 +179,13 @@ export function AsyncSearch<T>({
|
|||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
value={searchTerm}
|
value={searchTerm}
|
||||||
className="max-h-8"
|
className="max-h-8"
|
||||||
|
onFocus={handleFocus}
|
||||||
onValueChange={(value) => {
|
onValueChange={(value) => {
|
||||||
setSearchTerm(value)
|
setSearchTerm(value)
|
||||||
if (value && !open) setOpen(true)
|
if (!open) setOpen(true)
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
{loading && options.length > 0 && (
|
{loading && (
|
||||||
<div className="absolute top-1/2 right-2 flex -translate-y-1/2 transform items-center">
|
<div className="absolute top-1/2 right-2 flex -translate-y-1/2 transform items-center">
|
||||||
<Loader2 className="h-4 w-4 animate-spin" />
|
<Loader2 className="h-4 w-4 animate-spin" />
|
||||||
</div>
|
</div>
|
||||||
@@ -209,8 +207,8 @@ export function AsyncSearch<T>({
|
|||||||
key={getOptionValue(option) + `${idx}`}
|
key={getOptionValue(option) + `${idx}`}
|
||||||
value={getOptionValue(option)}
|
value={getOptionValue(option)}
|
||||||
onSelect={handleSelect}
|
onSelect={handleSelect}
|
||||||
onMouseEnter={() => handleFocus(getOptionValue(option))}
|
onMouseMove={() => onFocus(getOptionValue(option))}
|
||||||
className="truncate"
|
className="truncate cmd-item"
|
||||||
>
|
>
|
||||||
{renderOption(option)}
|
{renderOption(option)}
|
||||||
</CommandItem>
|
</CommandItem>
|
||||||
|
Reference in New Issue
Block a user