@rnwonder/react-date-picker
Version:
A lightweight, customizable, and accessible date picker component for React applications.
168 lines (167 loc) • 4.48 kB
JavaScript
"use client";
import { jsxs as K, jsx as i } from "react/jsx-runtime";
import { useState as a, useRef as O, useCallback as U, useEffect as l } from "react";
import { C as V } from "./index-DtmLLrql.js";
import { d as W } from "./utils-CJCqI964.js";
import { c as P } from "./class-DLUjLwnh.js";
const Q = (c) => {
const [S, z] = a(), [x, A] = a(), [I, s] = a(!1), [d, b] = a(!1), [w, R] = a(!1), C = O(null), f = O(null), {
positionX: r = "center",
positionY: o = "auto",
isShown: k,
setIsShown: t,
onClickOutside: u,
handleChildrenClick: D,
className: T,
onOpen: m,
onClose: p,
zIndex: j = 1e3,
width: L,
contentClassName: X,
portalContainer: y,
useRefWidth: Y,
content: E,
children: $
} = c, h = U(
(e) => {
if (e || (e = {
x: r,
y: o
}), !f.current || !C.current) return;
const { left: v, top: H } = W({
inputRef: () => f.current,
dropDownRef: () => C.current,
positionX: e.x,
positionY: e.y
});
A(v), z(H);
},
[r, o]
), n = k || I;
l(() => {
if (!n) return;
const e = () => {
h({
y: o,
x: r
});
};
return document.addEventListener("scroll", e), window.addEventListener("resize", e), () => {
document.removeEventListener("scroll", e), window.removeEventListener("resize", e);
};
}, [n, h, r, o]), l(() => {
if (n) {
const e = setTimeout(() => {
b(!0);
}, 100);
return () => clearTimeout(e);
} else
b(!1);
}, [n]), l(() => {
if (!n && w) {
R(!1), p == null || p();
return;
}
n && !w && (R(!0), m == null || m());
}, [n, w, m, p]), l(() => {
d && h({
y: o,
x: r
});
}, [d, h, r, o]), l(() => {
if (y || document.getElementById("portal-island")) return;
const v = document.createElement("div");
v.id = "portal-island", document.body.appendChild(v);
}, [y]);
const g = () => {
if (D) {
D(t || s);
return;
}
t ? t(!k) : s(!I);
}, N = () => {
t ? t(!1) : s(!1);
}, B = () => {
if (typeof E == "function") {
const e = E({ close: N });
return /* @__PURE__ */ i("div", { "data-type": "dropdown", children: e });
}
return /* @__PURE__ */ i("div", { "data-type": "dropdown", children: E });
};
return /* @__PURE__ */ K(
"div",
{
onKeyUp: (e) => {
e.key === "Escape" && n && N();
},
children: [
/* @__PURE__ */ i(
"div",
{
style: {
...L && { width: L || "100%" }
},
className: P(T),
ref: f,
onClick: g,
children: $
}
),
/* @__PURE__ */ i(
V,
{
setIsShown: t || s,
isShown: n,
...y ? { portalContainer: y } : { referenceId: "portal-island" },
hideDefaultStyle: !0,
onClickOutside: u ? (e) => u == null ? void 0 : u(e, t || s) : void 0,
reference: f.current,
useRefWidth: Y,
style: {
zIndex: j,
position: "fixed",
...S && { top: S },
...x && { left: x }
},
children: /* @__PURE__ */ i(
"div",
{
className: `
${c.disableOpenAnimation || d ? "rn-translate-y-[0rem] rn-opacity-100" : "-rn-translate-y-[1rem] rn-opacity-0"}
rn-duration-350
rn-delay-50
rn-transition-transform
rn-ease-in-out
motion-reduce:rn-transition-none
`,
ref: C,
children: /* @__PURE__ */ i(
"div",
{
className: P(
`
${d ? "scale-100 rn-opacity-100" : "scale-90 rn-opacity-0"}
rn-transition-opacity
rn-ease-in-out
motion-reduce:rn-transition-none
`,
{
"rn-duration-350 ": !c.disableOpenAnimation,
"rn-duration-0 ": c.disableOpenAnimation
},
X
),
children: B()
}
)
}
)
}
)
]
}
);
};
export {
Q as P
};