@brightsoftware/date-np
Version:
Simple & minimal Nepali date picker that just works.
279 lines (278 loc) • 8.34 kB
JavaScript
import { jsx as g } from "react/jsx-runtime";
import { createContext as x, useState as w, useContext as b } from "react";
const $ = x(null);
let M = {
onTimeChange: (c) => {
},
onVisibilityChange: (c) => {
}
};
const J = (c = M.onTimeChange, f = M.onVisibilityChange) => {
M.onTimeChange = c, M.onVisibilityChange = f;
const l = b($);
if (!l)
throw new Error("useTimePicker must be used within a TimePickerProvider");
const { timePickerState: r, setTimePickerState: u } = l, d = (e) => {
let t = e;
r.format === "24hr" ? t = Math.max(0, Math.min(23, e)) : t = Math.max(1, Math.min(12, e));
const n = {
...r,
selectedTime: {
...r.selectedTime,
hours: t
}
};
u(n), c == null || c(n.selectedTime);
}, a = (e, t = {}) => {
const n = {
...r,
selectedTime: {
...r.selectedTime,
minutes: Math.max(0, Math.min(59, e))
},
...t
};
u(n), c == null || c(n.selectedTime);
}, P = (e) => {
const t = {
...r,
selectedTime: {
...r.selectedTime,
seconds: Math.max(0, Math.min(59, e))
}
};
c == null || c(t.selectedTime), u(t);
}, T = (e) => {
if (r.format === "am/pm") {
const t = {
...r,
selectedTime: {
...r.selectedTime,
period: e
}
};
c == null || c(t.selectedTime), u(t);
}
};
return {
timePickerState: r,
updateHours: d,
updateMinutes: a,
updateSeconds: P,
updatePeriod: T,
incrementHours: () => {
const e = r.selectedTime.hours;
r.format === "24hr" ? d(e === 23 ? 0 : e + 1) : d(e === 12 ? 1 : e + 1);
},
decrementHours: () => {
const e = r.selectedTime.hours;
r.format === "24hr" ? d(e === 0 ? 23 : e - 1) : d(e === 1 ? 12 : e - 1);
},
incrementMinutes: () => {
const e = r.selectedTime.minutes;
a(e === 59 ? 0 : e + 1);
},
decrementMinutes: () => {
const e = r.selectedTime.minutes;
a(e === 0 ? 59 : e - 1);
},
incrementSeconds: () => {
const e = r.selectedTime.seconds;
P(e === 59 ? 0 : e + 1);
},
decrementSeconds: () => {
const e = r.selectedTime.seconds;
P(e === 0 ? 59 : e - 1);
},
togglePeriod: () => {
r.format === "am/pm" && r.selectedTime.period && T(r.selectedTime.period === "AM" ? "PM" : "AM");
},
setVisibility: (e) => {
u((t) => ({
...t,
isVisible: e
})), f == null || f(e);
},
getFormattedTime: (e) => {
const { hours: t, minutes: n, seconds: S, period: p } = e ?? r.selectedTime, h = t.toString().padStart(2, "0"), o = n.toString().padStart(2, "0"), s = S.toString().padStart(2, "0"), { shouldInclude: i } = r;
if (r.format === "am/pm")
return `${h}:${o}:${s} ${p}`;
let m = "";
return i.hours && (m += `${h}:`), i.minutes && (m += `${o}:`), i.seconds ? m += `${s}` : m = m.slice(0, -1), m.trimEnd();
},
setCurrentInputPosition: (e) => {
u((t) => ({
...t,
currentInputPosition: e,
inputBuffer: ""
// Clear buffer when changing position
}));
},
handleKeyInput: (e) => {
const { shouldInclude: t } = r;
if (!/^\d$/.test(e)) return !1;
const n = parseInt(e), { currentInputPosition: S, inputBuffer: p, format: h } = r;
switch (S) {
case "hours": {
const o = p + n;
if (o.length === 1)
if (h === "24hr")
if (n >= 0 && n <= 2) {
const s = n * 10;
return d(s), u((i) => ({
...i,
inputBuffer: o
})), !0;
} else
return d(n), u((s) => ({
...s,
currentInputPosition: t.minutes ? "minutes" : t.seconds ? "seconds" : "hours",
inputBuffer: ""
})), !0;
else
return n === 0 ? (d(n), u((s) => ({
...s,
inputBuffer: o
})), !0) : n === 1 ? (d(10), u((s) => ({
...s,
inputBuffer: o
})), !0) : (d(n), u((s) => ({
...s,
currentInputPosition: t.minutes ? "minutes" : t.seconds ? "seconds" : "hours",
inputBuffer: ""
})), !0);
else if (o.length === 2) {
const s = parseInt(o);
if (h === "24hr") {
if (s <= 23)
return d(s), u((i) => ({
...i,
currentInputPosition: t.minutes ? "minutes" : t.seconds ? "seconds" : "hours",
inputBuffer: ""
})), !0;
} else if (s >= 1 && s <= 12)
return d(s), u((i) => ({
...i,
currentInputPosition: t.minutes ? "minutes" : t.seconds ? "seconds" : "hours",
inputBuffer: ""
})), !0;
return u((i) => ({
...i,
inputBuffer: ""
})), !1;
}
break;
}
case "minutes": {
const o = p + n;
if (o.length === 1)
if (n >= 0 && n <= 5) {
const s = n * 10;
return a(s), u((i) => ({
...i,
inputBuffer: o
})), !0;
} else
return a(n), u((s) => ({
...s,
currentInputPosition: t.seconds ? "seconds" : t.hours ? "hours" : "minutes",
inputBuffer: ""
})), !0;
else if (o.length === 2) {
const s = parseInt(o);
return s <= 59 ? (a(s, {
currentInputPosition: t.seconds ? "seconds" : t.hours ? "hours" : "minutes",
inputBuffer: ""
}), !0) : (u((i) => ({
...i,
inputBuffer: ""
})), !1);
}
break;
}
case "seconds": {
const o = p + n;
if (o.length === 1)
if (n >= 0 && n <= 5) {
const s = n * 10;
return P(s), u((i) => ({
...i,
inputBuffer: o
})), !0;
} else
return P(n), u((s) => ({
...s,
inputBuffer: ""
})), !0;
else if (o.length === 2) {
const s = parseInt(o);
return s <= 59 ? (P(s), u((i) => ({
...i,
inputBuffer: ""
})), !0) : (u((i) => ({
...i,
currentInputPosition: t.hours ? "hours" : t.minutes ? "minutes" : "seconds",
inputBuffer: ""
})), !1);
}
break;
}
}
return !1;
},
getFormattedTimeWithHighlight: () => {
const { hours: e, minutes: t, seconds: n, period: S } = r.selectedTime, { currentInputPosition: p, shouldInclude: h } = r, {
hours: o,
minutes: s,
seconds: i
} = h, m = e.toString().padStart(2, "0"), k = t.toString().padStart(2, "0"), H = n.toString().padStart(2, "0");
let I = "";
switch (p) {
case "hours":
I = m;
break;
case "minutes":
I = k;
break;
case "seconds":
I = H;
break;
}
let B = "";
return o && (B += `${m}:`), s && (B += `${k}:`), i && (B += `${H}`), r.format === "am/pm" && (B += `${S}`), { display: B, highlightedPart: I };
},
setTime: (e) => {
u((t) => ({
...t,
selectedTime: {
...t.selectedTime,
...e
}
}));
}
};
}, N = ({
children: c,
format: f = "am/pm",
defaultTime: l = {},
shouldInclude: r
}) => {
const u = () => l.hours ?? 8, [d, a] = w({
isVisible: !1,
selectedTime: {
hours: u(),
minutes: l.minutes ?? 30,
seconds: l.seconds ?? 45,
period: f === "am/pm" ? l.period ?? "PM" : void 0
},
format: f,
currentInputPosition: "hours",
inputBuffer: "",
shouldInclude: r
});
return /* @__PURE__ */ g($.Provider, { value: { timePickerState: d, setTimePickerState: a }, children: c });
};
export {
N as TimePickerProvider,
J as useTimePicker
};