-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathSelect.tsx
More file actions
executable file
·311 lines (283 loc) · 11.8 KB
/
Select.tsx
File metadata and controls
executable file
·311 lines (283 loc) · 11.8 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import React, { useCallback, useEffect, useRef, useState } from "react";
import { twMerge } from "tailwind-merge";
import { COLORS, DEFAULT_THEME, THEME_DATA } from "../constants";
import useOnClickOutside from "../hooks/use-onclick-outside";
import { ChevronIcon, CloseIcon } from "./Icons";
import Options from "./Options";
import SearchInput from "./SearchInput";
import SelectProvider from "./SelectProvider";
import Spinner from "./Spinner";
import { Option, Options as ListOption, SelectProps } from "./type";
const Select: React.FC<SelectProps> = ({
options = [],
value = null,
onChange,
onSearchInputChange,
placeholder = "Select...",
searchInputPlaceholder = "Search...",
isMultiple = false,
isClearable = false,
isSearchable = false,
isDisabled = false,
loading = false,
menuIsOpen = false,
noOptionsMessage = "No options found",
primaryColor = DEFAULT_THEME,
formatGroupLabel = null,
formatOptionLabel = null,
classNames
}) => {
const [open, setOpen] = useState<boolean>(menuIsOpen);
const [list, setList] = useState<ListOption>(options);
const [inputValue, setInputValue] = useState<string>("");
const ref = useRef<HTMLDivElement>(null);
const searchBoxRef = useRef<HTMLInputElement>(null);
useEffect(() => {
const formatItem = (item: Option) => {
if ("disabled" in item) return item;
return {
...item,
disabled: false
};
};
setList(
options.map(item => {
if ("options" in item) {
return {
label: item.label,
options: item.options.map(formatItem)
};
} else {
return formatItem(item);
}
})
);
}, [options]);
useEffect(() => {
if (isSearchable) {
if (open) {
searchBoxRef.current?.select();
} else {
setInputValue("");
}
}
}, [open, isSearchable]);
const toggle = useCallback(() => {
if (!isDisabled) {
setOpen(!open);
}
}, [isDisabled, open]);
const closeDropDown = useCallback(() => {
if (open) setOpen(false);
}, [open]);
useOnClickOutside(ref, () => {
closeDropDown();
});
const onPressEnterOrSpace = useCallback(
(e: React.KeyboardEvent<HTMLDivElement>) => {
e.preventDefault();
if ((e.code === "Enter" || e.code === "Space") && !isDisabled) {
toggle();
}
},
[isDisabled, toggle]
);
const handleValueChange = useCallback(
(selected: Option) => {
function update() {
if (!isMultiple && !Array.isArray(value)) {
closeDropDown();
onChange(selected);
}
if (isMultiple && (Array.isArray(value) || value === null)) {
onChange(value === null ? [selected] : [...value, selected]);
}
}
if (selected !== value) {
update();
}
},
[closeDropDown, isMultiple, onChange, value]
);
const clearValue = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
e.stopPropagation();
onChange(null);
},
[onChange]
);
const removeItem = useCallback(
(e: React.MouseEvent<HTMLDivElement>, item: Option) => {
if (isMultiple && Array.isArray(value) && value.length) {
e.stopPropagation();
const result = value.filter(current => item.value !== current.value);
onChange(result.length ? result : null);
}
},
[isMultiple, onChange, value]
);
const getSelectClass = useCallback(() => {
let ringColor = THEME_DATA.ring[DEFAULT_THEME];
if (COLORS.includes(primaryColor)) {
ringColor = THEME_DATA.ring[primaryColor as keyof typeof THEME_DATA.ring];
}
let borderFocus = THEME_DATA.borderFocus[DEFAULT_THEME];
if (COLORS.includes(primaryColor)) {
borderFocus =
THEME_DATA.borderFocus[primaryColor as keyof typeof THEME_DATA.borderFocus];
}
const baseClass =
"flex text-sm text-gray-500 border border-gray-300 rounded shadow-sm transition-all duration-300 focus:outline-none";
const defaultClass = `${baseClass} ${
isDisabled
? "bg-gray-200"
: `bg-white hover:border-gray-400 ${borderFocus} focus:ring ${ringColor}`
}`;
return classNames && classNames.menuButton
? classNames.menuButton({ isDisabled })
: defaultClass;
}, [classNames, isDisabled, primaryColor]);
const getTagItemClass = useCallback(
(item: Option) => {
const baseClasse = "bg-gray-200 border rounded-sm flex space-x-1";
const disabledClass = isDisabled ? "border-gray-500 px-1" : "pl-1";
return classNames?.tagItem
? classNames.tagItem({ item, isDisabled })
: `${baseClasse} ${disabledClass}`;
},
[classNames, isDisabled]
);
return (
<SelectProvider
otherData={{
formatGroupLabel,
formatOptionLabel,
classNames
}}
value={value}
handleValueChange={handleValueChange}
>
<div className="relative w-full" ref={ref}>
<div
aria-expanded={open}
onKeyDown={onPressEnterOrSpace}
onClick={toggle}
className={getSelectClass()}
>
<div
className={twMerge(
"grow pl-2.5 py-2 pr-2 flex flex-wrap gap-1",
!isMultiple ? "truncate" : ""
)}
>
{!isMultiple ? (
<p className="truncate cursor-default select-none">
{value && !Array.isArray(value) ? value.label : placeholder}
</p>
) : (
<>
{value === null && placeholder}
{Array.isArray(value) &&
value.map((item, index) => (
<div className={getTagItemClass(item)} key={index}>
<p
className={
classNames?.tagItemText
? classNames.tagItemText
: "text-gray-600 cursor-default select-none"
}
>
{item.label}
</p>
{!isDisabled && (
<div
role="button"
tabIndex={0}
onClick={e => removeItem(e, item)}
className={
classNames?.tagItemIconContainer
? classNames.tagItemIconContainer
: "flex items-center px-1 cursor-pointer rounded-r-sm hover:bg-red-200 hover:text-red-600"
}
>
<CloseIcon
className={
classNames?.tagItemIcon
? classNames.tagItemIcon
: "w-3 h-3 mt-0.5"
}
/>
</div>
)}
</div>
))}
</>
)}
</div>
<div className="flex flex-none items-center py-1.5">
{loading && (
<div className="px-1.5">
<Spinner primaryColor={primaryColor} />
</div>
)}
{isClearable && !isDisabled && value !== null && (
<div className="px-1.5 cursor-pointer" onClick={clearValue}>
<CloseIcon
className={
classNames?.closeIcon
? classNames.closeIcon
: "w-5 h-5 p-0.5"
}
/>
</div>
)}
<div className="h-full">
<span className="w-px h-full inline-block text-white bg-gray-300 text-opacity-0" />
</div>
<div className="px-1.5">
<ChevronIcon
className={`transition duration-300 w-6 h-6 p-0.5${
open ? " transform rotate-90 text-gray-500" : " text-gray-300"
}`}
/>
</div>
</div>
</div>
{open && !isDisabled && (
<div
className={
classNames?.menu
? classNames.menu
: "absolute z-10 w-full bg-white shadow-lg border rounded py-1 mt-1.5 text-sm text-gray-700"
}
>
{isSearchable && (
<SearchInput
ref={searchBoxRef}
value={inputValue}
placeholder={searchInputPlaceholder}
onChange={e => {
if (
onSearchInputChange &&
typeof onSearchInputChange === "function"
)
onSearchInputChange(e);
setInputValue(e.target.value);
}}
/>
)}
<Options
list={list}
noOptionsMessage={noOptionsMessage}
text={inputValue}
isMultiple={isMultiple}
value={value}
primaryColor={primaryColor || DEFAULT_THEME}
/>
</div>
)}
</div>
</SelectProvider>
);
};
export default Select;