@loykin/datetime-range 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,1627 @@
1
+ import React, { useState, useMemo, useCallback, useEffect, useRef } from 'react';
2
+ import { ChevronUpIcon, ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon, CalendarDays, Clock, CheckIcon } from 'lucide-react';
3
+ import { clsx } from 'clsx';
4
+ import { Button as Button$1 } from '@base-ui/react/button';
5
+ import { jsxs, jsx } from 'react/jsx-runtime';
6
+ import { Popover as Popover$1 } from '@base-ui/react/popover';
7
+ import { Tabs as Tabs$1 } from '@base-ui/react/tabs';
8
+ import { cva } from 'class-variance-authority';
9
+ import { Input as Input$1 } from '@base-ui/react/input';
10
+ import { Select as Select$1 } from '@base-ui/react/select';
11
+ import { HugeiconsIcon } from '@hugeicons/react';
12
+ import { UnfoldMoreIcon, Tick02Icon, ArrowUp01Icon, ArrowDown01Icon } from '@hugeicons/core-free-icons';
13
+ import { Switch as Switch$1 } from '@base-ui/react/switch';
14
+ import { Separator as Separator$1 } from '@base-ui/react/separator';
15
+ import { setYear, getYear, addMonths, subMonths, format, startOfYear, endOfYear, startOfMonth, endOfMonth, getMonth, setHours, endOfHour, startOfHour, setMinutes, endOfMinute, startOfMinute, setMilliseconds, setSeconds, startOfDay, endOfDay, addHours, subHours, parse } from 'date-fns';
16
+ import { TZDate, DayPicker } from 'react-day-picker';
17
+ import { setMonth } from 'date-fns/setMonth';
18
+ import { ScrollArea as ScrollArea$1 } from '@base-ui/react/scroll-area';
19
+ import * as chrono from 'chrono-node';
20
+
21
+ // src/DatetimeRange.tsx
22
+ function cn(...inputs) {
23
+ return clsx(inputs);
24
+ }
25
+ function buttonVariants({
26
+ variant = "default",
27
+ size = "default",
28
+ className
29
+ } = {}) {
30
+ return cn("dr-btn", className, variant && `dr-btn--${variant}`, size && `dr-btn--${size}`);
31
+ }
32
+ function Button({
33
+ className,
34
+ variant = "default",
35
+ size = "default",
36
+ ...props
37
+ }) {
38
+ return /* @__PURE__ */ jsx(
39
+ Button$1,
40
+ {
41
+ "data-slot": "button",
42
+ "data-variant": variant,
43
+ "data-size": size,
44
+ className: buttonVariants({ variant, size, className: typeof className === "string" ? className : void 0 }),
45
+ ...props
46
+ }
47
+ );
48
+ }
49
+ function Popover({ ...props }) {
50
+ return /* @__PURE__ */ jsx(Popover$1.Root, { "data-slot": "popover", ...props });
51
+ }
52
+ function PopoverTrigger({ ...props }) {
53
+ return /* @__PURE__ */ jsx(Popover$1.Trigger, { "data-slot": "popover-trigger", ...props });
54
+ }
55
+ function PopoverContent({
56
+ className,
57
+ align = "center",
58
+ alignOffset = 0,
59
+ side = "bottom",
60
+ sideOffset = 4,
61
+ disableAnchorTracking,
62
+ collisionAvoidance,
63
+ ...props
64
+ }) {
65
+ return /* @__PURE__ */ jsx(Popover$1.Portal, { children: /* @__PURE__ */ jsx(
66
+ Popover$1.Positioner,
67
+ {
68
+ align,
69
+ alignOffset,
70
+ side,
71
+ sideOffset,
72
+ disableAnchorTracking,
73
+ collisionAvoidance,
74
+ className: "isolate z-50",
75
+ children: /* @__PURE__ */ jsx(
76
+ Popover$1.Popup,
77
+ {
78
+ "data-slot": "popover-content",
79
+ className: cn(
80
+ "z-50 flex w-72 origin-(--transform-origin) flex-col gap-2.5 rounded-[var(--bk-radius)] bg-[var(--bk-popover)] p-2.5 text-xs text-[var(--bk-popover-foreground)] shadow-md ring-1 ring-[var(--bk-foreground)]/10 outline-hidden duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95",
81
+ className
82
+ ),
83
+ ...props
84
+ }
85
+ )
86
+ }
87
+ ) });
88
+ }
89
+ function Tabs({
90
+ className,
91
+ orientation = "horizontal",
92
+ ...props
93
+ }) {
94
+ return /* @__PURE__ */ jsx(
95
+ Tabs$1.Root,
96
+ {
97
+ "data-slot": "tabs",
98
+ "data-orientation": orientation,
99
+ className: cn("group/tabs flex gap-2 data-horizontal:flex-col", className),
100
+ ...props
101
+ }
102
+ );
103
+ }
104
+ var tabsListVariants = cva(
105
+ "group/tabs-list inline-flex w-fit items-center justify-center rounded-[var(--bk-radius)] p-[3px] text-[var(--bk-muted-foreground)] group-data-horizontal/tabs:h-8 group-data-vertical/tabs:h-fit group-data-vertical/tabs:flex-col data-[variant=line]:rounded-none",
106
+ {
107
+ variants: {
108
+ variant: {
109
+ default: "bg-[var(--bk-muted)]",
110
+ line: "gap-1 bg-transparent"
111
+ }
112
+ },
113
+ defaultVariants: {
114
+ variant: "default"
115
+ }
116
+ }
117
+ );
118
+ function TabsList({
119
+ className,
120
+ variant = "default",
121
+ ...props
122
+ }) {
123
+ return /* @__PURE__ */ jsx(
124
+ Tabs$1.List,
125
+ {
126
+ "data-slot": "tabs-list",
127
+ "data-variant": variant,
128
+ className: cn(tabsListVariants({ variant }), className),
129
+ ...props
130
+ }
131
+ );
132
+ }
133
+ function TabsTrigger({ className, ...props }) {
134
+ return /* @__PURE__ */ jsx(
135
+ Tabs$1.Tab,
136
+ {
137
+ "data-slot": "tabs-trigger",
138
+ className: cn(
139
+ "relative inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-[max(0px,calc(var(--bk-radius)-3px))] border border-transparent px-1.5 py-0.5 text-xs font-medium whitespace-nowrap text-[var(--bk-muted-foreground)] transition-all group-data-vertical/tabs:w-full group-data-vertical/tabs:justify-start group-data-vertical/tabs:py-[calc(--spacing(1.25))] hover:text-[var(--bk-foreground)] focus-visible:border-[var(--bk-ring)] focus-visible:ring-[3px] focus-visible:ring-[var(--bk-ring)]/50 focus-visible:outline-1 focus-visible:outline-[var(--bk-ring)] disabled:pointer-events-none disabled:opacity-50 has-data-[icon=inline-end]:pr-1 has-data-[icon=inline-start]:pl-1 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
140
+ "group-data-[variant=line]/tabs-list:bg-transparent group-data-[variant=line]/tabs-list:data-active:bg-transparent",
141
+ "data-active:bg-[var(--bk-background)] data-active:text-[var(--bk-foreground)]",
142
+ "after:absolute after:bg-[var(--bk-foreground)] after:opacity-0 after:transition-opacity group-data-horizontal/tabs:after:inset-x-0 group-data-horizontal/tabs:after:bottom-[-5px] group-data-horizontal/tabs:after:h-0.5 group-data-vertical/tabs:after:inset-y-0 group-data-vertical/tabs:after:-right-1 group-data-vertical/tabs:after:w-0.5 group-data-[variant=line]/tabs-list:data-active:after:opacity-100",
143
+ className
144
+ ),
145
+ ...props
146
+ }
147
+ );
148
+ }
149
+ function TabsContent({ className, ...props }) {
150
+ return /* @__PURE__ */ jsx(
151
+ Tabs$1.Panel,
152
+ {
153
+ "data-slot": "tabs-content",
154
+ className: cn("flex-1 text-xs/relaxed outline-none", className),
155
+ ...props
156
+ }
157
+ );
158
+ }
159
+ function Input({ className, type, ...props }) {
160
+ return /* @__PURE__ */ jsx(
161
+ Input$1,
162
+ {
163
+ type,
164
+ "data-slot": "input",
165
+ className: cn(
166
+ "dr-input",
167
+ className
168
+ ),
169
+ ...props
170
+ }
171
+ );
172
+ }
173
+ var Select = Select$1.Root;
174
+ function SelectValue({ className, ...props }) {
175
+ return /* @__PURE__ */ jsx(
176
+ Select$1.Value,
177
+ {
178
+ "data-slot": "select-value",
179
+ className: cn("flex flex-1 text-left", className),
180
+ ...props
181
+ }
182
+ );
183
+ }
184
+ function SelectTrigger({
185
+ className,
186
+ size = "default",
187
+ children,
188
+ ...props
189
+ }) {
190
+ return /* @__PURE__ */ jsxs(
191
+ Select$1.Trigger,
192
+ {
193
+ "data-slot": "select-trigger",
194
+ "data-size": size,
195
+ className: cn(
196
+ "flex w-fit items-center justify-between gap-1.5 rounded-[var(--bk-radius)] border border-[var(--bk-input)] bg-transparent py-2 pr-2 pl-2.5 text-xs whitespace-nowrap transition-colors outline-none select-none focus-visible:border-[var(--bk-ring)] focus-visible:ring-1 focus-visible:ring-[var(--bk-ring)]/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-[var(--bk-destructive)] aria-invalid:ring-1 aria-invalid:ring-[var(--bk-destructive)]/20 data-placeholder:text-[var(--bk-muted-foreground)] data-[size=default]:h-8 data-[size=sm]:h-7 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
197
+ className
198
+ ),
199
+ ...props,
200
+ children: [
201
+ children,
202
+ /* @__PURE__ */ jsx(
203
+ Select$1.Icon,
204
+ {
205
+ render: /* @__PURE__ */ jsx(
206
+ HugeiconsIcon,
207
+ {
208
+ icon: UnfoldMoreIcon,
209
+ strokeWidth: 2,
210
+ className: "pointer-events-none size-4 text-[var(--bk-muted-foreground)]"
211
+ }
212
+ )
213
+ }
214
+ )
215
+ ]
216
+ }
217
+ );
218
+ }
219
+ function SelectContent({
220
+ className,
221
+ children,
222
+ side = "bottom",
223
+ sideOffset = 4,
224
+ align = "center",
225
+ alignOffset = 0,
226
+ alignItemWithTrigger = true,
227
+ ...props
228
+ }) {
229
+ return /* @__PURE__ */ jsx(Select$1.Portal, { children: /* @__PURE__ */ jsx(
230
+ Select$1.Positioner,
231
+ {
232
+ side,
233
+ sideOffset,
234
+ align,
235
+ alignOffset,
236
+ alignItemWithTrigger,
237
+ className: "isolate z-50",
238
+ children: /* @__PURE__ */ jsxs(
239
+ Select$1.Popup,
240
+ {
241
+ "data-slot": "select-content",
242
+ "data-align-trigger": alignItemWithTrigger,
243
+ className: cn(
244
+ "isolate z-50 max-h-(--available-height) w-(--anchor-width) min-w-36 origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-[var(--bk-radius)] bg-[var(--bk-popover)] text-[var(--bk-popover-foreground)] shadow-md ring-1 ring-[var(--bk-foreground)]/10 duration-100 data-[align-trigger=true]:animate-none data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95 animate-none!",
245
+ className
246
+ ),
247
+ ...props,
248
+ children: [
249
+ /* @__PURE__ */ jsx(SelectScrollUpButton, {}),
250
+ /* @__PURE__ */ jsx(Select$1.List, { children }),
251
+ /* @__PURE__ */ jsx(SelectScrollDownButton, {})
252
+ ]
253
+ }
254
+ )
255
+ }
256
+ ) });
257
+ }
258
+ function SelectItem({
259
+ className,
260
+ children,
261
+ ...props
262
+ }) {
263
+ return /* @__PURE__ */ jsxs(
264
+ Select$1.Item,
265
+ {
266
+ "data-slot": "select-item",
267
+ className: cn(
268
+ "relative flex w-full cursor-default items-center gap-2 rounded-none py-2 pr-8 pl-2 text-xs outline-hidden select-none focus:bg-[var(--bk-accent)] focus:text-[var(--bk-accent-foreground)] data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
269
+ className
270
+ ),
271
+ ...props,
272
+ children: [
273
+ /* @__PURE__ */ jsx(Select$1.ItemText, { className: "flex flex-1 shrink-0 gap-2 whitespace-nowrap", children }),
274
+ /* @__PURE__ */ jsx(
275
+ Select$1.ItemIndicator,
276
+ {
277
+ render: /* @__PURE__ */ jsx("span", { className: "pointer-events-none absolute right-2 flex size-4 items-center justify-center" }),
278
+ children: /* @__PURE__ */ jsx(HugeiconsIcon, { icon: Tick02Icon, strokeWidth: 2, className: "pointer-events-none" })
279
+ }
280
+ )
281
+ ]
282
+ }
283
+ );
284
+ }
285
+ function SelectScrollUpButton({
286
+ className,
287
+ ...props
288
+ }) {
289
+ return /* @__PURE__ */ jsx(
290
+ Select$1.ScrollUpArrow,
291
+ {
292
+ "data-slot": "select-scroll-up-button",
293
+ className: cn(
294
+ "top-0 z-10 flex w-full cursor-default items-center justify-center bg-[var(--bk-popover)] py-1 [&_svg:not([class*='size-'])]:size-4",
295
+ className
296
+ ),
297
+ ...props,
298
+ children: /* @__PURE__ */ jsx(HugeiconsIcon, { icon: ArrowUp01Icon, strokeWidth: 2 })
299
+ }
300
+ );
301
+ }
302
+ function SelectScrollDownButton({
303
+ className,
304
+ ...props
305
+ }) {
306
+ return /* @__PURE__ */ jsx(
307
+ Select$1.ScrollDownArrow,
308
+ {
309
+ "data-slot": "select-scroll-down-button",
310
+ className: cn(
311
+ "bottom-0 z-10 flex w-full cursor-default items-center justify-center bg-[var(--bk-popover)] py-1 [&_svg:not([class*='size-'])]:size-4",
312
+ className
313
+ ),
314
+ ...props,
315
+ children: /* @__PURE__ */ jsx(HugeiconsIcon, { icon: ArrowDown01Icon, strokeWidth: 2 })
316
+ }
317
+ );
318
+ }
319
+ function Switch({
320
+ className,
321
+ size = "default",
322
+ ...props
323
+ }) {
324
+ return /* @__PURE__ */ jsx(
325
+ Switch$1.Root,
326
+ {
327
+ "data-slot": "switch",
328
+ "data-size": size,
329
+ className: cn(
330
+ "peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-transparent transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-[var(--bk-ring)] focus-visible:ring-1 focus-visible:ring-[var(--bk-ring)]/50 aria-invalid:border-[var(--bk-destructive)] aria-invalid:ring-1 aria-invalid:ring-[var(--bk-destructive)]/20 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] data-checked:bg-[var(--bk-primary)] data-unchecked:bg-[var(--bk-input)] data-disabled:cursor-not-allowed data-disabled:opacity-50",
331
+ className
332
+ ),
333
+ ...props,
334
+ children: /* @__PURE__ */ jsx(
335
+ Switch$1.Thumb,
336
+ {
337
+ "data-slot": "switch-thumb",
338
+ className: "pointer-events-none block rounded-full bg-[var(--bk-switch-thumb)] ring-0 transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0"
339
+ }
340
+ )
341
+ }
342
+ );
343
+ }
344
+ function Separator({
345
+ className,
346
+ orientation = "horizontal",
347
+ ...props
348
+ }) {
349
+ return /* @__PURE__ */ jsx(
350
+ Separator$1,
351
+ {
352
+ "data-slot": "separator",
353
+ orientation,
354
+ className: cn(
355
+ "shrink-0 bg-[var(--bk-border)] data-horizontal:h-px data-horizontal:w-full data-vertical:w-px data-vertical:self-stretch",
356
+ className
357
+ ),
358
+ ...props
359
+ }
360
+ );
361
+ }
362
+ function ScrollArea({
363
+ className,
364
+ style,
365
+ children,
366
+ viewportRef,
367
+ ...props
368
+ }) {
369
+ return /* @__PURE__ */ jsxs(
370
+ ScrollArea$1.Root,
371
+ {
372
+ "data-slot": "scroll-area",
373
+ style: { position: "relative", ...style },
374
+ className: cn("w-full", className),
375
+ ...props,
376
+ children: [
377
+ /* @__PURE__ */ jsx(
378
+ ScrollArea$1.Viewport,
379
+ {
380
+ ref: viewportRef,
381
+ "data-slot": "scroll-area-viewport",
382
+ className: "size-full outline-none rounded-[inherit]",
383
+ children
384
+ }
385
+ ),
386
+ /* @__PURE__ */ jsx(ScrollBar, { orientation: "vertical" }),
387
+ /* @__PURE__ */ jsx(ScrollBar, { orientation: "horizontal" }),
388
+ /* @__PURE__ */ jsx(ScrollArea$1.Corner, {})
389
+ ]
390
+ }
391
+ );
392
+ }
393
+ function ScrollBar({
394
+ className,
395
+ orientation = "vertical",
396
+ ...props
397
+ }) {
398
+ return /* @__PURE__ */ jsx(
399
+ ScrollArea$1.Scrollbar,
400
+ {
401
+ "data-slot": "scroll-area-scrollbar",
402
+ orientation,
403
+ className: cn(
404
+ "flex touch-none select-none p-px transition-opacity z-[11]",
405
+ orientation === "vertical" && "h-full w-2.5",
406
+ orientation === "horizontal" && "h-2.5 flex-col",
407
+ className
408
+ ),
409
+ ...props,
410
+ children: /* @__PURE__ */ jsx(
411
+ ScrollArea$1.Thumb,
412
+ {
413
+ "data-slot": "scroll-area-thumb",
414
+ className: "relative flex-1 rounded-full bg-[var(--bk-muted-foreground)]/30 hover:bg-[var(--bk-muted-foreground)]/50 transition-colors"
415
+ }
416
+ )
417
+ }
418
+ );
419
+ }
420
+ var AM_VALUE = 0;
421
+ var PM_VALUE = 1;
422
+ function MonthYearPicker({ value, minDate, maxDate, mode = "month", onChange, className }) {
423
+ const yearRef = useRef(null);
424
+ const years = useMemo(() => {
425
+ const result = [];
426
+ for (let i = 1912; i < 2100; i++) {
427
+ let disabled = false;
428
+ const startY = startOfYear(setYear(value, i));
429
+ const endY = endOfYear(setYear(value, i));
430
+ if (minDate && endY < minDate) disabled = true;
431
+ if (maxDate && startY > maxDate) disabled = true;
432
+ result.push({ value: i, label: i.toString(), disabled });
433
+ }
434
+ return result;
435
+ }, [value, minDate, maxDate]);
436
+ const months = useMemo(() => {
437
+ const result = [];
438
+ for (let i = 0; i < 12; i++) {
439
+ let disabled = false;
440
+ const startM = startOfMonth(setMonth(value, i));
441
+ const endM = endOfMonth(setMonth(value, i));
442
+ if (minDate && endM < minDate) disabled = true;
443
+ if (maxDate && startM > maxDate) disabled = true;
444
+ result.push({ value: i, label: format(new Date(0, i), "MMM"), disabled });
445
+ }
446
+ return result;
447
+ }, [value, minDate, maxDate]);
448
+ const onYearChange = useCallback(
449
+ (v) => {
450
+ let newDate = setYear(value, v.value);
451
+ if (minDate && newDate < minDate) newDate = setMonth(newDate, getMonth(minDate));
452
+ if (maxDate && newDate > maxDate) newDate = setMonth(newDate, getMonth(maxDate));
453
+ onChange(newDate, "year");
454
+ },
455
+ [onChange, value, minDate, maxDate]
456
+ );
457
+ useEffect(() => {
458
+ if (mode === "year") {
459
+ yearRef.current?.scrollIntoView({ behavior: "auto", block: "center" });
460
+ }
461
+ }, [mode, value]);
462
+ return /* @__PURE__ */ jsx("div", { className: cn(className), children: /* @__PURE__ */ jsxs(ScrollArea, { className: "h-full", children: [
463
+ mode === "year" && /* @__PURE__ */ jsx("div", { className: "grid grid-cols-4", children: years.map((year) => /* @__PURE__ */ jsx("div", { ref: year.value === getYear(value) ? yearRef : void 0, children: /* @__PURE__ */ jsx(
464
+ Button,
465
+ {
466
+ disabled: year.disabled,
467
+ variant: getYear(value) === year.value ? "default" : "ghost",
468
+ className: "rounded-none",
469
+ onClick: () => onYearChange(year),
470
+ children: year.label
471
+ }
472
+ ) }, year.value)) }),
473
+ mode === "month" && /* @__PURE__ */ jsx("div", { className: "grid grid-cols-3 gap-4 mt-7", children: months.map((month) => /* @__PURE__ */ jsx(
474
+ Button,
475
+ {
476
+ size: "lg",
477
+ disabled: month.disabled,
478
+ variant: getMonth(value) === month.value ? "default" : "ghost",
479
+ className: "rounded-none",
480
+ onClick: () => onChange(setMonth(value, month.value), "month"),
481
+ children: month.label
482
+ },
483
+ month.value
484
+ )) })
485
+ ] }) });
486
+ }
487
+ function TimeItem({ option, selected, onSelect, className, disabled }) {
488
+ return /* @__PURE__ */ jsxs(
489
+ Button,
490
+ {
491
+ variant: "ghost",
492
+ className: cn("flex justify-center px-1 pe-2 ps-1", className),
493
+ onClick: () => onSelect(option),
494
+ disabled,
495
+ children: [
496
+ /* @__PURE__ */ jsx("div", { className: "w-4", children: selected && /* @__PURE__ */ jsx(CheckIcon, { className: "my-auto size-4" }) }),
497
+ /* @__PURE__ */ jsx("span", { className: "ms-2", children: option.label })
498
+ ]
499
+ }
500
+ );
501
+ }
502
+ function buildTime({ use12HourFormat, value, formatStr, hour, minute, second, ampm }) {
503
+ if (use12HourFormat) {
504
+ const raw = format(value, formatStr);
505
+ let s = raw.slice(0, 11) + hour.toString().padStart(2, "0") + raw.slice(13);
506
+ s = s.slice(0, 14) + minute.toString().padStart(2, "0") + s.slice(16);
507
+ s = s.slice(0, 17) + second.toString().padStart(2, "0") + s.slice(19);
508
+ s = s.slice(0, 24) + (ampm === AM_VALUE ? "AM" : "PM") + s.slice(26);
509
+ return parse(s, formatStr, value);
510
+ }
511
+ return setHours(setMinutes(setSeconds(value, second), minute), hour);
512
+ }
513
+ function TimePicker({ value, onChange, use12HourFormat, min, max, timePicker }) {
514
+ const formatStr = useMemo(
515
+ () => use12HourFormat ? "yyyy-MM-dd hh:mm:ss.SSS a xxxx" : "yyyy-MM-dd HH:mm:ss.SSS xxxx",
516
+ [use12HourFormat]
517
+ );
518
+ const [ampm, setAmpm] = useState(format(value, "a") === "AM" ? AM_VALUE : PM_VALUE);
519
+ const [hour, setHour] = useState(use12HourFormat ? +format(value, "hh") : value.getHours());
520
+ const [minute, setMinute] = useState(value.getMinutes());
521
+ const [second, setSecond] = useState(value.getSeconds());
522
+ useEffect(() => {
523
+ onChange(buildTime({ use12HourFormat, value, formatStr, hour, minute, second, ampm }));
524
+ }, [hour, minute, second, ampm, formatStr, use12HourFormat]);
525
+ const hourIn24h = useMemo(
526
+ () => use12HourFormat ? hour % 12 + ampm * 12 : hour,
527
+ [hour, use12HourFormat, ampm]
528
+ );
529
+ const hours = useMemo(
530
+ () => Array.from({ length: use12HourFormat ? 12 : 24 }, (_, i) => {
531
+ const hourValue = use12HourFormat ? i === 0 ? 12 : i : i;
532
+ const hDate = setHours(value, use12HourFormat ? i + ampm * 12 : i);
533
+ let disabled = false;
534
+ if (min && endOfHour(hDate) < min) disabled = true;
535
+ if (max && startOfHour(hDate) > max) disabled = true;
536
+ return { value: hourValue, label: hourValue.toString().padStart(2, "0"), disabled };
537
+ }),
538
+ [value, min, max, use12HourFormat, ampm]
539
+ );
540
+ const minutes = useMemo(() => {
541
+ const anchor = setHours(value, hourIn24h);
542
+ return Array.from({ length: 60 }, (_, i) => {
543
+ const mDate = setMinutes(anchor, i);
544
+ let disabled = false;
545
+ if (min && endOfMinute(mDate) < min) disabled = true;
546
+ if (max && startOfMinute(mDate) > max) disabled = true;
547
+ return { value: i, label: i.toString().padStart(2, "0"), disabled };
548
+ });
549
+ }, [value, min, max, hourIn24h]);
550
+ const seconds = useMemo(() => {
551
+ const anchor = setMilliseconds(setMinutes(setHours(value, hourIn24h), minute), 0);
552
+ const _min = min ? setMilliseconds(min, 0) : void 0;
553
+ const _max = max ? setMilliseconds(max, 0) : void 0;
554
+ return Array.from({ length: 60 }, (_, i) => {
555
+ const sDate = setSeconds(anchor, i);
556
+ let disabled = false;
557
+ if (_min && sDate < _min) disabled = true;
558
+ if (_max && sDate > _max) disabled = true;
559
+ return { value: i, label: i.toString().padStart(2, "0"), disabled };
560
+ });
561
+ }, [value, minute, min, max, hourIn24h]);
562
+ const ampmOptions = useMemo(() => {
563
+ const startD = startOfDay(value);
564
+ const endD = endOfDay(value);
565
+ return [
566
+ { value: AM_VALUE, label: "AM" },
567
+ { value: PM_VALUE, label: "PM" }
568
+ ].map((v) => {
569
+ let disabled = false;
570
+ const start = addHours(startD, v.value * 12);
571
+ const end = subHours(endD, (1 - v.value) * 12);
572
+ if (min && end < min) disabled = true;
573
+ if (max && start > max) disabled = true;
574
+ return { ...v, disabled };
575
+ });
576
+ }, [value, min, max]);
577
+ const [open, setOpen] = useState(false);
578
+ const hourViewportRef = useRef(null);
579
+ const minuteViewportRef = useRef(null);
580
+ const secondViewportRef = useRef(null);
581
+ useEffect(() => {
582
+ const id = setTimeout(() => {
583
+ if (open) {
584
+ const ITEM_H = 32;
585
+ const hourIdx = hours.findIndex((h) => h.value === hour);
586
+ if (hourViewportRef.current) hourViewportRef.current.scrollTop = hourIdx * ITEM_H;
587
+ if (minuteViewportRef.current) minuteViewportRef.current.scrollTop = minute * ITEM_H;
588
+ if (secondViewportRef.current) secondViewportRef.current.scrollTop = second * ITEM_H;
589
+ }
590
+ }, 1);
591
+ return () => clearTimeout(id);
592
+ }, [open, hours, hour, minute, second]);
593
+ const onHourChange = useCallback(
594
+ (v) => {
595
+ if (min) {
596
+ const t = buildTime({ use12HourFormat, value, formatStr, hour: v.value, minute, second, ampm });
597
+ if (t < min) {
598
+ setMinute(min.getMinutes());
599
+ setSecond(min.getSeconds());
600
+ }
601
+ }
602
+ if (max) {
603
+ const t = buildTime({ use12HourFormat, value, formatStr, hour: v.value, minute, second, ampm });
604
+ if (t > max) {
605
+ setMinute(max.getMinutes());
606
+ setSecond(max.getSeconds());
607
+ }
608
+ }
609
+ setHour(v.value);
610
+ },
611
+ [use12HourFormat, value, formatStr, minute, second, ampm, min, max]
612
+ );
613
+ const onMinuteChange = useCallback(
614
+ (v) => {
615
+ if (min) {
616
+ const t = buildTime({ use12HourFormat, value, formatStr, hour, minute: v.value, second, ampm });
617
+ if (t < min) setSecond(min.getSeconds());
618
+ }
619
+ if (max) {
620
+ const t = buildTime({ use12HourFormat, value, formatStr, hour, minute: v.value, second, ampm });
621
+ if (t > max) setSecond(max.getSeconds());
622
+ }
623
+ setMinute(v.value);
624
+ },
625
+ [use12HourFormat, value, formatStr, hour, second, ampm, min, max]
626
+ );
627
+ const onAmpmChange = useCallback(
628
+ (v) => {
629
+ if (min) {
630
+ const t = buildTime({ use12HourFormat, value, formatStr, hour, minute, second, ampm: v.value });
631
+ if (t < min) {
632
+ const h = min.getHours() % 12;
633
+ setHour(h === 0 ? 12 : h);
634
+ setMinute(min.getMinutes());
635
+ setSecond(min.getSeconds());
636
+ }
637
+ }
638
+ if (max) {
639
+ const t = buildTime({ use12HourFormat, value, formatStr, hour, minute, second, ampm: v.value });
640
+ if (t > max) {
641
+ const h = max.getHours() % 12;
642
+ setHour(h === 0 ? 12 : h);
643
+ setMinute(max.getMinutes());
644
+ setSecond(max.getSeconds());
645
+ }
646
+ }
647
+ setAmpm(v.value);
648
+ },
649
+ [use12HourFormat, value, formatStr, hour, minute, second, min, max]
650
+ );
651
+ const display = useMemo(() => {
652
+ const parts = [];
653
+ for (const el of ["hour", "minute", "second"]) {
654
+ if (!timePicker || timePicker[el]) {
655
+ parts.push(el === "hour" ? use12HourFormat ? "hh" : "HH" : el === "minute" ? "mm" : "ss");
656
+ }
657
+ }
658
+ return format(value, parts.join(":") + (use12HourFormat ? " a" : ""));
659
+ }, [value, use12HourFormat, timePicker]);
660
+ return /* @__PURE__ */ jsxs(Popover, { open, onOpenChange: setOpen, children: [
661
+ /* @__PURE__ */ jsx(
662
+ PopoverTrigger,
663
+ {
664
+ render: (props) => /* @__PURE__ */ jsxs(
665
+ Button,
666
+ {
667
+ ...props,
668
+ variant: "ghost",
669
+ role: "combobox",
670
+ "aria-label": "Select time",
671
+ "aria-expanded": open,
672
+ className: "w-full justify-between px-2",
673
+ children: [
674
+ /* @__PURE__ */ jsx(Clock, { className: "mr-2 size-4" }),
675
+ display,
676
+ /* @__PURE__ */ jsx(ChevronDownIcon, { className: "ml-2 size-4 shrink-0 opacity-50" })
677
+ ]
678
+ }
679
+ )
680
+ }
681
+ ),
682
+ /* @__PURE__ */ jsx(PopoverContent, { className: "p-0", side: "top", children: /* @__PURE__ */ jsx("div", { className: "flex-col gap-2 p-2", children: /* @__PURE__ */ jsxs("div", { className: "flex h-56 grow", children: [
683
+ (!timePicker || timePicker.hour) && /* @__PURE__ */ jsx(ScrollArea, { className: "h-full grow", viewportRef: hourViewportRef, children: /* @__PURE__ */ jsx("div", { className: "flex grow flex-col items-stretch overflow-y-auto pe-2 pb-48", children: hours.map((v) => /* @__PURE__ */ jsx(TimeItem, { option: v, selected: v.value === hour, onSelect: onHourChange, className: "h-8", disabled: v.disabled }, v.value)) }) }),
684
+ (!timePicker || timePicker.minute) && /* @__PURE__ */ jsx(ScrollArea, { className: "h-full grow", viewportRef: minuteViewportRef, children: /* @__PURE__ */ jsx("div", { className: "flex grow flex-col items-stretch overflow-y-auto pe-2 pb-48", children: minutes.map((v) => /* @__PURE__ */ jsx(TimeItem, { option: v, selected: v.value === minute, onSelect: onMinuteChange, className: "h-8", disabled: v.disabled }, v.value)) }) }),
685
+ (!timePicker || timePicker.second) && /* @__PURE__ */ jsx(ScrollArea, { className: "h-full grow", viewportRef: secondViewportRef, children: /* @__PURE__ */ jsx("div", { className: "flex grow flex-col items-stretch overflow-y-auto pe-2 pb-48", children: seconds.map((v) => /* @__PURE__ */ jsx(TimeItem, { option: v, selected: v.value === second, onSelect: (v2) => setSecond(v2.value), className: "h-8", disabled: v.disabled }, v.value)) }) }),
686
+ use12HourFormat && /* @__PURE__ */ jsx(ScrollArea, { className: "h-full grow", children: /* @__PURE__ */ jsx("div", { className: "flex grow flex-col items-stretch overflow-y-auto pe-2", children: ampmOptions.map((v) => /* @__PURE__ */ jsx(TimeItem, { option: v, selected: v.value === ampm, onSelect: onAmpmChange, className: "h-8", disabled: v.disabled }, v.value)) }) })
687
+ ] }) }) })
688
+ ] });
689
+ }
690
+ function precisionToTimePicker(precision) {
691
+ if (precision === "date") return void 0;
692
+ return {
693
+ hour: true,
694
+ minute: precision === "minute" || precision === "second",
695
+ second: precision === "second"
696
+ };
697
+ }
698
+ function DateTimePanel({
699
+ value,
700
+ onChangeAction,
701
+ min,
702
+ max,
703
+ timezone,
704
+ precision = "second",
705
+ use12HourFormat,
706
+ immediate = false,
707
+ isError,
708
+ compareValue,
709
+ title,
710
+ validateDateRange,
711
+ getRangeErrorMessage,
712
+ ...props
713
+ }) {
714
+ const [monthYearPicker, setMonthYearPicker] = useState(false);
715
+ const initDate = useMemo(() => new TZDate(value || /* @__PURE__ */ new Date(), timezone), [value, timezone]);
716
+ const [month, setMonth] = useState(initDate);
717
+ const [date, setDate] = useState(initDate);
718
+ const [errorType, setErrorType] = useState(null);
719
+ const [previousDate, setPreviousDate] = useState(initDate);
720
+ const endMonth = useMemo(() => setYear(month, getYear(month) + 1), [month]);
721
+ const minDate = useMemo(() => min ? new TZDate(min, timezone) : void 0, [min, timezone]);
722
+ const maxDate = useMemo(() => max ? new TZDate(max, timezone) : void 0, [max, timezone]);
723
+ const validate = useCallback(
724
+ (cur, cmp) => {
725
+ if (validateDateRange) return validateDateRange(cur, cmp);
726
+ if (!cmp) return { isValid: true, errorType: null };
727
+ if (title === "Start Date" && cur >= cmp) return { isValid: false, errorType: "validation" };
728
+ if (title === "End Date" && cur <= cmp) return { isValid: false, errorType: "validation" };
729
+ return { isValid: true, errorType: null };
730
+ },
731
+ [validateDateRange, title]
732
+ );
733
+ const onDayChanged = useCallback(
734
+ (d) => {
735
+ d.setHours(date.getHours(), date.getMinutes(), date.getSeconds());
736
+ if (min && d < min) d.setHours(min.getHours(), min.getMinutes(), min.getSeconds());
737
+ if (max && d > max) d.setHours(max.getHours(), max.getMinutes(), max.getSeconds());
738
+ setDate(d);
739
+ if (immediate) onChangeAction(new Date(d), true);
740
+ },
741
+ [date, immediate, onChangeAction, min, max]
742
+ );
743
+ const onTimeChanged = useCallback(
744
+ (d) => {
745
+ setDate(d);
746
+ if (immediate) onChangeAction(new Date(d), true);
747
+ },
748
+ [immediate, onChangeAction]
749
+ );
750
+ const onSubmit = useCallback(() => {
751
+ const result = validate(date, compareValue);
752
+ setErrorType(result.isValid ? null : result.errorType);
753
+ if (result.isValid) {
754
+ onChangeAction(new Date(date), true);
755
+ setPreviousDate(new Date(date));
756
+ }
757
+ }, [date, onChangeAction, compareValue, validate]);
758
+ const onCancel = useCallback(() => {
759
+ setDate(previousDate);
760
+ setMonth(previousDate);
761
+ setErrorType(null);
762
+ onChangeAction(void 0, false);
763
+ }, [previousDate, onChangeAction]);
764
+ const onMonthYearChanged = useCallback((d, mode) => {
765
+ setMonth(d);
766
+ setMonthYearPicker(mode === "year" ? "month" : false);
767
+ }, []);
768
+ const onNextMonth = useCallback(() => setMonth(addMonths(month, 1)), [month]);
769
+ const onPrevMonth = useCallback(() => setMonth(subMonths(month, 1)), [month]);
770
+ useEffect(() => {
771
+ setDate(initDate);
772
+ setMonth(initDate);
773
+ setMonthYearPicker(false);
774
+ setPreviousDate(initDate);
775
+ }, [initDate]);
776
+ return /* @__PURE__ */ jsxs("div", { children: [
777
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between px-3 pt-3", children: [
778
+ /* @__PURE__ */ jsxs("div", { className: "text-md font-bold flex gap-2 items-center cursor-pointer", children: [
779
+ /* @__PURE__ */ jsxs("div", { children: [
780
+ /* @__PURE__ */ jsx("span", { onClick: () => setMonthYearPicker(monthYearPicker === "month" ? false : "month"), children: format(month, "MMMM") }),
781
+ /* @__PURE__ */ jsx("span", { className: "ms-1", onClick: () => setMonthYearPicker(monthYearPicker === "year" ? false : "year"), children: format(month, "yyyy") })
782
+ ] }),
783
+ /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "icon", className: "w-7 h-7", onClick: () => setMonthYearPicker(monthYearPicker ? false : "year"), children: monthYearPicker ? /* @__PURE__ */ jsx(ChevronUpIcon, { className: "w-4 h-4 stroke-muted-foreground" }) : /* @__PURE__ */ jsx(ChevronDownIcon, { className: "w-4 h-4 stroke-muted-foreground" }) })
784
+ ] }),
785
+ /* @__PURE__ */ jsxs("div", { className: cn("flex space-x-2", monthYearPicker ? "hidden" : ""), children: [
786
+ /* @__PURE__ */ jsx(
787
+ Button,
788
+ {
789
+ variant: "ghost",
790
+ size: "icon",
791
+ className: "w-7 h-7",
792
+ "aria-label": "Previous month",
793
+ onClick: onPrevMonth,
794
+ children: /* @__PURE__ */ jsx(ChevronLeftIcon, { className: "w-4 h-4 stroke-muted-foreground" })
795
+ }
796
+ ),
797
+ /* @__PURE__ */ jsx(
798
+ Button,
799
+ {
800
+ variant: "ghost",
801
+ size: "icon",
802
+ className: "w-7 h-7",
803
+ "aria-label": "Next month",
804
+ onClick: onNextMonth,
805
+ children: /* @__PURE__ */ jsx(ChevronRightIcon, { className: "w-4 h-4 stroke-muted-foreground" })
806
+ }
807
+ )
808
+ ] })
809
+ ] }),
810
+ /* @__PURE__ */ jsxs("div", { className: "relative overflow-hidden px-3", children: [
811
+ /* @__PURE__ */ jsx(
812
+ DayPicker,
813
+ {
814
+ timeZone: timezone,
815
+ mode: "single",
816
+ selected: date,
817
+ modifiers: {
818
+ selected: (sel) => {
819
+ const a = new Date(sel);
820
+ const b = new Date(date);
821
+ return a.setHours(0, 0, 0, 0) === b.setHours(0, 0, 0, 0);
822
+ },
823
+ range_middle: (cur) => {
824
+ const d = new Date(date);
825
+ const c = new Date(cur);
826
+ return !!max && cur < max && c > d || !!min && cur > min && c < d;
827
+ }
828
+ },
829
+ onSelect: (d) => d && onDayChanged(d),
830
+ month,
831
+ endMonth,
832
+ disabled: [max ? { after: max } : null, min ? { before: min } : null].filter(Boolean),
833
+ onMonthChange: setMonth,
834
+ classNames: {
835
+ dropdowns: "flex w-full gap-2",
836
+ months: "flex w-full h-fit",
837
+ month: "flex flex-col w-full",
838
+ month_caption: "hidden",
839
+ button_previous: "hidden",
840
+ button_next: "hidden",
841
+ month_grid: "w-full border-collapse",
842
+ weekdays: "flex justify-between mt-2",
843
+ weekday: "text-[var(--bk-muted-foreground)] rounded-md w-9 font-normal text-[0.8rem]",
844
+ week: "flex w-full justify-between mt-2",
845
+ day: "h-9 w-9 text-center text-sm p-0 relative flex items-center justify-center focus-within:relative focus-within:z-20 [&[aria-selected]>button:hover]:bg-[var(--bk-primary)] [&[aria-selected]>button:hover]:text-[var(--bk-primary-foreground)]",
846
+ day_button: cn(buttonVariants({ variant: "ghost" }), "size-9 p-0 font-normal"),
847
+ range_end: "day-range-end bg-[var(--bk-primary)]",
848
+ selected: "bg-[var(--bk-primary)] text-[var(--bk-primary-foreground)] hover:bg-[var(--bk-primary)] hover:text-[var(--bk-primary-foreground)] focus:bg-[var(--bk-primary)] focus:text-[var(--bk-primary-foreground)]",
849
+ today: "bg-[var(--bk-accent)] text-[var(--bk-accent-foreground)]",
850
+ outside: "day-outside text-[var(--bk-muted-foreground)]/80 aria-selected:bg-[var(--bk-primary)] aria-selected:text-[var(--bk-primary-foreground)]",
851
+ disabled: "text-[var(--bk-muted-foreground)] opacity-50",
852
+ range_middle: "w-full bg-[var(--bk-secondary)] aria-selected:bg-[var(--bk-primary)] aria-selected:text-[var(--bk-primary-foreground)]",
853
+ hidden: "invisible"
854
+ },
855
+ showOutsideDays: true,
856
+ ...props
857
+ }
858
+ ),
859
+ /* @__PURE__ */ jsx("div", { className: cn("absolute top-0 left-0 bottom-0 right-0", monthYearPicker ? "bg-[var(--bk-popover)]" : "hidden") }),
860
+ /* @__PURE__ */ jsx(
861
+ MonthYearPicker,
862
+ {
863
+ value: month,
864
+ mode: monthYearPicker,
865
+ onChange: onMonthYearChanged,
866
+ minDate,
867
+ maxDate,
868
+ className: cn("absolute top-0 left-0 bottom-0 right-0", monthYearPicker ? "" : "hidden")
869
+ }
870
+ )
871
+ ] }),
872
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3 px-3 pb-2", children: [
873
+ precision !== "date" && /* @__PURE__ */ jsx("div", { className: "mt-2 border-t border-[var(--bk-border)] pt-2", children: /* @__PURE__ */ jsx(
874
+ TimePicker,
875
+ {
876
+ timePicker: precisionToTimePicker(precision),
877
+ value: date,
878
+ onChange: onTimeChanged,
879
+ use12HourFormat,
880
+ min: minDate,
881
+ max: maxDate
882
+ }
883
+ ) }),
884
+ (isError || errorType !== null) && /* @__PURE__ */ jsx("div", { className: "py-3 px-4 border border-[var(--bk-destructive)] max-w-full", children: /* @__PURE__ */ jsx("span", { className: "text-xs text-[var(--bk-destructive)] leading-snug block", children: getRangeErrorMessage?.(errorType, title) ?? "Invalid date range." }) }),
885
+ !immediate && /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-2", children: [
886
+ /* @__PURE__ */ jsx(Button, { variant: "outline", size: "sm", onClick: onCancel, children: "Cancel" }),
887
+ /* @__PURE__ */ jsx(Button, { size: "sm", onClick: onSubmit, children: "Done" })
888
+ ] })
889
+ ] })
890
+ ] });
891
+ }
892
+ var SEGMENTS = [
893
+ { key: "year", min: 1900, max: 2099, digits: 4, label: "YYYY" },
894
+ { key: "month", min: 1, max: 12, digits: 2, label: "MM" },
895
+ { key: "day", min: 1, max: 31, digits: 2, label: "DD" },
896
+ { key: "hour", min: 0, max: 23, digits: 2, label: "HH" },
897
+ { key: "minute", min: 0, max: 59, digits: 2, label: "mm" },
898
+ { key: "second", min: 0, max: 59, digits: 2, label: "ss" }
899
+ ];
900
+ function getSegmentValue(date, key) {
901
+ switch (key) {
902
+ case "year":
903
+ return date.getFullYear();
904
+ case "month":
905
+ return date.getMonth() + 1;
906
+ case "day":
907
+ return date.getDate();
908
+ case "hour":
909
+ return date.getHours();
910
+ case "minute":
911
+ return date.getMinutes();
912
+ case "second":
913
+ return date.getSeconds();
914
+ }
915
+ }
916
+ function applySegment(date, key, value) {
917
+ const d = new Date(date);
918
+ switch (key) {
919
+ case "year":
920
+ d.setFullYear(value);
921
+ break;
922
+ case "month":
923
+ d.setMonth(value - 1);
924
+ break;
925
+ case "day":
926
+ d.setDate(value);
927
+ break;
928
+ case "hour":
929
+ d.setHours(value);
930
+ break;
931
+ case "minute":
932
+ d.setMinutes(value);
933
+ break;
934
+ case "second":
935
+ d.setSeconds(value);
936
+ break;
937
+ }
938
+ return d;
939
+ }
940
+ function clamp(value, min, max) {
941
+ return Math.max(min, Math.min(max, value));
942
+ }
943
+ function DatetimeSegmentInput({
944
+ value,
945
+ onChange,
946
+ disabled = false,
947
+ className,
948
+ isError = false,
949
+ precision = "second"
950
+ }) {
951
+ const containerRef = useRef(null);
952
+ const spanRefs = useRef({
953
+ year: null,
954
+ month: null,
955
+ day: null,
956
+ hour: null,
957
+ minute: null,
958
+ second: null
959
+ });
960
+ const [typing, setTyping] = useState({
961
+ year: null,
962
+ month: null,
963
+ day: null,
964
+ hour: null,
965
+ minute: null,
966
+ second: null
967
+ });
968
+ const date = useMemo(
969
+ () => !Number.isNaN(value.getTime()) ? value : /* @__PURE__ */ new Date(),
970
+ [value]
971
+ );
972
+ const focusSegment = useCallback((key) => {
973
+ spanRefs.current[key]?.focus();
974
+ }, []);
975
+ const commitTyping = useCallback((key, buf) => {
976
+ const num = parseInt(buf, 10);
977
+ const seg = SEGMENTS.find((s) => s.key === key);
978
+ if (!Number.isNaN(num)) {
979
+ const clamped = clamp(num, seg.min, seg.max);
980
+ onChange(applySegment(date, key, clamped));
981
+ }
982
+ setTyping((prev) => ({ ...prev, [key]: null }));
983
+ }, [date, onChange]);
984
+ const activeSegments = useMemo(() => {
985
+ const count = precision === "date" ? 3 : precision === "hour" ? 4 : precision === "minute" ? 5 : 6;
986
+ return SEGMENTS.slice(0, count);
987
+ }, [precision]);
988
+ const handleKeyDown = useCallback((e, seg) => {
989
+ if (disabled) return;
990
+ const { key } = seg;
991
+ const idx = activeSegments.findIndex((s) => s.key === key);
992
+ const cur = typing[key];
993
+ if (e.key === "ArrowUp" || e.key === "ArrowDown") {
994
+ e.preventDefault();
995
+ if (cur !== null) commitTyping(key, cur);
996
+ const delta = e.key === "ArrowUp" ? 1 : -1;
997
+ const current = getSegmentValue(date, key);
998
+ const next = clamp(current + delta, seg.min, seg.max);
999
+ onChange(applySegment(date, key, next));
1000
+ return;
1001
+ }
1002
+ if (e.key === "ArrowLeft" || e.key === "Tab" && e.shiftKey) {
1003
+ e.preventDefault();
1004
+ if (cur !== null) commitTyping(key, cur);
1005
+ if (idx > 0) focusSegment(activeSegments[idx - 1].key);
1006
+ return;
1007
+ }
1008
+ if (e.key === "ArrowRight" || e.key === "Tab" && !e.shiftKey) {
1009
+ e.preventDefault();
1010
+ if (cur !== null) commitTyping(key, cur);
1011
+ if (idx < activeSegments.length - 1) focusSegment(activeSegments[idx + 1].key);
1012
+ return;
1013
+ }
1014
+ if (e.key === "Backspace" || e.key === "Delete") {
1015
+ e.preventDefault();
1016
+ setTyping((prev) => ({ ...prev, [key]: null }));
1017
+ return;
1018
+ }
1019
+ if (/^\d$/.test(e.key)) {
1020
+ e.preventDefault();
1021
+ const next = (cur === null ? "" : cur) + e.key;
1022
+ if (next.length >= seg.digits) {
1023
+ commitTyping(key, next);
1024
+ if (idx < activeSegments.length - 1) {
1025
+ setTimeout(() => focusSegment(activeSegments[idx + 1].key), 0);
1026
+ }
1027
+ } else {
1028
+ setTyping((prev) => ({ ...prev, [key]: next }));
1029
+ }
1030
+ return;
1031
+ }
1032
+ }, [disabled, typing, date, onChange, commitTyping, focusSegment, activeSegments]);
1033
+ const handleBlur = useCallback((key) => {
1034
+ const buf = typing[key];
1035
+ if (buf !== null) commitTyping(key, buf);
1036
+ }, [typing, commitTyping]);
1037
+ const displayValue = (seg) => {
1038
+ const buf = typing[seg.key];
1039
+ if (buf !== null) return buf.padStart(seg.digits, "_").slice(-seg.digits);
1040
+ const val = getSegmentValue(date, seg.key);
1041
+ return String(val).padStart(seg.digits, "0");
1042
+ };
1043
+ return /* @__PURE__ */ jsx(
1044
+ "div",
1045
+ {
1046
+ ref: containerRef,
1047
+ className: cn(
1048
+ "inline-flex h-8 px-2.5 rounded-[var(--bk-radius)] border border-[var(--bk-input)] bg-transparent text-xs transition-colors",
1049
+ "focus-within:border-[var(--bk-ring)] focus-within:ring-1 focus-within:ring-[var(--bk-ring)]/50 items-center",
1050
+ isError && "border-[var(--bk-destructive)] ring-1 ring-[var(--bk-destructive)]/20",
1051
+ disabled && "opacity-50 cursor-not-allowed pointer-events-none bg-[var(--bk-input)]/50",
1052
+ className
1053
+ ),
1054
+ onMouseDown: (e) => {
1055
+ const target = e.target;
1056
+ if (target.tagName === "SPAN") {
1057
+ e.preventDefault();
1058
+ focusSegment(precision === "date" ? "year" : "hour");
1059
+ }
1060
+ },
1061
+ children: activeSegments.map((seg, idx) => {
1062
+ const sep = idx === 0 ? null : idx === 3 ? "\xA0" : idx < 3 ? "-" : ":";
1063
+ const isFocused = typing[seg.key] !== null;
1064
+ return /* @__PURE__ */ jsxs(React.Fragment, { children: [
1065
+ sep !== null && /* @__PURE__ */ jsx("span", { className: "text-[var(--bk-muted-foreground)] select-none items-center", "aria-hidden": true, children: sep }),
1066
+ /* @__PURE__ */ jsx(
1067
+ "div",
1068
+ {
1069
+ ref: (el) => {
1070
+ spanRefs.current[seg.key] = el;
1071
+ },
1072
+ role: "spinbutton",
1073
+ "aria-label": seg.label,
1074
+ "aria-valuenow": getSegmentValue(date, seg.key),
1075
+ "aria-valuemin": seg.min,
1076
+ "aria-valuemax": seg.max,
1077
+ tabIndex: disabled ? -1 : 0,
1078
+ className: cn(
1079
+ "outline-none rounded-sm px-1 cursor-default select-none",
1080
+ "focus:bg-[var(--bk-primary)] focus:text-[var(--bk-primary-foreground)]",
1081
+ isFocused && "bg-[var(--bk-primary)]/20"
1082
+ ),
1083
+ style: { height: "24px", lineHeight: "24px" },
1084
+ onKeyDown: (e) => handleKeyDown(e, seg),
1085
+ onBlur: () => handleBlur(seg.key),
1086
+ children: displayValue(seg)
1087
+ }
1088
+ )
1089
+ ] }, seg.key);
1090
+ })
1091
+ }
1092
+ );
1093
+ }
1094
+
1095
+ // src/types.ts
1096
+ var DEFAULT_LABELS = {
1097
+ start: "Start",
1098
+ end: "End",
1099
+ quickRanges: "Quick ranges",
1100
+ apply: "Apply",
1101
+ cancel: "Cancel",
1102
+ absolute: "Absolute",
1103
+ relative: "Relative",
1104
+ amount: "Amount",
1105
+ now: "Now",
1106
+ nowDescription: "Set to current time"
1107
+ };
1108
+ var RELATIVE_FORMATS = [
1109
+ "Seconds ago",
1110
+ "Minutes ago",
1111
+ "Hours ago",
1112
+ "Days ago",
1113
+ "Weeks ago",
1114
+ "Months ago"
1115
+ ];
1116
+ var QUICK_PRESETS = [
1117
+ { label: "Last 5 minutes", start: { type: "relative", relativeValue: "5", relativeFormat: "Minutes ago" }, end: { type: "relative", relativeNow: true } },
1118
+ { label: "Last 15 minutes", start: { type: "relative", relativeValue: "15", relativeFormat: "Minutes ago" }, end: { type: "relative", relativeNow: true } },
1119
+ { label: "Last 30 minutes", start: { type: "relative", relativeValue: "30", relativeFormat: "Minutes ago" }, end: { type: "relative", relativeNow: true } },
1120
+ { label: "Last 1 hour", start: { type: "relative", relativeValue: "1", relativeFormat: "Hours ago" }, end: { type: "relative", relativeNow: true } },
1121
+ { label: "Last 3 hours", start: { type: "relative", relativeValue: "3", relativeFormat: "Hours ago" }, end: { type: "relative", relativeNow: true } },
1122
+ { label: "Last 6 hours", start: { type: "relative", relativeValue: "6", relativeFormat: "Hours ago" }, end: { type: "relative", relativeNow: true } },
1123
+ { label: "Last 12 hours", start: { type: "relative", relativeValue: "12", relativeFormat: "Hours ago" }, end: { type: "relative", relativeNow: true } },
1124
+ { label: "Last 24 hours", start: { type: "relative", relativeValue: "24", relativeFormat: "Hours ago" }, end: { type: "relative", relativeNow: true } },
1125
+ { label: "Last 2 days", start: { type: "relative", relativeValue: "2", relativeFormat: "Days ago" }, end: { type: "relative", relativeNow: true } },
1126
+ { label: "Last 7 days", start: { type: "relative", relativeValue: "7", relativeFormat: "Days ago" }, end: { type: "relative", relativeNow: true } },
1127
+ { label: "Last 30 days", start: { type: "relative", relativeValue: "30", relativeFormat: "Days ago" }, end: { type: "relative", relativeNow: true } },
1128
+ { label: "Last 90 days", start: { type: "relative", relativeValue: "90", relativeFormat: "Days ago" }, end: { type: "relative", relativeNow: true } },
1129
+ { label: "Last 6 months", start: { type: "relative", relativeValue: "6", relativeFormat: "Months ago" }, end: { type: "relative", relativeNow: true } },
1130
+ { label: "Last 1 year", start: { type: "relative", relativeValue: "12", relativeFormat: "Months ago" }, end: { type: "relative", relativeNow: true } }
1131
+ ];
1132
+ function toDate(value) {
1133
+ if (value.type === "absolute") {
1134
+ if (value.absoluteValue instanceof Date) return value.absoluteValue;
1135
+ if (value.absoluteValue) return new Date(value.absoluteValue);
1136
+ return /* @__PURE__ */ new Date();
1137
+ }
1138
+ if (value.relativeNow) return /* @__PURE__ */ new Date();
1139
+ let relStr;
1140
+ if (value.relativeFormat) {
1141
+ relStr = `${value.relativeValue} ${value.relativeFormat}`;
1142
+ } else {
1143
+ relStr = String(value.relativeValue ?? "");
1144
+ }
1145
+ const parsed = chrono.parseDate(relStr);
1146
+ return parsed ?? /* @__PURE__ */ new Date();
1147
+ }
1148
+ function toTimestampMs(value) {
1149
+ return toDate(value).getTime();
1150
+ }
1151
+ function toTimestamp(value) {
1152
+ return Math.floor(toTimestampMs(value) / 1e3);
1153
+ }
1154
+ function toDisplayString(value, options) {
1155
+ if (value.type === "absolute") {
1156
+ if (!value.absoluteValue) return "None";
1157
+ const p = options?.precision ?? "second";
1158
+ const fmt = { year: "numeric", month: "2-digit", day: "2-digit" };
1159
+ if (p === "hour" || p === "minute" || p === "second") fmt.hour = "2-digit";
1160
+ if (p === "minute" || p === "second") fmt.minute = "2-digit";
1161
+ if (p === "second") fmt.second = "2-digit";
1162
+ return value.absoluteValue.toLocaleString(navigator.language, fmt);
1163
+ }
1164
+ if (value.relativeNow) return "now";
1165
+ return `${value.relativeValue ?? ""}${value.relativeFormat ? " " + value.relativeFormat : ""}`;
1166
+ }
1167
+ function toUrlString(value) {
1168
+ if (value.type === "absolute") {
1169
+ const date = value.absoluteValue instanceof Date ? value.absoluteValue : value.absoluteValue ? new Date(value.absoluteValue) : /* @__PURE__ */ new Date();
1170
+ return date.getTime().toString();
1171
+ }
1172
+ if (value.relativeNow) return "now";
1173
+ if (typeof value.relativeValue === "string" && !value.relativeFormat) {
1174
+ return value.relativeValue.startsWith("now") ? value.relativeValue : `now${value.relativeValue}`;
1175
+ }
1176
+ const unitMap = {
1177
+ "Seconds ago": "s",
1178
+ "Minutes ago": "m",
1179
+ "Hours ago": "h",
1180
+ "Days ago": "d",
1181
+ "Weeks ago": "w",
1182
+ "Months ago": "M",
1183
+ "Years ago": "y"
1184
+ };
1185
+ const unit = value.relativeFormat ? unitMap[value.relativeFormat] : "h";
1186
+ const amount = value.relativeValue ?? 1;
1187
+ return `now-${amount}${unit}`;
1188
+ }
1189
+ function fromUrlString(urlString) {
1190
+ if (urlString === "now") return { type: "relative", relativeNow: true };
1191
+ if (urlString.startsWith("now")) {
1192
+ const match = urlString.match(/^now([+-]?)(\d+)([smhdwMy])$/);
1193
+ if (match) {
1194
+ const [, sign, amount, unit] = match;
1195
+ const unitToFormat = {
1196
+ s: "Seconds ago",
1197
+ m: "Minutes ago",
1198
+ h: "Hours ago",
1199
+ d: "Days ago",
1200
+ w: "Weeks ago",
1201
+ M: "Months ago",
1202
+ y: "Years ago"
1203
+ };
1204
+ const relativeFormat = unitToFormat[unit];
1205
+ if (relativeFormat && sign !== "+") {
1206
+ return { type: "relative", relativeValue: amount, relativeFormat, relativeNow: false };
1207
+ }
1208
+ return { type: "relative", relativeValue: `${sign}${amount}${unit}`, relativeNow: false };
1209
+ }
1210
+ return { type: "relative", relativeNow: true };
1211
+ }
1212
+ const timestamp = parseInt(urlString, 10);
1213
+ if (!isNaN(timestamp)) return { type: "absolute", absoluteValue: new Date(timestamp) };
1214
+ return { type: "relative", relativeNow: true };
1215
+ }
1216
+ function relativeNow() {
1217
+ return { type: "relative", relativeNow: true };
1218
+ }
1219
+ function relativeAgo(value, format3) {
1220
+ return { type: "relative", relativeValue: String(value), relativeFormat: format3 };
1221
+ }
1222
+ function absoluteDate(date) {
1223
+ return { type: "absolute", absoluteValue: date };
1224
+ }
1225
+ function validateRange(start, end) {
1226
+ const startDate = toDate(start);
1227
+ const endDate = toDate(end);
1228
+ if (startDate >= endDate) return "validation";
1229
+ return null;
1230
+ }
1231
+ function RelativeSidePanel({ value, onChange, relativeFormats, showNow, labels }) {
1232
+ const [numValue, setNumValue] = useState(
1233
+ value.relativeValue !== void 0 ? String(value.relativeValue) : "5"
1234
+ );
1235
+ const [format3, setFormat] = useState(
1236
+ value.relativeFormat ?? relativeFormats[0] ?? "Minutes ago"
1237
+ );
1238
+ const [isNow, setIsNow] = useState(value.relativeNow ?? false);
1239
+ useEffect(() => {
1240
+ if (value.relativeNow) {
1241
+ setIsNow(true);
1242
+ } else {
1243
+ setIsNow(false);
1244
+ if (value.relativeValue !== void 0) setNumValue(String(value.relativeValue));
1245
+ if (value.relativeFormat) setFormat(value.relativeFormat);
1246
+ }
1247
+ }, [value]);
1248
+ const emit = (next) => {
1249
+ if (next.isNow) {
1250
+ onChange({ type: "relative", relativeNow: true });
1251
+ } else {
1252
+ onChange({
1253
+ type: "relative",
1254
+ relativeValue: next.numValue,
1255
+ relativeFormat: next.format,
1256
+ relativeNow: false
1257
+ });
1258
+ }
1259
+ };
1260
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3 mt-3", children: [
1261
+ /* @__PURE__ */ jsxs("div", { children: [
1262
+ /* @__PURE__ */ jsx("p", { className: "text-[11px] text-muted-foreground mb-1.5", children: labels.amount }),
1263
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-row gap-1.5", children: [
1264
+ /* @__PURE__ */ jsx(
1265
+ Input,
1266
+ {
1267
+ type: "text",
1268
+ inputMode: "numeric",
1269
+ pattern: "[0-9]*",
1270
+ className: "h-8 w-20 text-xs",
1271
+ disabled: isNow,
1272
+ value: numValue,
1273
+ onChange: (e) => {
1274
+ const val = e.target.value;
1275
+ if (/^\d*$/.test(val)) {
1276
+ setNumValue(val);
1277
+ emit({ numValue: val, format: format3, isNow });
1278
+ }
1279
+ },
1280
+ onWheel: (e) => e.currentTarget.blur(),
1281
+ onKeyDown: (e) => {
1282
+ if (["e", "E", "+", "-", "."].includes(e.key)) e.preventDefault();
1283
+ }
1284
+ }
1285
+ ),
1286
+ /* @__PURE__ */ jsxs(
1287
+ Select,
1288
+ {
1289
+ disabled: isNow,
1290
+ value: format3,
1291
+ onValueChange: (v) => {
1292
+ const f = v;
1293
+ setFormat(f);
1294
+ emit({ numValue, format: f, isNow });
1295
+ },
1296
+ children: [
1297
+ /* @__PURE__ */ jsx(SelectTrigger, { className: "h-8 text-xs text-left leading-tight", children: /* @__PURE__ */ jsx(SelectValue, { children: format3 }) }),
1298
+ /* @__PURE__ */ jsx(SelectContent, { children: relativeFormats.map((f) => /* @__PURE__ */ jsx(SelectItem, { value: f, className: "text-xs", children: f }, f)) })
1299
+ ]
1300
+ }
1301
+ )
1302
+ ] })
1303
+ ] }),
1304
+ showNow && /* @__PURE__ */ jsxs("div", { className: "flex flex-row gap-2 items-center pt-0.5", children: [
1305
+ /* @__PURE__ */ jsx(
1306
+ Switch,
1307
+ {
1308
+ checked: isNow,
1309
+ onCheckedChange: (checked) => {
1310
+ setIsNow(checked);
1311
+ emit({ numValue, format: format3, isNow: checked });
1312
+ }
1313
+ }
1314
+ ),
1315
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
1316
+ /* @__PURE__ */ jsx("label", { className: "text-xs font-medium", children: labels.now }),
1317
+ /* @__PURE__ */ jsx("span", { className: "text-[11px] text-muted-foreground", children: labels.nowDescription })
1318
+ ] })
1319
+ ] })
1320
+ ] });
1321
+ }
1322
+ function AbsoluteContent({
1323
+ value,
1324
+ compareValue,
1325
+ title,
1326
+ onChange,
1327
+ precision,
1328
+ min,
1329
+ max,
1330
+ timezone,
1331
+ use12HourFormat
1332
+ }) {
1333
+ const compareDate = compareValue ? toDate(compareValue) : void 0;
1334
+ const absoluteDate_ = value.type === "absolute" ? toDate(value) : /* @__PURE__ */ new Date();
1335
+ return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5 mt-3", children: [
1336
+ /* @__PURE__ */ jsx(
1337
+ DatetimeSegmentInput,
1338
+ {
1339
+ value: absoluteDate_,
1340
+ onChange: (d) => onChange(absoluteDate(d)),
1341
+ className: "flex-1",
1342
+ precision
1343
+ }
1344
+ ),
1345
+ /* @__PURE__ */ jsxs(Popover, { children: [
1346
+ /* @__PURE__ */ jsx(
1347
+ PopoverTrigger,
1348
+ {
1349
+ render: (triggerProps) => /* @__PURE__ */ jsx(
1350
+ Button,
1351
+ {
1352
+ ...triggerProps,
1353
+ type: "button",
1354
+ variant: "outline",
1355
+ size: "icon",
1356
+ className: "h-8 w-8 shrink-0",
1357
+ title: "Pick from calendar",
1358
+ children: /* @__PURE__ */ jsx(CalendarDays, { size: 14 })
1359
+ }
1360
+ )
1361
+ }
1362
+ ),
1363
+ /* @__PURE__ */ jsx(
1364
+ PopoverContent,
1365
+ {
1366
+ className: "p-0 w-auto",
1367
+ side: "bottom",
1368
+ align: "start",
1369
+ sideOffset: 6,
1370
+ disableAnchorTracking: true,
1371
+ children: /* @__PURE__ */ jsx(
1372
+ DateTimePanel,
1373
+ {
1374
+ value: value.type === "absolute" ? toDate(value) : void 0,
1375
+ compareValue: compareDate,
1376
+ title,
1377
+ immediate: true,
1378
+ precision,
1379
+ min,
1380
+ max,
1381
+ timezone,
1382
+ use12HourFormat,
1383
+ onChangeAction: (d) => {
1384
+ if (!d) return;
1385
+ onChange(absoluteDate(d));
1386
+ }
1387
+ }
1388
+ )
1389
+ }
1390
+ )
1391
+ ] })
1392
+ ] });
1393
+ }
1394
+ function SidePanel({
1395
+ title,
1396
+ value,
1397
+ compareValue,
1398
+ onChange,
1399
+ showAbsolute,
1400
+ showRelative,
1401
+ precision,
1402
+ relativeFormats,
1403
+ showNow,
1404
+ min,
1405
+ max,
1406
+ timezone,
1407
+ use12HourFormat,
1408
+ labels
1409
+ }) {
1410
+ useEffect(() => {
1411
+ if (!showRelative && value.type === "relative") {
1412
+ onChange(absoluteDate(/* @__PURE__ */ new Date()));
1413
+ } else if (!showAbsolute && value.type === "absolute") {
1414
+ onChange({ type: "relative", relativeValue: "5", relativeFormat: relativeFormats[0] ?? "Minutes ago" });
1415
+ }
1416
+ }, [showRelative, showAbsolute]);
1417
+ const hasBothModes = showAbsolute && showRelative;
1418
+ const absoluteContent = /* @__PURE__ */ jsx(
1419
+ AbsoluteContent,
1420
+ {
1421
+ value,
1422
+ compareValue,
1423
+ title,
1424
+ onChange,
1425
+ precision,
1426
+ min,
1427
+ max,
1428
+ timezone,
1429
+ use12HourFormat
1430
+ }
1431
+ );
1432
+ const relativeContent = /* @__PURE__ */ jsx(
1433
+ RelativeSidePanel,
1434
+ {
1435
+ value,
1436
+ onChange,
1437
+ relativeFormats,
1438
+ showNow,
1439
+ labels
1440
+ }
1441
+ );
1442
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col w-[268px]", children: [
1443
+ /* @__PURE__ */ jsx("p", { className: "text-[11px] font-semibold text-muted-foreground uppercase tracking-widest pb-2", children: title }),
1444
+ hasBothModes ? /* @__PURE__ */ jsxs(
1445
+ Tabs,
1446
+ {
1447
+ value: value.type,
1448
+ className: "flex-col",
1449
+ onValueChange: (t) => {
1450
+ if (t === "absolute") {
1451
+ onChange(absoluteDate(value.type === "absolute" ? toDate(value) : /* @__PURE__ */ new Date()));
1452
+ } else if (value.type !== "relative") {
1453
+ onChange({ type: "relative", relativeValue: "5", relativeFormat: relativeFormats[0] ?? "Minutes ago" });
1454
+ }
1455
+ },
1456
+ children: [
1457
+ /* @__PURE__ */ jsxs(TabsList, { className: "grid w-full grid-cols-2", children: [
1458
+ /* @__PURE__ */ jsx(TabsTrigger, { value: "absolute", children: labels.absolute }),
1459
+ /* @__PURE__ */ jsx(TabsTrigger, { value: "relative", children: labels.relative })
1460
+ ] }),
1461
+ /* @__PURE__ */ jsx(TabsContent, { value: "absolute", children: absoluteContent }),
1462
+ /* @__PURE__ */ jsx(TabsContent, { value: "relative", children: relativeContent })
1463
+ ]
1464
+ }
1465
+ ) : showAbsolute ? absoluteContent : relativeContent
1466
+ ] });
1467
+ }
1468
+ function DatetimeRange({
1469
+ startTime,
1470
+ endTime,
1471
+ onChange,
1472
+ quickPresets = QUICK_PRESETS,
1473
+ showQuickRanges = true,
1474
+ showAbsolute = true,
1475
+ showRelative = true,
1476
+ precision = "second",
1477
+ relativeFormats = RELATIVE_FORMATS,
1478
+ min,
1479
+ max,
1480
+ timezone,
1481
+ use12HourFormat = false,
1482
+ showNow = true,
1483
+ disabled = false,
1484
+ labels: labelsProp,
1485
+ popoverAlign = "start",
1486
+ popoverSide = "bottom",
1487
+ renderTrigger,
1488
+ className
1489
+ }) {
1490
+ const labels = { ...DEFAULT_LABELS, ...labelsProp };
1491
+ const [isOpen, setIsOpen] = useState(false);
1492
+ const [draftStart, setDraftStart] = useState(startTime);
1493
+ const [draftEnd, setDraftEnd] = useState(endTime);
1494
+ const [error, setError] = useState(null);
1495
+ useEffect(() => {
1496
+ if (isOpen) {
1497
+ setDraftStart(startTime);
1498
+ setDraftEnd(endTime);
1499
+ setError(null);
1500
+ }
1501
+ }, [isOpen, startTime, endTime]);
1502
+ const handleApply = () => {
1503
+ const err = validateRange(draftStart, draftEnd);
1504
+ if (err) {
1505
+ setError(err);
1506
+ return;
1507
+ }
1508
+ onChange(draftStart, draftEnd);
1509
+ setIsOpen(false);
1510
+ };
1511
+ const handleCancel = () => {
1512
+ setDraftStart(startTime);
1513
+ setDraftEnd(endTime);
1514
+ setError(null);
1515
+ setIsOpen(false);
1516
+ };
1517
+ const handlePreset = (preset) => {
1518
+ onChange(preset.start, preset.end);
1519
+ setIsOpen(false);
1520
+ };
1521
+ const sidePanelProps = {
1522
+ showAbsolute,
1523
+ showRelative,
1524
+ precision,
1525
+ relativeFormats,
1526
+ showNow,
1527
+ min,
1528
+ max,
1529
+ timezone,
1530
+ use12HourFormat,
1531
+ labels
1532
+ };
1533
+ return /* @__PURE__ */ jsxs(Popover, { open: isOpen, onOpenChange: disabled ? void 0 : setIsOpen, children: [
1534
+ /* @__PURE__ */ jsx(
1535
+ PopoverTrigger,
1536
+ {
1537
+ disabled,
1538
+ render: (triggerProps) => renderTrigger ? renderTrigger(triggerProps, { open: isOpen, startTime, endTime }) : /* @__PURE__ */ jsxs(
1539
+ Button,
1540
+ {
1541
+ ...triggerProps,
1542
+ variant: "outline",
1543
+ disabled,
1544
+ className: cn(
1545
+ "h-8 px-3 gap-2 text-xs font-normal justify-start",
1546
+ isOpen && "border-[var(--bk-foreground)]",
1547
+ className
1548
+ ),
1549
+ children: [
1550
+ /* @__PURE__ */ jsx(CalendarDays, { size: 14, className: "text-muted-foreground shrink-0" }),
1551
+ /* @__PURE__ */ jsx("span", { children: toDisplayString(startTime, { precision }) }),
1552
+ /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "\u2192" }),
1553
+ /* @__PURE__ */ jsx("span", { children: toDisplayString(endTime, { precision }) })
1554
+ ]
1555
+ }
1556
+ )
1557
+ }
1558
+ ),
1559
+ /* @__PURE__ */ jsx(
1560
+ PopoverContent,
1561
+ {
1562
+ className: "p-0 w-auto",
1563
+ align: popoverAlign,
1564
+ side: popoverSide,
1565
+ sideOffset: 4,
1566
+ children: /* @__PURE__ */ jsxs("div", { className: "flex", children: [
1567
+ showQuickRanges && quickPresets.length > 0 && /* @__PURE__ */ jsx("div", { className: "shrink-0 border-r w-40 relative", children: /* @__PURE__ */ jsxs("div", { className: "absolute inset-0 flex flex-col overflow-hidden", children: [
1568
+ /* @__PURE__ */ jsx("div", { className: "px-3 pt-2.5 pb-1 text-[11px] font-semibold text-muted-foreground uppercase tracking-widest shrink-0", children: labels.quickRanges }),
1569
+ /* @__PURE__ */ jsx("div", { className: "flex-1 overflow-y-auto", children: quickPresets.map((preset) => /* @__PURE__ */ jsx(
1570
+ "button",
1571
+ {
1572
+ type: "button",
1573
+ className: "block w-full text-left text-xs px-3 py-1.5 hover:bg-accent hover:text-accent-foreground transition-colors",
1574
+ onClick: () => handlePreset(preset),
1575
+ children: preset.label
1576
+ },
1577
+ preset.label
1578
+ )) })
1579
+ ] }) }),
1580
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col justify-between relative", children: [
1581
+ /* @__PURE__ */ jsx("div", { className: "pointer-events-none absolute top-0 bottom-0 left-1/2 w-px bg-border" }),
1582
+ /* @__PURE__ */ jsxs("div", { className: "flex", children: [
1583
+ /* @__PURE__ */ jsx("div", { className: "px-4 pb-4 pt-3", children: /* @__PURE__ */ jsx(
1584
+ SidePanel,
1585
+ {
1586
+ title: labels.start,
1587
+ value: draftStart,
1588
+ compareValue: draftEnd,
1589
+ onChange: (v) => {
1590
+ setDraftStart(v);
1591
+ setError(null);
1592
+ },
1593
+ ...sidePanelProps
1594
+ }
1595
+ ) }),
1596
+ /* @__PURE__ */ jsx("div", { className: "px-4 pb-4 pt-3", children: /* @__PURE__ */ jsx(
1597
+ SidePanel,
1598
+ {
1599
+ title: labels.end,
1600
+ value: draftEnd,
1601
+ compareValue: draftStart,
1602
+ onChange: (v) => {
1603
+ setDraftEnd(v);
1604
+ setError(null);
1605
+ },
1606
+ ...sidePanelProps
1607
+ }
1608
+ ) })
1609
+ ] }),
1610
+ /* @__PURE__ */ jsx(Separator, {}),
1611
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between px-4 py-2 gap-4", children: [
1612
+ error === "validation" ? /* @__PURE__ */ jsx("span", { className: "text-xs text-destructive", children: "Start must be earlier than end." }) : /* @__PURE__ */ jsx("span", {}),
1613
+ /* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
1614
+ /* @__PURE__ */ jsx(Button, { variant: "outline", size: "sm", onClick: handleCancel, children: labels.cancel }),
1615
+ /* @__PURE__ */ jsx(Button, { size: "sm", onClick: handleApply, children: labels.apply })
1616
+ ] })
1617
+ ] })
1618
+ ] })
1619
+ ] })
1620
+ }
1621
+ )
1622
+ ] });
1623
+ }
1624
+
1625
+ export { DateTimePanel, DatetimeRange, DatetimeSegmentInput, QUICK_PRESETS, RELATIVE_FORMATS, absoluteDate, fromUrlString, relativeAgo, relativeNow, toDate, toDisplayString, toTimestamp, toTimestampMs, toUrlString, validateRange };
1626
+ //# sourceMappingURL=index.js.map
1627
+ //# sourceMappingURL=index.js.map