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