cynx-ui 1.2.35 → 1.2.36
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/components/DatePicker.jsx +39 -15
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState, useRef, useEffect } from 'react';
|
|
1
|
+
import { useState, useRef, useEffect, useLayoutEffect, useCallback } from 'react';
|
|
2
2
|
import { createPortal } from 'react-dom';
|
|
3
3
|
import { ChevronLeft, ChevronRight, Calendar } from 'lucide-react';
|
|
4
4
|
|
|
@@ -58,20 +58,44 @@ export function DatePicker({ value, onChange, placeholder = 'Select date', badge
|
|
|
58
58
|
if (d) setCursor(d);
|
|
59
59
|
}, [value]);
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
61
|
+
const calcPos = useCallback(() => {
|
|
62
|
+
if (!btnRef.current) return;
|
|
63
|
+
const r = btnRef.current.getBoundingClientRect();
|
|
64
|
+
const dropW = Math.max(r.width, 260);
|
|
65
|
+
const dropH = dropRef.current ? dropRef.current.offsetHeight : 280;
|
|
66
|
+
|
|
67
|
+
let left = r.left;
|
|
68
|
+
if (left + dropW > window.innerWidth - 12) {
|
|
69
|
+
left = Math.max(12, r.right - dropW);
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
if (left < 12) left = 12;
|
|
72
|
+
|
|
73
|
+
let top = r.bottom + 6;
|
|
74
|
+
if (top + dropH > window.innerHeight - 12 && r.top - 6 - dropH > 12) {
|
|
75
|
+
top = r.top - 6 - dropH;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
setPos({ top, left, width: dropW });
|
|
79
|
+
}, []);
|
|
80
|
+
|
|
81
|
+
useLayoutEffect(() => {
|
|
82
|
+
if (!open) return;
|
|
83
|
+
calcPos();
|
|
84
|
+
window.addEventListener('scroll', calcPos, true);
|
|
85
|
+
window.addEventListener('resize', calcPos);
|
|
86
|
+
return () => {
|
|
87
|
+
window.removeEventListener('scroll', calcPos, true);
|
|
88
|
+
window.removeEventListener('resize', calcPos);
|
|
89
|
+
};
|
|
90
|
+
}, [open, calcPos]);
|
|
91
|
+
|
|
92
|
+
const toggleOpen = () => {
|
|
93
|
+
if (disabled) return;
|
|
94
|
+
if (!open) {
|
|
95
|
+
calcPos();
|
|
96
|
+
}
|
|
97
|
+
setOpen(o => !o);
|
|
98
|
+
};
|
|
75
99
|
|
|
76
100
|
function pick(date) {
|
|
77
101
|
if (typeof value === 'string') {
|
|
@@ -240,7 +264,7 @@ export function DatePicker({ value, onChange, placeholder = 'Select date', badge
|
|
|
240
264
|
return (
|
|
241
265
|
<div ref={ref} style={{ position: 'relative', display: 'inline-block', ...style }}>
|
|
242
266
|
{/* Trigger */}
|
|
243
|
-
<button ref={btnRef} type="button" onClick={
|
|
267
|
+
<button ref={btnRef} type="button" onClick={toggleOpen} style={{
|
|
244
268
|
display: 'flex', alignItems: 'center', gap: 7,
|
|
245
269
|
padding: '5px 10px', borderRadius: 'var(--rads)',
|
|
246
270
|
border: '1px solid var(--border)', background: 'var(--surface)',
|