freshyo/apps/admin-web/src/components/SearchableSelect.tsx
2026-09-06 23:05:20 +05:30

114 lines
3.7 KiB
TypeScript

import { useMemo, useState } from 'react'
import { BottomDialog, SearchBar, p as P } from 'web-components'
import { Check, ChevronDown } from 'lucide-react'
export interface SearchableSelectOption {
label: string
value: string | number
disabled?: boolean
}
interface SearchableSelectProps {
label: string
options: SearchableSelectOption[]
value: string | number
onValueChange: (v: string | number) => void
onSearch?: (q: string) => void
placeholder?: string
disabled?: boolean
error?: boolean
className?: string
testID?: string
// Tolerated for screen compatibility (RN BottomDropdown leftover) — ignored on web
triggerComponent?: any
}
export function SearchableSelect({
label,
options,
value,
onValueChange,
onSearch,
placeholder,
disabled = false,
error = false,
className,
testID,
}: SearchableSelectProps) {
const [open, setOpen] = useState(false)
const [query, setQuery] = useState('')
const selectedLabel = useMemo(() => {
const found = options.find((o) => String(o.value) === String(value))
return found ? found.label : null
}, [options, value])
const displayText = selectedLabel ?? placeholder ?? label
const filtered = useMemo(() => {
const q = query.trim().toLowerCase()
if (!q) return options
return options.filter((o) => o.label.toLowerCase().includes(q))
}, [options, query])
const handleSelect = (optionValue: string | number) => {
onValueChange(optionValue)
setOpen(false)
}
return (
<div className={className} data-testid={testID}>
<P className="mb-1 text-sm text-gray-500 font-medium">{label}</P>
<button
type="button"
disabled={disabled}
onClick={() => setOpen(true)}
className={`flex w-full items-center justify-between rounded-md border bg-white px-3 py-2 text-sm text-left disabled:cursor-not-allowed disabled:opacity-50 ${error ? 'border-red-500' : 'border-gray-300'}`}
>
<span className={`truncate ${selectedLabel ? 'text-gray-800' : 'text-gray-400'}`}>
{displayText}
</span>
<ChevronDown className="h-4 w-4 shrink-0 text-gray-400" />
</button>
<BottomDialog open={open} onClose={() => setOpen(false)}>
<div className="p-4">
<P className="text-lg font-semibold mb-4">{label}</P>
<SearchBar
placeholder="Search..."
value={query}
onChange={(q) => {
setQuery(q)
onSearch?.(q)
}}
onSearch={onSearch}
className="mb-3"
/>
<div className="overflow-y-auto" style={{ maxHeight: 320 }}>
{filtered.map((option) => {
const selected = String(option.value) === String(value)
return (
<button
key={String(option.value)}
type="button"
disabled={option.disabled}
onClick={() => handleSelect(option.value)}
className={`flex flex-row items-center w-full p-3 border-b border-gray-100 text-left ${option.disabled ? 'opacity-50' : 'cursor-pointer hover:bg-gray-50'} ${selected ? 'bg-blue-50' : ''}`}
>
<span className={`text-sm flex-1 ${selected ? 'font-semibold text-blue-700' : 'text-gray-800'}`}>
{option.label}
</span>
{selected ? <Check className="h-4 w-4 text-blue-600" /> : null}
</button>
)
})}
{filtered.length === 0 ? (
<P className="text-center text-gray-500 text-sm p-4">No options found</P>
) : null}
</div>
</div>
</BottomDialog>
</div>
)
}
export default SearchableSelect