"use client"; /** * SmartSelect * * 옵션 개수에 따라 자동으로 검색 기능을 제공하는 셀렉트 컴포넌트. * - 옵션 5개 미만: 기본 Select (드롭다운) * - 옵션 5개 이상: Combobox (검색 + 드롭다운) */ import React, { useState, useMemo } from "react"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Button } from "@/components/ui/button"; import { Check, ChevronsUpDown } from "lucide-react"; import { cn } from "@/lib/utils"; const SEARCH_THRESHOLD = 5; export interface SmartSelectOption { code: string; label: string; } interface SmartSelectProps { options: SmartSelectOption[]; value: string; onValueChange: (value: string) => void; placeholder?: string; disabled?: boolean; className?: string; } export function SmartSelect({ options, value, onValueChange, placeholder = "선택", disabled = false, className, }: SmartSelectProps) { const [open, setOpen] = useState(false); const selectedLabel = useMemo( () => options.find((o) => o.code === value)?.label, [options, value], ); if (options.length < SEARCH_THRESHOLD) { return ( ); } return ( { if (!search) return 1; return val.toLowerCase().includes(search.toLowerCase()) ? 1 : 0; }} > 검색 결과가 없습니다. {options.map((o) => ( { onValueChange(o.code); setOpen(false); }} > {o.label} ))} ); }