@nubitio/ui 0.6.9 → 0.7.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 +175 -277
- package/dist/index.d.cts +69 -48
- package/dist/index.d.mts +69 -48
- package/dist/index.mjs +175 -277
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -540,84 +540,111 @@ function FileDropzone({ accept, value = null, image = false, disabled = false, r
|
|
|
540
540
|
});
|
|
541
541
|
}
|
|
542
542
|
//#endregion
|
|
543
|
-
//#region packages/ui/
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
const
|
|
560
|
-
const
|
|
561
|
-
const left = Math.max(8, Math.min(rect.left, window.innerWidth - menuWidth - 8));
|
|
543
|
+
//#region packages/ui/useFloatingPanel.ts
|
|
544
|
+
/** Style applied to an anchored panel before its first position is computed. */
|
|
545
|
+
const HIDDEN_PANEL_STYLE = {
|
|
546
|
+
position: "fixed",
|
|
547
|
+
visibility: "hidden",
|
|
548
|
+
margin: 0
|
|
549
|
+
};
|
|
550
|
+
/**
|
|
551
|
+
* Fixed-position style for a panel anchored to an element, clamped to the
|
|
552
|
+
* viewport with an 8px gutter and flipped above the anchor when it fits
|
|
553
|
+
* better there.
|
|
554
|
+
*/
|
|
555
|
+
function computeAnchoredStyle(anchor, options = {}) {
|
|
556
|
+
if (!anchor) return HIDDEN_PANEL_STYLE;
|
|
557
|
+
const { align = "start", minWidth = 0, matchAnchorWidth = false, offset = 4, flipThreshold = 250, zIndex = 9999 } = options;
|
|
558
|
+
const rect = anchor.getBoundingClientRect();
|
|
559
|
+
const width = Math.max(matchAnchorWidth ? rect.width : 0, minWidth);
|
|
560
|
+
const left = align === "end" ? Math.max(8, Math.min(rect.right - width, window.innerWidth - width - 8)) : Math.max(8, Math.min(rect.left, window.innerWidth - width - 8));
|
|
562
561
|
const spaceBelow = window.innerHeight - rect.bottom;
|
|
563
|
-
|
|
564
|
-
if (spaceBelow < 250 && spaceAbove > spaceBelow) return {
|
|
562
|
+
if (spaceBelow < flipThreshold && rect.top > spaceBelow) return {
|
|
565
563
|
position: "fixed",
|
|
566
564
|
left: `${left}px`,
|
|
567
|
-
width: `${
|
|
568
|
-
bottom: `${window.innerHeight - rect.top +
|
|
565
|
+
width: `${width}px`,
|
|
566
|
+
bottom: `${window.innerHeight - rect.top + offset}px`,
|
|
569
567
|
top: "auto",
|
|
570
568
|
margin: 0,
|
|
571
|
-
zIndex
|
|
569
|
+
zIndex,
|
|
572
570
|
visibility: "visible"
|
|
573
571
|
};
|
|
574
572
|
return {
|
|
575
573
|
position: "fixed",
|
|
576
574
|
left: `${left}px`,
|
|
577
|
-
width: `${
|
|
578
|
-
top: `${rect.bottom +
|
|
575
|
+
width: `${width}px`,
|
|
576
|
+
top: `${rect.bottom + offset}px`,
|
|
579
577
|
bottom: "auto",
|
|
580
578
|
margin: 0,
|
|
581
|
-
zIndex
|
|
579
|
+
zIndex,
|
|
582
580
|
visibility: "visible"
|
|
583
581
|
};
|
|
584
582
|
}
|
|
585
|
-
function
|
|
586
|
-
const
|
|
587
|
-
const
|
|
588
|
-
const [open, setOpen] = useState(false);
|
|
583
|
+
function useFloatingPanel(options = {}) {
|
|
584
|
+
const [open, setOpenState] = useState(false);
|
|
585
|
+
const [panelStyle, setPanelStyle] = useState(HIDDEN_PANEL_STYLE);
|
|
589
586
|
const containerRef = useRef(null);
|
|
590
|
-
const
|
|
591
|
-
const
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
587
|
+
const panelRef = useRef(null);
|
|
588
|
+
const optionsRef = useRef(options);
|
|
589
|
+
optionsRef.current = options;
|
|
590
|
+
const anchored = !!options.computeStyle;
|
|
591
|
+
const dismissEvent = options.dismissEvent ?? "pointerdown";
|
|
592
|
+
const setOpen = useCallback((next) => {
|
|
593
|
+
setOpenState(next);
|
|
594
|
+
if (!next) optionsRef.current.onClose?.();
|
|
595
|
+
optionsRef.current.onOpenChange?.(next);
|
|
596
|
+
}, []);
|
|
597
|
+
const applyStyle = useCallback(() => {
|
|
598
|
+
const compute = optionsRef.current.computeStyle;
|
|
599
|
+
if (!compute) return;
|
|
600
|
+
const style = compute(containerRef.current);
|
|
601
|
+
if (style) setPanelStyle(style);
|
|
602
|
+
}, []);
|
|
603
|
+
const openPanel = useCallback(() => {
|
|
604
|
+
applyStyle();
|
|
605
|
+
setOpen(true);
|
|
606
|
+
}, [applyStyle, setOpen]);
|
|
607
|
+
const toggle = useCallback(() => open ? setOpen(false) : openPanel(), [
|
|
608
|
+
open,
|
|
609
|
+
openPanel,
|
|
610
|
+
setOpen
|
|
611
|
+
]);
|
|
602
612
|
useEffect(() => {
|
|
603
613
|
if (!open) return;
|
|
604
|
-
const
|
|
614
|
+
const handleOutside = (e) => {
|
|
605
615
|
const target = e.target;
|
|
606
|
-
if (
|
|
616
|
+
if (containerRef.current?.contains(target) || panelRef.current?.contains(target)) return;
|
|
617
|
+
setOpen(false);
|
|
618
|
+
};
|
|
619
|
+
const handleKeyDown = (e) => {
|
|
620
|
+
if (e.key !== "Escape") return;
|
|
621
|
+
setOpen(false);
|
|
622
|
+
optionsRef.current.onEscape?.(e);
|
|
607
623
|
};
|
|
608
|
-
document.addEventListener(
|
|
609
|
-
|
|
610
|
-
|
|
624
|
+
document.addEventListener(dismissEvent, handleOutside);
|
|
625
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
626
|
+
return () => {
|
|
627
|
+
document.removeEventListener(dismissEvent, handleOutside);
|
|
628
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
629
|
+
};
|
|
630
|
+
}, [
|
|
631
|
+
dismissEvent,
|
|
632
|
+
open,
|
|
633
|
+
setOpen
|
|
634
|
+
]);
|
|
611
635
|
useLayoutEffect(() => {
|
|
612
|
-
if (!open) return;
|
|
636
|
+
if (!open || !anchored) return;
|
|
613
637
|
const update = () => {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
if (
|
|
617
|
-
|
|
618
|
-
|
|
638
|
+
const container = containerRef.current;
|
|
639
|
+
if (!container) return;
|
|
640
|
+
if (optionsRef.current.closeOnViewportExit) {
|
|
641
|
+
const rect = container.getBoundingClientRect();
|
|
642
|
+
if ((rect.width > 0 || rect.height > 0) && (rect.top >= window.innerHeight || rect.bottom <= 0)) {
|
|
643
|
+
setOpen(false);
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
619
646
|
}
|
|
620
|
-
|
|
647
|
+
applyStyle();
|
|
621
648
|
};
|
|
622
649
|
update();
|
|
623
650
|
window.addEventListener("resize", update);
|
|
@@ -630,14 +657,47 @@ function AppDropdown({ id: idProp, label, icon, value, options, onChange, disabl
|
|
|
630
657
|
window.removeEventListener("scroll", update, { capture: true });
|
|
631
658
|
};
|
|
632
659
|
}, [
|
|
660
|
+
anchored,
|
|
661
|
+
applyStyle,
|
|
633
662
|
open,
|
|
634
|
-
|
|
635
|
-
setMenuOpen
|
|
663
|
+
setOpen
|
|
636
664
|
]);
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
665
|
+
return {
|
|
666
|
+
open,
|
|
667
|
+
setOpen,
|
|
668
|
+
toggle,
|
|
669
|
+
openPanel,
|
|
670
|
+
containerRef,
|
|
671
|
+
panelRef,
|
|
672
|
+
panelStyle
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
//#endregion
|
|
676
|
+
//#region packages/ui/AppDropdown.tsx
|
|
677
|
+
function cx$2(...values) {
|
|
678
|
+
return values.filter(Boolean).join(" ");
|
|
679
|
+
}
|
|
680
|
+
function normalizeIcon$2(icon) {
|
|
681
|
+
if (!icon) return void 0;
|
|
682
|
+
if (icon.startsWith("ph ")) return icon;
|
|
683
|
+
if (icon.startsWith("ph-")) return `ph ${icon}`;
|
|
684
|
+
return `ph ph-${icon}`;
|
|
685
|
+
}
|
|
686
|
+
function AppDropdown({ id: idProp, label, icon, value, options, onChange, disabled = false, placeholder = "Seleccionar…", variant = "form", className, showFieldLabel = variant === "form", menuMinWidth, error, helpText, menuHeader, menuFooter, onOpenChange }) {
|
|
687
|
+
const generatedId = useId();
|
|
688
|
+
const id = idProp ?? generatedId;
|
|
689
|
+
const { open, setOpen: setMenuOpen, openPanel: openMenu, containerRef, panelRef: menuRef, panelStyle: menuStyle } = useFloatingPanel({
|
|
690
|
+
onOpenChange,
|
|
691
|
+
dismissEvent: "mousedown",
|
|
692
|
+
closeOnViewportExit: true,
|
|
693
|
+
computeStyle: (container) => {
|
|
694
|
+
return computeAnchoredStyle(container?.querySelector(".nb-dropdown__trigger") ?? container, {
|
|
695
|
+
matchAnchorWidth: true,
|
|
696
|
+
minWidth: menuMinWidth ?? 72
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
const selected = options.find((option) => option.value === value);
|
|
641
701
|
const control = /* @__PURE__ */ jsxs("div", {
|
|
642
702
|
ref: containerRef,
|
|
643
703
|
className: cx$2("nb-dropdown", `nb-dropdown--${variant}`, open && "nb-dropdown--open", disabled && "nb-dropdown--disabled", className),
|
|
@@ -1370,38 +1430,6 @@ const Toggle = ({ checked, onChange, label, size = "md", disabled = false, "aria
|
|
|
1370
1430
|
//#endregion
|
|
1371
1431
|
//#region packages/ui/ContextMenu.tsx
|
|
1372
1432
|
const MENU_MIN_WIDTH = 190;
|
|
1373
|
-
function getDropdownStyle(anchor, align) {
|
|
1374
|
-
if (!anchor) return {
|
|
1375
|
-
position: "fixed",
|
|
1376
|
-
visibility: "hidden",
|
|
1377
|
-
margin: 0
|
|
1378
|
-
};
|
|
1379
|
-
const rect = anchor.getBoundingClientRect();
|
|
1380
|
-
const menuWidth = MENU_MIN_WIDTH;
|
|
1381
|
-
const left = align === "right" ? Math.max(8, Math.min(rect.right - menuWidth, window.innerWidth - menuWidth - 8)) : Math.max(8, Math.min(rect.left, window.innerWidth - menuWidth - 8));
|
|
1382
|
-
const spaceBelow = window.innerHeight - rect.bottom;
|
|
1383
|
-
const spaceAbove = rect.top;
|
|
1384
|
-
if (spaceBelow < 200 && spaceAbove > spaceBelow) return {
|
|
1385
|
-
position: "fixed",
|
|
1386
|
-
left: `${left}px`,
|
|
1387
|
-
width: `${menuWidth}px`,
|
|
1388
|
-
bottom: `${window.innerHeight - rect.top + 4}px`,
|
|
1389
|
-
top: "auto",
|
|
1390
|
-
margin: 0,
|
|
1391
|
-
zIndex: 1600,
|
|
1392
|
-
visibility: "visible"
|
|
1393
|
-
};
|
|
1394
|
-
return {
|
|
1395
|
-
position: "fixed",
|
|
1396
|
-
left: `${left}px`,
|
|
1397
|
-
width: `${menuWidth}px`,
|
|
1398
|
-
top: `${rect.bottom + 4}px`,
|
|
1399
|
-
bottom: "auto",
|
|
1400
|
-
margin: 0,
|
|
1401
|
-
zIndex: 1600,
|
|
1402
|
-
visibility: "visible"
|
|
1403
|
-
};
|
|
1404
|
-
}
|
|
1405
1433
|
/**
|
|
1406
1434
|
* Three-dot overflow context menu.
|
|
1407
1435
|
* Replaces the legacy `shared/ui/library/card-menu/CardMenu` component.
|
|
@@ -1416,53 +1444,16 @@ function getDropdownStyle(anchor, align) {
|
|
|
1416
1444
|
* - Portalled dropdown (avoids clip inside scrollable tables)
|
|
1417
1445
|
*/
|
|
1418
1446
|
const ContextMenu = ({ items, visible = true, trigger, triggerLabel = "Opciones", align = "right", className = "" }) => {
|
|
1419
|
-
const
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1447
|
+
const { open, setOpen, toggle, containerRef, panelRef: menuRef, panelStyle: menuStyle } = useFloatingPanel({
|
|
1448
|
+
dismissEvent: "mousedown",
|
|
1449
|
+
closeOnViewportExit: true,
|
|
1450
|
+
computeStyle: (container) => computeAnchoredStyle(container, {
|
|
1451
|
+
align: align === "right" ? "end" : "start",
|
|
1452
|
+
minWidth: MENU_MIN_WIDTH,
|
|
1453
|
+
flipThreshold: 200,
|
|
1454
|
+
zIndex: 1600
|
|
1455
|
+
})
|
|
1426
1456
|
});
|
|
1427
|
-
const computeMenuStyle = useCallback(() => getDropdownStyle(containerRef.current, align), [align]);
|
|
1428
|
-
useEffect(() => {
|
|
1429
|
-
if (!open) return;
|
|
1430
|
-
const onMouseDown = (e) => {
|
|
1431
|
-
const target = e.target;
|
|
1432
|
-
if (!containerRef.current?.contains(target) && !menuRef.current?.contains(target)) setOpen(false);
|
|
1433
|
-
};
|
|
1434
|
-
const onKeyDown = (e) => {
|
|
1435
|
-
if (e.key === "Escape") setOpen(false);
|
|
1436
|
-
};
|
|
1437
|
-
document.addEventListener("mousedown", onMouseDown);
|
|
1438
|
-
document.addEventListener("keydown", onKeyDown);
|
|
1439
|
-
return () => {
|
|
1440
|
-
document.removeEventListener("mousedown", onMouseDown);
|
|
1441
|
-
document.removeEventListener("keydown", onKeyDown);
|
|
1442
|
-
};
|
|
1443
|
-
}, [open]);
|
|
1444
|
-
useLayoutEffect(() => {
|
|
1445
|
-
if (!open) return;
|
|
1446
|
-
const update = () => {
|
|
1447
|
-
if (!containerRef.current) return;
|
|
1448
|
-
const rect = containerRef.current.getBoundingClientRect();
|
|
1449
|
-
if ((rect.width > 0 || rect.height > 0) && (rect.top >= window.innerHeight || rect.bottom <= 0)) {
|
|
1450
|
-
setOpen(false);
|
|
1451
|
-
return;
|
|
1452
|
-
}
|
|
1453
|
-
setMenuStyle(computeMenuStyle());
|
|
1454
|
-
};
|
|
1455
|
-
update();
|
|
1456
|
-
window.addEventListener("resize", update);
|
|
1457
|
-
window.addEventListener("scroll", update, {
|
|
1458
|
-
capture: true,
|
|
1459
|
-
passive: true
|
|
1460
|
-
});
|
|
1461
|
-
return () => {
|
|
1462
|
-
window.removeEventListener("resize", update);
|
|
1463
|
-
window.removeEventListener("scroll", update, { capture: true });
|
|
1464
|
-
};
|
|
1465
|
-
}, [open, computeMenuStyle]);
|
|
1466
1457
|
if (!visible) return null;
|
|
1467
1458
|
const dropdown = open ? /* @__PURE__ */ jsx("ul", {
|
|
1468
1459
|
ref: menuRef,
|
|
@@ -1496,17 +1487,14 @@ const ContextMenu = ({ items, visible = true, trigger, triggerLabel = "Opciones"
|
|
|
1496
1487
|
ref: containerRef,
|
|
1497
1488
|
className: `nb-context-menu${className ? ` ${className}` : ""}`,
|
|
1498
1489
|
children: [trigger ? /* @__PURE__ */ jsx("span", {
|
|
1499
|
-
onClick:
|
|
1490
|
+
onClick: toggle,
|
|
1500
1491
|
children: trigger
|
|
1501
1492
|
}) : /* @__PURE__ */ jsx(IconButton, {
|
|
1502
1493
|
icon: "ph ph-dots-three",
|
|
1503
1494
|
label: triggerLabel,
|
|
1504
1495
|
"aria-haspopup": "menu",
|
|
1505
1496
|
"aria-expanded": open,
|
|
1506
|
-
onClick:
|
|
1507
|
-
if (!open) setMenuStyle(computeMenuStyle());
|
|
1508
|
-
setOpen((v) => !v);
|
|
1509
|
-
}
|
|
1497
|
+
onClick: toggle
|
|
1510
1498
|
}), dropdown && createPortal(dropdown, document.body)]
|
|
1511
1499
|
});
|
|
1512
1500
|
};
|
|
@@ -2000,18 +1988,35 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
|
|
|
2000
1988
|
const selectedDate = parseIsoDate$1(value);
|
|
2001
1989
|
const resolvedLocale = getLocale$1(locale);
|
|
2002
1990
|
const displayValue = formatDisplayDate$1(value, resolvedLocale);
|
|
2003
|
-
const [open, setOpen] = useState(false);
|
|
2004
1991
|
const [viewMode, setViewMode] = useState("days");
|
|
2005
1992
|
const [month, setMonth] = useState(() => startOfMonth$1(selectedDate ?? /* @__PURE__ */ new Date()));
|
|
2006
1993
|
const [yearRangeStart, setYearRangeStart] = useState(() => getYearRangeStart((/* @__PURE__ */ new Date()).getFullYear()));
|
|
2007
|
-
const [panelStyle, setPanelStyle] = useState({});
|
|
2008
1994
|
const [focusedIso, setFocusedIso] = useState(null);
|
|
2009
1995
|
const [inputText, setInputText] = useState("");
|
|
2010
1996
|
const [isEditing, setIsEditing] = useState(false);
|
|
2011
|
-
const rootRef = useRef(null);
|
|
2012
1997
|
const inputRef = useRef(null);
|
|
2013
1998
|
const triggerRef = useRef(null);
|
|
2014
|
-
const panelRef =
|
|
1999
|
+
const { open, setOpen, containerRef: rootRef, panelRef, panelStyle } = useFloatingPanel({
|
|
2000
|
+
onClose: () => {
|
|
2001
|
+
setFocusedIso(null);
|
|
2002
|
+
setViewMode("days");
|
|
2003
|
+
},
|
|
2004
|
+
onEscape: (e) => {
|
|
2005
|
+
e.preventDefault();
|
|
2006
|
+
inputRef.current?.focus();
|
|
2007
|
+
},
|
|
2008
|
+
computeStyle: () => {
|
|
2009
|
+
const rect = triggerRef.current?.getBoundingClientRect();
|
|
2010
|
+
if (!rect) return null;
|
|
2011
|
+
const panelWidth = panelRef.current?.offsetWidth ?? 296;
|
|
2012
|
+
const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
|
|
2013
|
+
const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
|
|
2014
|
+
return {
|
|
2015
|
+
left,
|
|
2016
|
+
top: Math.max(8, top)
|
|
2017
|
+
};
|
|
2018
|
+
}
|
|
2019
|
+
});
|
|
2015
2020
|
const days = useMemo(() => buildCalendarDays$1(month, min, max, resolvedLocale), [
|
|
2016
2021
|
max,
|
|
2017
2022
|
min,
|
|
@@ -2024,52 +2029,6 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
|
|
|
2024
2029
|
useEffect(() => {
|
|
2025
2030
|
if (selectedDate) setMonth(startOfMonth$1(selectedDate));
|
|
2026
2031
|
}, [value]);
|
|
2027
|
-
useEffect(() => {
|
|
2028
|
-
if (!open) {
|
|
2029
|
-
setFocusedIso(null);
|
|
2030
|
-
setViewMode("days");
|
|
2031
|
-
return;
|
|
2032
|
-
}
|
|
2033
|
-
const updatePosition = () => {
|
|
2034
|
-
const rect = triggerRef.current?.getBoundingClientRect();
|
|
2035
|
-
if (!rect) return;
|
|
2036
|
-
const panelWidth = panelRef.current?.offsetWidth ?? 296;
|
|
2037
|
-
const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
|
|
2038
|
-
const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
|
|
2039
|
-
setPanelStyle({
|
|
2040
|
-
left,
|
|
2041
|
-
top: Math.max(8, top)
|
|
2042
|
-
});
|
|
2043
|
-
};
|
|
2044
|
-
updatePosition();
|
|
2045
|
-
window.addEventListener("resize", updatePosition);
|
|
2046
|
-
window.addEventListener("scroll", updatePosition, true);
|
|
2047
|
-
return () => {
|
|
2048
|
-
window.removeEventListener("resize", updatePosition);
|
|
2049
|
-
window.removeEventListener("scroll", updatePosition, true);
|
|
2050
|
-
};
|
|
2051
|
-
}, [open]);
|
|
2052
|
-
useEffect(() => {
|
|
2053
|
-
if (!open) return;
|
|
2054
|
-
const handlePointerDown = (e) => {
|
|
2055
|
-
const target = e.target;
|
|
2056
|
-
if (rootRef.current?.contains(target) || panelRef.current?.contains(target)) return;
|
|
2057
|
-
setOpen(false);
|
|
2058
|
-
};
|
|
2059
|
-
const handleKeyDown = (e) => {
|
|
2060
|
-
if (e.key === "Escape") {
|
|
2061
|
-
e.preventDefault();
|
|
2062
|
-
setOpen(false);
|
|
2063
|
-
inputRef.current?.focus();
|
|
2064
|
-
}
|
|
2065
|
-
};
|
|
2066
|
-
document.addEventListener("pointerdown", handlePointerDown);
|
|
2067
|
-
document.addEventListener("keydown", handleKeyDown);
|
|
2068
|
-
return () => {
|
|
2069
|
-
document.removeEventListener("pointerdown", handlePointerDown);
|
|
2070
|
-
document.removeEventListener("keydown", handleKeyDown);
|
|
2071
|
-
};
|
|
2072
|
-
}, [open]);
|
|
2073
2032
|
useEffect(() => {
|
|
2074
2033
|
if (!open) return;
|
|
2075
2034
|
const initialIso = selectedDate ? toIsoDate$1(selectedDate) : toIsoDate$1(/* @__PURE__ */ new Date());
|
|
@@ -2084,7 +2043,7 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
|
|
|
2084
2043
|
setIsEditing(false);
|
|
2085
2044
|
setInputText("");
|
|
2086
2045
|
window.setTimeout(() => inputRef.current?.focus());
|
|
2087
|
-
}, [onChange]);
|
|
2046
|
+
}, [onChange, setOpen]);
|
|
2088
2047
|
const handleToday = useCallback(() => {
|
|
2089
2048
|
const today = toIsoDate$1(/* @__PURE__ */ new Date());
|
|
2090
2049
|
if (isIsoDateDisabled(today, min, max)) return;
|
|
@@ -2133,7 +2092,7 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
|
|
|
2133
2092
|
const handleCalendarPointerDown = (e) => {
|
|
2134
2093
|
e.preventDefault();
|
|
2135
2094
|
if (readOnly || disabled) return;
|
|
2136
|
-
setOpen(
|
|
2095
|
+
setOpen(!open);
|
|
2137
2096
|
};
|
|
2138
2097
|
const handleDayKeyDown = useCallback((e, iso) => {
|
|
2139
2098
|
if (![
|
|
@@ -2169,7 +2128,8 @@ const DatePicker = forwardRef(function DatePicker({ id, name, value, placeholder
|
|
|
2169
2128
|
}, [
|
|
2170
2129
|
days,
|
|
2171
2130
|
month,
|
|
2172
|
-
commitValue
|
|
2131
|
+
commitValue,
|
|
2132
|
+
panelRef
|
|
2173
2133
|
]);
|
|
2174
2134
|
const handleMonthSelect = (monthIndex) => {
|
|
2175
2135
|
setMonth(new Date(month.getFullYear(), monthIndex, 1));
|
|
@@ -2548,18 +2508,14 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
|
|
|
2548
2508
|
const endDisplay = formatDisplayDate(endValue, resolvedLocale);
|
|
2549
2509
|
const startIso = parseIsoDate(startValue) ? startValue : null;
|
|
2550
2510
|
const endIso = parseIsoDate(endValue) ? endValue : null;
|
|
2551
|
-
const [open, setOpen] = useState(false);
|
|
2552
2511
|
const [phase, setPhase] = useState("idle");
|
|
2553
2512
|
const [pendingStart, setPendingStart] = useState(null);
|
|
2554
2513
|
const [hoverIso, setHoverIso] = useState(null);
|
|
2555
2514
|
const [month, setMonth] = useState(() => startOfMonth(parseIsoDate(startValue) ?? /* @__PURE__ */ new Date()));
|
|
2556
|
-
const [panelStyle, setPanelStyle] = useState({});
|
|
2557
2515
|
const [startText, setStartText] = useState("");
|
|
2558
2516
|
const [endText, setEndText] = useState("");
|
|
2559
2517
|
const [editingField, setEditingField] = useState(null);
|
|
2560
|
-
const rootRef = useRef(null);
|
|
2561
2518
|
const triggerRef = useRef(null);
|
|
2562
|
-
const panelRef = useRef(null);
|
|
2563
2519
|
const startInputRef = useRef(null);
|
|
2564
2520
|
const endInputRef = useRef(null);
|
|
2565
2521
|
const days = useMemo(() => buildCalendarDays(month, min, max, resolvedLocale), [
|
|
@@ -2574,54 +2530,31 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
|
|
|
2574
2530
|
const anchor = parseIsoDate(startValue) ?? parseIsoDate(endValue);
|
|
2575
2531
|
if (anchor) setMonth(startOfMonth(anchor));
|
|
2576
2532
|
}, [startValue]);
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2533
|
+
const { open, setOpen, containerRef: rootRef, panelRef, panelStyle } = useFloatingPanel({
|
|
2534
|
+
onClose: () => {
|
|
2535
|
+
setPhase("idle");
|
|
2536
|
+
setPendingStart(null);
|
|
2537
|
+
setHoverIso(null);
|
|
2538
|
+
},
|
|
2539
|
+
onEscape: (e) => {
|
|
2540
|
+
e.preventDefault();
|
|
2541
|
+
startInputRef.current?.focus();
|
|
2542
|
+
},
|
|
2543
|
+
computeStyle: () => {
|
|
2580
2544
|
const rect = triggerRef.current?.getBoundingClientRect();
|
|
2581
|
-
if (!rect) return;
|
|
2545
|
+
if (!rect) return null;
|
|
2582
2546
|
const panelWidth = panelRef.current?.offsetWidth ?? 296;
|
|
2583
2547
|
const left = Math.min(Math.max(8, rect.left), window.innerWidth - panelWidth - 8);
|
|
2584
2548
|
const top = Math.min(rect.bottom + 6, window.innerHeight - 380);
|
|
2585
|
-
|
|
2549
|
+
return {
|
|
2586
2550
|
left,
|
|
2587
2551
|
top: Math.max(8, top)
|
|
2588
|
-
}
|
|
2589
|
-
}
|
|
2590
|
-
|
|
2591
|
-
window.addEventListener("resize", update);
|
|
2592
|
-
window.addEventListener("scroll", update, true);
|
|
2593
|
-
return () => {
|
|
2594
|
-
window.removeEventListener("resize", update);
|
|
2595
|
-
window.removeEventListener("scroll", update, true);
|
|
2596
|
-
};
|
|
2597
|
-
}, [open]);
|
|
2552
|
+
};
|
|
2553
|
+
}
|
|
2554
|
+
});
|
|
2598
2555
|
const closePanel = useCallback(() => {
|
|
2599
2556
|
setOpen(false);
|
|
2600
|
-
|
|
2601
|
-
setPendingStart(null);
|
|
2602
|
-
setHoverIso(null);
|
|
2603
|
-
}, []);
|
|
2604
|
-
useEffect(() => {
|
|
2605
|
-
if (!open) return;
|
|
2606
|
-
const handlePointerDown = (e) => {
|
|
2607
|
-
const target = e.target;
|
|
2608
|
-
if (rootRef.current?.contains(target) || panelRef.current?.contains(target)) return;
|
|
2609
|
-
closePanel();
|
|
2610
|
-
};
|
|
2611
|
-
const handleKeyDown = (e) => {
|
|
2612
|
-
if (e.key === "Escape") {
|
|
2613
|
-
e.preventDefault();
|
|
2614
|
-
closePanel();
|
|
2615
|
-
startInputRef.current?.focus();
|
|
2616
|
-
}
|
|
2617
|
-
};
|
|
2618
|
-
document.addEventListener("pointerdown", handlePointerDown);
|
|
2619
|
-
document.addEventListener("keydown", handleKeyDown);
|
|
2620
|
-
return () => {
|
|
2621
|
-
document.removeEventListener("pointerdown", handlePointerDown);
|
|
2622
|
-
document.removeEventListener("keydown", handleKeyDown);
|
|
2623
|
-
};
|
|
2624
|
-
}, [open, closePanel]);
|
|
2557
|
+
}, [setOpen]);
|
|
2625
2558
|
const commitRange = useCallback((start, end) => {
|
|
2626
2559
|
const [a, b] = start <= end ? [start, end] : [end, start];
|
|
2627
2560
|
onChange?.(a, b);
|
|
@@ -2723,7 +2656,7 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
|
|
|
2723
2656
|
const handleCalendarPointerDown = (e) => {
|
|
2724
2657
|
e.preventDefault();
|
|
2725
2658
|
if (readOnly || disabled) return;
|
|
2726
|
-
setOpen(
|
|
2659
|
+
setOpen(!open);
|
|
2727
2660
|
};
|
|
2728
2661
|
const handleClear = (e) => {
|
|
2729
2662
|
e.preventDefault();
|
|
@@ -2921,41 +2854,6 @@ function DateRangePicker({ startValue, endValue, placeholder = "dd/mm/yyyy", dis
|
|
|
2921
2854
|
});
|
|
2922
2855
|
}
|
|
2923
2856
|
//#endregion
|
|
2924
|
-
//#region packages/ui/useFloatingPanel.ts
|
|
2925
|
-
function useFloatingPanel(options = {}) {
|
|
2926
|
-
const [open, setOpenState] = useState(false);
|
|
2927
|
-
const containerRef = useRef(null);
|
|
2928
|
-
const onCloseRef = useRef(options.onClose);
|
|
2929
|
-
onCloseRef.current = options.onClose;
|
|
2930
|
-
const setOpen = useCallback((next) => {
|
|
2931
|
-
setOpenState(next);
|
|
2932
|
-
if (!next) onCloseRef.current?.();
|
|
2933
|
-
}, []);
|
|
2934
|
-
const toggle = useCallback(() => setOpen(!open), [open, setOpen]);
|
|
2935
|
-
useEffect(() => {
|
|
2936
|
-
if (!open) return;
|
|
2937
|
-
const handlePointerDown = (e) => {
|
|
2938
|
-
const container = containerRef.current;
|
|
2939
|
-
if (container && !container.contains(e.target)) setOpen(false);
|
|
2940
|
-
};
|
|
2941
|
-
const handleKeyDown = (e) => {
|
|
2942
|
-
if (e.key === "Escape") setOpen(false);
|
|
2943
|
-
};
|
|
2944
|
-
document.addEventListener("pointerdown", handlePointerDown);
|
|
2945
|
-
document.addEventListener("keydown", handleKeyDown);
|
|
2946
|
-
return () => {
|
|
2947
|
-
document.removeEventListener("pointerdown", handlePointerDown);
|
|
2948
|
-
document.removeEventListener("keydown", handleKeyDown);
|
|
2949
|
-
};
|
|
2950
|
-
}, [open, setOpen]);
|
|
2951
|
-
return {
|
|
2952
|
-
open,
|
|
2953
|
-
setOpen,
|
|
2954
|
-
toggle,
|
|
2955
|
-
containerRef
|
|
2956
|
-
};
|
|
2957
|
-
}
|
|
2958
|
-
//#endregion
|
|
2959
2857
|
//#region packages/ui/Popover.tsx
|
|
2960
2858
|
const Popover = ({ trigger, panel, align = "end", ariaLabel, className }) => {
|
|
2961
2859
|
const { open, setOpen, toggle, containerRef } = useFloatingPanel();
|