rockin 0.0.34 → 0.0.35

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.
Files changed (90) hide show
  1. package/dist/chunk-3CZ2LLUN.js +198 -0
  2. package/dist/{chunk-7ZCRGTLA.js → chunk-54ZJAEO5.js} +5 -5
  3. package/dist/chunk-5GM3YR42.js +228 -0
  4. package/dist/chunk-6XUTSMTJ.js +183 -0
  5. package/dist/chunk-7JJBATB2.js +35 -0
  6. package/dist/chunk-BITCJZQZ.js +31 -0
  7. package/dist/chunk-COPQJFDI.js +158 -0
  8. package/dist/chunk-D6TULSM6.js +785 -0
  9. package/dist/chunk-DVNTJNDP.js +55 -0
  10. package/dist/chunk-EKDBYSMI.js +87 -0
  11. package/dist/chunk-FGWWRPMR.js +71 -0
  12. package/dist/chunk-G233W3IR.js +17 -0
  13. package/dist/chunk-G4NDAZTB.js +13 -0
  14. package/dist/chunk-GY2BX5SK.js +26 -0
  15. package/dist/chunk-MZBLIA6C.js +20 -0
  16. package/dist/chunk-OSVLAX37.js +101 -0
  17. package/dist/chunk-PRGKTXJU.js +20 -0
  18. package/dist/chunk-RUKQ532H.js +253 -0
  19. package/dist/chunk-S4H2HENE.js +113 -0
  20. package/dist/chunk-UHZTEDHK.js +61 -0
  21. package/dist/chunk-VFDNWPOI.js +293 -0
  22. package/dist/chunk-W4EIJWHF.js +150 -0
  23. package/dist/hooks/index.js +18 -9
  24. package/dist/index.js +4 -4
  25. package/dist/provider.js +3 -3
  26. package/dist/ui/accordion.js +95 -0
  27. package/dist/ui/alert-dialog.js +178 -0
  28. package/dist/ui/apple/english.js +7 -0
  29. package/dist/ui/apple/hindi.js +7 -0
  30. package/dist/ui/apple/index.js +52 -0
  31. package/dist/ui/apple/spanish.js +7 -0
  32. package/dist/ui/ariakit.js +7 -0
  33. package/dist/ui/autocomplete.js +296 -0
  34. package/dist/ui/avatar.js +106 -0
  35. package/dist/ui/badge.js +50 -0
  36. package/dist/ui/bridge.js +109 -0
  37. package/dist/ui/bubble.js +118 -0
  38. package/dist/ui/button-group.js +14 -0
  39. package/dist/ui/button.js +9 -0
  40. package/dist/ui/card.js +102 -0
  41. package/dist/ui/carousel.js +205 -0
  42. package/dist/ui/chart.js +253 -0
  43. package/dist/ui/checkbox.js +34 -0
  44. package/dist/ui/code-block.js +128 -0
  45. package/dist/ui/combobox.js +41 -0
  46. package/dist/ui/command.js +199 -0
  47. package/dist/ui/dashed.js +57 -0
  48. package/dist/ui/data-table/index.js +233 -0
  49. package/dist/ui/dialog.js +26 -0
  50. package/dist/ui/dock.js +86 -0
  51. package/dist/ui/drawer.js +216 -0
  52. package/dist/ui/dropdown-menu.js +35 -0
  53. package/dist/ui/empty.js +17 -0
  54. package/dist/ui/farmer/div.js +38 -0
  55. package/dist/ui/farmer/variants.js +314 -0
  56. package/dist/ui/field.js +224 -0
  57. package/dist/ui/filters.js +1524 -0
  58. package/dist/ui/image.js +88 -0
  59. package/dist/ui/index.d.ts +1 -1
  60. package/dist/ui/index.js +57 -8502
  61. package/dist/ui/input-group.js +20 -0
  62. package/dist/ui/input-otp.js +83 -0
  63. package/dist/ui/input.js +7 -0
  64. package/dist/ui/item.js +200 -0
  65. package/dist/ui/jelly.js +30 -0
  66. package/dist/ui/kbd.js +9 -0
  67. package/dist/ui/label.js +7 -0
  68. package/dist/ui/message-scroller.js +122 -0
  69. package/dist/ui/message.js +93 -0
  70. package/dist/ui/numberstepper.js +416 -0
  71. package/dist/ui/otp.js +403 -0
  72. package/dist/ui/popover.js +83 -0
  73. package/dist/ui/qr-code.js +124 -0
  74. package/dist/ui/quantity.js +48 -0
  75. package/dist/ui/radio-group.js +42 -0
  76. package/dist/ui/resizable.js +52 -0
  77. package/dist/ui/scroll-area.js +9 -0
  78. package/dist/ui/select.js +194 -0
  79. package/dist/ui/separator.js +7 -0
  80. package/dist/ui/skeleton.js +25 -0
  81. package/dist/ui/sonner.js +22 -0
  82. package/dist/ui/sortable.js +503 -0
  83. package/dist/ui/swipe-actions.js +45 -0
  84. package/dist/ui/switch.js +34 -0
  85. package/dist/ui/textarea.js +7 -0
  86. package/dist/ui/theme-toggle.js +141 -0
  87. package/dist/ui/tooltip.js +13 -0
  88. package/package.json +1 -1
  89. package/dist/chunk-4KFB6AJH.js +0 -1330
  90. /package/dist/{chunk-FYFBWCMJ.js → chunk-GBVAFFZR.js} +0 -0
@@ -0,0 +1,1524 @@
1
+ "use client";
2
+ "use no memo";
3
+ import {
4
+ Kbd
5
+ } from "../chunk-BITCJZQZ.js";
6
+ import {
7
+ DropdownMenu,
8
+ DropdownMenuCheckboxItem,
9
+ DropdownMenuContent,
10
+ DropdownMenuGroup,
11
+ DropdownMenuItem,
12
+ DropdownMenuSeparator,
13
+ DropdownMenuSub,
14
+ DropdownMenuSubContent,
15
+ DropdownMenuSubTrigger,
16
+ DropdownMenuTrigger
17
+ } from "../chunk-RUKQ532H.js";
18
+ import {
19
+ ScrollArea
20
+ } from "../chunk-UHZTEDHK.js";
21
+ import {
22
+ ButtonGroup,
23
+ ButtonGroupText
24
+ } from "../chunk-EKDBYSMI.js";
25
+ import "../chunk-GY2BX5SK.js";
26
+ import {
27
+ Tooltip,
28
+ TooltipContent,
29
+ TooltipTrigger
30
+ } from "../chunk-GBVAFFZR.js";
31
+ import {
32
+ InputGroup,
33
+ InputGroupAddon,
34
+ InputGroupButton,
35
+ InputGroupInput,
36
+ InputGroupText
37
+ } from "../chunk-COPQJFDI.js";
38
+ import "../chunk-MZBLIA6C.js";
39
+ import {
40
+ Input
41
+ } from "../chunk-7JJBATB2.js";
42
+ import {
43
+ Button
44
+ } from "../chunk-DVNTJNDP.js";
45
+
46
+ // src/ui/filters.tsx
47
+ import { useRender } from "@base-ui/react/use-render";
48
+ import { cva } from "class-variance-authority";
49
+ import {
50
+ createContext,
51
+ useCallback,
52
+ useContext,
53
+ useEffect,
54
+ useId,
55
+ useMemo,
56
+ useRef,
57
+ useState
58
+ } from "react";
59
+ import { cn } from "cn";
60
+ import { AlertCircleIcon, CheckIcon, PlusIcon, XIcon } from "lucide-react";
61
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
62
+ var DEFAULT_I18N = {
63
+ // UI Labels
64
+ addFilter: "Filter",
65
+ searchFields: "Filter...",
66
+ noFieldsFound: "No filters found.",
67
+ noResultsFound: "No results found.",
68
+ select: "Select...",
69
+ true: "True",
70
+ false: "False",
71
+ min: "Min",
72
+ max: "Max",
73
+ to: "to",
74
+ typeAndPressEnter: "Type and press Enter to add tag",
75
+ selected: "selected",
76
+ selectedCount: "selected",
77
+ percent: "%",
78
+ defaultCurrency: "$",
79
+ defaultColor: "#000000",
80
+ addFilterTitle: "Add filter",
81
+ loadingOptions: "Loading...",
82
+ errorLoadingOptions: "Failed to load options.",
83
+ // Operators
84
+ operators: {
85
+ is: "is",
86
+ isNot: "is not",
87
+ isAnyOf: "is any of",
88
+ isNotAnyOf: "is not any of",
89
+ includesAll: "includes all",
90
+ excludesAll: "excludes all",
91
+ before: "before",
92
+ after: "after",
93
+ between: "between",
94
+ notBetween: "not between",
95
+ contains: "contains",
96
+ notContains: "does not contain",
97
+ startsWith: "starts with",
98
+ endsWith: "ends with",
99
+ isExactly: "is exactly",
100
+ equals: "equals",
101
+ notEquals: "not equals",
102
+ greaterThan: "greater than",
103
+ lessThan: "less than",
104
+ overlaps: "overlaps",
105
+ includes: "includes",
106
+ excludes: "excludes",
107
+ includesAllOf: "includes all of",
108
+ includesAnyOf: "includes any of",
109
+ empty: "is empty",
110
+ notEmpty: "is not empty"
111
+ },
112
+ // Placeholders
113
+ placeholders: {
114
+ enterField: (fieldType) => `Enter ${fieldType}...`,
115
+ selectField: "Select...",
116
+ searchField: (fieldName) => `Search ${fieldName.toLowerCase()}...`,
117
+ enterKey: "Enter key...",
118
+ enterValue: "Enter value..."
119
+ },
120
+ // Helper functions
121
+ helpers: {
122
+ formatOperator: (operator) => operator.replace(/_/g, " ")
123
+ },
124
+ // Validation
125
+ validation: {
126
+ invalidEmail: "Invalid email format",
127
+ invalidUrl: "Invalid URL format",
128
+ invalidTel: "Invalid phone format",
129
+ invalid: "Invalid input format"
130
+ }
131
+ };
132
+ var FilterContext = createContext({
133
+ variant: "default",
134
+ size: "default",
135
+ radius: "default",
136
+ i18n: DEFAULT_I18N,
137
+ className: void 0,
138
+ showSearchInput: true,
139
+ trigger: void 0,
140
+ allowMultiple: true
141
+ });
142
+ var useFilterContext = () => useContext(FilterContext);
143
+ var filtersContainerVariants = cva("flex flex-wrap items-center", {
144
+ variants: {
145
+ variant: {
146
+ solid: "gap-2",
147
+ default: ""
148
+ },
149
+ size: {
150
+ sm: "gap-1.5",
151
+ default: "gap-2.5",
152
+ lg: "gap-3.5"
153
+ }
154
+ },
155
+ defaultVariants: {
156
+ variant: "default",
157
+ size: "default"
158
+ }
159
+ });
160
+ function FilterInput({
161
+ field,
162
+ onBlur,
163
+ onKeyDown,
164
+ className,
165
+ ...props
166
+ }) {
167
+ const context = useFilterContext();
168
+ const [isValid, setIsValid] = useState(true);
169
+ const [validationMessage, setValidationMessage] = useState("");
170
+ const inputRef = useRef(null);
171
+ useEffect(() => {
172
+ if (props.autoFocus) {
173
+ const timer = setTimeout(() => {
174
+ inputRef.current?.focus();
175
+ }, 300);
176
+ return () => clearTimeout(timer);
177
+ }
178
+ }, [props.autoFocus]);
179
+ const validateInput = (value, pattern) => {
180
+ if (!pattern || !value) return true;
181
+ const regex = new RegExp(pattern);
182
+ return regex.test(value);
183
+ };
184
+ const getValidationMessage = () => {
185
+ return context.i18n.validation.invalid;
186
+ };
187
+ const handleBlur = (e) => {
188
+ const value = e.target.value;
189
+ const pattern = field?.pattern || props.pattern;
190
+ if (value && (pattern || field?.validation)) {
191
+ let valid = true;
192
+ let customMessage = "";
193
+ if (field?.validation) {
194
+ const result = field.validation(value);
195
+ if (typeof result === "boolean") {
196
+ valid = result;
197
+ } else {
198
+ valid = result.valid;
199
+ customMessage = result.message || "";
200
+ }
201
+ } else if (pattern) {
202
+ valid = validateInput(value, pattern);
203
+ }
204
+ setIsValid(valid);
205
+ setValidationMessage(valid ? "" : customMessage || getValidationMessage());
206
+ } else {
207
+ setIsValid(true);
208
+ setValidationMessage("");
209
+ }
210
+ onBlur?.(e);
211
+ };
212
+ const handleKeyDown = (e) => {
213
+ if (!isValid && ![
214
+ "Tab",
215
+ "Escape",
216
+ "Enter",
217
+ "ArrowUp",
218
+ "ArrowDown",
219
+ "ArrowLeft",
220
+ "ArrowRight"
221
+ ].includes(e.key)) {
222
+ setIsValid(true);
223
+ setValidationMessage("");
224
+ }
225
+ onKeyDown?.(e);
226
+ };
227
+ return /* @__PURE__ */ jsxs(
228
+ InputGroup,
229
+ {
230
+ className: cn(
231
+ "w-36",
232
+ // Height follows each style's own control ladder. `default` sets no
233
+ // height on purpose so the style's `.cn-input-group` applies (h-8 nova,
234
+ // h-9 maia/luma, h-7 mira, h-10 sera); sm/lg step down/up from it.
235
+ // Base covers nova/lyra/rhea/vega; only deviating styles are listed.
236
+ context.size == "sm" && "h-7!",
237
+ context.size == "lg" && "h-9!",
238
+ // Sera's `.cn-input` is `px-0` (underline inputs sit flush); inside a
239
+ // segmented chip that collides with the neighbouring segment, so give
240
+ // the value input the same inline padding sera uses elsewhere.
241
+ "",
242
+ className
243
+ ),
244
+ children: [
245
+ field?.prefix && /* @__PURE__ */ jsx(InputGroupAddon, { children: /* @__PURE__ */ jsx(InputGroupText, { children: field.prefix }) }),
246
+ /* @__PURE__ */ jsx(
247
+ InputGroupInput,
248
+ {
249
+ ref: inputRef,
250
+ "aria-invalid": !isValid,
251
+ "aria-describedby": !isValid && validationMessage ? `${field?.key || "input"}-error` : void 0,
252
+ onBlur: handleBlur,
253
+ onKeyDown: handleKeyDown,
254
+ className: cn(
255
+ context.size == "sm" && "h-7! text-xs",
256
+ context.size == "lg" && "h-9!"
257
+ ),
258
+ ...props
259
+ }
260
+ ),
261
+ !isValid && validationMessage && /* @__PURE__ */ jsx(InputGroupAddon, { align: "inline-end", children: /* @__PURE__ */ jsxs(Tooltip, { children: [
262
+ /* @__PURE__ */ jsx(TooltipTrigger, { render: /* @__PURE__ */ jsx(InputGroupButton, { size: "icon-xs" }), children: /* @__PURE__ */ jsx(AlertCircleIcon, { className: "size-3.5 text-destructive" }) }),
263
+ /* @__PURE__ */ jsx(TooltipContent, { children: /* @__PURE__ */ jsx("p", { className: "text-sm", children: validationMessage }) })
264
+ ] }) }),
265
+ field?.suffix && /* @__PURE__ */ jsx(InputGroupAddon, { align: "inline-end", children: /* @__PURE__ */ jsx(InputGroupText, { children: field.suffix }) })
266
+ ]
267
+ }
268
+ );
269
+ }
270
+ function FilterRemoveButton({
271
+ className,
272
+ icon = /* @__PURE__ */ jsx(XIcon, {}),
273
+ ...props
274
+ }) {
275
+ const context = useFilterContext();
276
+ return /* @__PURE__ */ jsx(
277
+ Button,
278
+ {
279
+ variant: "outline",
280
+ size: context.size === "sm" ? "icon-sm" : context.size === "lg" ? "icon-lg" : "icon",
281
+ className,
282
+ ...props,
283
+ children: icon
284
+ }
285
+ );
286
+ }
287
+ var isFieldGroup = (item) => {
288
+ return "fields" in item && Array.isArray(item.fields);
289
+ };
290
+ var isGroupLevelField = (field) => {
291
+ return Boolean(field.group && field.fields);
292
+ };
293
+ var flattenFields = (fields) => {
294
+ return fields.reduce((acc, item) => {
295
+ if (isFieldGroup(item)) {
296
+ return [...acc, ...item.fields];
297
+ }
298
+ if (isGroupLevelField(item)) {
299
+ return [...acc, ...item.fields];
300
+ }
301
+ return [...acc, item];
302
+ }, []);
303
+ };
304
+ var getFieldsMap = (fields) => {
305
+ const flatFields = flattenFields(fields);
306
+ return flatFields.reduce(
307
+ (acc, field) => {
308
+ if (field.key) {
309
+ acc[field.key] = field;
310
+ }
311
+ return acc;
312
+ },
313
+ {}
314
+ );
315
+ };
316
+ var fieldHasOptions = (field) => (field.options?.length ?? 0) > 0 || typeof field.loadOptions === "function";
317
+ var fieldOptionCaches = /* @__PURE__ */ new WeakMap();
318
+ var getFieldOptionCache = (field) => {
319
+ let cache = fieldOptionCaches.get(field);
320
+ if (!cache) {
321
+ cache = /* @__PURE__ */ new Map();
322
+ fieldOptionCaches.set(field, cache);
323
+ }
324
+ return cache;
325
+ };
326
+ function useFieldOptions(field, searchInput, enabled) {
327
+ const isAsync = typeof field.loadOptions === "function";
328
+ if (isAsync && field.options) {
329
+ const cache = getFieldOptionCache(field);
330
+ for (const opt of field.options) {
331
+ cache.set(opt.value, opt);
332
+ }
333
+ }
334
+ const [state, setState] = useState(() => ({ options: field.options ?? [], loading: false, error: false }));
335
+ const [debouncedQuery, setDebouncedQuery] = useState(searchInput);
336
+ useEffect(() => {
337
+ if (!isAsync) return;
338
+ const timer = setTimeout(() => setDebouncedQuery(searchInput), 250);
339
+ return () => clearTimeout(timer);
340
+ }, [searchInput, isAsync]);
341
+ const requestIdRef = useRef(0);
342
+ const loaderRef = useRef(field.loadOptions);
343
+ loaderRef.current = field.loadOptions;
344
+ useEffect(() => {
345
+ if (!isAsync || !enabled) return;
346
+ const loader = loaderRef.current;
347
+ if (!loader) return;
348
+ const requestId = ++requestIdRef.current;
349
+ let cancelled = false;
350
+ setState((prev) => ({ ...prev, loading: true, error: false }));
351
+ Promise.resolve().then(() => loader(debouncedQuery)).then((result) => {
352
+ if (cancelled || requestId !== requestIdRef.current) return;
353
+ const cache = getFieldOptionCache(field);
354
+ for (const opt of result) cache.set(opt.value, opt);
355
+ setState({ options: result, loading: false, error: false });
356
+ }).catch(() => {
357
+ if (cancelled || requestId !== requestIdRef.current) return;
358
+ setState((prev) => ({ ...prev, loading: false, error: true }));
359
+ });
360
+ return () => {
361
+ cancelled = true;
362
+ };
363
+ }, [isAsync, enabled, debouncedQuery]);
364
+ const resolveSelected = useCallback(
365
+ (values) => {
366
+ const cache = getFieldOptionCache(field);
367
+ return values.map(
368
+ (value) => cache.get(value) ?? { value, label: String(value) }
369
+ );
370
+ },
371
+ [field]
372
+ );
373
+ if (!isAsync) {
374
+ return {
375
+ isAsync: false,
376
+ options: field.options ?? [],
377
+ loading: false,
378
+ error: false,
379
+ resolveSelected
380
+ };
381
+ }
382
+ return {
383
+ isAsync: true,
384
+ options: state.options,
385
+ loading: state.loading,
386
+ error: state.error,
387
+ resolveSelected
388
+ };
389
+ }
390
+ var createOperatorsFromI18n = (i18n) => ({
391
+ select: [
392
+ { value: "is", label: i18n.operators.is },
393
+ { value: "is_not", label: i18n.operators.isNot },
394
+ { value: "empty", label: i18n.operators.empty },
395
+ { value: "not_empty", label: i18n.operators.notEmpty }
396
+ ],
397
+ multiselect: [
398
+ { value: "is_any_of", label: i18n.operators.isAnyOf },
399
+ { value: "is_not_any_of", label: i18n.operators.isNotAnyOf },
400
+ { value: "includes_all", label: i18n.operators.includesAll },
401
+ { value: "excludes_all", label: i18n.operators.excludesAll },
402
+ { value: "empty", label: i18n.operators.empty },
403
+ { value: "not_empty", label: i18n.operators.notEmpty }
404
+ ],
405
+ text: [
406
+ { value: "contains", label: i18n.operators.contains },
407
+ { value: "not_contains", label: i18n.operators.notContains },
408
+ { value: "starts_with", label: i18n.operators.startsWith },
409
+ { value: "ends_with", label: i18n.operators.endsWith },
410
+ { value: "is", label: i18n.operators.isExactly },
411
+ { value: "empty", label: i18n.operators.empty },
412
+ { value: "not_empty", label: i18n.operators.notEmpty }
413
+ ],
414
+ custom: [
415
+ { value: "is", label: i18n.operators.is },
416
+ { value: "after", label: i18n.operators.after },
417
+ { value: "between", label: i18n.operators.between },
418
+ { value: "empty", label: i18n.operators.empty },
419
+ { value: "not_empty", label: i18n.operators.notEmpty }
420
+ ]
421
+ });
422
+ var DEFAULT_OPERATORS = createOperatorsFromI18n(DEFAULT_I18N);
423
+ var getOperatorsForField = (field, values, i18n) => {
424
+ if (field.operators) return field.operators;
425
+ const operators = createOperatorsFromI18n(i18n);
426
+ let fieldType = field.type || "select";
427
+ if (fieldType === "select" && values.length > 1) {
428
+ fieldType = "multiselect";
429
+ }
430
+ if (fieldType === "multiselect" || field.type === "multiselect") {
431
+ return operators.multiselect;
432
+ }
433
+ return operators[fieldType] || operators.select;
434
+ };
435
+ function FilterOperatorDropdown({
436
+ field,
437
+ operator,
438
+ values,
439
+ onChange
440
+ }) {
441
+ const context = useFilterContext();
442
+ const operators = useMemo(
443
+ () => getOperatorsForField(field, values, context.i18n),
444
+ [field, values, context.i18n]
445
+ );
446
+ const operatorLabel = operators.find((op) => op.value === operator)?.label || context.i18n.helpers.formatOperator(operator);
447
+ return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
448
+ /* @__PURE__ */ jsx(
449
+ DropdownMenuTrigger,
450
+ {
451
+ render: /* @__PURE__ */ jsx(
452
+ Button,
453
+ {
454
+ variant: "outline",
455
+ size: context.size,
456
+ className: "text-muted-foreground hover:text-foreground",
457
+ children: operatorLabel
458
+ }
459
+ )
460
+ }
461
+ ),
462
+ /* @__PURE__ */ jsx(DropdownMenuContent, { align: "start", className: "w-fit min-w-fit", children: operators.map((op) => /* @__PURE__ */ jsxs(
463
+ DropdownMenuItem,
464
+ {
465
+ onClick: () => onChange(op.value),
466
+ className: cn(
467
+ "flex items-center justify-between data-highlighted:bg-accent data-highlighted:text-accent-foreground"
468
+ ),
469
+ children: [
470
+ /* @__PURE__ */ jsx("span", { children: op.label }),
471
+ /* @__PURE__ */ jsx(
472
+ CheckIcon,
473
+ {
474
+ className: cn(
475
+ "ms-auto text-primary",
476
+ op.value === operator ? "opacity-100" : "opacity-0"
477
+ )
478
+ }
479
+ )
480
+ ]
481
+ },
482
+ op.value
483
+ )) })
484
+ ] });
485
+ }
486
+ function SelectOptionsPopover({
487
+ field,
488
+ values,
489
+ onChange,
490
+ onClose,
491
+ inline = false
492
+ }) {
493
+ const [open, setOpen] = useState(false);
494
+ const [searchInput, setSearchInput] = useState("");
495
+ const [highlightedIndex, setHighlightedIndex] = useState(-1);
496
+ const inputRef = useRef(null);
497
+ const context = useFilterContext();
498
+ const baseId = useId();
499
+ useEffect(() => {
500
+ if (open) {
501
+ inputRef.current?.focus();
502
+ }
503
+ }, [open]);
504
+ useEffect(() => {
505
+ setHighlightedIndex(-1);
506
+ }, [searchInput, open]);
507
+ useEffect(() => {
508
+ if (highlightedIndex >= 0 && open) {
509
+ const element = document.getElementById(
510
+ `${baseId}-item-${highlightedIndex}`
511
+ );
512
+ element?.scrollIntoView({ block: "nearest" });
513
+ }
514
+ }, [highlightedIndex, open, baseId]);
515
+ const {
516
+ isAsync,
517
+ options: resolvedOptions,
518
+ loading,
519
+ error,
520
+ resolveSelected
521
+ } = useFieldOptions(field, searchInput, inline || open);
522
+ const isMultiSelect = field.type === "multiselect" || values.length > 1;
523
+ const effectiveValues = (field.value !== void 0 ? field.value : values) || [];
524
+ const selectedOptions = isAsync ? resolveSelected(effectiveValues) : field.options?.filter((opt) => effectiveValues.includes(opt.value)) || [];
525
+ const unselectedOptions = isAsync ? resolvedOptions.filter((opt) => !effectiveValues.includes(opt.value)) : field.options?.filter((opt) => !effectiveValues.includes(opt.value)) || [];
526
+ const filteredSelectedOptions = selectedOptions;
527
+ const filteredUnselectedOptions = isAsync ? unselectedOptions : unselectedOptions.filter(
528
+ (opt) => opt.label.toLowerCase().includes(searchInput.toLowerCase())
529
+ );
530
+ const allFilteredOptions = useMemo(
531
+ () => [...filteredSelectedOptions, ...filteredUnselectedOptions],
532
+ [filteredSelectedOptions, filteredUnselectedOptions]
533
+ );
534
+ const handleClose = () => {
535
+ setOpen(false);
536
+ onClose?.();
537
+ };
538
+ const toggleOption = (option) => {
539
+ const isSelected = effectiveValues.includes(option.value);
540
+ const next = isSelected ? effectiveValues.filter((v) => v !== option.value) : isMultiSelect ? [...effectiveValues, option.value] : [option.value];
541
+ if (!isSelected && isMultiSelect && field.maxSelections && next.length > field.maxSelections) {
542
+ return;
543
+ }
544
+ if (field.onValueChange) {
545
+ field.onValueChange(next);
546
+ } else {
547
+ onChange(next);
548
+ }
549
+ if (!isMultiSelect) handleClose();
550
+ };
551
+ const renderOptionItem = (option, overallIndex) => {
552
+ const isSelected = effectiveValues.includes(option.value);
553
+ const isHighlighted = highlightedIndex === overallIndex;
554
+ const itemId = `${baseId}-item-${overallIndex}`;
555
+ return /* @__PURE__ */ jsxs(
556
+ DropdownMenuCheckboxItem,
557
+ {
558
+ id: itemId,
559
+ role: "option",
560
+ "aria-selected": isHighlighted,
561
+ "data-highlighted": isHighlighted || void 0,
562
+ onMouseEnter: () => setHighlightedIndex(overallIndex),
563
+ checked: isSelected,
564
+ className: cn(
565
+ "data-highlighted:bg-accent data-highlighted:text-accent-foreground",
566
+ option.className
567
+ ),
568
+ onSelect: (e) => {
569
+ if (isMultiSelect) e.preventDefault();
570
+ },
571
+ onCheckedChange: () => toggleOption(option),
572
+ children: [
573
+ option.icon && option.icon,
574
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: option.label })
575
+ ]
576
+ },
577
+ String(option.value)
578
+ );
579
+ };
580
+ const renderMenuContent = () => /* @__PURE__ */ jsxs(Fragment, { children: [
581
+ field.searchable !== false && /* @__PURE__ */ jsxs(Fragment, { children: [
582
+ /* @__PURE__ */ jsx(
583
+ Input,
584
+ {
585
+ ref: inputRef,
586
+ role: "combobox",
587
+ "aria-autocomplete": "list",
588
+ "aria-expanded": true,
589
+ "aria-haspopup": "listbox",
590
+ "aria-controls": `${baseId}-listbox`,
591
+ "aria-activedescendant": highlightedIndex >= 0 ? `${baseId}-item-${highlightedIndex}` : void 0,
592
+ placeholder: context.i18n.placeholders.searchField(
593
+ field.label || ""
594
+ ),
595
+ className: cn(
596
+ "h-8 rounded-none border-0 border-input bg-transparent! px-2 text-sm shadow-none",
597
+ "focus-visible:border-border focus-visible:ring-0 focus-visible:ring-offset-0",
598
+ open && "placeholder:text-foreground"
599
+ ),
600
+ value: searchInput,
601
+ onChange: (e) => setSearchInput(e.target.value),
602
+ onBlur: () => open && inputRef.current?.focus(),
603
+ onClick: (e) => e.stopPropagation(),
604
+ onKeyDown: (e) => {
605
+ if (e.key === "ArrowDown") {
606
+ e.preventDefault();
607
+ if (allFilteredOptions.length > 0) {
608
+ setHighlightedIndex(
609
+ (prev) => prev < allFilteredOptions.length - 1 ? prev + 1 : 0
610
+ );
611
+ }
612
+ } else if (e.key === "ArrowUp") {
613
+ e.preventDefault();
614
+ if (allFilteredOptions.length > 0) {
615
+ setHighlightedIndex(
616
+ (prev) => prev > 0 ? prev - 1 : allFilteredOptions.length - 1
617
+ );
618
+ }
619
+ } else if (e.key === "ArrowLeft") {
620
+ e.preventDefault();
621
+ setOpen(false);
622
+ } else if (e.key === "Enter" && highlightedIndex >= 0) {
623
+ e.preventDefault();
624
+ const option = allFilteredOptions[highlightedIndex];
625
+ if (option) {
626
+ const isSelected = effectiveValues.includes(option.value);
627
+ const next = isSelected ? effectiveValues.filter((v) => v !== option.value) : isMultiSelect ? [...effectiveValues, option.value] : [option.value];
628
+ if (!isSelected && isMultiSelect && field.maxSelections && next.length > field.maxSelections) {
629
+ return;
630
+ }
631
+ if (field.onValueChange) {
632
+ field.onValueChange(next);
633
+ } else {
634
+ onChange(next);
635
+ }
636
+ if (!isMultiSelect) handleClose();
637
+ }
638
+ }
639
+ e.stopPropagation();
640
+ }
641
+ }
642
+ ),
643
+ /* @__PURE__ */ jsx(DropdownMenuSeparator, {})
644
+ ] }),
645
+ /* @__PURE__ */ jsx("div", { className: "relative flex max-h-full", children: /* @__PURE__ */ jsx(
646
+ "div",
647
+ {
648
+ className: "flex max-h-[min(var(--available-height),24rem)] w-full scroll-pt-2 scroll-pb-2 flex-col overscroll-contain",
649
+ role: "listbox",
650
+ id: `${baseId}-listbox`,
651
+ children: isAsync && loading && allFilteredOptions.length === 0 ? /* @__PURE__ */ jsx("div", { className: "py-2 text-center text-sm text-muted-foreground", children: context.i18n.loadingOptions ?? DEFAULT_I18N.loadingOptions }) : isAsync && error ? /* @__PURE__ */ jsx("div", { className: "py-2 text-center text-sm text-muted-foreground", children: context.i18n.errorLoadingOptions ?? DEFAULT_I18N.errorLoadingOptions }) : allFilteredOptions.length === 0 ? /* @__PURE__ */ jsx("div", { className: "py-2 text-center text-sm text-muted-foreground", children: context.i18n.noResultsFound }) : field.renderOptionList ? field.renderOptionList({
652
+ options: allFilteredOptions,
653
+ highlightedIndex,
654
+ renderOption: renderOptionItem
655
+ }) : /* @__PURE__ */ jsxs(ScrollArea, { className: "size-full min-h-0 **:data-[slot=scroll-area-scrollbar]:m-0 **:data-[slot=scroll-area-viewport]:h-full **:data-[slot=scroll-area-viewport]:overscroll-contain", children: [
656
+ filteredSelectedOptions.length > 0 && /* @__PURE__ */ jsx(DropdownMenuGroup, { className: "px-1", children: filteredSelectedOptions.map(
657
+ (option, index) => renderOptionItem(option, index)
658
+ ) }),
659
+ filteredSelectedOptions.length > 0 && filteredUnselectedOptions.length > 0 && /* @__PURE__ */ jsx(DropdownMenuSeparator, { className: "mx-0" }),
660
+ filteredUnselectedOptions.length > 0 && /* @__PURE__ */ jsx(DropdownMenuGroup, { className: "px-1", children: filteredUnselectedOptions.map(
661
+ (option, index) => renderOptionItem(
662
+ option,
663
+ index + filteredSelectedOptions.length
664
+ )
665
+ ) })
666
+ ] })
667
+ }
668
+ ) })
669
+ ] });
670
+ if (inline) {
671
+ return /* @__PURE__ */ jsx("div", { className: "w-full", children: renderMenuContent() });
672
+ }
673
+ return /* @__PURE__ */ jsxs(
674
+ DropdownMenu,
675
+ {
676
+ open,
677
+ onOpenChange: (open2) => {
678
+ setOpen(open2);
679
+ if (!open2) {
680
+ setTimeout(() => setSearchInput(""), 200);
681
+ }
682
+ },
683
+ children: [
684
+ /* @__PURE__ */ jsx(
685
+ DropdownMenuTrigger,
686
+ {
687
+ render: /* @__PURE__ */ jsx(Button, { variant: "outline", size: context.size, children: /* @__PURE__ */ jsx("div", { className: "flex items-center gap-1.5", children: field.customValueRenderer ? field.customValueRenderer(
688
+ values,
689
+ isAsync ? resolveSelected(values) : field.options || []
690
+ ) : /* @__PURE__ */ jsxs(Fragment, { children: [
691
+ selectedOptions.length > 0 && /* @__PURE__ */ jsx("div", { className: "flex items-center -space-x-1.5", children: selectedOptions.slice(0, 3).map((option) => /* @__PURE__ */ jsx("div", { children: option.icon }, String(option.value))) }),
692
+ selectedOptions.length === 1 ? selectedOptions[0].label : selectedOptions.length > 1 ? `${selectedOptions.length} ${context.i18n.selectedCount}` : context.i18n.select
693
+ ] }) }) })
694
+ }
695
+ ),
696
+ /* @__PURE__ */ jsx(
697
+ DropdownMenuContent,
698
+ {
699
+ align: "start",
700
+ className: cn("w-50 px-0", field.className),
701
+ children: renderMenuContent()
702
+ }
703
+ )
704
+ ]
705
+ }
706
+ );
707
+ }
708
+ function FilterValueSelector({
709
+ field,
710
+ values,
711
+ onChange,
712
+ operator,
713
+ autoFocus
714
+ }) {
715
+ if (operator === "empty" || operator === "not_empty") {
716
+ return null;
717
+ }
718
+ if (field.customRenderer) {
719
+ return /* @__PURE__ */ jsx(ButtonGroupText, { className: "bg-background text-start whitespace-nowrap outline-hidden hover:bg-accent aria-expanded:bg-accent dark:bg-input/30", children: field.customRenderer({ field, values, onChange, operator }) });
720
+ }
721
+ if (field.type === "text") {
722
+ return /* @__PURE__ */ jsx(
723
+ FilterInput,
724
+ {
725
+ type: "text",
726
+ value: values[0] || "",
727
+ onChange: (e) => onChange([e.target.value]),
728
+ placeholder: field.placeholder,
729
+ pattern: field.pattern,
730
+ field,
731
+ className: cn("w-36", field.className),
732
+ autoFocus
733
+ }
734
+ );
735
+ }
736
+ if (field.type === "select" || field.type === "multiselect") {
737
+ return /* @__PURE__ */ jsx(SelectOptionsPopover, { field, values, onChange });
738
+ }
739
+ return /* @__PURE__ */ jsx(SelectOptionsPopover, { field, values, onChange });
740
+ }
741
+ var FiltersContent = ({
742
+ filters,
743
+ fields,
744
+ onChange
745
+ }) => {
746
+ const context = useFilterContext();
747
+ const fieldsMap = useMemo(() => getFieldsMap(fields), [fields]);
748
+ const updateFilter = useCallback(
749
+ (filterId, updates) => {
750
+ onChange(
751
+ filters.map((filter) => {
752
+ if (filter.id === filterId) {
753
+ const updatedFilter = { ...filter, ...updates };
754
+ if (updates.operator === "empty" || updates.operator === "not_empty") {
755
+ updatedFilter.values = [];
756
+ }
757
+ return updatedFilter;
758
+ }
759
+ return filter;
760
+ })
761
+ );
762
+ },
763
+ [filters, onChange]
764
+ );
765
+ const removeFilter = useCallback(
766
+ (filterId) => {
767
+ onChange(filters.filter((filter) => filter.id !== filterId));
768
+ },
769
+ [filters, onChange]
770
+ );
771
+ return /* @__PURE__ */ jsx(
772
+ "div",
773
+ {
774
+ className: cn(
775
+ filtersContainerVariants({
776
+ variant: context.variant,
777
+ size: context.size
778
+ }),
779
+ context.className
780
+ ),
781
+ children: filters.map((filter) => {
782
+ const field = fieldsMap[filter.field];
783
+ if (!field) return null;
784
+ return /* @__PURE__ */ jsxs(
785
+ ButtonGroup,
786
+ {
787
+ className: "",
788
+ children: [
789
+ /* @__PURE__ */ jsxs(ButtonGroupText, { children: [
790
+ field.icon && field.icon,
791
+ field.label
792
+ ] }),
793
+ /* @__PURE__ */ jsx(
794
+ FilterOperatorDropdown,
795
+ {
796
+ field,
797
+ operator: filter.operator,
798
+ values: filter.values,
799
+ onChange: (operator) => updateFilter(filter.id, { operator })
800
+ }
801
+ ),
802
+ /* @__PURE__ */ jsx(
803
+ FilterValueSelector,
804
+ {
805
+ field,
806
+ values: filter.values,
807
+ onChange: (values) => updateFilter(filter.id, { values }),
808
+ operator: filter.operator,
809
+ autoFocus: false
810
+ }
811
+ ),
812
+ /* @__PURE__ */ jsx(FilterRemoveButton, { onClick: () => removeFilter(filter.id) })
813
+ ]
814
+ },
815
+ filter.id
816
+ );
817
+ })
818
+ }
819
+ );
820
+ };
821
+ function FilterSubmenuContent({
822
+ field,
823
+ currentValues,
824
+ isMultiSelect,
825
+ onToggle,
826
+ i18n,
827
+ isActive,
828
+ onActive,
829
+ onBack,
830
+ onClose
831
+ }) {
832
+ const [searchInput, setSearchInput] = useState("");
833
+ const [highlightedIndex, setHighlightedIndex] = useState(-1);
834
+ const inputRef = useRef(null);
835
+ const baseId = useId();
836
+ const {
837
+ isAsync,
838
+ options: resolvedOptions,
839
+ loading,
840
+ error,
841
+ resolveSelected
842
+ } = useFieldOptions(field, searchInput, true);
843
+ useEffect(() => {
844
+ if (isActive) {
845
+ if (field.searchable !== false) {
846
+ inputRef.current?.focus();
847
+ } else {
848
+ const listbox = document.getElementById(`${baseId}-listbox`);
849
+ listbox?.focus();
850
+ }
851
+ }
852
+ }, [isActive, field.searchable, baseId]);
853
+ useEffect(() => {
854
+ setHighlightedIndex(-1);
855
+ }, [searchInput]);
856
+ useEffect(() => {
857
+ if (highlightedIndex >= 0 && isActive) {
858
+ const element = document.getElementById(
859
+ `${baseId}-item-${highlightedIndex}`
860
+ );
861
+ element?.scrollIntoView({ block: "nearest" });
862
+ }
863
+ }, [highlightedIndex, isActive, baseId]);
864
+ const filteredOptions = useMemo(() => {
865
+ if (isAsync) {
866
+ const selectedSet = new Set(currentValues);
867
+ return [
868
+ ...resolveSelected(currentValues),
869
+ ...resolvedOptions.filter((option) => !selectedSet.has(option.value))
870
+ ];
871
+ }
872
+ return field.options?.filter((option) => {
873
+ const isSelected = currentValues.includes(option.value);
874
+ if (isSelected) return true;
875
+ if (!searchInput) return true;
876
+ return option.label.toLowerCase().includes(searchInput.toLowerCase());
877
+ }) || [];
878
+ }, [
879
+ isAsync,
880
+ resolvedOptions,
881
+ resolveSelected,
882
+ field.options,
883
+ searchInput,
884
+ currentValues
885
+ ]);
886
+ const renderOptionItem = (option, index) => {
887
+ const isSelected = currentValues.includes(option.value);
888
+ const isHighlighted = highlightedIndex === index;
889
+ const itemId = `${baseId}-item-${index}`;
890
+ return /* @__PURE__ */ jsxs(
891
+ DropdownMenuCheckboxItem,
892
+ {
893
+ id: itemId,
894
+ role: "option",
895
+ "aria-selected": isHighlighted,
896
+ "data-highlighted": isHighlighted || void 0,
897
+ onMouseEnter: () => setHighlightedIndex(index),
898
+ checked: isSelected,
899
+ className: cn(
900
+ "data-highlighted:bg-accent data-highlighted:text-accent-foreground",
901
+ option.className
902
+ ),
903
+ onSelect: (e) => {
904
+ if (isMultiSelect) e.preventDefault();
905
+ },
906
+ onCheckedChange: () => onToggle(option.value, isSelected),
907
+ children: [
908
+ option.icon && option.icon,
909
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: option.label })
910
+ ]
911
+ },
912
+ String(option.value)
913
+ );
914
+ };
915
+ useEffect(() => {
916
+ if (isActive && filteredOptions.length > 0) {
917
+ setHighlightedIndex(0);
918
+ }
919
+ }, [isActive, filteredOptions.length]);
920
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col", onMouseEnter: onActive, children: [
921
+ field.searchable !== false && /* @__PURE__ */ jsxs(Fragment, { children: [
922
+ /* @__PURE__ */ jsx(
923
+ Input,
924
+ {
925
+ ref: inputRef,
926
+ role: "combobox",
927
+ "aria-autocomplete": "list",
928
+ "aria-expanded": true,
929
+ "aria-haspopup": "listbox",
930
+ "aria-controls": `${baseId}-listbox`,
931
+ "aria-activedescendant": highlightedIndex >= 0 ? `${baseId}-item-${highlightedIndex}` : void 0,
932
+ placeholder: i18n.placeholders.searchField(field.label || ""),
933
+ className: cn(
934
+ "h-8 rounded-none border-0 bg-transparent! px-2 text-sm shadow-none",
935
+ "focus-visible:border-border focus-visible:ring-0 focus-visible:ring-offset-0",
936
+ isActive && "placeholder:text-foreground"
937
+ ),
938
+ value: searchInput,
939
+ onBlur: () => isActive && inputRef.current?.focus(),
940
+ onChange: (e) => setSearchInput(e.target.value),
941
+ onFocus: () => onActive?.(),
942
+ onMouseEnter: (e) => {
943
+ onActive?.();
944
+ e.stopPropagation();
945
+ },
946
+ onClick: (e) => e.stopPropagation(),
947
+ onKeyDown: (e) => {
948
+ if (e.key === "ArrowDown") {
949
+ e.preventDefault();
950
+ if (filteredOptions.length > 0) {
951
+ setHighlightedIndex(
952
+ (prev) => prev < filteredOptions.length - 1 ? prev + 1 : 0
953
+ );
954
+ }
955
+ } else if (e.key === "ArrowUp") {
956
+ e.preventDefault();
957
+ if (filteredOptions.length > 0) {
958
+ setHighlightedIndex(
959
+ (prev) => prev > 0 ? prev - 1 : filteredOptions.length - 1
960
+ );
961
+ }
962
+ } else if (e.key === "ArrowLeft") {
963
+ e.preventDefault();
964
+ onBack?.();
965
+ } else if (e.key === "Enter" && highlightedIndex >= 0) {
966
+ e.preventDefault();
967
+ const option = filteredOptions[highlightedIndex];
968
+ if (option) {
969
+ onToggle(
970
+ option.value,
971
+ currentValues.includes(option.value)
972
+ );
973
+ if (!isMultiSelect) {
974
+ onBack?.();
975
+ }
976
+ }
977
+ } else if (e.key === "Escape") {
978
+ e.preventDefault();
979
+ onClose?.();
980
+ }
981
+ e.stopPropagation();
982
+ }
983
+ }
984
+ ),
985
+ /* @__PURE__ */ jsx(DropdownMenuSeparator, {})
986
+ ] }),
987
+ /* @__PURE__ */ jsx("div", { className: "relative flex max-h-full", children: /* @__PURE__ */ jsx(
988
+ "div",
989
+ {
990
+ className: "flex max-h-[min(var(--available-height),24rem)] w-full scroll-pt-2 scroll-pb-2 flex-col overscroll-contain outline-hidden",
991
+ role: "listbox",
992
+ id: `${baseId}-listbox`,
993
+ tabIndex: field.searchable === false ? 0 : -1,
994
+ onKeyDown: (e) => {
995
+ if (field.searchable === false) {
996
+ if (e.key === "ArrowDown") {
997
+ e.preventDefault();
998
+ if (filteredOptions.length > 0) {
999
+ setHighlightedIndex(
1000
+ (prev) => prev < filteredOptions.length - 1 ? prev + 1 : 0
1001
+ );
1002
+ }
1003
+ } else if (e.key === "ArrowUp") {
1004
+ e.preventDefault();
1005
+ if (filteredOptions.length > 0) {
1006
+ setHighlightedIndex(
1007
+ (prev) => prev > 0 ? prev - 1 : filteredOptions.length - 1
1008
+ );
1009
+ }
1010
+ } else if (e.key === "ArrowLeft") {
1011
+ e.preventDefault();
1012
+ onBack?.();
1013
+ } else if (e.key === "Enter" && highlightedIndex >= 0) {
1014
+ e.preventDefault();
1015
+ const option = filteredOptions[highlightedIndex];
1016
+ if (option) {
1017
+ onToggle(
1018
+ option.value,
1019
+ currentValues.includes(option.value)
1020
+ );
1021
+ if (!isMultiSelect) {
1022
+ onBack?.();
1023
+ }
1024
+ }
1025
+ } else if (e.key === "Escape") {
1026
+ e.preventDefault();
1027
+ onClose?.();
1028
+ }
1029
+ e.stopPropagation();
1030
+ }
1031
+ },
1032
+ children: isAsync && loading && filteredOptions.length === 0 ? /* @__PURE__ */ jsx("div", { className: "py-2 text-center text-sm text-muted-foreground", children: i18n.loadingOptions ?? DEFAULT_I18N.loadingOptions }) : isAsync && error ? /* @__PURE__ */ jsx("div", { className: "py-2 text-center text-sm text-muted-foreground", children: i18n.errorLoadingOptions ?? DEFAULT_I18N.errorLoadingOptions }) : filteredOptions.length === 0 ? /* @__PURE__ */ jsx("div", { className: "py-2 text-center text-sm text-muted-foreground", children: i18n.noResultsFound }) : field.renderOptionList ? field.renderOptionList({
1033
+ options: filteredOptions,
1034
+ highlightedIndex,
1035
+ renderOption: renderOptionItem
1036
+ }) : /* @__PURE__ */ jsx(ScrollArea, { className: "size-full min-h-0 **:data-[slot=scroll-area-scrollbar]:m-0 **:data-[slot=scroll-area-viewport]:h-full **:data-[slot=scroll-area-viewport]:overscroll-contain", children: /* @__PURE__ */ jsx(DropdownMenuGroup, { children: filteredOptions.map(
1037
+ (option, index) => renderOptionItem(option, index)
1038
+ ) }) })
1039
+ }
1040
+ ) })
1041
+ ] });
1042
+ }
1043
+ function Filters({
1044
+ filters,
1045
+ fields,
1046
+ onChange,
1047
+ className,
1048
+ variant = "default",
1049
+ size = "default",
1050
+ radius = "default",
1051
+ i18n,
1052
+ showSearchInput = true,
1053
+ trigger,
1054
+ allowMultiple = true,
1055
+ menuPopupClassName,
1056
+ enableShortcut = false,
1057
+ shortcutKey = "f",
1058
+ shortcutLabel = "F"
1059
+ }) {
1060
+ const [addFilterOpen, setAddFilterOpen] = useState(false);
1061
+ const [menuSearchInput, setMenuSearchInput] = useState("");
1062
+ const [activeMenu, setActiveMenu] = useState("root");
1063
+ const [openSubMenu, setOpenSubMenu] = useState(null);
1064
+ const [highlightedIndex, setHighlightedIndex] = useState(-1);
1065
+ const [lastAddedFilterId, setLastAddedFilterId] = useState(
1066
+ null
1067
+ );
1068
+ const rootInputRef = useRef(null);
1069
+ const rootId = useId();
1070
+ useEffect(() => {
1071
+ if (!enableShortcut) return;
1072
+ const handleKeyDown = (e) => {
1073
+ if (e.key.toLowerCase() === shortcutKey.toLowerCase() && !addFilterOpen && !(document.activeElement instanceof HTMLInputElement || document.activeElement instanceof HTMLTextAreaElement)) {
1074
+ e.preventDefault();
1075
+ setAddFilterOpen(true);
1076
+ }
1077
+ };
1078
+ window.addEventListener("keydown", handleKeyDown);
1079
+ return () => window.removeEventListener("keydown", handleKeyDown);
1080
+ }, [enableShortcut, shortcutKey, addFilterOpen]);
1081
+ useEffect(() => {
1082
+ if (addFilterOpen && activeMenu === "root") {
1083
+ rootInputRef.current?.focus();
1084
+ }
1085
+ }, [addFilterOpen, activeMenu]);
1086
+ useEffect(() => {
1087
+ setHighlightedIndex(-1);
1088
+ }, [menuSearchInput]);
1089
+ useEffect(() => {
1090
+ if (highlightedIndex >= 0 && addFilterOpen) {
1091
+ const element = document.getElementById(
1092
+ `${rootId}-item-${highlightedIndex}`
1093
+ );
1094
+ element?.scrollIntoView({ block: "nearest" });
1095
+ }
1096
+ }, [highlightedIndex, addFilterOpen, rootId]);
1097
+ useEffect(() => {
1098
+ if (!addFilterOpen) {
1099
+ setOpenSubMenu(null);
1100
+ }
1101
+ }, [addFilterOpen]);
1102
+ const [sessionFilterIds, setSessionFilterIds] = useState({});
1103
+ useEffect(() => {
1104
+ if (lastAddedFilterId) {
1105
+ const timer = setTimeout(() => {
1106
+ setLastAddedFilterId(null);
1107
+ }, 1e3);
1108
+ return () => clearTimeout(timer);
1109
+ }
1110
+ }, [lastAddedFilterId]);
1111
+ const mergedI18n = useMemo(
1112
+ () => ({
1113
+ ...DEFAULT_I18N,
1114
+ ...i18n,
1115
+ operators: { ...DEFAULT_I18N.operators, ...i18n?.operators },
1116
+ placeholders: { ...DEFAULT_I18N.placeholders, ...i18n?.placeholders },
1117
+ validation: { ...DEFAULT_I18N.validation, ...i18n?.validation }
1118
+ }),
1119
+ [i18n]
1120
+ );
1121
+ const fieldsMap = useMemo(() => getFieldsMap(fields), [fields]);
1122
+ const updateFilter = useCallback(
1123
+ (filterId, updates) => {
1124
+ onChange(
1125
+ filters.map((filter) => {
1126
+ if (filter.id === filterId) {
1127
+ const updatedFilter = { ...filter, ...updates };
1128
+ if (updates.operator === "empty" || updates.operator === "not_empty") {
1129
+ updatedFilter.values = [];
1130
+ }
1131
+ return updatedFilter;
1132
+ }
1133
+ return filter;
1134
+ })
1135
+ );
1136
+ },
1137
+ [filters, onChange]
1138
+ );
1139
+ const removeFilter = useCallback(
1140
+ (filterId) => {
1141
+ onChange(filters.filter((filter) => filter.id !== filterId));
1142
+ },
1143
+ [filters, onChange]
1144
+ );
1145
+ const addFilter = useCallback(
1146
+ (fieldKey) => {
1147
+ const field = fieldsMap[fieldKey];
1148
+ if (field && field.key) {
1149
+ const defaultOperator = field.defaultOperator || (field.type === "multiselect" ? "is_any_of" : "is");
1150
+ const defaultValues = field.type === "text" ? [""] : [];
1151
+ const newFilter = createFilter(
1152
+ fieldKey,
1153
+ defaultOperator,
1154
+ defaultValues
1155
+ );
1156
+ setLastAddedFilterId(newFilter.id);
1157
+ onChange([...filters, newFilter]);
1158
+ setAddFilterOpen(false);
1159
+ setMenuSearchInput("");
1160
+ }
1161
+ },
1162
+ [fieldsMap, filters, onChange]
1163
+ );
1164
+ const selectableFields = useMemo(() => {
1165
+ const flatFields = flattenFields(fields);
1166
+ return flatFields.filter((field) => {
1167
+ if (!field.key || field.type === "separator") return false;
1168
+ if (allowMultiple) return true;
1169
+ return !filters.some((filter) => filter.field === field.key);
1170
+ });
1171
+ }, [fields, filters, allowMultiple]);
1172
+ const filteredFields = useMemo(() => {
1173
+ return selectableFields.filter(
1174
+ (f) => !menuSearchInput || f.label?.toLowerCase().includes(menuSearchInput.toLowerCase())
1175
+ );
1176
+ }, [selectableFields, menuSearchInput]);
1177
+ useEffect(() => {
1178
+ if (addFilterOpen && filteredFields.length > 0) {
1179
+ setHighlightedIndex(0);
1180
+ }
1181
+ }, [addFilterOpen, filteredFields.length]);
1182
+ const triggerButton = useRender({
1183
+ render: trigger ?? /* @__PURE__ */ jsxs(Button, { variant: "outline", children: [
1184
+ /* @__PURE__ */ jsx(PlusIcon, {}),
1185
+ mergedI18n.addFilter
1186
+ ] }),
1187
+ defaultTagName: "button"
1188
+ });
1189
+ const contextValue = useMemo(
1190
+ () => ({
1191
+ variant,
1192
+ size,
1193
+ radius,
1194
+ i18n: mergedI18n,
1195
+ className,
1196
+ trigger,
1197
+ allowMultiple
1198
+ }),
1199
+ [variant, size, radius, mergedI18n, className, trigger, allowMultiple]
1200
+ );
1201
+ return /* @__PURE__ */ jsx(FilterContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxs(
1202
+ "div",
1203
+ {
1204
+ className: cn(filtersContainerVariants({ variant, size }), className),
1205
+ children: [
1206
+ selectableFields.length > 0 && /* @__PURE__ */ jsxs(
1207
+ DropdownMenu,
1208
+ {
1209
+ open: addFilterOpen,
1210
+ onOpenChange: (open) => {
1211
+ setAddFilterOpen(open);
1212
+ if (!open) {
1213
+ setMenuSearchInput("");
1214
+ setSessionFilterIds({});
1215
+ } else {
1216
+ setActiveMenu("root");
1217
+ }
1218
+ },
1219
+ children: [
1220
+ /* @__PURE__ */ jsx(DropdownMenuTrigger, { render: triggerButton }),
1221
+ /* @__PURE__ */ jsxs(
1222
+ DropdownMenuContent,
1223
+ {
1224
+ className: cn("w-55", menuPopupClassName),
1225
+ align: "start",
1226
+ children: [
1227
+ showSearchInput && /* @__PURE__ */ jsxs(Fragment, { children: [
1228
+ /* @__PURE__ */ jsxs("div", { className: "relative", children: [
1229
+ /* @__PURE__ */ jsx(
1230
+ Input,
1231
+ {
1232
+ ref: rootInputRef,
1233
+ role: "combobox",
1234
+ "aria-controls": `${rootId}-listbox`,
1235
+ "aria-activedescendant": highlightedIndex >= 0 ? `${rootId}-item-${highlightedIndex}` : void 0,
1236
+ placeholder: mergedI18n.searchFields,
1237
+ className: cn(
1238
+ "h-8 rounded-none border-0 bg-transparent! px-2 text-sm shadow-none",
1239
+ "focus-visible:border-border focus-visible:ring-0 focus-visible:ring-offset-0",
1240
+ activeMenu === "root" && "placeholder:text-foreground"
1241
+ ),
1242
+ value: menuSearchInput,
1243
+ onFocus: () => setActiveMenu("root"),
1244
+ onMouseEnter: () => setActiveMenu("root"),
1245
+ onBlur: () => activeMenu === "root" && rootInputRef.current?.focus(),
1246
+ onChange: (e) => setMenuSearchInput(e.target.value),
1247
+ onClick: (e) => e.stopPropagation(),
1248
+ onKeyDown: (e) => {
1249
+ if (e.key === "ArrowDown") {
1250
+ e.preventDefault();
1251
+ if (filteredFields.length > 0) {
1252
+ setHighlightedIndex(
1253
+ (prev) => prev < filteredFields.length - 1 ? prev + 1 : 0
1254
+ );
1255
+ }
1256
+ } else if (e.key === "ArrowUp") {
1257
+ e.preventDefault();
1258
+ if (filteredFields.length > 0) {
1259
+ setHighlightedIndex(
1260
+ (prev) => prev > 0 ? prev - 1 : filteredFields.length - 1
1261
+ );
1262
+ }
1263
+ } else if ((e.key === "ArrowRight" || e.key === "ArrowLeft") && highlightedIndex >= 0) {
1264
+ const field = filteredFields[highlightedIndex];
1265
+ const hasSubMenu = field && (field.type === "select" || field.type === "multiselect") && fieldHasOptions(field);
1266
+ if (e.key === "ArrowRight" && hasSubMenu) {
1267
+ e.preventDefault();
1268
+ setOpenSubMenu(field.key || null);
1269
+ setActiveMenu(field.key || "root");
1270
+ } else if (e.key === "ArrowLeft") {
1271
+ e.preventDefault();
1272
+ if (openSubMenu) {
1273
+ setOpenSubMenu(null);
1274
+ setActiveMenu("root");
1275
+ }
1276
+ }
1277
+ } else if (e.key === "Enter" && highlightedIndex >= 0) {
1278
+ e.preventDefault();
1279
+ const field = filteredFields[highlightedIndex];
1280
+ if (field.key) {
1281
+ const hasSubMenu = (field.type === "select" || field.type === "multiselect") && fieldHasOptions(field);
1282
+ if (!hasSubMenu) {
1283
+ addFilter(field.key);
1284
+ } else {
1285
+ if (openSubMenu === field.key) {
1286
+ setOpenSubMenu(null);
1287
+ setActiveMenu("root");
1288
+ } else {
1289
+ setOpenSubMenu(field.key);
1290
+ setActiveMenu(field.key);
1291
+ }
1292
+ }
1293
+ }
1294
+ } else if (e.key === "Escape") {
1295
+ setAddFilterOpen(false);
1296
+ }
1297
+ e.stopPropagation();
1298
+ }
1299
+ }
1300
+ ),
1301
+ enableShortcut && shortcutLabel && /* @__PURE__ */ jsx(Kbd, { className: "absolute top-1/2 right-2 -translate-y-1/2 border bg-background", children: shortcutLabel })
1302
+ ] }),
1303
+ /* @__PURE__ */ jsx(DropdownMenuSeparator, {})
1304
+ ] }),
1305
+ /* @__PURE__ */ jsx("div", { className: "relative flex max-h-full", children: /* @__PURE__ */ jsx(
1306
+ "div",
1307
+ {
1308
+ className: "flex max-h-[min(var(--available-height),24rem)] w-full scroll-pt-2 scroll-pb-2 flex-col overscroll-contain",
1309
+ role: "listbox",
1310
+ id: `${rootId}-listbox`,
1311
+ onMouseEnter: () => setActiveMenu("root"),
1312
+ children: /* @__PURE__ */ jsx(ScrollArea, { className: "**:data-[slot=scroll-area-scrollbar]:m-0", children: (() => {
1313
+ if (filteredFields.length === 0) {
1314
+ return /* @__PURE__ */ jsx("div", { className: "py-2 text-center text-sm text-muted-foreground", children: mergedI18n.noFieldsFound });
1315
+ }
1316
+ return filteredFields.map((field, index) => {
1317
+ const isHighlighted = highlightedIndex === index;
1318
+ const itemId = `${rootId}-item-${index}`;
1319
+ const hasSubMenu = (field.type === "select" || field.type === "multiselect") && fieldHasOptions(field);
1320
+ if (hasSubMenu) {
1321
+ const isMultiSelect = field.type === "multiselect";
1322
+ const fieldKey = field.key;
1323
+ const sessionFilterId = sessionFilterIds[fieldKey];
1324
+ const sessionFilter = sessionFilterId ? filters.find((f) => f.id === sessionFilterId) : null;
1325
+ const currentValues = sessionFilter?.values || [];
1326
+ return /* @__PURE__ */ jsxs(
1327
+ DropdownMenuSub,
1328
+ {
1329
+ open: openSubMenu === fieldKey,
1330
+ onOpenChange: (open) => {
1331
+ if (open) {
1332
+ setOpenSubMenu(fieldKey);
1333
+ } else {
1334
+ if (openSubMenu === fieldKey) {
1335
+ setOpenSubMenu(null);
1336
+ setActiveMenu("root");
1337
+ }
1338
+ }
1339
+ },
1340
+ children: [
1341
+ /* @__PURE__ */ jsxs(
1342
+ DropdownMenuSubTrigger,
1343
+ {
1344
+ id: itemId,
1345
+ role: "option",
1346
+ "aria-selected": isHighlighted,
1347
+ "data-highlighted": isHighlighted || void 0,
1348
+ onMouseEnter: () => {
1349
+ setHighlightedIndex(index);
1350
+ setActiveMenu("root");
1351
+ },
1352
+ className: "data-highlighted:bg-accent data-highlighted:text-accent-foreground data-popup-open:bg-accent data-popup-open:text-accent-foreground",
1353
+ children: [
1354
+ field.icon,
1355
+ /* @__PURE__ */ jsx("span", { children: field.label })
1356
+ ]
1357
+ }
1358
+ ),
1359
+ /* @__PURE__ */ jsx(
1360
+ DropdownMenuSubContent,
1361
+ {
1362
+ className: "w-50",
1363
+ side: "right",
1364
+ children: /* @__PURE__ */ jsx(
1365
+ FilterSubmenuContent,
1366
+ {
1367
+ field,
1368
+ currentValues,
1369
+ isMultiSelect,
1370
+ i18n: mergedI18n,
1371
+ isActive: activeMenu === fieldKey,
1372
+ onActive: () => {
1373
+ if (field.searchable !== false) {
1374
+ setActiveMenu(fieldKey);
1375
+ }
1376
+ },
1377
+ onBack: () => {
1378
+ setOpenSubMenu(null);
1379
+ setActiveMenu("root");
1380
+ },
1381
+ onClose: () => setAddFilterOpen(false),
1382
+ onToggle: (value, isSelected) => {
1383
+ if (isMultiSelect) {
1384
+ const nextValues = isSelected ? currentValues.filter(
1385
+ (v) => v !== value
1386
+ ) : [...currentValues, value];
1387
+ if (sessionFilter) {
1388
+ if (nextValues.length === 0) {
1389
+ onChange(
1390
+ filters.filter(
1391
+ (f) => f.id !== sessionFilter.id
1392
+ )
1393
+ );
1394
+ setSessionFilterIds((prev) => ({
1395
+ ...prev,
1396
+ [fieldKey]: ""
1397
+ }));
1398
+ } else {
1399
+ onChange(
1400
+ filters.map(
1401
+ (f) => f.id === sessionFilter.id ? { ...f, values: nextValues } : f
1402
+ )
1403
+ );
1404
+ }
1405
+ } else {
1406
+ const newFilter = createFilter(
1407
+ fieldKey,
1408
+ field.defaultOperator || "is_any_of",
1409
+ nextValues
1410
+ );
1411
+ onChange([...filters, newFilter]);
1412
+ setSessionFilterIds((prev) => ({
1413
+ ...prev,
1414
+ [fieldKey]: newFilter.id
1415
+ }));
1416
+ }
1417
+ } else {
1418
+ const newFilter = createFilter(
1419
+ fieldKey,
1420
+ field.defaultOperator || "is",
1421
+ [value]
1422
+ );
1423
+ setLastAddedFilterId(newFilter.id);
1424
+ onChange([...filters, newFilter]);
1425
+ setAddFilterOpen(false);
1426
+ }
1427
+ }
1428
+ }
1429
+ )
1430
+ }
1431
+ )
1432
+ ]
1433
+ },
1434
+ fieldKey
1435
+ );
1436
+ }
1437
+ return /* @__PURE__ */ jsxs(
1438
+ DropdownMenuItem,
1439
+ {
1440
+ id: itemId,
1441
+ role: "option",
1442
+ "aria-selected": isHighlighted,
1443
+ "data-highlighted": isHighlighted || void 0,
1444
+ onMouseEnter: () => setHighlightedIndex(index),
1445
+ onClick: () => field.key && addFilter(field.key),
1446
+ className: "data-highlighted:bg-accent data-highlighted:text-accent-foreground",
1447
+ children: [
1448
+ field.icon,
1449
+ /* @__PURE__ */ jsx("span", { children: field.label })
1450
+ ]
1451
+ },
1452
+ field.key
1453
+ );
1454
+ });
1455
+ })() })
1456
+ }
1457
+ ) })
1458
+ ]
1459
+ }
1460
+ )
1461
+ ]
1462
+ }
1463
+ ),
1464
+ filters.map((filter) => {
1465
+ const field = fieldsMap[filter.field];
1466
+ if (!field) return null;
1467
+ return /* @__PURE__ */ jsxs(
1468
+ ButtonGroup,
1469
+ {
1470
+ className: "",
1471
+ children: [
1472
+ /* @__PURE__ */ jsxs(ButtonGroupText, { className: "bg-background dark:bg-input/30", children: [
1473
+ field.icon && field.icon,
1474
+ field.label
1475
+ ] }),
1476
+ /* @__PURE__ */ jsx(
1477
+ FilterOperatorDropdown,
1478
+ {
1479
+ field,
1480
+ operator: filter.operator,
1481
+ values: filter.values,
1482
+ onChange: (operator) => updateFilter(filter.id, { operator })
1483
+ }
1484
+ ),
1485
+ /* @__PURE__ */ jsx(
1486
+ FilterValueSelector,
1487
+ {
1488
+ field,
1489
+ values: filter.values,
1490
+ operator: filter.operator,
1491
+ onChange: (values) => updateFilter(filter.id, { values }),
1492
+ autoFocus: filter.id === lastAddedFilterId
1493
+ }
1494
+ ),
1495
+ /* @__PURE__ */ jsx(FilterRemoveButton, { onClick: () => removeFilter(filter.id) })
1496
+ ]
1497
+ },
1498
+ filter.id
1499
+ );
1500
+ })
1501
+ ]
1502
+ }
1503
+ ) });
1504
+ }
1505
+ var createFilter = (field, operator, values = []) => ({
1506
+ id: `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`,
1507
+ field,
1508
+ operator: operator || "is",
1509
+ values
1510
+ });
1511
+ var createFilterGroup = (id, label, fields, initialFilters = []) => ({
1512
+ id,
1513
+ label,
1514
+ filters: initialFilters,
1515
+ fields
1516
+ });
1517
+ export {
1518
+ DEFAULT_I18N,
1519
+ DEFAULT_OPERATORS,
1520
+ Filters,
1521
+ FiltersContent,
1522
+ createFilter,
1523
+ createFilterGroup
1524
+ };