UNPKG

@brightsoftware/date-np

Version:

Simple & minimal Nepali date picker that just works.

102 lines (101 loc) 4.38 kB
import { jsx as t, jsxs as M } from "react/jsx-runtime"; import "react"; import { MAX_AD_YEAR as Y, MAX_BS_YEAR as A, MIN_AD_YEAR as R, MIN_BS_YEAR as b } from "../../../data/constants.js"; import { CALENDAR as x } from "../../../data/locale.js"; import { getTotalDaysInMonth as P, getStartingDayOfMonth as B, getEndingDayOfMonth as C } from "../../../utils/helpers.js"; import { useRangePicker as j } from "../hooks/useRangePicker.js"; import I from "./range-day.js"; import O from "./range-month.js"; import X from "./range-year.js"; import { RangeWeekRow as L } from "./range-week-row.js"; import { NepaliDate as U } from "../../NepaliDate.js"; import S from "./empty-range-day.js"; const at = ({ panel: l }) => { const { rangePickerState: d, resetToOriginalState: _ } = j(), { locale: e, shouldShowSinglePanel: v } = d, w = l === "left" ? d.leftPanel : d.rightPanel, i = l === "left" ? d.rightPanel : d.leftPanel, { activeYear: r, activeMonth: h, mode: g } = w, E = e === "en" && r === R || e === "ne" && r === b || e === "en" && r === Y || e === "ne" && r === A, T = () => { _(); }; if (g === "date" && E) return /* @__PURE__ */ t("div", { className: "flex items-center justify-center w-full h-72 text-center", children: /* @__PURE__ */ M("div", { className: "text-gray-600", children: [ /* @__PURE__ */ t("div", { className: "text-lg font-medium mb-2", children: "Date not supported" }), /* @__PURE__ */ t("div", { className: "text-sm text-gray-500 mb-4", children: e === "en" ? "Dates before 1945 are not supported" : "Dates before 2001 are not supported" }), /* @__PURE__ */ t( "button", { onClick: T, className: "px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 cursor-pointer", children: "Pick another" } ) ] }) }); const m = (o, s, a) => e === "ne" ? new U(o, s, a || 1) : new Date(o, s, a || 1); if (g === "date") { const o = m(r, h, 1), s = P({ date: o, locale: e }), a = B({ date: o, locale: e }), n = m(r, h, s), p = m(r, h - 1, 1), c = C({ date: n, locale: e }), k = P({ date: p, locale: e }), N = a; return /* @__PURE__ */ M("div", { className: "w-full", children: [ /* @__PURE__ */ t("div", { className: "grid grid-cols-7 gap-1 mb-2", children: /* @__PURE__ */ t(L, { locale: e }) }), /* @__PURE__ */ M("div", { className: "grid grid-cols-7 gap-1", children: [ /* @__PURE__ */ t(() => [...Array(N)].map((D, f) => { const u = k - (N - (f + 1)); return m(r, h - 1, u), /* @__PURE__ */ t( S, {} ); }), {}), /* @__PURE__ */ t(() => [...Array(s)].map((D, f) => { const u = f + 1, y = m(r, h, u); return /* @__PURE__ */ t( I, { date: y, panel: l }, f ); }), {}), /* @__PURE__ */ t(() => { const D = 6 - c; return [...Array(D)].map((f, u) => { const y = u + 1; return m(r, h + 1, y), /* @__PURE__ */ t( S, {} ); }); }, {}) ] }) ] }); } if (g === "month") { const o = e === "en" ? x.AD.months : x.BS.months; return /* @__PURE__ */ t("div", { className: "grid grid-cols-3 gap-2 p-2", children: o.map((s, a) => { let n = !0; return l === "left" ? (r === i.activeYear && a >= i.activeMonth && (n = !1), r > i.activeYear && (n = !1), v && (n = !0)) : (r === i.activeYear && a <= i.activeMonth && (n = !1), r < i.activeYear && (n = !1)), n ? /* @__PURE__ */ t( O, { month: a, panel: l }, a ) : null; }) }); } if (g === "year") { const o = e === "en" ? R : b, s = e === "en" ? Y : A; let a = o + 1, n = s - 1; l === "left" ? (n = Math.min(s, i.activeYear), v && (a = o + 1, n = s - 1)) : a = Math.max(o, i.activeYear); const p = []; for (let c = a; c <= n; c++) p.push(c); return /* @__PURE__ */ t("div", { className: "grid grid-cols-4 gap-2 p-2 max-h-60 overflow-y-auto", children: p.map((c) => /* @__PURE__ */ t( X, { year: c, panel: l }, c )) }); } return null; }; export { at as default };