Files
fn-design-system/components/search_bar.tsx
T
Egutierrez 5a824c2eee initial: mirror of @fn_library from fn_registry
75 components + DESIGN_SYSTEM.md + sync script.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 19:06:49 +02:00

50 lines
1.3 KiB
TypeScript

import * as React from 'react'
import { TextInput, CloseButton } from '@mantine/core'
import { IconSearch } from '@tabler/icons-react'
interface SearchBarProps {
onSearch: (query: string) => void
placeholder?: string
debounceMs?: number
className?: string
}
function SearchBar({
onSearch,
placeholder = 'Search...',
debounceMs = 300,
className,
}: SearchBarProps) {
const [query, setQuery] = React.useState('')
const timerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null)
const onSearchRef = React.useRef(onSearch)
onSearchRef.current = onSearch
React.useEffect(() => {
if (timerRef.current) clearTimeout(timerRef.current)
timerRef.current = setTimeout(() => {
onSearchRef.current(query)
}, debounceMs)
return () => {
if (timerRef.current) clearTimeout(timerRef.current)
}
}, [query, debounceMs])
return (
<TextInput
value={query}
onChange={(e) => setQuery(e.currentTarget.value)}
placeholder={placeholder}
leftSection={<IconSearch size={14} />}
rightSection={query ? (
<CloseButton size="sm" onClick={() => setQuery('')} aria-label="Clear search" />
) : undefined}
className={className}
size="sm"
/>
)
}
export { SearchBar }
export type { SearchBarProps }