File size: 6,996 Bytes
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
f3f5382
 
89493ee
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
f3f5382
89493ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useRef, useEffect } from "react";

const Dropdown = ({
  label,
  selectedValue,
  onSelect,
  options = [],
  placeholder = "Select an option...",
  disabled = false,
  loading = false,
  icon = null,
  className = "",
  dropdownClassName = "",
  optionClassName = "",
  showSearch = false,
  searchPlaceholder = "Search...",
  emptyMessage = "No options available",
  loadingMessage = "Loading...",
}) => {
  const [isOpen, setIsOpen] = useState(false);
  const [searchTerm, setSearchTerm] = useState("");
  const dropdownRef = useRef(null);

  useEffect(() => {
    const handleClickOutside = (event) => {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
        setIsOpen(false);
      }
    };
    document.addEventListener("mousedown", handleClickOutside);
    return () => document.removeEventListener("mousedown", handleClickOutside);
  }, []);

  const filteredOptions = options.filter((option) =>
    option.label
      ? option.label.toLowerCase().includes(searchTerm.toLowerCase())
      : option.toLowerCase().includes(searchTerm.toLowerCase())
  );

  const displayValue = selectedValue?.label || selectedValue || placeholder;
  const isSelected = selectedValue && selectedValue !== placeholder;

  return (
    <div className={`relative ${className}`} ref={dropdownRef}>
      {label && (
        <label className="block text-sm font-medium text-foreground mb-2">
          {label}
        </label>
      )}
      <button
        onClick={() => !disabled && setIsOpen(!isOpen)}
        disabled={disabled}
        className={`w-full p-4 rounded-xl text-left transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-primary-500 disabled:opacity-50 disabled:cursor-not-allowed bg-background border-2 border-secondary-300 hover:bg-primary-50 hover:shadow-lg ${className}`}
      >
        <div className="flex items-center justify-between">
          <div className="flex items-center space-x-3">
            {icon && <div className="text-lg">{icon}</div>}
            <span
              className={`${
                isSelected ? "text-foreground font-medium" : "text-foreground"
              } ${loading ? "text-foreground" : ""}`}
            >
              {loading ? loadingMessage : displayValue}
            </span>
          </div>
          <div className="flex items-center space-x-2">
            {loading && (
              <div className="animate-spin w-4 h-4 border-2 border-primary-500 border-t-transparent rounded-full"></div>
            )}
            <svg
              className={`w-5 h-5 text-primary-600 transition-transform duration-200 ${
                isOpen ? "rotate-180" : ""
              } ${disabled ? "text-foreground" : ""}`}
              fill="none"
              stroke="currentColor"
              viewBox="0 0 24 24"
            >
              <path
                strokeLinecap="round"
                strokeLinejoin="round"
                strokeWidth="2"
                d="M19 9l-7 7-7-7"
              />
            </svg>
          </div>
        </div>
      </button>
      {isOpen && !disabled && (
        <div
          className={`absolute z-50 w-full mt-2 rounded-xl max-h-60 overflow-hidden bg-background border-2 border-primary-200 shadow-lg ${dropdownClassName}`}
        >
          {showSearch && (
            <div className="p-3 border-b border-secondary-100">
              <div className="relative">
                <svg
                  className="absolute left-3 top-1/2 transform -translate-y-1/2 w-4 h-4 text-foreground"
                  fill="none"
                  stroke="currentColor"
                  viewBox="0 0 24 24"
                >
                  <path
                    strokeLinecap="round"
                    strokeLinejoin="round"
                    strokeWidth="2"
                    d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
                  />
                </svg>
                <input
                  type="text"
                  placeholder={searchPlaceholder}
                  value={searchTerm}
                  onChange={(e) => setSearchTerm(e.target.value)}
                  className="w-full pl-9 pr-3 py-2 border border-secondary-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent hover:border-primary-300 transition-colors text-sm"
                  onClick={(e) => e.stopPropagation()}
                />
              </div>
            </div>
          )}
          <div className="max-h-48 overflow-y-auto p-2">
            {loading ? (
              <div className="p-4 text-center text-foreground">
                <div className="animate-spin w-6 h-6 border-2 border-primary-500 border-t-transparent rounded-full mx-auto mb-2"></div>
                {loadingMessage}
              </div>
            ) : filteredOptions.length === 0 ? (
              <div className="p-4 text-center text-foreground">
                {emptyMessage}
              </div>
            ) : (
              filteredOptions.map((option) => {
                const optionValue = option.value || option;
                const optionLabel = option.label || option;
                const optionIcon = option.icon;
                const optionDescription = option.description;
                const isOptionSelected =
                  selectedValue === optionValue ||
                  (selectedValue?.value && selectedValue.value === optionValue);
                return (
                  <button
                    key={optionValue}
                    onClick={() => {
                      onSelect(option);
                      setIsOpen(false);
                      setSearchTerm("");
                    }}
                    className={`w-full p-3 text-left rounded-lg transition-all duration-200 hover:bg-blue-100 hover:text-blue-900 ${
                      isOptionSelected
                        ? "bg-gradient-to-r from-primary-200 to-accent-200 text-primary-800 font-medium"
                        : "text-foreground hover:bg-blue-50"
                    } ${optionClassName}`}
                  >
                    <div className="flex items-center space-x-3">
                      {optionIcon && (
                        <div className="text-lg">{optionIcon}</div>
                      )}
                      <div className="flex-1 min-w-0">
                        <div className="truncate font-medium">
                          {optionLabel}
                        </div>
                        {optionDescription && (
                          <div className="text-xs text-foreground truncate mt-1">
                            {optionDescription}
                          </div>
                        )}
                      </div>
                    </div>
                  </button>
                );
              })
            )}
          </div>
        </div>
      )}
    </div>
  );
};

export default Dropdown;