1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
"use client";
import React, { useState, useCallback, useEffect } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Badge } from "@/components/ui/badge";
import {
Command,
CommandList,
CommandEmpty,
CommandGroup,
CommandItem,
} from "@/components/ui/command";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { Check, ChevronsUpDown, X, Search } from "lucide-react";
import { cn } from "@/lib/utils";
import { useDebounce } from "@/hooks/use-debounce";
import { searchProcurementItemsForSelector, ProcurementSearchItem, getProcurementItemByCode } from "./procurement-item-service";
interface ProcurementItemSelectorProps {
selectedProcurementItem?: ProcurementSearchItem | null;
onProcurementItemSelect?: (item: ProcurementSearchItem | null) => void;
onClear?: () => void;
placeholder?: string;
disabled?: boolean;
className?: string;
}
export function ProcurementItemSelector({
selectedProcurementItem,
onProcurementItemSelect,
onClear,
placeholder = "품목을 검색하세요...",
disabled = false,
className,
}: ProcurementItemSelectorProps) {
const [open, setOpen] = useState(false);
const [searchQuery, setSearchQuery] = useState("");
const [searchResults, setSearchResults] = useState<ProcurementSearchItem[]>([]);
const [isLoading, setIsLoading] = useState(false);
const debouncedSearchQuery = useDebounce(searchQuery, 300);
// 검색 쿼리가 변경될 때마다 검색 실행
useEffect(() => {
const performSearch = async () => {
if (debouncedSearchQuery.length < 1) {
setSearchResults([]);
return;
}
setIsLoading(true);
try {
const results = await searchProcurementItemsForSelector(debouncedSearchQuery);
setSearchResults(results);
} catch (error) {
console.error("품목 검색 실패:", error);
setSearchResults([]);
} finally {
setIsLoading(false);
}
};
performSearch();
}, [debouncedSearchQuery]);
// 품목 선택 핸들러
const handleSelect = useCallback((item: ProcurementSearchItem) => {
onProcurementItemSelect?.(item);
setOpen(false);
setSearchQuery("");
setSearchResults([]);
}, [onProcurementItemSelect]);
// 선택 해제 핸들러
const handleClear = useCallback(() => {
onProcurementItemSelect?.(null);
onClear?.();
setSearchQuery("");
setSearchResults([]);
}, [onProcurementItemSelect, onClear]);
return (
<div className={cn("flex items-center space-x-2", className)}>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="w-full justify-between"
disabled={disabled}
>
{selectedProcurementItem ? (
<span className="truncate">
{selectedProcurementItem.displayText}
</span>
) : (
<span className="text-muted-foreground">{placeholder}</span>
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<div className="p-2">
<div className="relative">
<Search className="absolute left-2 top-2.5 h-4 w-4 text-muted-foreground" />
<Input
placeholder="품목코드 또는 품목명으로 검색..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="pl-8"
autoFocus
/>
</div>
</div>
<Command>
<CommandList>
<CommandEmpty>
{isLoading ? (
<div className="py-6 text-center text-sm text-muted-foreground">
검색 중...
</div>
) : searchQuery.length < 1 ? (
<div className="py-6 text-center text-sm text-muted-foreground">
품목코드 또는 품목명을 입력하세요
</div>
) : (
<div className="py-6 text-center text-sm text-muted-foreground">
검색 결과가 없습니다
</div>
)}
</CommandEmpty>
<CommandGroup>
{searchResults.map((item) => (
<CommandItem
key={item.itemCode}
value={item.itemCode}
onSelect={() => handleSelect(item)}
className="cursor-pointer"
>
<Check
className={cn(
"mr-2 h-4 w-4",
selectedProcurementItem?.itemCode === item.itemCode
? "opacity-100"
: "opacity-0"
)}
/>
<div className="flex flex-col">
<span className="font-medium">{item.itemCode}</span>
<span className="text-sm text-muted-foreground">{item.itemName}</span>
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
{/* 선택 해제 버튼 */}
{selectedProcurementItem && (
<Button
variant="ghost"
size="sm"
onClick={handleClear}
disabled={disabled}
className="h-8 w-8 p-0"
>
<X className="h-4 w-4" />
</Button>
)}
</div>
);
}
|