@loykin/filter-input 0.0.1-dev.0

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/dist/index.js ADDED
@@ -0,0 +1,758 @@
1
+ import { useState, useMemo, useRef, useCallback, useEffect } from 'react';
2
+ import { clsx } from 'clsx';
3
+ import { Select } from '@base-ui/react/select';
4
+ import { jsx, jsxs } from 'react/jsx-runtime';
5
+ import { Popover } from '@base-ui/react/popover';
6
+
7
+ // src/FilterInput.tsx
8
+ function cn(...inputs) {
9
+ return clsx(inputs);
10
+ }
11
+ function ChevronDownIcon() {
12
+ return /* @__PURE__ */ jsx("svg", { width: "12", height: "12", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M2 4l4 4 4-4" }) });
13
+ }
14
+ function CheckIcon() {
15
+ return /* @__PURE__ */ jsx("svg", { width: "12", height: "12", viewBox: "0 0 12 12", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M2 6l3 3 5-5" }) });
16
+ }
17
+ function XIcon() {
18
+ return /* @__PURE__ */ jsx("svg", { width: "10", height: "10", viewBox: "0 0 10 10", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", "aria-hidden": "true", children: /* @__PURE__ */ jsx("path", { d: "M2 2l6 6M8 2l-6 6" }) });
19
+ }
20
+ function RefreshIcon() {
21
+ return /* @__PURE__ */ jsxs("svg", { width: "12", height: "12", viewBox: "-1 -1 14 14", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: [
22
+ /* @__PURE__ */ jsx("path", { d: "M10 2a5 5 0 1 0 .9 3" }),
23
+ /* @__PURE__ */ jsx("path", { d: "M10 1v3h-3" })
24
+ ] });
25
+ }
26
+ function FiSelect({
27
+ options,
28
+ value,
29
+ placeholder = "Select...",
30
+ disabled,
31
+ required,
32
+ loading,
33
+ error,
34
+ searchable,
35
+ query = "",
36
+ onQueryChange,
37
+ onChange,
38
+ onOpen,
39
+ onReload,
40
+ className,
41
+ classNames
42
+ }) {
43
+ return /* @__PURE__ */ jsxs(
44
+ Select.Root,
45
+ {
46
+ value,
47
+ onValueChange: (v) => onChange(v ?? ""),
48
+ onOpenChange: (open) => {
49
+ if (open) onOpen?.();
50
+ else onQueryChange?.("");
51
+ },
52
+ disabled,
53
+ children: [
54
+ /* @__PURE__ */ jsxs(Select.Trigger, { className: cn("fi-select-trigger", classNames?.trigger, className), children: [
55
+ /* @__PURE__ */ jsx(Select.Value, { placeholder, className: "fi-select-value" }),
56
+ /* @__PURE__ */ jsx(Select.Icon, { className: "fi-select-icon", children: /* @__PURE__ */ jsx(ChevronDownIcon, {}) })
57
+ ] }),
58
+ /* @__PURE__ */ jsx(Select.Portal, { children: /* @__PURE__ */ jsx(Select.Positioner, { sideOffset: 4, className: "isolate z-50", children: /* @__PURE__ */ jsxs(Select.Popup, { className: cn("fi-select-popup", classNames?.popup), children: [
59
+ (searchable || onReload) && /* @__PURE__ */ jsxs("div", { className: "fi-popup-header", children: [
60
+ searchable && /* @__PURE__ */ jsx(
61
+ "input",
62
+ {
63
+ className: "fi-control",
64
+ value: query,
65
+ placeholder: "Search...",
66
+ autoFocus: true,
67
+ onChange: (e) => onQueryChange?.(e.target.value),
68
+ onKeyDown: (e) => {
69
+ const passThrough = ["Escape", "Tab", "ArrowDown", "ArrowUp", "Enter", "Home", "End"];
70
+ if (!passThrough.includes(e.key)) e.stopPropagation();
71
+ }
72
+ }
73
+ ),
74
+ onReload && /* @__PURE__ */ jsx("button", { type: "button", className: "fi-reload-button", onClick: onReload, disabled: loading, "aria-label": "Reload", children: /* @__PURE__ */ jsx(RefreshIcon, {}) })
75
+ ] }),
76
+ (loading || error) && /* @__PURE__ */ jsxs("div", { className: "fi-popup-status", children: [
77
+ loading ? /* @__PURE__ */ jsx("span", { className: "fi-loading", children: "Loading\u2026" }) : /* @__PURE__ */ jsx("span", { className: "fi-error", children: error }),
78
+ onReload && !loading && /* @__PURE__ */ jsx("button", { type: "button", className: "fi-reload-button", onClick: onReload, "aria-label": "Retry", children: /* @__PURE__ */ jsx(RefreshIcon, {}) })
79
+ ] }),
80
+ /* @__PURE__ */ jsxs(Select.List, { children: [
81
+ !loading && !error && !required && /* @__PURE__ */ jsx(Select.Item, { value: "", className: cn("fi-select-item", classNames?.item), children: /* @__PURE__ */ jsx(Select.ItemText, { className: "fi-select-item-text", children: placeholder }) }),
82
+ !loading && !error && options.map((option) => /* @__PURE__ */ jsxs(
83
+ Select.Item,
84
+ {
85
+ value: option.value,
86
+ disabled: option.disabled,
87
+ className: cn("fi-select-item", classNames?.item),
88
+ children: [
89
+ /* @__PURE__ */ jsx(Select.ItemText, { className: "fi-select-item-text", children: option.label }),
90
+ /* @__PURE__ */ jsx(Select.ItemIndicator, { className: "fi-select-item-indicator", children: /* @__PURE__ */ jsx(CheckIcon, {}) })
91
+ ]
92
+ },
93
+ option.value
94
+ )),
95
+ !loading && !error && !options.length && !required && /* @__PURE__ */ jsx("div", { className: "fi-popup-status", children: /* @__PURE__ */ jsx("span", { className: "fi-empty", children: "No options" }) })
96
+ ] }),
97
+ onReload && !searchable && !loading && !error && /* @__PURE__ */ jsx("div", { className: "fi-popup-footer", children: /* @__PURE__ */ jsx("button", { type: "button", className: "fi-reload-button", onClick: onReload, "aria-label": "Reload", children: /* @__PURE__ */ jsx(RefreshIcon, {}) }) })
98
+ ] }) }) })
99
+ ]
100
+ }
101
+ );
102
+ }
103
+ function stringifyValue(value) {
104
+ return String(value);
105
+ }
106
+ function optionMatches(option, value) {
107
+ return option.value === value || stringifyValue(option.value) === stringifyValue(value);
108
+ }
109
+ function FiMultiSelect({
110
+ options,
111
+ value,
112
+ placeholder = "Select...",
113
+ disabled,
114
+ searchable,
115
+ loading,
116
+ error,
117
+ query,
118
+ onQueryChange,
119
+ onToggle,
120
+ onOpen,
121
+ onReload,
122
+ renderTriggerValue,
123
+ renderOption,
124
+ classNames
125
+ }) {
126
+ return /* @__PURE__ */ jsxs(Popover.Root, { onOpenChange: (open) => {
127
+ if (open) onOpen?.();
128
+ }, children: [
129
+ /* @__PURE__ */ jsxs(
130
+ Popover.Trigger,
131
+ {
132
+ className: cn("fi-multi-trigger", classNames?.trigger),
133
+ disabled,
134
+ children: [
135
+ /* @__PURE__ */ jsx("span", { className: "fi-multi-value", children: renderTriggerValue() }),
136
+ /* @__PURE__ */ jsx("span", { className: "fi-chevron", children: /* @__PURE__ */ jsx(ChevronDownIcon, {}) })
137
+ ]
138
+ }
139
+ ),
140
+ /* @__PURE__ */ jsx(Popover.Portal, { children: /* @__PURE__ */ jsx(Popover.Positioner, { sideOffset: 4, align: "start", className: "isolate z-50", children: /* @__PURE__ */ jsxs(Popover.Popup, { className: cn("fi-multi-popup", classNames?.popup), children: [
141
+ (searchable || onReload) && /* @__PURE__ */ jsxs("div", { className: "fi-popup-header", children: [
142
+ searchable && /* @__PURE__ */ jsx(
143
+ "input",
144
+ {
145
+ className: "fi-control",
146
+ value: query,
147
+ placeholder,
148
+ onChange: (e) => onQueryChange(e.target.value)
149
+ }
150
+ ),
151
+ onReload && /* @__PURE__ */ jsx(
152
+ "button",
153
+ {
154
+ type: "button",
155
+ className: "fi-reload-button",
156
+ onClick: onReload,
157
+ disabled: loading,
158
+ "aria-label": "Reload",
159
+ children: /* @__PURE__ */ jsx(RefreshIcon, {})
160
+ }
161
+ )
162
+ ] }),
163
+ loading && /* @__PURE__ */ jsx("div", { className: "fi-popup-status", children: /* @__PURE__ */ jsx("span", { className: "fi-loading", children: "Loading\u2026" }) }),
164
+ !loading && error && /* @__PURE__ */ jsx("div", { className: "fi-popup-status", children: /* @__PURE__ */ jsx("span", { className: "fi-error", children: error }) }),
165
+ !loading && !error && options.map((option) => {
166
+ const checked = value.some((item) => optionMatches(option, item));
167
+ return /* @__PURE__ */ jsxs(
168
+ "button",
169
+ {
170
+ type: "button",
171
+ className: cn("fi-option", classNames?.item),
172
+ disabled: option.disabled,
173
+ onMouseDown: (e) => e.preventDefault(),
174
+ onClick: () => onToggle(option),
175
+ children: [
176
+ /* @__PURE__ */ jsx("span", { className: "fi-option-check", "data-checked": checked || void 0, children: checked && /* @__PURE__ */ jsx(CheckIcon, {}) }),
177
+ /* @__PURE__ */ jsx("span", { className: "fi-option-label", children: renderOption ? renderOption(option) : option.label })
178
+ ]
179
+ },
180
+ stringifyValue(option.value)
181
+ );
182
+ }),
183
+ !loading && !error && !options.length && /* @__PURE__ */ jsx("div", { className: "fi-popup-status", children: /* @__PURE__ */ jsx("span", { className: "fi-empty", children: "No options" }) })
184
+ ] }) }) })
185
+ ] });
186
+ }
187
+ var EMPTY_OPTIONS = [];
188
+ function stringifyValue2(value) {
189
+ return String(value);
190
+ }
191
+ function normalizeText(value) {
192
+ if (value === null || value === void 0) return "";
193
+ return String(value);
194
+ }
195
+ function toMultiValue(value) {
196
+ if (!Array.isArray(value)) return [];
197
+ return value;
198
+ }
199
+ function getDateRange(value) {
200
+ if (!value || Array.isArray(value) || typeof value !== "object") return {};
201
+ return value;
202
+ }
203
+ function getNumberRange(value) {
204
+ if (!value || Array.isArray(value) || typeof value !== "object") return {};
205
+ return value;
206
+ }
207
+ function optionMatches2(option, value) {
208
+ return option.value === value || stringifyValue2(option.value) === stringifyValue2(value);
209
+ }
210
+ function selectedOptions(options, value) {
211
+ if (Array.isArray(value)) {
212
+ return value.map((item) => options.find((option2) => optionMatches2(option2, item)) ?? {
213
+ label: stringifyValue2(item),
214
+ value: item
215
+ });
216
+ }
217
+ if (value === null || value === void 0 || typeof value === "object") return [];
218
+ const option = options.find((item) => optionMatches2(item, value));
219
+ return option ? [option] : [{ label: stringifyValue2(value), value }];
220
+ }
221
+ function optionColor(option) {
222
+ if (option.color) return option.color;
223
+ return void 0;
224
+ }
225
+ function valueFromString(value, options) {
226
+ const option = options.find((item) => stringifyValue2(item.value) === value);
227
+ return option?.value ?? value;
228
+ }
229
+ function hasRemoteSource(config) {
230
+ return config.dataSource?.type === "remote" && !!config.dataSource.fetch;
231
+ }
232
+ function useOptions({
233
+ config,
234
+ value,
235
+ filters,
236
+ query
237
+ }) {
238
+ const staticOptions = config.options ?? EMPTY_OPTIONS;
239
+ const [remoteOptions, setRemoteOptions] = useState([]);
240
+ const [loading, setLoading] = useState(false);
241
+ const [error, setError] = useState(null);
242
+ const requestRef = useRef(0);
243
+ const fetcher = config.dataSource?.fetch;
244
+ const remote = hasRemoteSource(config);
245
+ const debounceMs = config.behavior?.debounceMs ?? 200;
246
+ const minSearchLength = config.behavior?.minSearchLength ?? 0;
247
+ const isLazy = config.dataSource?.trigger === "open";
248
+ const [activated, setActivated] = useState(!isLazy);
249
+ const [reloadKey, setReloadKey] = useState(0);
250
+ const activate = useCallback(() => setActivated(true), []);
251
+ const reload = useCallback(() => {
252
+ setActivated(true);
253
+ setReloadKey((k) => k + 1);
254
+ }, []);
255
+ useEffect(() => {
256
+ if (!remote || !fetcher || !activated) return;
257
+ if (query.length < minSearchLength) {
258
+ setRemoteOptions([]);
259
+ setLoading(false);
260
+ setError(null);
261
+ return;
262
+ }
263
+ const requestId = requestRef.current + 1;
264
+ requestRef.current = requestId;
265
+ const controller = new AbortController();
266
+ const timer = window.setTimeout(() => {
267
+ setLoading(true);
268
+ setError(null);
269
+ fetcher({ query, value, filters, signal: controller.signal }).then((items) => {
270
+ if (requestRef.current === requestId) setRemoteOptions(items);
271
+ }).catch((err) => {
272
+ if (controller.signal.aborted || requestRef.current !== requestId) return;
273
+ setRemoteOptions([]);
274
+ setError(err instanceof Error ? err.message : "Failed to load options");
275
+ }).finally(() => {
276
+ if (requestRef.current === requestId) setLoading(false);
277
+ });
278
+ }, debounceMs);
279
+ return () => {
280
+ controller.abort();
281
+ window.clearTimeout(timer);
282
+ };
283
+ }, [activated, debounceMs, fetcher, filters, minSearchLength, query, reloadKey, remote, value]);
284
+ return {
285
+ options: remote ? remoteOptions : staticOptions,
286
+ loading,
287
+ error,
288
+ activate,
289
+ reload
290
+ };
291
+ }
292
+ function SelectedValueDisplay({
293
+ options,
294
+ value,
295
+ display,
296
+ onRemove
297
+ }) {
298
+ const items = selectedOptions(options, value);
299
+ const emptyText = display.emptyText ?? "No value";
300
+ const variant = display.variant ?? (Array.isArray(value) ? "tags" : "text");
301
+ if (!items.length) {
302
+ return /* @__PURE__ */ jsx("span", { className: "fi-selected-empty", children: emptyText });
303
+ }
304
+ if (variant === "count") {
305
+ return /* @__PURE__ */ jsxs("span", { className: "fi-selected-count", children: [
306
+ items.length,
307
+ " selected"
308
+ ] });
309
+ }
310
+ if (variant === "summary") {
311
+ return /* @__PURE__ */ jsxs("span", { className: "fi-selected-summary", children: [
312
+ items.length,
313
+ " ",
314
+ display.summaryLabel ?? "selected"
315
+ ] });
316
+ }
317
+ const maxVisible = display.maxVisible ?? items.length;
318
+ const visible = items.slice(0, maxVisible);
319
+ const hidden = Math.max(0, items.length - visible.length);
320
+ if (variant === "text") {
321
+ return /* @__PURE__ */ jsx("span", { className: "fi-selected-text", children: items.map((item) => display.formatLabel?.(item) ?? item.label).join(", ") });
322
+ }
323
+ return /* @__PURE__ */ jsxs("div", { className: "fi-selected-list", children: [
324
+ visible.map((item) => {
325
+ const label = display.formatLabel?.(item) ?? item.label;
326
+ return /* @__PURE__ */ jsxs(
327
+ "span",
328
+ {
329
+ className: "fi-tag",
330
+ "data-size": display.size ?? "sm",
331
+ title: label,
332
+ style: display.colorBy !== "none" ? { borderColor: optionColor(item), color: optionColor(item) } : void 0,
333
+ children: [
334
+ /* @__PURE__ */ jsx("span", { className: "fi-tag-label", children: label }),
335
+ display.removable && onRemove && /* @__PURE__ */ jsx(
336
+ "button",
337
+ {
338
+ type: "button",
339
+ className: "fi-tag-remove",
340
+ "aria-label": `Remove ${label}`,
341
+ onClick: () => onRemove(item.value),
342
+ children: /* @__PURE__ */ jsx(XIcon, {})
343
+ }
344
+ )
345
+ ]
346
+ },
347
+ stringifyValue2(item.value)
348
+ );
349
+ }),
350
+ hidden > 0 && display.overflow !== "collapse" && /* @__PURE__ */ jsxs(
351
+ "span",
352
+ {
353
+ className: "fi-tag-overflow",
354
+ title: display.overflow === "tooltip" ? items.slice(maxVisible).map((item) => item.label).join(", ") : void 0,
355
+ children: [
356
+ "+",
357
+ hidden
358
+ ]
359
+ }
360
+ )
361
+ ] });
362
+ }
363
+ function FilterInput({
364
+ config,
365
+ value,
366
+ filters,
367
+ onChange,
368
+ onSearch,
369
+ className,
370
+ inputClassName,
371
+ classNames,
372
+ renderOption
373
+ }) {
374
+ const [query, setQuery] = useState("");
375
+ const { options, loading, error, activate, reload } = useOptions({ config, value, filters, query });
376
+ const behavior = config.behavior ?? {};
377
+ const display = config.display ?? {};
378
+ const searchable = !!behavior.searchable || config.type === "autocomplete" || config.type === "combobox";
379
+ const disabled = !!behavior.disabled;
380
+ const remote = hasRemoteSource(config);
381
+ const filteredOptions = useMemo(() => {
382
+ if (remote || !query || !searchable) return options;
383
+ const normalized = query.toLowerCase();
384
+ return options.filter((option) => option.label.toLowerCase().includes(normalized) || stringifyValue2(option.value).toLowerCase().includes(normalized));
385
+ }, [options, query, remote, searchable]);
386
+ const emit = (nextValue, option) => {
387
+ onChange(nextValue, { key: config.key, option, options });
388
+ };
389
+ const clearButton = behavior.clearable && value !== void 0 && value !== null && value !== "" && /* @__PURE__ */ jsx(
390
+ "button",
391
+ {
392
+ type: "button",
393
+ className: cn("fi-clear-button", classNames?.clearButton),
394
+ disabled,
395
+ onClick: () => emit(Array.isArray(value) ? [] : null),
396
+ children: "Clear"
397
+ }
398
+ );
399
+ const commonInputClass = cn(
400
+ "fi-control",
401
+ classNames?.control,
402
+ inputClassName
403
+ );
404
+ const control = (() => {
405
+ switch (config.type) {
406
+ case "textarea":
407
+ return /* @__PURE__ */ jsx(
408
+ "textarea",
409
+ {
410
+ className: cn("fi-textarea", classNames?.control, inputClassName),
411
+ value: normalizeText(value),
412
+ placeholder: config.placeholder,
413
+ disabled,
414
+ required: behavior.required,
415
+ onChange: (event) => emit(event.target.value)
416
+ }
417
+ );
418
+ case "number":
419
+ return /* @__PURE__ */ jsx(
420
+ "input",
421
+ {
422
+ className: commonInputClass,
423
+ type: "number",
424
+ value: normalizeText(value),
425
+ min: config.validation?.min,
426
+ max: config.validation?.max,
427
+ placeholder: config.placeholder,
428
+ disabled,
429
+ required: behavior.required,
430
+ onKeyDown: (event) => {
431
+ if (["e", "E", "+"].includes(event.key)) event.preventDefault();
432
+ if (event.key === "-" && (config.validation?.min ?? Number.NEGATIVE_INFINITY) >= 0) event.preventDefault();
433
+ },
434
+ onChange: (event) => emit(event.target.value === "" ? null : Number(event.target.value))
435
+ }
436
+ );
437
+ case "boolean":
438
+ return /* @__PURE__ */ jsxs("div", { className: cn("fi-row", classNames?.row), children: [
439
+ /* @__PURE__ */ jsx(
440
+ FiSelect,
441
+ {
442
+ className: "flex-1",
443
+ options: [
444
+ { label: "True", value: "true" },
445
+ { label: "False", value: "false" }
446
+ ],
447
+ value: value === null || value === void 0 ? "" : String(value),
448
+ placeholder: config.placeholder ?? "Any",
449
+ disabled,
450
+ onChange: (next) => {
451
+ if (next === "") emit(null);
452
+ else emit(next === "true");
453
+ }
454
+ }
455
+ ),
456
+ clearButton
457
+ ] });
458
+ case "multi-select": {
459
+ const selected = toMultiValue(value);
460
+ const toggleOption = (option) => {
461
+ const exists = selected.some((item) => optionMatches2(option, item));
462
+ const next = exists ? selected.filter((item) => !optionMatches2(option, item)) : [...selected, option.value];
463
+ emit(next, option);
464
+ };
465
+ return /* @__PURE__ */ jsxs("div", { className: cn("fi-multi", classNames?.multi), children: [
466
+ /* @__PURE__ */ jsx(
467
+ FiMultiSelect,
468
+ {
469
+ options: filteredOptions,
470
+ value: selected,
471
+ placeholder: config.placeholder ?? "Select...",
472
+ disabled,
473
+ searchable,
474
+ loading,
475
+ error,
476
+ query,
477
+ onQueryChange: (q) => {
478
+ setQuery(q);
479
+ onSearch?.(q, { key: config.key });
480
+ },
481
+ onToggle: toggleOption,
482
+ onOpen: activate,
483
+ onReload: hasRemoteSource(config) && behavior.showReload !== false ? reload : void 0,
484
+ renderTriggerValue: () => /* @__PURE__ */ jsx(
485
+ SelectedValueDisplay,
486
+ {
487
+ options,
488
+ value,
489
+ display: { variant: "tags", maxVisible: 2, overflow: "count", removable: false, emptyText: config.placeholder ?? "Select...", ...display }
490
+ }
491
+ ),
492
+ renderOption: renderOption ? (o) => renderOption(o) : void 0,
493
+ classNames: classNames ? { trigger: classNames.multiTrigger, popup: classNames.popover, item: classNames.option } : void 0
494
+ }
495
+ ),
496
+ /* @__PURE__ */ jsx("div", { className: "fi-row", children: clearButton })
497
+ ] });
498
+ }
499
+ case "select":
500
+ return /* @__PURE__ */ jsxs("div", { className: cn("fi-row", classNames?.row), children: [
501
+ /* @__PURE__ */ jsx(
502
+ FiSelect,
503
+ {
504
+ className: "flex-1",
505
+ options: filteredOptions.map((o) => ({ label: o.label, value: stringifyValue2(o.value), disabled: o.disabled })),
506
+ value: value === null || value === void 0 ? "" : normalizeText(value),
507
+ placeholder: config.placeholder ?? "Select...",
508
+ disabled,
509
+ required: behavior.required,
510
+ loading,
511
+ error,
512
+ searchable,
513
+ query,
514
+ onQueryChange: (q) => {
515
+ setQuery(q);
516
+ onSearch?.(q, { key: config.key });
517
+ },
518
+ onOpen: activate,
519
+ onReload: hasRemoteSource(config) && behavior.showReload !== false ? reload : void 0,
520
+ onChange: (next) => {
521
+ const resolved = valueFromString(next, options);
522
+ emit(next === "" ? null : resolved, options.find((o) => optionMatches2(o, resolved)));
523
+ }
524
+ }
525
+ ),
526
+ clearButton
527
+ ] });
528
+ case "autocomplete":
529
+ case "combobox":
530
+ return /* @__PURE__ */ jsxs("div", { className: cn("fi-stack", classNames?.stack), children: [
531
+ /* @__PURE__ */ jsxs("div", { className: cn("fi-row", classNames?.row), children: [
532
+ /* @__PURE__ */ jsx(
533
+ "input",
534
+ {
535
+ className: commonInputClass,
536
+ value: query,
537
+ placeholder: config.placeholder ?? "Search...",
538
+ disabled,
539
+ onChange: (event) => {
540
+ const nextQuery = event.target.value;
541
+ setQuery(nextQuery);
542
+ onSearch?.(nextQuery, { key: config.key });
543
+ if (config.type === "combobox" && behavior.allowCustomValue) emit(nextQuery);
544
+ },
545
+ onBlur: () => {
546
+ if (config.type === "combobox" && behavior.selectOnBlur && query) emit(query);
547
+ }
548
+ }
549
+ ),
550
+ clearButton
551
+ ] }),
552
+ (loading || error || filteredOptions.length > 0) && /* @__PURE__ */ jsxs("div", { className: cn("fi-popover", classNames?.popover), children: [
553
+ loading && /* @__PURE__ */ jsx("div", { className: "fi-loading", children: "Loading..." }),
554
+ error && /* @__PURE__ */ jsx("div", { className: "fi-error", children: error }),
555
+ !loading && !error && filteredOptions.map((option) => /* @__PURE__ */ jsxs(
556
+ "button",
557
+ {
558
+ type: "button",
559
+ className: cn("fi-option", classNames?.option),
560
+ disabled: disabled || option.disabled,
561
+ onMouseDown: (event) => event.preventDefault(),
562
+ onClick: () => {
563
+ setQuery(option.label);
564
+ emit(option.value, option);
565
+ },
566
+ children: [
567
+ /* @__PURE__ */ jsx("span", { className: cn("fi-option-label", classNames?.optionLabel), children: renderOption ? renderOption(option) : option.label }),
568
+ optionMatches2(option, value) && /* @__PURE__ */ jsx("span", { className: "fi-selected-mark", children: "Selected" })
569
+ ]
570
+ },
571
+ stringifyValue2(option.value)
572
+ ))
573
+ ] }),
574
+ /* @__PURE__ */ jsx(SelectedValueDisplay, { options, value, display })
575
+ ] });
576
+ case "date":
577
+ case "datetime":
578
+ return /* @__PURE__ */ jsx(
579
+ "input",
580
+ {
581
+ className: commonInputClass,
582
+ type: config.type === "date" ? "date" : "datetime-local",
583
+ value: normalizeText(value),
584
+ placeholder: config.placeholder,
585
+ disabled,
586
+ required: behavior.required,
587
+ onChange: (event) => emit(event.target.value || null)
588
+ }
589
+ );
590
+ case "date-range":
591
+ case "datetime-range": {
592
+ const range = getDateRange(value);
593
+ const inputType = config.type === "date-range" ? "date" : "datetime-local";
594
+ return /* @__PURE__ */ jsxs("div", { className: cn("fi-range", classNames?.range), children: [
595
+ /* @__PURE__ */ jsx("input", { className: commonInputClass, type: inputType, value: range.from ?? "", disabled, onChange: (event) => emit({ ...range, from: event.target.value || void 0 }) }),
596
+ /* @__PURE__ */ jsx("input", { className: commonInputClass, type: inputType, value: range.to ?? "", disabled, onChange: (event) => emit({ ...range, to: event.target.value || void 0 }) })
597
+ ] });
598
+ }
599
+ case "range": {
600
+ const range = getNumberRange(value);
601
+ return /* @__PURE__ */ jsxs("div", { className: cn("fi-range", classNames?.range), children: [
602
+ /* @__PURE__ */ jsx("input", { className: commonInputClass, type: "number", value: range.min ?? "", placeholder: "Min", disabled, min: config.validation?.min, max: config.validation?.max, onChange: (event) => emit({ ...range, min: event.target.value === "" ? void 0 : Number(event.target.value) }) }),
603
+ /* @__PURE__ */ jsx("input", { className: commonInputClass, type: "number", value: range.max ?? "", placeholder: "Max", disabled, min: config.validation?.min, max: config.validation?.max, onChange: (event) => emit({ ...range, max: event.target.value === "" ? void 0 : Number(event.target.value) }) })
604
+ ] });
605
+ }
606
+ case "tag": {
607
+ const tags = toMultiValue(value).map(stringifyValue2);
608
+ const addTag = () => {
609
+ const nextTag = query.trim();
610
+ if (!nextTag) return;
611
+ if (!tags.includes(nextTag)) emit([...tags, nextTag]);
612
+ setQuery("");
613
+ };
614
+ return /* @__PURE__ */ jsxs(
615
+ "div",
616
+ {
617
+ className: cn(
618
+ "fi-tag-input",
619
+ classNames?.tagInput
620
+ ),
621
+ "data-disabled": disabled || void 0,
622
+ onClick: () => {
623
+ if (!disabled) {
624
+ const input = document.getElementById(`${config.key}-tag-input`);
625
+ input?.focus();
626
+ }
627
+ },
628
+ children: [
629
+ tags.map((tag) => /* @__PURE__ */ jsxs(
630
+ "span",
631
+ {
632
+ className: cn("fi-tag", classNames?.tag),
633
+ title: tag,
634
+ children: [
635
+ /* @__PURE__ */ jsx("span", { className: cn("fi-tag-label", classNames?.tagLabel), children: tag }),
636
+ /* @__PURE__ */ jsx(
637
+ "button",
638
+ {
639
+ type: "button",
640
+ className: cn("fi-tag-remove", classNames?.tagRemove),
641
+ "aria-label": `Remove ${tag}`,
642
+ disabled,
643
+ onClick: (event) => {
644
+ event.stopPropagation();
645
+ emit(tags.filter((item) => item !== tag));
646
+ },
647
+ children: /* @__PURE__ */ jsx(XIcon, {})
648
+ }
649
+ )
650
+ ]
651
+ },
652
+ tag
653
+ )),
654
+ /* @__PURE__ */ jsx(
655
+ "input",
656
+ {
657
+ id: `${config.key}-tag-input`,
658
+ className: cn("fi-tag-text-input", classNames?.tagTextInput),
659
+ value: query,
660
+ placeholder: tags.length ? "" : config.placeholder ?? "Add tag...",
661
+ disabled,
662
+ onChange: (event) => setQuery(event.target.value),
663
+ onKeyDown: (event) => {
664
+ if (event.key === "Enter") {
665
+ event.preventDefault();
666
+ addTag();
667
+ }
668
+ if (event.key === "Backspace" && !query && tags.length) {
669
+ emit(tags.slice(0, -1));
670
+ }
671
+ },
672
+ onBlur: addTag
673
+ }
674
+ )
675
+ ]
676
+ }
677
+ );
678
+ }
679
+ case "text":
680
+ default:
681
+ return /* @__PURE__ */ jsxs("div", { className: cn("fi-row", classNames?.row), children: [
682
+ /* @__PURE__ */ jsx(
683
+ "input",
684
+ {
685
+ className: commonInputClass,
686
+ type: "text",
687
+ value: normalizeText(value),
688
+ pattern: config.validation?.pattern,
689
+ placeholder: config.placeholder,
690
+ disabled,
691
+ required: behavior.required,
692
+ onChange: (event) => emit(event.target.value)
693
+ }
694
+ ),
695
+ clearButton
696
+ ] });
697
+ }
698
+ })();
699
+ return /* @__PURE__ */ jsxs("div", { className: cn("fi-field", classNames?.root, className), children: [
700
+ config.label && /* @__PURE__ */ jsxs("label", { className: cn("fi-field-label", classNames?.label), children: [
701
+ config.label,
702
+ behavior.required && /* @__PURE__ */ jsx("span", { className: "fi-field-required", children: " *" })
703
+ ] }),
704
+ control
705
+ ] });
706
+ }
707
+ function FilterVariable({
708
+ config,
709
+ value,
710
+ values,
711
+ onChange,
712
+ className,
713
+ labelClassName,
714
+ valueClassName,
715
+ classNames
716
+ }) {
717
+ const valueConfig = { ...config, label: void 0 };
718
+ const disabled = !!config.behavior?.disabled;
719
+ const label = config.label ?? config.key;
720
+ return /* @__PURE__ */ jsxs(
721
+ "div",
722
+ {
723
+ className: cn(
724
+ "fi-variable",
725
+ classNames?.root,
726
+ className
727
+ ),
728
+ "data-disabled": disabled || void 0,
729
+ children: [
730
+ label && /* @__PURE__ */ jsx(
731
+ "div",
732
+ {
733
+ className: cn(
734
+ "fi-variable-label",
735
+ classNames?.label,
736
+ labelClassName
737
+ ),
738
+ children: label
739
+ }
740
+ ),
741
+ /* @__PURE__ */ jsx("div", { className: cn("fi-variable-value", classNames?.value, valueClassName), children: /* @__PURE__ */ jsx(
742
+ FilterInput,
743
+ {
744
+ config: valueConfig,
745
+ value,
746
+ filters: values,
747
+ classNames: classNames?.input,
748
+ onChange
749
+ }
750
+ ) })
751
+ ]
752
+ }
753
+ );
754
+ }
755
+
756
+ export { FilterInput, FilterVariable };
757
+ //# sourceMappingURL=index.js.map
758
+ //# sourceMappingURL=index.js.map