File size: 9,201 Bytes
0fcfa0b | 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | import type React from "react";
import { useState, useEffect, useRef } from "react";
interface Option {
value: string;
text: string;
}
interface MultiSelectProps {
label: string;
options: Option[];
defaultSelected?: string[];
value?: string[];
onChange?: (selected: string[]) => void;
disabled?: boolean;
placeholder?: string;
}
const MultiSelect: React.FC<MultiSelectProps> = ({
label,
options,
defaultSelected = [],
value,
onChange,
disabled = false,
placeholder = "Select options",
}) => {
const isControlled = value !== undefined;
const [internalSelected, setInternalSelected] =
useState<string[]>(defaultSelected);
const selectedOptions = isControlled ? value : internalSelected;
const [isOpen, setIsOpen] = useState(false);
const [focusedIndex, setFocusedIndex] = useState(-1);
const dropdownRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target as Node)
) {
setIsOpen(false);
}
};
if (isOpen) {
document.addEventListener("mousedown", handleClickOutside);
return () =>
document.removeEventListener("mousedown", handleClickOutside);
}
}, [isOpen]);
const updateSelection = (newSelected: string[]) => {
if (!isControlled) setInternalSelected(newSelected);
onChange?.(newSelected);
};
const toggleDropdown = () => {
if (!disabled) {
setIsOpen((prev) => !prev);
setFocusedIndex(-1);
}
};
const handleSelect = (optionValue: string) => {
const newSelected = selectedOptions.includes(optionValue)
? selectedOptions.filter((v) => v !== optionValue)
: [...selectedOptions, optionValue];
updateSelection(newSelected);
};
const removeOption = (optionValue: string) => {
updateSelection(selectedOptions.filter((v) => v !== optionValue));
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (disabled) return;
e.preventDefault();
switch (e.key) {
case "Enter":
if (!isOpen) {
setIsOpen(true);
} else if (focusedIndex >= 0) {
handleSelect(options[focusedIndex].value);
}
break;
case "Escape":
setIsOpen(false);
break;
case "ArrowDown":
if (!isOpen) {
setIsOpen(true);
} else {
setFocusedIndex((prev) => (prev < options.length - 1 ? prev + 1 : 0));
}
break;
case "ArrowUp":
if (isOpen) {
setFocusedIndex((prev) => (prev > 0 ? prev - 1 : options.length - 1));
}
break;
}
};
return (
<div className="w-full" ref={dropdownRef}>
<label
className="mb-1.5 block text-sm font-medium text-gray-700 dark:text-gray-400"
id={`${label}-label`}
>
{label}
</label>
<div className="relative z-20 inline-block w-full">
<div className="relative flex flex-col items-center">
<div
onClick={toggleDropdown}
onKeyDown={handleKeyDown}
className="w-full"
role="combobox"
aria-expanded={isOpen}
aria-haspopup="listbox"
aria-labelledby={`${label}-label`}
aria-disabled={disabled}
tabIndex={disabled ? -1 : 0}
>
<div
className={`mb-2 flex min-h-11 rounded-lg border border-gray-300 py-1.5 pl-3 pr-3 shadow-theme-xs outline-hidden transition focus:border-brand-300 focus:shadow-focus-ring dark:border-gray-700 dark:bg-gray-900 dark:focus:border-brand-300 ${
disabled
? "opacity-50 cursor-not-allowed bg-gray-50 dark:bg-gray-800"
: "cursor-pointer"
}`}
>
<div className="flex flex-wrap flex-auto gap-2">
{selectedOptions.length > 0 ? (
selectedOptions.map((value) => {
const text =
options.find((opt) => opt.value === value)?.text || value;
return (
<div
key={value}
className="group flex items-center justify-center rounded-full border-[0.7px] border-transparent bg-gray-100 py-1 pl-2.5 pr-2 text-sm text-gray-800 hover:border-gray-200 dark:bg-gray-800 dark:text-white/90 dark:hover:border-gray-800"
>
<span className="flex-initial max-w-full">{text}</span>
<button
type="button"
onClick={(e) => {
e.stopPropagation();
if (!disabled) removeOption(value);
}}
disabled={disabled}
className="pl-2 text-gray-500 cursor-pointer group-hover:text-gray-400 dark:text-gray-400 disabled:cursor-not-allowed"
aria-label={`Remove ${text}`}
>
<svg
className="fill-current"
width="14"
height="14"
viewBox="0 0 14 14"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M3.40717 4.46881C3.11428 4.17591 3.11428 3.70104 3.40717 3.40815C3.70006 3.11525 4.17494 3.11525 4.46783 3.40815L6.99943 5.93975L9.53095 3.40822C9.82385 3.11533 10.2987 3.11533 10.5916 3.40822C10.8845 3.70112 10.8845 4.17599 10.5916 4.46888L8.06009 7.00041L10.5916 9.53193C10.8845 9.82482 10.8845 10.2997 10.5916 10.5926C10.2987 10.8855 9.82385 10.8855 9.53095 10.5926L6.99943 8.06107L4.46783 10.5927C4.17494 10.8856 3.70006 10.8856 3.40717 10.5927C3.11428 10.2998 3.11428 9.8249 3.40717 9.53201L5.93877 7.00041L3.40717 4.46881Z"
/>
</svg>
</button>
</div>
);
})
) : (
<div className="w-full h-full p-1 pr-2 text-sm text-gray-400 dark:text-gray-500 pointer-events-none">
{placeholder}
</div>
)}
</div>
<div className="flex items-center self-start py-1 pl-1 pr-1 w-7">
<button
type="button"
onClick={(e) => {
e.stopPropagation();
toggleDropdown();
}}
disabled={disabled}
className="w-5 h-5 text-gray-700 outline-hidden cursor-pointer focus:outline-hidden dark:text-gray-400 disabled:cursor-not-allowed"
>
<svg
className={`stroke-current transition-transform ${
isOpen ? "rotate-180" : ""
}`}
width="20"
height="20"
viewBox="0 0 20 20"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M4.79175 7.39551L10.0001 12.6038L15.2084 7.39551"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
</div>
</div>
</div>
{isOpen && (
<div
className="absolute left-0 z-40 w-full overflow-y-auto bg-white rounded-lg shadow-sm top-full max-h-select dark:bg-gray-900"
onClick={(e) => e.stopPropagation()}
role="listbox"
aria-label={label}
>
{options.map((option, index) => {
const isSelected = selectedOptions.includes(option.value);
const isFocused = index === focusedIndex;
return (
<div
key={option.value}
className={`hover:bg-primary/5 w-full cursor-pointer rounded-t border-b border-gray-200 dark:border-gray-800 ${
isFocused ? "bg-primary/5" : ""
} ${isSelected ? "bg-primary/10" : ""}`}
onClick={() => handleSelect(option.value)}
role="option"
aria-selected={isSelected}
>
<div className="relative flex w-full items-center p-2 pl-2">
<div className="mx-2 leading-6 text-gray-800 dark:text-white/90">
{option.text}
</div>
</div>
</div>
);
})}
</div>
)}
</div>
</div>
</div>
);
};
export default MultiSelect;
|