UNPKG

@brightsoftware/date-np

Version:

Simple & minimal Nepali date picker that just works.

76 lines (75 loc) 2.25 kB
import { useState as Y, useCallback as p } from "react"; import { NepaliDate as l } from "../NepaliDate.js"; const v = ({ onDateChange: I, minDate: e, maxDate: r, currentDate: d }) => { const [b, S] = Y(null), D = /^\d{4}-\d{2}-\d{2}$/, c = p((s) => s.trim() ? D.test(s.trim()) : !1, []), a = p((s, n) => { const i = s.trim(); if (!c(i)) return null; try { let t; if (n === "nepali") t = new l(i); else { const [o, f, m] = i.split("-").map(Number); t = new Date(o, f - 1, m); } if (e) { const o = n === "nepali" ? e instanceof l ? e : l.fromADDate(e) : e instanceof Date ? e : e.toADDate(); if (n === "nepali" ? t.compare(o) < 0 : t.getTime() < o.getTime()) return null; } if (r) { const o = n === "nepali" ? r instanceof l ? r : l.fromADDate(r) : r instanceof Date ? r : r.toADDate(); if (n === "nepali" ? t.compare(o) > 0 : t.getTime() > o.getTime()) return null; } return t; } catch { return null; } }, [e, r, c]), A = p((s, n) => { const i = s.trim(); if (!i) return null; if (!c(i)) return `Please enter date in YYYY-MM-DD format (e.g., ${n === "nepali" ? "2082-03-20" : "2025-07-02"})`; if (!a(i, n)) { if (e || r) { const o = (u) => n === "nepali" ? (u instanceof l ? u : l.fromADDate(u)).toString() : (u instanceof Date ? u : u.toADDate()).toISOString().split("T")[0], f = e ? o(e) : "", m = r ? o(r) : ""; if (e && r) return `Date must be between ${f} and ${m}`; if (e) return `Date must be after ${f}`; if (r) return `Date must be before ${m}`; } return "Invalid date"; } return null; }, [c, a, e, r]), E = p((s, n) => { const i = A(s, n); if (S(i), !i) { const t = s.trim() ? a(s, n) : null; if (d && t && (n === "nepali" ? t.equals(d) : t.getTime() === d.getTime())) return; I(t); } }, [A, a, I, d]), M = p(() => { S(null); }, []); return { isValidInput: c, parseInputToDate: a, handleInputChange: E, error: b, clearError: M }; }; export { v as useEditableDateInput };