forked from balahero03/eOrbitor_Pulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiSelectSearch.tsx
More file actions
168 lines (152 loc) · 5.17 KB
/
Copy pathMultiSelectSearch.tsx
File metadata and controls
168 lines (152 loc) · 5.17 KB
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
'use client';
import { useState, useRef, useEffect } from 'react';
interface Option {
id?: string;
label: string;
}
interface MultiSelectSearchProps {
options: Option[];
selected: string[];
onChange: (selected: string[]) => void;
placeholder?: string;
allowCustom?: boolean;
label?: string;
required?: boolean;
}
export default function MultiSelectSearch({
options,
selected,
onChange,
placeholder = 'Search and select...',
allowCustom = false,
label,
required = false,
}: MultiSelectSearchProps) {
const [search, setSearch] = useState('');
const [isOpen, setIsOpen] = useState(false);
const [customInput, setCustomInput] = useState('');
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
function handleClickOutside(event: MouseEvent) {
if (containerRef.current && !containerRef.current.contains(event.target as Node)) {
setIsOpen(false);
}
}
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, []);
const filteredOptions = options.filter(opt =>
opt.label.toLowerCase().includes(search.toLowerCase())
);
const handleSelect = (optionId: string) => {
if (selected.includes(optionId)) {
onChange(selected.filter(id => id !== optionId));
} else {
onChange([...selected, optionId]);
}
};
const handleAddCustom = () => {
if (customInput.trim()) {
onChange([...selected, customInput.trim()]);
setCustomInput('');
setSearch('');
}
};
const handleRemove = (item: string) => {
onChange(selected.filter(id => id !== item));
};
const getDisplayLabel = (id: string): string => {
const option = options.find(opt => opt.id === id);
return option ? option.label : id;
};
return (
<div ref={containerRef} className="w-full">
{label && (
<label className="block text-sm font-medium mb-1">
{label}
{required && <span className="text-red-500">*</span>}
</label>
)}
{/* Selected items as chips */}
{selected.length > 0 && (
<div className="flex flex-wrap gap-2 mb-2">
{selected.map(item => (
<div
key={item}
className="inline-flex items-center gap-1 bg-blue-100 text-blue-800 px-3 py-1 rounded-full text-sm"
>
<span>{getDisplayLabel(item)}</span>
<button
type="button"
onClick={() => handleRemove(item)}
className="text-blue-600 hover:text-blue-800 font-bold cursor-pointer"
>
×
</button>
</div>
))}
</div>
)}
{/* Search input and dropdown */}
<div className="relative">
<input
type="text"
value={search}
onChange={e => setSearch(e.target.value)}
onFocus={() => setIsOpen(true)}
placeholder={placeholder}
className="w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-200"
/>
{/* Dropdown menu */}
{isOpen && (
<div className="absolute top-full left-0 right-0 mt-1 bg-white border border-gray-200 rounded-lg shadow-lg z-10 max-h-60 overflow-y-auto">
{filteredOptions.length > 0 ? (
filteredOptions.map(option => (
<label
key={option.id}
className="flex items-center gap-2 px-3 py-2 hover:bg-gray-50 cursor-pointer"
>
<input
type="checkbox"
checked={selected.includes(option.id || option.label)}
onChange={() => handleSelect(option.id || option.label)}
className="rounded"
/>
<span className="text-sm">{option.label}</span>
</label>
))
) : (
<div className="px-3 py-2 text-sm text-gray-500">No results found</div>
)}
{/* Add custom option */}
{allowCustom && (
<div className="border-t border-gray-100 p-2 space-y-2">
<input
type="text"
value={customInput}
onChange={e => setCustomInput(e.target.value)}
placeholder="Add new..."
className="w-full border border-gray-200 rounded px-2 py-1 text-sm focus:outline-none focus:ring-2 focus:ring-blue-200"
onKeyPress={e => {
if (e.key === 'Enter') {
e.preventDefault();
handleAddCustom();
}
}}
/>
<button
type="button"
onClick={handleAddCustom}
disabled={!customInput.trim()}
className="w-full px-2 py-1 text-sm bg-blue-600 text-white rounded hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
Add
</button>
</div>
)}
</div>
)}
</div>
</div>
);
}