import { useEffect, useRef, useState } from "react"; import * as PopoverPrimitive from "@radix-ui/react-popover"; import { Check, ChevronsUpDown, Search, X, Loader2 } from "lucide-react"; import { cn } from "@/lib/utils"; // ─── Types ──────────────────────────────────────────────────────────────────── export interface InfiniteSelectOption { value: string; label: string; } export interface InfiniteSelectProps { value?: string; onChange?: (value: string) => void; options?: InfiniteSelectOption[]; placeholder?: string; searchPlaceholder?: string; label?: string; required?: boolean; error?: string; disabled?: boolean; loading?: boolean; isLoadingMore?: boolean; hasNextPage?: boolean; onLoadMore?: () => void; searchValue?: string; onSearchChange?: (value: string) => void; onOpenChange?: (open: boolean) => void; } /** * Searchable select with infinite scroll. * * The consumer owns the data fetching (e.g. TanStack Query `useInfiniteQuery`): * it passes the flattened options, and `hasNextPage`/`onLoadMore` drive loading * additional pages when the list bottom is reached. */ export function InfiniteSelect({ value = "", onChange, options = [], placeholder = "Saýlamak üçin basyň", searchPlaceholder = "Gözle...", label, required, error, disabled, loading = false, isLoadingMore = false, hasNextPage = false, onLoadMore, searchValue, onSearchChange, onOpenChange, }: InfiniteSelectProps) { const [open, setOpen] = useState(false); const containerRef = useRef(null); const scrollContainerRef = useRef(null); const sentinelRef = useRef(null); const [portalContainer, setPortalContainer] = useState(undefined); useEffect(() => { if (containerRef.current) { const dialog = containerRef.current.closest( '[role="dialog"], [role="alertdialog"], [data-slot="alert-dialog-content"], [data-slot="dialog-content"]', ); if (dialog) { setPortalContainer(dialog); } } }, []); useEffect(() => { const el = scrollContainerRef.current; if (!el || !open) return; const handleWheel = (e: WheelEvent) => { e.stopPropagation(); }; el.addEventListener("wheel", handleWheel, { passive: true }); return () => el.removeEventListener("wheel", handleWheel); }, [open]); const selected = options.find((o) => o.value === value); const handleSelect = (val: string) => { onChange?.(val); setOpen(false); }; const handleClear = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); onChange?.(""); }; const loadingRef = useRef(false); useEffect(() => { if (!isLoadingMore) { loadingRef.current = false; } }, [isLoadingMore]); useEffect(() => { if (!open || !sentinelRef.current || !hasNextPage || isLoadingMore) return; const el = sentinelRef.current; const observer = new IntersectionObserver( (entries) => { if (entries[0]?.isIntersecting && hasNextPage && !isLoadingMore && !loadingRef.current) { loadingRef.current = true; onLoadMore?.(); } }, { rootMargin: "120px" }, ); observer.observe(el); return () => observer.disconnect(); }, [open, hasNextPage, isLoadingMore, onLoadMore]); return (
{label && ( )} { if (!disabled) { setOpen(next); onOpenChange?.(next); } }} > e.stopPropagation()} onTouchMove={(e) => e.stopPropagation()} > {onSearchChange && (
onSearchChange(e.target.value)} placeholder={searchPlaceholder} className="flex-1 bg-transparent text-sm text-foreground placeholder:text-muted-foreground/50 focus:outline-none" /> {searchValue && ( )}
)}
e.stopPropagation()} onTouchMove={(e) => e.stopPropagation()} className="max-h-60 overflow-y-auto overscroll-contain scrollbar-thin py-1" > {loading && options.length === 0 ? (

Loading...

) : options.length === 0 ? (

Netije tapylmady

) : ( options.map((opt) => { const isSelected = opt.value === value; return ( ); }) )} {hasNextPage && (
{isLoadingMore && ( Loading... )}
)}
{error && (

{error}

)}
); }