"use client"; import * as SelectPrimitive from "@radix-ui/react-select"; import { Check, ChevronDown } from "lucide-react"; import * as React from "react"; import { useEffectOnce } from "usehooks-ts"; import { cn } from "@/libs/utils"; import { Input } from "./input/input"; import LIcon from "./lucid-icon/lucid-icon"; const Select = SelectPrimitive.Root; const SelectGroup = SelectPrimitive.Group; const SelectValue = SelectPrimitive.Value; const SelectTrigger = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => ( {children} )); SelectTrigger.displayName = SelectPrimitive.Trigger.displayName; type SelectSearchProps = { onSearch?: (e: React.ChangeEvent) => void; searchTerm?: string; }; const SelectSearchInput = ({ searchTerm, onSearch }: SelectSearchProps) => { function handleDelayedFocus() { setTimeout(() => { const element = document.getElementById("select-search"); element?.focus(); }, 100); } useEffectOnce(() => { handleDelayedFocus(); }); return (
} onChange={(e) => { onSearch?.(e); handleDelayedFocus(); }} />
); }; type SearchFieldProps = { searchable?: boolean; } & SelectSearchProps; /** * SelectContent - Searchable select content using * @param {boolean} searchable - Enable search field * @param {string} searchTerm - Search term * @param {function} onSearch - Search handler * * @returns {JSX.Element} * * @example * * * Option 1 * Option 2 * */ const SelectContent = React.forwardRef< React.ElementRef & SearchFieldProps, React.ComponentPropsWithoutRef & SearchFieldProps >( ( { className, children, position = "popper", searchable, onSearch, searchTerm, ...props }, ref, ) => ( {searchable && ( )} {children} ), ); SelectContent.displayName = SelectPrimitive.Content.displayName; const SelectLabel = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); SelectLabel.displayName = SelectPrimitive.Label.displayName; const SelectItem = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => ( {children} )); SelectItem.displayName = SelectPrimitive.Item.displayName; const SelectSeparator = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); SelectSeparator.displayName = SelectPrimitive.Separator.displayName; export { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectSeparator, SelectTrigger, SelectValue, };