UNPKG

vuux

Version:

Vue3 Nuxt3 Nuxt4 组件库

114 lines (113 loc) 4.6 kB
import h from "../day/index.mjs"; import { ref as j, toRaw as C, reactive as Y, computed as E, onMounted as V } from "vue"; import { getMonthDate as c } from "../date.mjs"; const H = (g, u, M) => { const o = j(C(u.value)), { startKey: s, endKey: l } = g, m = /* @__PURE__ */ new Date(), w = m.getFullYear(), $ = m.getMonth() + 1, S = m.getDate(), P = h(`${w}-${$}-${S}`), e = Y({ year1: 0, year2: 0, month1: 0, month2: 0 }), d = Y({ leftPanel: c(1), rightPanel: c(2) }), r = (t) => { t === "left" ? d.leftPanel = c(e.year1, e.month1) : d.rightPanel = c(e.year2, e.month2); }; r("left"), r("right"); const _ = () => { e.month1 > 1 ? e.month1 -= 1 : (e.year1 -= 1, e.month1 = 12), r("left"); }, k = () => { e.month1 < 12 ? e.month1 += 1 : (e.year1 += 1, e.month1 = 1), e.year1 > e.year2 && (e.year2 = e.year1), e.year1 === e.year2 && e.month1 >= e.month2 && (e.month1 === 12 ? (e.month2 = 1, e.year2 += 1) : e.month2 = e.month1 + 1), r("left"), r("right"); }, p = () => { e.month2 > 1 ? e.month2 -= 1 : (e.year2 -= 1, e.month2 = 12), e.year2 < e.year1 && (e.year1 = e.year2), e.year1 === e.year2 && e.month2 <= e.month1 && (e.month2 === 1 ? (e.month1 = 12, e.year1 -= 1) : e.month1 = e.month2 - 1), r("left"), r("right"); }, b = () => { e.month2 < 12 ? e.month2 += 1 : (e.year2 += 1, e.month2 = 1), r("right"); }, x = () => { e.year1 -= 1, r("left"); }, B = () => { e.year1 += 1, e.year1 > e.year2 && (e.year2 = e.year1), e.year1 === e.year2 && e.month1 >= e.month2 && (e.month1 === 12 ? (e.month2 = 1, e.year2 += 1) : e.month2 = e.month1 + 1), r("left"), r("right"); }, A = () => { e.year2 -= 1, e.year2 < e.year1 && (e.year1 = e.year2), e.year1 === e.year2 && e.month2 <= e.month1 && (e.month2 === 1 ? (e.month1 = 12, e.year1 -= 1) : e.month1 = e.month2 - 1), r("left"), r("right"); }, N = () => { e.year2 += 1, r("right"); }; let v = 0, y = ""; const R = (t) => { if (t.date != t.showDate || v % 2 === 0) return; const a = `${t.year}-${t.month}-${t.showDate}`; o.value = { [s]: y, [l]: a }; }, F = (t, a) => { if (a == "left" && t.month !== e.month1 || a == "right" && t.month !== e.month2 || (v += 1, !o.value) || t.date != t.showDate) return; const n = `${t.year}-${t.month}-${t.showDate}`, f = h(n); v % 2 === 0 ? (f.isBefore(h(o.value[s])) ? (o.value[s] = h(n).format("YYYY-MM-DD"), o.value[l] = h(y).format("YYYY-MM-DD")) : (o.value[s] = h(y).format("YYYY-MM-DD"), o.value[l] = h(n).format("YYYY-MM-DD")), u.value = { ...o.value }, M("change", o.value)) : (o.value[s] = n, o.value[l] = "", y = n); }, K = E(() => (t, a) => { if (!a.year || !a.month || !a.showDate) return; const n = h(`${a.year}-${a.month}-${a.showDate}`), f = h(o.value[s]), i = h(o.value[l]); switch (t) { case "today": if (P.isSame(n) && a.date === a.showDate) return !0; break; case "rangeMonth": if (a.date === a.showDate) return "current-month"; break; case "notRangeMonth": if (a.date !== a.showDate) return ""; break; case "selected": if ((f.isSame(n) || i.isSame(n)) && a.date === a.showDate) return "is-selected"; break; case "rangeSelected": if (a.date !== a.showDate) return; if (f.isSame(n)) return f.isAfter(i) ? "is-selecteds-end" : "is-selecteds-start"; if (i.isSame(n)) return f.isAfter(i) ? "is-selecteds-start" : "is-selecteds-end"; if (f.isBefore(i)) { if (f.isBefore(n) && i.isAfter(n) && a.date === a.showDate) return "is-selecteds"; } else if (i.isBefore(n) && f.isAfter(n)) return "is-selecteds"; break; default: return ""; } }), D = () => { let t = { ...u.value }; t || (t = { [s]: void 0, [l]: void 0 }), o.value = t, e.year1 = h(t[s]).year(), e.month1 = h(t[s]).month() + 1, e.year2 = h(t[l]).year(), e.month2 = h(t[l]).month() + 1, e.year1 === e.year2 && e.month1 === e.month2 && (e.month2 += 1), e.month2 > 12 && (e.year2 += 1, e.month2 = 1), r("left"), r("right"); }; return V(() => { D(); }), { state: d, dateState: e, computeClass: K, init: D, setValue: F, hoverValueFn: R, handlePrveYear1: x, handlePrveMonth1: _, handleNextMonth1: k, handleNextYear1: B, handlePrveYear2: A, handlePrveMonth2: p, handleNextMonth2: b, handleNextYear2: N }; }; export { H as useRange };