UNPKG

@brightsoftware/date-np

Version:

Simple & minimal Nepali date picker that just works.

94 lines (93 loc) 2.51 kB
import { useState as v, useCallback as d } from "react"; import { NepaliDate as n } from "../../NepaliDate.js"; const T = ({ onStartDateChange: m, onEndDateChange: I, minDate: s, maxDate: r, currentStartDate: i, currentEndDate: t }) => { const [$, p] = v({ start: null, end: null }), g = /^\d{4}-\d{2}-\d{2}$/, c = d((f) => f.trim() ? g.test(f.trim()) : !1, []), b = d((f) => { const e = f.trim(); if (!c(e)) return null; try { const l = new n(e); if (s) { const o = s instanceof n ? s : n.fromADDate(s); if (l.compare(o) < 0) return null; } if (r) { const o = r instanceof n ? r : n.fromADDate(r); if (l.compare(o) > 0) return null; } return l; } catch { return null; } }, [s, r, c]), A = d((f, e) => { const l = f.trim(); if (!l) return null; if (!c(l)) return "Please enter date in YYYY-MM-DD format (e.g., 2082-03-20)"; const o = b(l); if (!o) { if (s || r) { const u = s ? s instanceof n ? s.toString() : n.fromADDate(s).toString() : "", N = r ? r instanceof n ? r.toString() : n.fromADDate(r).toString() : ""; if (s && r) return `Date must be between ${u} and ${N}`; if (s) return `Date must be after ${u}`; if (r) return `Date must be before ${N}`; } return "Invalid date"; } if (e === "start" && t) { const u = t instanceof n ? t : n.fromADDate(t); if (o.compare(u) > 0) return "Start date must be before end date"; } else if (e === "end" && i) { const u = i instanceof n ? i : n.fromADDate(i); if (o.compare(u) < 0) return "End date must be after start date"; } return null; }, [c, b, s, r, i, t]), q = d((f, e) => { const l = A(f, e); if (p((o) => ({ ...o, [e]: l })), !l) { const o = f.trim() ? b(f) : null; if (e === "start") { if (i && (o != null && o.equals(i)) || t && (o != null && o.equals(t))) return; m(o); } else if (e === "end") { if (t && (o != null && o.equals(t)) || i && (o != null && o.equals(i))) return; I(o); } } }, [A, b, m, I]), Y = d(() => { p({ start: null, end: null }); }, []); return { isValidInput: c, parseInputToDate: b, handleInputChange: q, errors: $, clearErrors: Y }; }; export { T as useEditableDateInput };