@oppulence/design-system 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hooks/use-resize-observer.ts +24 -0
- package/lib/ai.ts +31 -0
- package/package.json +19 -1
- package/src/components/atoms/animated-size-container.tsx +59 -0
- package/src/components/atoms/currency-input.tsx +16 -0
- package/src/components/atoms/icons.tsx +840 -0
- package/src/components/atoms/image.tsx +23 -0
- package/src/components/atoms/index.ts +10 -0
- package/src/components/atoms/loader.tsx +92 -0
- package/src/components/atoms/quantity-input.tsx +103 -0
- package/src/components/atoms/record-button.tsx +178 -0
- package/src/components/atoms/submit-button.tsx +26 -0
- package/src/components/atoms/text-effect.tsx +251 -0
- package/src/components/atoms/text-shimmer.tsx +74 -0
- package/src/components/molecules/actions.tsx +53 -0
- package/src/components/molecules/branch.tsx +192 -0
- package/src/components/molecules/code-block.tsx +151 -0
- package/src/components/molecules/form.tsx +177 -0
- package/src/components/molecules/index.ts +12 -0
- package/src/components/molecules/inline-citation.tsx +295 -0
- package/src/components/molecules/message.tsx +64 -0
- package/src/components/molecules/sources.tsx +116 -0
- package/src/components/molecules/suggestion.tsx +53 -0
- package/src/components/molecules/task.tsx +74 -0
- package/src/components/molecules/time-range-input.tsx +73 -0
- package/src/components/molecules/tool-call-indicator.tsx +42 -0
- package/src/components/molecules/tool.tsx +130 -0
- package/src/components/organisms/combobox-dropdown.tsx +171 -0
- package/src/components/organisms/conversation.tsx +98 -0
- package/src/components/organisms/date-range-picker.tsx +53 -0
- package/src/components/organisms/editor/extentions/bubble-menu/bubble-item.tsx +30 -0
- package/src/components/organisms/editor/extentions/bubble-menu/bubble-menu-button.tsx +27 -0
- package/src/components/organisms/editor/extentions/bubble-menu/index.tsx +63 -0
- package/src/components/organisms/editor/extentions/bubble-menu/link-item.tsx +104 -0
- package/src/components/organisms/editor/extentions/register.ts +22 -0
- package/src/components/organisms/editor/index.tsx +50 -0
- package/src/components/organisms/editor/styles.css +31 -0
- package/src/components/organisms/editor/utils.ts +19 -0
- package/src/components/organisms/index.ts +11 -0
- package/src/components/organisms/multiple-selector.tsx +632 -0
- package/src/components/organisms/prompt-input.tsx +747 -0
- package/src/components/organisms/reasoning.tsx +170 -0
- package/src/components/organisms/response.tsx +121 -0
- package/src/components/organisms/toast-toaster.tsx +84 -0
- package/src/components/organisms/toast.tsx +124 -0
- package/src/components/organisms/use-toast.tsx +206 -0
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Command as CommandPrimitive, useCommandState } from "cmdk";
|
|
4
|
+
import { X } from "lucide-react";
|
|
5
|
+
import * as React from "react";
|
|
6
|
+
import { forwardRef, useEffect } from "react";
|
|
7
|
+
|
|
8
|
+
import { cn } from "../../../lib/utils";
|
|
9
|
+
import { badgeVariants } from "../atoms/badge";
|
|
10
|
+
|
|
11
|
+
export interface Option {
|
|
12
|
+
value: string;
|
|
13
|
+
label: string;
|
|
14
|
+
create?: boolean;
|
|
15
|
+
disable?: boolean;
|
|
16
|
+
/** fixed option that can't be removed. */
|
|
17
|
+
fixed?: boolean;
|
|
18
|
+
/** Group the options by providing key. */
|
|
19
|
+
[key: string]: string | boolean | undefined;
|
|
20
|
+
}
|
|
21
|
+
interface GroupOption {
|
|
22
|
+
[key: string]: Option[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface MultipleSelectorProps {
|
|
26
|
+
value?: Option[];
|
|
27
|
+
defaultOptions?: Option[];
|
|
28
|
+
/** manually controlled options */
|
|
29
|
+
options?: Option[];
|
|
30
|
+
placeholder?: string;
|
|
31
|
+
/** Loading component. */
|
|
32
|
+
loadingIndicator?: React.ReactNode;
|
|
33
|
+
/** Empty component. */
|
|
34
|
+
emptyIndicator?: React.ReactNode;
|
|
35
|
+
/** Debounce time for async search. Only work with `onSearch`. */
|
|
36
|
+
delay?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Only work with `onSearch` prop. Trigger search when `onFocus`.
|
|
39
|
+
* For example, when user click on the input, it will trigger the search to get initial options.
|
|
40
|
+
**/
|
|
41
|
+
triggerSearchOnFocus?: boolean;
|
|
42
|
+
/** async search */
|
|
43
|
+
onSearch?: (value: string) => Promise<Option[]>;
|
|
44
|
+
/**
|
|
45
|
+
* sync search. This search will not showing loadingIndicator.
|
|
46
|
+
* The rest props are the same as async search.
|
|
47
|
+
* i.e.: creatable, groupBy, delay.
|
|
48
|
+
**/
|
|
49
|
+
onSearchSync?: (value: string) => Option[];
|
|
50
|
+
onChange?: (options: Option[]) => void;
|
|
51
|
+
onCreate?: (option: Option) => void;
|
|
52
|
+
/** Limit the maximum number of selected options. */
|
|
53
|
+
maxSelected?: number;
|
|
54
|
+
/** When the number of selected options exceeds the limit, the onMaxSelected will be called. */
|
|
55
|
+
onMaxSelected?: (maxLimit: number) => void;
|
|
56
|
+
/** Hide the placeholder when there are options selected. */
|
|
57
|
+
hidePlaceholderWhenSelected?: boolean;
|
|
58
|
+
disabled?: boolean;
|
|
59
|
+
/** Group the options base on provided key. */
|
|
60
|
+
groupBy?: string;
|
|
61
|
+
/**
|
|
62
|
+
* First item selected is a default behavior by cmdk. That is why the default is true.
|
|
63
|
+
* This is a workaround solution by add a dummy item.
|
|
64
|
+
*
|
|
65
|
+
* @reference: https://github.com/pacocoursey/cmdk/issues/171
|
|
66
|
+
*/
|
|
67
|
+
selectFirstItem?: boolean;
|
|
68
|
+
/** Allow user to create option when there is no option matched. */
|
|
69
|
+
creatable?: boolean;
|
|
70
|
+
/** Props of `Command` */
|
|
71
|
+
commandProps?: Omit<
|
|
72
|
+
React.ComponentPropsWithoutRef<typeof CommandPrimitive>,
|
|
73
|
+
"className"
|
|
74
|
+
>;
|
|
75
|
+
/** Props of `CommandInput` */
|
|
76
|
+
inputProps?: Omit<
|
|
77
|
+
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>,
|
|
78
|
+
"value" | "placeholder" | "disabled" | "className"
|
|
79
|
+
>;
|
|
80
|
+
|
|
81
|
+
renderOption?: (option: Option) => React.ReactNode;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface MultipleSelectorRef {
|
|
85
|
+
selectedValue: Option[];
|
|
86
|
+
input: HTMLInputElement;
|
|
87
|
+
focus: () => void;
|
|
88
|
+
reset: () => void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function useDebounce<T>(value: T, delay?: number): T {
|
|
92
|
+
const [debouncedValue, setDebouncedValue] = React.useState<T>(value);
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
const timer = setTimeout(() => setDebouncedValue(value), delay || 500);
|
|
96
|
+
|
|
97
|
+
return () => {
|
|
98
|
+
clearTimeout(timer);
|
|
99
|
+
};
|
|
100
|
+
}, [value, delay]);
|
|
101
|
+
|
|
102
|
+
return debouncedValue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function transToGroupOption(options: Option[], groupBy?: string) {
|
|
106
|
+
if (options.length === 0) {
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
109
|
+
if (!groupBy) {
|
|
110
|
+
return {
|
|
111
|
+
"": options,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const groupOption: GroupOption = {};
|
|
116
|
+
for (const option of options) {
|
|
117
|
+
const key = (option[groupBy] as string) || "";
|
|
118
|
+
if (!groupOption[key]) {
|
|
119
|
+
groupOption[key] = [];
|
|
120
|
+
}
|
|
121
|
+
groupOption[key].push(option);
|
|
122
|
+
}
|
|
123
|
+
return groupOption;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function removePickedOption(groupOption: GroupOption, picked: Option[]) {
|
|
127
|
+
const cloneOption = JSON.parse(JSON.stringify(groupOption)) as GroupOption;
|
|
128
|
+
|
|
129
|
+
for (const [key, value] of Object.entries(cloneOption)) {
|
|
130
|
+
cloneOption[key] = value.filter(
|
|
131
|
+
(val) => !picked.find((p) => p.value === val.value),
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return cloneOption;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isOptionsExist(groupOption: GroupOption, targetOption: Option[]) {
|
|
138
|
+
for (const [, value] of Object.entries(groupOption)) {
|
|
139
|
+
if (
|
|
140
|
+
value.some((option) => targetOption.find((p) => p.value === option.value))
|
|
141
|
+
) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The `CommandEmpty` of shadcn/ui will cause the cmdk empty not rendering correctly.
|
|
150
|
+
* So we create one and copy the `Empty` implementation from `cmdk`.
|
|
151
|
+
*
|
|
152
|
+
* @reference: https://github.com/hsuanyi-chou/shadcn-ui-expansions/issues/34#issuecomment-1949561607
|
|
153
|
+
**/
|
|
154
|
+
const CommandEmpty = forwardRef<
|
|
155
|
+
HTMLDivElement,
|
|
156
|
+
React.ComponentProps<typeof CommandPrimitive.Empty>
|
|
157
|
+
>(({ ...props }, forwardedRef) => {
|
|
158
|
+
const render = useCommandState((state) => state.filtered.count === 0);
|
|
159
|
+
|
|
160
|
+
if (!render) return null;
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<div
|
|
164
|
+
ref={forwardedRef}
|
|
165
|
+
className="py-6 text-center text-sm"
|
|
166
|
+
cmdk-empty=""
|
|
167
|
+
role="presentation"
|
|
168
|
+
{...props}
|
|
169
|
+
/>
|
|
170
|
+
);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
CommandEmpty.displayName = "CommandEmpty";
|
|
174
|
+
|
|
175
|
+
const commandItemClassName =
|
|
176
|
+
"data-[selected=true]:bg-muted data-[selected=true]:text-foreground data-[selected=true]:**:[svg]:text-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none [&_svg:not([class*='size-'])]:size-4 [[data-slot=dialog-content]_&]:rounded-lg group/command-item data-[disabled=true]:pointer-events-none data-[disabled=true]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0";
|
|
177
|
+
const commandGroupClassName =
|
|
178
|
+
"text-foreground [&_[cmdk-group-heading]]:text-muted-foreground overflow-hidden p-1 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium";
|
|
179
|
+
const commandListClassName =
|
|
180
|
+
"no-scrollbar scroll-py-1 outline-none overflow-x-hidden overflow-y-auto";
|
|
181
|
+
|
|
182
|
+
const MultipleSelector = React.forwardRef<
|
|
183
|
+
MultipleSelectorRef,
|
|
184
|
+
MultipleSelectorProps
|
|
185
|
+
>(
|
|
186
|
+
(
|
|
187
|
+
{
|
|
188
|
+
value,
|
|
189
|
+
onChange,
|
|
190
|
+
onCreate,
|
|
191
|
+
placeholder,
|
|
192
|
+
defaultOptions: arrayDefaultOptions = [],
|
|
193
|
+
options: arrayOptions,
|
|
194
|
+
delay,
|
|
195
|
+
onSearch,
|
|
196
|
+
onSearchSync,
|
|
197
|
+
loadingIndicator,
|
|
198
|
+
emptyIndicator,
|
|
199
|
+
maxSelected = Number.MAX_SAFE_INTEGER,
|
|
200
|
+
onMaxSelected,
|
|
201
|
+
hidePlaceholderWhenSelected = true,
|
|
202
|
+
disabled,
|
|
203
|
+
groupBy,
|
|
204
|
+
selectFirstItem = true,
|
|
205
|
+
creatable = false,
|
|
206
|
+
triggerSearchOnFocus = false,
|
|
207
|
+
commandProps,
|
|
208
|
+
inputProps,
|
|
209
|
+
renderOption,
|
|
210
|
+
}: MultipleSelectorProps,
|
|
211
|
+
ref: React.Ref<MultipleSelectorRef>,
|
|
212
|
+
) => {
|
|
213
|
+
const inputRef = React.useRef<HTMLInputElement>(null);
|
|
214
|
+
const [open, setOpen] = React.useState(false);
|
|
215
|
+
const [onScrollbar, setOnScrollbar] = React.useState(false);
|
|
216
|
+
const [isLoading, setIsLoading] = React.useState(false);
|
|
217
|
+
const dropdownRef = React.useRef<HTMLDivElement>(null); // Added this
|
|
218
|
+
|
|
219
|
+
const [selected, setSelected] = React.useState<Option[]>(value || []);
|
|
220
|
+
const [options, setOptions] = React.useState<GroupOption>(
|
|
221
|
+
transToGroupOption(arrayDefaultOptions, groupBy),
|
|
222
|
+
);
|
|
223
|
+
const [inputValue, setInputValue] = React.useState("");
|
|
224
|
+
const debouncedSearchTerm = useDebounce(inputValue, delay || 500);
|
|
225
|
+
|
|
226
|
+
React.useImperativeHandle(
|
|
227
|
+
ref,
|
|
228
|
+
() => ({
|
|
229
|
+
selectedValue: [...selected],
|
|
230
|
+
input: inputRef.current as HTMLInputElement,
|
|
231
|
+
focus: () => inputRef?.current?.focus(),
|
|
232
|
+
reset: () => setSelected([]),
|
|
233
|
+
}),
|
|
234
|
+
[selected],
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
const handleClickOutside = (event: MouseEvent | TouchEvent) => {
|
|
238
|
+
if (
|
|
239
|
+
dropdownRef.current &&
|
|
240
|
+
!dropdownRef.current.contains(event.target as Node) &&
|
|
241
|
+
inputRef.current &&
|
|
242
|
+
!inputRef.current.contains(event.target as Node)
|
|
243
|
+
) {
|
|
244
|
+
setOpen(false);
|
|
245
|
+
inputRef.current.blur();
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const handleUnselect = React.useCallback(
|
|
250
|
+
(option: Option) => {
|
|
251
|
+
const newOptions = selected.filter((s) => s.value !== option.value);
|
|
252
|
+
setSelected(newOptions);
|
|
253
|
+
onChange?.(newOptions);
|
|
254
|
+
},
|
|
255
|
+
[onChange, selected],
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const handleKeyDown = React.useCallback(
|
|
259
|
+
(e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
260
|
+
const input = inputRef.current;
|
|
261
|
+
if (input) {
|
|
262
|
+
if (e.key === "Delete" || e.key === "Backspace") {
|
|
263
|
+
if (input.value === "" && selected.length > 0) {
|
|
264
|
+
const lastSelectOption = selected[selected.length - 1];
|
|
265
|
+
// If last item is fixed, we should not remove it.
|
|
266
|
+
if (lastSelectOption && !lastSelectOption.fixed) {
|
|
267
|
+
handleUnselect(lastSelectOption);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
// This is not a default behavior of the <input /> field
|
|
272
|
+
if (e.key === "Escape") {
|
|
273
|
+
input.blur();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
[handleUnselect, selected],
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
useEffect(() => {
|
|
281
|
+
if (open) {
|
|
282
|
+
document.addEventListener("mousedown", handleClickOutside);
|
|
283
|
+
document.addEventListener("touchend", handleClickOutside);
|
|
284
|
+
} else {
|
|
285
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
286
|
+
document.removeEventListener("touchend", handleClickOutside);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return () => {
|
|
290
|
+
document.removeEventListener("mousedown", handleClickOutside);
|
|
291
|
+
document.removeEventListener("touchend", handleClickOutside);
|
|
292
|
+
};
|
|
293
|
+
}, [open]);
|
|
294
|
+
|
|
295
|
+
useEffect(() => {
|
|
296
|
+
if (value) {
|
|
297
|
+
setSelected(value);
|
|
298
|
+
}
|
|
299
|
+
}, [value]);
|
|
300
|
+
|
|
301
|
+
useEffect(() => {
|
|
302
|
+
/** If `onSearch` is provided, do not trigger options updated. */
|
|
303
|
+
if (!arrayOptions || onSearch) {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const newOption = transToGroupOption(arrayOptions || [], groupBy);
|
|
307
|
+
if (JSON.stringify(newOption) !== JSON.stringify(options)) {
|
|
308
|
+
setOptions(newOption);
|
|
309
|
+
}
|
|
310
|
+
}, [arrayDefaultOptions, arrayOptions, groupBy, onSearch, options]);
|
|
311
|
+
|
|
312
|
+
useEffect(() => {
|
|
313
|
+
/** sync search */
|
|
314
|
+
|
|
315
|
+
const doSearchSync = () => {
|
|
316
|
+
const res = onSearchSync?.(debouncedSearchTerm);
|
|
317
|
+
setOptions(transToGroupOption(res || [], groupBy));
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const exec = async () => {
|
|
321
|
+
if (!onSearchSync || !open) return;
|
|
322
|
+
|
|
323
|
+
if (triggerSearchOnFocus) {
|
|
324
|
+
doSearchSync();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (debouncedSearchTerm) {
|
|
328
|
+
doSearchSync();
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
void exec();
|
|
333
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
334
|
+
}, [debouncedSearchTerm, groupBy, open, triggerSearchOnFocus]);
|
|
335
|
+
|
|
336
|
+
useEffect(() => {
|
|
337
|
+
/** async search */
|
|
338
|
+
|
|
339
|
+
const doSearch = async () => {
|
|
340
|
+
setIsLoading(true);
|
|
341
|
+
const res = await onSearch?.(debouncedSearchTerm);
|
|
342
|
+
setOptions(transToGroupOption(res || [], groupBy));
|
|
343
|
+
setIsLoading(false);
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const exec = async () => {
|
|
347
|
+
if (!onSearch || !open) return;
|
|
348
|
+
|
|
349
|
+
if (triggerSearchOnFocus) {
|
|
350
|
+
await doSearch();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (debouncedSearchTerm) {
|
|
354
|
+
await doSearch();
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
void exec();
|
|
359
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
360
|
+
}, [debouncedSearchTerm, groupBy, open, triggerSearchOnFocus]);
|
|
361
|
+
|
|
362
|
+
const CreatableItem = () => {
|
|
363
|
+
if (!creatable) return undefined;
|
|
364
|
+
if (
|
|
365
|
+
isOptionsExist(options, [{ value: inputValue, label: inputValue }]) ||
|
|
366
|
+
selected.find((s) => s.value === inputValue)
|
|
367
|
+
) {
|
|
368
|
+
return undefined;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const Item = (
|
|
372
|
+
<CommandPrimitive.Item
|
|
373
|
+
value={inputValue}
|
|
374
|
+
className={cn(commandItemClassName, "cursor-pointer")}
|
|
375
|
+
onMouseDown={(e) => {
|
|
376
|
+
e.preventDefault();
|
|
377
|
+
e.stopPropagation();
|
|
378
|
+
}}
|
|
379
|
+
onSelect={(value: string) => {
|
|
380
|
+
if (selected.length >= maxSelected) {
|
|
381
|
+
onMaxSelected?.(selected.length);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
setInputValue("");
|
|
385
|
+
const newOption = { value: inputValue, label: inputValue };
|
|
386
|
+
const newOptions = [...selected, newOption];
|
|
387
|
+
|
|
388
|
+
setSelected(newOptions);
|
|
389
|
+
onChange?.(newOptions);
|
|
390
|
+
onCreate?.(newOption);
|
|
391
|
+
}}
|
|
392
|
+
>
|
|
393
|
+
{`Create "${inputValue}"`}
|
|
394
|
+
</CommandPrimitive.Item>
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
// For normal creatable
|
|
398
|
+
if (!onSearch && inputValue.length > 0) {
|
|
399
|
+
return Item;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// For async search creatable. avoid showing creatable item before loading at first.
|
|
403
|
+
if (onSearch && debouncedSearchTerm.length > 0 && !isLoading) {
|
|
404
|
+
return Item;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return undefined;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const EmptyItem = React.useCallback(() => {
|
|
411
|
+
if (!emptyIndicator) return undefined;
|
|
412
|
+
|
|
413
|
+
// For async search that showing emptyIndicator
|
|
414
|
+
if (onSearch && !creatable && Object.keys(options).length === 0) {
|
|
415
|
+
return (
|
|
416
|
+
<CommandPrimitive.Item
|
|
417
|
+
value="-"
|
|
418
|
+
disabled
|
|
419
|
+
className={commandItemClassName}
|
|
420
|
+
>
|
|
421
|
+
{emptyIndicator}
|
|
422
|
+
</CommandPrimitive.Item>
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return <CommandEmpty>{emptyIndicator}</CommandEmpty>;
|
|
427
|
+
}, [creatable, emptyIndicator, onSearch, options]);
|
|
428
|
+
|
|
429
|
+
const selectables = React.useMemo<GroupOption>(
|
|
430
|
+
() => removePickedOption(options, selected),
|
|
431
|
+
[options, selected],
|
|
432
|
+
);
|
|
433
|
+
|
|
434
|
+
/** Avoid Creatable Selector freezing or lagging when paste a long string. */
|
|
435
|
+
const commandFilter = React.useCallback(() => {
|
|
436
|
+
if (commandProps?.filter) {
|
|
437
|
+
return commandProps.filter;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (creatable) {
|
|
441
|
+
return (value: string, search: string) => {
|
|
442
|
+
return value.toLowerCase().includes(search.toLowerCase()) ? 1 : -1;
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
// Using default filter in `cmdk`. We don't have to provide it.
|
|
446
|
+
return undefined;
|
|
447
|
+
}, [creatable, commandProps?.filter]);
|
|
448
|
+
|
|
449
|
+
return (
|
|
450
|
+
<CommandPrimitive
|
|
451
|
+
ref={dropdownRef}
|
|
452
|
+
{...commandProps}
|
|
453
|
+
onKeyDown={(e) => {
|
|
454
|
+
handleKeyDown(e);
|
|
455
|
+
commandProps?.onKeyDown?.(e);
|
|
456
|
+
}}
|
|
457
|
+
className="h-auto overflow-visible bg-transparent"
|
|
458
|
+
shouldFilter={
|
|
459
|
+
commandProps?.shouldFilter !== undefined
|
|
460
|
+
? commandProps.shouldFilter
|
|
461
|
+
: !onSearch
|
|
462
|
+
} // When onSearch is provided, we don't want to filter the options. You can still override it.
|
|
463
|
+
filter={commandFilter()}
|
|
464
|
+
>
|
|
465
|
+
<div
|
|
466
|
+
className={cn(
|
|
467
|
+
"min-h-10 border-b border-border text-sm",
|
|
468
|
+
{
|
|
469
|
+
"py-1": selected.length !== 0,
|
|
470
|
+
"cursor-text": !disabled && selected.length !== 0,
|
|
471
|
+
},
|
|
472
|
+
)}
|
|
473
|
+
onClick={() => {
|
|
474
|
+
if (disabled) return;
|
|
475
|
+
inputRef?.current?.focus();
|
|
476
|
+
}}
|
|
477
|
+
>
|
|
478
|
+
<div className="relative flex flex-nowrap gap-1 overflow-x-auto scrollbar-hide">
|
|
479
|
+
{selected.map((option) => {
|
|
480
|
+
return (
|
|
481
|
+
<span
|
|
482
|
+
key={option.value}
|
|
483
|
+
className={cn(
|
|
484
|
+
badgeVariants({ variant: "secondary" }),
|
|
485
|
+
"flex-shrink-0 rounded-full text-xs",
|
|
486
|
+
"data-[disabled]:bg-muted-foreground data-[disabled]:text-muted data-[disabled]:hover:bg-muted-foreground",
|
|
487
|
+
"data-[fixed]:bg-muted-foreground data-[fixed]:text-muted data-[fixed]:hover:bg-muted-foreground",
|
|
488
|
+
)}
|
|
489
|
+
data-fixed={option.fixed}
|
|
490
|
+
data-disabled={disabled || undefined}
|
|
491
|
+
>
|
|
492
|
+
{option.label}
|
|
493
|
+
<button
|
|
494
|
+
type="button"
|
|
495
|
+
className={cn(
|
|
496
|
+
"ml-1 rounded-full outline-none",
|
|
497
|
+
(disabled || option.fixed) && "hidden",
|
|
498
|
+
)}
|
|
499
|
+
onKeyDown={(e) => {
|
|
500
|
+
if (e.key === "Enter") {
|
|
501
|
+
handleUnselect(option);
|
|
502
|
+
}
|
|
503
|
+
}}
|
|
504
|
+
onMouseDown={(e) => {
|
|
505
|
+
e.preventDefault();
|
|
506
|
+
e.stopPropagation();
|
|
507
|
+
}}
|
|
508
|
+
onClick={() => handleUnselect(option)}
|
|
509
|
+
>
|
|
510
|
+
<X className="size-3 text-muted-foreground hover:text-foreground" />
|
|
511
|
+
</button>
|
|
512
|
+
</span>
|
|
513
|
+
);
|
|
514
|
+
})}
|
|
515
|
+
{/* Avoid having the "Search" Icon */}
|
|
516
|
+
<CommandPrimitive.Input
|
|
517
|
+
{...inputProps}
|
|
518
|
+
ref={inputRef}
|
|
519
|
+
value={inputValue}
|
|
520
|
+
disabled={disabled}
|
|
521
|
+
onValueChange={(value) => {
|
|
522
|
+
setInputValue(value);
|
|
523
|
+
inputProps?.onValueChange?.(value);
|
|
524
|
+
}}
|
|
525
|
+
onBlur={(event) => {
|
|
526
|
+
if (!onScrollbar) {
|
|
527
|
+
setOpen(false);
|
|
528
|
+
}
|
|
529
|
+
inputProps?.onBlur?.(event);
|
|
530
|
+
}}
|
|
531
|
+
onFocus={(event) => {
|
|
532
|
+
setOpen(true);
|
|
533
|
+
triggerSearchOnFocus && onSearch?.(debouncedSearchTerm);
|
|
534
|
+
inputProps?.onFocus?.(event);
|
|
535
|
+
}}
|
|
536
|
+
placeholder={
|
|
537
|
+
hidePlaceholderWhenSelected && selected.length !== 0
|
|
538
|
+
? ""
|
|
539
|
+
: placeholder
|
|
540
|
+
}
|
|
541
|
+
className={cn(
|
|
542
|
+
"flex-1 bg-transparent outline-none placeholder:text-muted-foreground",
|
|
543
|
+
{
|
|
544
|
+
"w-full": hidePlaceholderWhenSelected,
|
|
545
|
+
"py-1": selected.length === 0,
|
|
546
|
+
"ml-1": selected.length !== 0,
|
|
547
|
+
},
|
|
548
|
+
)}
|
|
549
|
+
/>
|
|
550
|
+
</div>
|
|
551
|
+
</div>
|
|
552
|
+
<div className="relative">
|
|
553
|
+
{open && (
|
|
554
|
+
<CommandPrimitive.List
|
|
555
|
+
className={cn(
|
|
556
|
+
commandListClassName,
|
|
557
|
+
"absolute top-1 z-10 w-full bg-popover text-popover-foreground shadow-md border border-border outline-none animate-in max-h-[200px]",
|
|
558
|
+
)}
|
|
559
|
+
onMouseLeave={() => {
|
|
560
|
+
setOnScrollbar(false);
|
|
561
|
+
}}
|
|
562
|
+
onMouseEnter={() => {
|
|
563
|
+
setOnScrollbar(true);
|
|
564
|
+
}}
|
|
565
|
+
onMouseUp={() => {
|
|
566
|
+
inputRef?.current?.focus();
|
|
567
|
+
}}
|
|
568
|
+
>
|
|
569
|
+
{isLoading ? (
|
|
570
|
+
<>{loadingIndicator}</>
|
|
571
|
+
) : (
|
|
572
|
+
<>
|
|
573
|
+
{EmptyItem()}
|
|
574
|
+
{CreatableItem()}
|
|
575
|
+
{!selectFirstItem && (
|
|
576
|
+
<CommandPrimitive.Item
|
|
577
|
+
value="-"
|
|
578
|
+
className={cn(commandItemClassName, "hidden")}
|
|
579
|
+
/>
|
|
580
|
+
)}
|
|
581
|
+
{Object.entries(selectables).map(([key, dropdowns]) => (
|
|
582
|
+
<CommandPrimitive.Group
|
|
583
|
+
key={key}
|
|
584
|
+
heading={key}
|
|
585
|
+
className={cn(commandGroupClassName, "h-full")}
|
|
586
|
+
>
|
|
587
|
+
{dropdowns.map((option) => {
|
|
588
|
+
return (
|
|
589
|
+
<CommandPrimitive.Item
|
|
590
|
+
key={option.value}
|
|
591
|
+
value={option.value}
|
|
592
|
+
disabled={option.disable}
|
|
593
|
+
onMouseDown={(e) => {
|
|
594
|
+
e.preventDefault();
|
|
595
|
+
e.stopPropagation();
|
|
596
|
+
}}
|
|
597
|
+
onSelect={() => {
|
|
598
|
+
if (selected.length >= maxSelected) {
|
|
599
|
+
onMaxSelected?.(selected.length);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
setInputValue("");
|
|
603
|
+
const newOptions = [...selected, option];
|
|
604
|
+
setSelected(newOptions);
|
|
605
|
+
onChange?.(newOptions);
|
|
606
|
+
}}
|
|
607
|
+
className={cn(
|
|
608
|
+
commandItemClassName,
|
|
609
|
+
"cursor-pointer w-full",
|
|
610
|
+
option.disable &&
|
|
611
|
+
"cursor-default text-muted-foreground",
|
|
612
|
+
)}
|
|
613
|
+
>
|
|
614
|
+
{renderOption ? renderOption(option) : option.label}
|
|
615
|
+
</CommandPrimitive.Item>
|
|
616
|
+
);
|
|
617
|
+
})}
|
|
618
|
+
</CommandPrimitive.Group>
|
|
619
|
+
))}
|
|
620
|
+
</>
|
|
621
|
+
)}
|
|
622
|
+
</CommandPrimitive.List>
|
|
623
|
+
)}
|
|
624
|
+
</div>
|
|
625
|
+
</CommandPrimitive>
|
|
626
|
+
);
|
|
627
|
+
},
|
|
628
|
+
);
|
|
629
|
+
|
|
630
|
+
MultipleSelector.displayName = "MultipleSelector";
|
|
631
|
+
export { MultipleSelector };
|
|
632
|
+
export default MultipleSelector;
|