UNPKG

ayongui

Version:
217 lines (216 loc) 8.13 kB
import { j as a } from "../../../react/jsx-runtime.mjs"; import xe, { useState as f, useEffect as $, useMemo as U, useImperativeHandle as $e } from "react"; import { parseTime as be, handleCreateDatePicker as V, isValidDate as ke, handleCrateDate as R, getDaysInMonth as X } from "./utils.mjs"; import "./index.less.mjs"; let Z = [], b = []; const S = [], z = {}, Pe = xe.forwardRef( ({ style: O, value: k, className: W, defaultValue: C, yearsRange: N = [1970, 2099], selectedMode: s = "noSelect", startOfWeek: i, rangMode: _ = "", picker: ee = "day", disabled: ne = !1, dayCellRender: P = null, dateRender: T = null, dateSelected: Ce = () => { }, onChange: g = (l) => { }, curYearChange: Ne = () => { }, curMonthChange: Ye = () => { }, disabledTime: A = () => !1, headerRender: E = null, footerRender: G = null }, te) => { const l = "{d}", se = { width: 120, height: 100, ...O }; let m = []; C instanceof Date && (m = C.toISOString().slice(0, 10).split("-"), m[1] && (m[1] = m[1] - 1)); const [w, re] = f(["一", "二", "三", "四", "五", "六", "日"]), [c, D] = f(Number(m[0]) || (/* @__PURE__ */ new Date()).getFullYear()), [o, p] = f(Number(m[1]) || (/* @__PURE__ */ new Date()).getMonth()), [ae] = f(be((/* @__PURE__ */ new Date()).getTime()), l), [H, ce] = f([]), [h, j] = f([]), [oe, M] = f(null), [x, le] = f(0), [v, ie] = f([]); f(0), $(() => { re([...w.slice(i - 1), ...w.slice(0, i - 1)]), u(c, o, i, l); }, [c, o, C]); const ue = U(() => x < 0 ? v[0] || [] : x > v.length - 1 ? v[v.length - 1] || [] : v[x] || [], [x]); $(() => { if (s === "month" && (Z = [ { value: "1月" }, { value: "2月" }, { value: "3月" }, { value: "4月" }, { value: "5月" }, { value: "6月" }, { value: "7月" }, { value: "8月" }, { value: "9月" }, { value: "10月" }, { value: "11月" }, { value: "12月" } ]), s === "year") { b = V(N).years.slice(0, 58).map((e) => ({ value: e.label })); for (let e = 0; e < b.length; e++) e % 12 == 0 && S.push([]), S[S.length - 1].push(b[e]), z[b[e].value.replace("年", "")] = S.length - 1; le((e) => z[c]), ie(S); } }, []), $(() => { if (!ke(k)) return; const e = k.split("-"); if (e[0] < N[0] || e[1] > N[1]) return console.warn("超出日期范围"); u(e[0], e[1] - 1, i, l); }, [k]); const u = async (e, n, t, r) => { const y = R({ year: e, month: n, start: 1, end: X(e, n), cFormat: r }), L = (/* @__PURE__ */ new Date(`${e}-${n + 1}-01`)).getDay(), I = { 1: "一", 2: "二", 3: "三", 4: "四", 5: "五", 6: "六", 0: "日" }[L], Q = w.indexOf(I), Se = R({ year: e, month: n, start: 1, end: Q + 1, type: "prev", cFormat: r }), ge = R({ year: e, month: n, start: 1, end: 42 - X(e, n) - Q, type: "rear", cFormat: r }), we = [...Se, ...y, ...ge], je = he(we); ce(je); }, he = (e, n = 7) => { const t = []; for (let r = 0; r < n; r++) { const y = e.slice(r * n, r * n + n); t.push(y); } return t; }, q = () => { o + 1 < 12 ? (p((e) => e + 1), u(c, o - 1, i, l)) : Y(); }, B = () => { o > 0 ? (p((e) => e - 1), u(c, o - 1, i, l)) : F(); }, Y = () => { D((e) => e + 1), u(c, o - 1, i, l), p(0); }, F = () => { D((e) => e - 1), u(c, o - 1, i, l), p(0); }, fe = (e = "") => { const n = e.split("-"), t = Number(n[0]), r = Number(n[1]) - 1; j({ date: n[2].toString().padStart(2, "0"), comprehensiveStr: n.join("-"), comprehensive: new Date(n.join("-")), isSelected: !0, isRangeSelected: !0, isToday: !0 }), D(t), p(r), u(t, r, i, l); }, pe = ({ year: e, month: n }) => { D(e), p(n), u(e, n, 1, l); }, de = U(() => { const e = {}; if (Array.isArray(h)) return h.map((n) => { e[n.comprehensiveStr] = n; }), e; if (Object.keys(h).length) { const n = h.comprehensiveStr.substring(0, 10); e[n] = h; } return e; }, [h]); $(() => { s === "multiple" && g(h); }, [h]); const J = (e, n, t, r, y = !1) => { if (!A(n) && (e == null || e.stopPropagation(), !ne)) { if (s === "week" && M(t), s === "noSelect") { const d = new Date(n.comprehensiveStr).getFullYear(); d === c ? (n.monthSortMode === 1 && B(), n.monthSortMode === 2 && o <= 12 && q()) : (d > c && Y(), d < c && F()); } s === "multiple" && j((d) => d.includes(n) ? d.filter((I) => I !== n) : [...d, n]), ["day", "single"].includes(s) && j(n), !y && (s === "week" && g(H[t], `${c}-${o + 1} ${t + 1}周`), ["day", "single"].includes(s) && g(n)); } }, ye = (e, n) => { let t = ""; _ === "rangbefore" && (t = `${e.comprehensive > h.comprehensive ? "range" : ""}`), _ === "rangeafter" && (t = `${e.comprehensive < h.comprehensive ? "range" : ""}`); const r = A(e) ? "dayDisabled" : ""; return `${e.monthSortMode ? "notCurMonth" : ""} ${e.date === ae ? "currentDay" : ""} ${e.isRangeSelected ? "rangeSelectd" : ""} ${e.isSelected ? "onSelect" : ""} ${e.isWeekend ? "weekend" : ""} ${e.isToday ? "today" : ""} ${de[e.comprehensiveStr] ? "onSelect" : ""} ${ee === "week" && n == oe ? "weekRow" : ""} ${t} ${r} `; }, me = (e) => { s === "year" && g({ comprehensiveStr: e }), u(e, o - 1, i, l), D(e); }, K = (e) => { u(c, e, i, l), p(e - 1); }, De = () => { s === "week" && M(null), s === "day" && j(() => ({})), s === "month" && p(-1), s === "year" && D(null); }; $e(te, () => ({ handleGetDays: u, handleCreateDatePicker: V, onYearChange: me, onMonthChange: K, prevMonth: B, nextMonth: q, nextYear: Y, prevYear: F, setDateSelected: fe, clearSetSelectedDates: De, handleItemClick: J, setCountYYYMMM: pe })); const ve = (e) => { e.stopPropagation(); }; return /* @__PURE__ */ a.jsxs("div", { onClick: ve, className: `calendar ${W}`, children: [ typeof E == "function" && E({ curYear: c, curMonth: o, years: ue }), typeof T == "function" ? T() : /* @__PURE__ */ a.jsxs(a.Fragment, { children: [ ["day", "single", "noSelect", "multiple", "week"].includes(s) && /* @__PURE__ */ a.jsxs("table", { className: "calendar-table", children: [ /* @__PURE__ */ a.jsx("thead", { children: /* @__PURE__ */ a.jsx("tr", { children: w.map((e, n) => /* @__PURE__ */ a.jsx("th", { className: "week", children: e }, n)) }) }), /* @__PURE__ */ a.jsx("tbody", { children: H.map((e, n) => /* @__PURE__ */ a.jsx( "tr", { children: e.map((t, r) => /* @__PURE__ */ a.jsx( "td", { onClick: (y) => J(y, t, n), children: typeof P == "function" ? /* @__PURE__ */ a.jsx(a.Fragment, { children: P(t) }) : /* @__PURE__ */ a.jsx( "div", { style: se, className: `day ${ye(t, n)} `, children: t.date } ) }, r )) }, n )) }) ] }), s === "month" && /* @__PURE__ */ a.jsx("main", { className: "block-12", children: Z.map((e, n) => /* @__PURE__ */ a.jsx( "div", { className: `block-chlid ${e.value === o + 1 + "月" ? "onSelect" : ""}`, onClick: () => { K(e); }, children: e.value }, n )) }) ] }), typeof G == "function" && G(), /* @__PURE__ */ a.jsx("div", { className: "background" }) ] }); } ); export { Pe as default };