UNPKG

datepicker-nepali-reactjs

Version:
1,916 lines (1,880 loc) 55.2 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var React = require('react'); var React__default = _interopDefault(React); var Config = { maxBSYear: 2100, minBSYear: 1970, outputSeparator: "-", bsMonthTotalDaysCount: [ [30, 31], [31, 32], [31, 32], [31, 32], [31, 32], [30, 31], [29, 30], [29, 30], [29, 30], [29, 30], [29, 30], [30, 31], ], monthReferences: [ [0, 1, 1, 22, 1, 3, 1, 1, 1, 3, 1, 22, 1, 3, 1, 3, 1, 22, 1, 3, 1, 19, 1, 3, 1, 1, 3, 1, 2, 2, 1, 3, 1], [ 1, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 3, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 1, 3, 1, 1, 2, ], [ 0, 1, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 3, 1, 1, 2, ], [ 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 2, 2, 1, 3, 1, 2, 2, 2, 1, 2, ], [59, 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1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 20, 3, ], ], referenceDate: { AD: "1913-03-13", BS: "1970-01-01", }, }; var Validator = /** @class */ (function () { function Validator() { } Validator.dateType = function (type) { if (!["BS", "AD"].includes(type)) { throw new TypeError("Invalid date type. Only 'AD' or 'BS' type supported."); } return true; }; Validator.adYear = function (year) { var minAdYear = Config.minBSYear - 57; var maxAdYear = Config.maxBSYear - 57; if (year < minAdYear || year > maxAdYear) { throw new RangeError("AD year should be in range of " + minAdYear + " to " + maxAdYear); } }; Validator.adMonth = function (month) { if (month < 1 || month > 12) { throw new RangeError("AD month should be in range of 1 to 12"); } }; Validator.adDay = function (day) { if (day < 1 || day > 31) { throw new RangeError("AD day should be in range of 1 to 31"); } }; Validator.bsYear = function (year) { var midBsYear = Config.minBSYear; var maxBsYear = Config.maxBSYear; if (year < midBsYear || year > maxBsYear) { throw new RangeError("BS year should be in range of " + midBsYear + " to " + maxBsYear); } }; Validator.bsMonth = function (month) { if (month < 1 || month > 12) { throw new RangeError("BS month should be in range of 1 to 12"); } }; Validator.bsDay = function (day) { if (day < 1 || day > 32) { throw new RangeError("BS day should be in range of 1 to 32"); } }; return Validator; }()); var BikramSambat = /** @class */ (function () { function BikramSambat(date, type) { if (type === void 0) { type = "AD"; } this.dateType = "AD"; this.date = { year: 0, month: 0, day: 0 }; if (date) { this.setDate(date, type); } else { this.setDate(new Date()); } } BikramSambat.prototype.setDate = function (date, type) { if (type === void 0) { type = "AD"; } Validator.dateType(type); if (type === "BS") { if (typeof date !== "string") { throw new TypeError("BS date should be 'string' type."); } this.dateType = "BS"; this.date = this.splitBsDate(date); return this; } this.dateType = "AD"; this.date = this.splitAdDate(date); return this; }; BikramSambat.prototype.toAD = function () { if (this.dateType === "AD") { return this.format(this.date); } var totalDaysSince = this.totalDaysSince(); var _a = this.splitDate(Config.referenceDate.AD), year = _a.year, month = _a.month, day = _a.day; var adDate = new Date(year, month, day - 1); adDate.setDate(adDate.getDate() + totalDaysSince); return this.format(this.splitAdDate(adDate)); }; BikramSambat.prototype.toBS = function () { if (this.dateType === "BS") { return this.format(this.date); } var _a = this.date, adYear = _a.year, adMonth = _a.month, adDay = _a.day; var bsYear = adYear + 57; var bsMonth = (adMonth + 9) % 12 || 12; var bsDay = 1; if (adMonth < 4) { bsYear -= 1; } else if (adMonth === 4) { var bsNewYearAdDate = new BikramSambat(this.format({ year: bsYear, month: 1, day: 1 }), "BS").toAD(); if (adDay < new Date(bsNewYearAdDate).getDate()) { bsYear -= 1; } } var bsFirstDayOfMonthAdDate = new Date(new BikramSambat(this.format({ year: bsYear, month: bsMonth, day: 1 }), "BS").toAD()); if (adDay >= 1 && adDay < bsFirstDayOfMonthAdDate.getDate()) { bsMonth = bsMonth !== 1 ? bsMonth - 1 : 12; var daysInBSMonth = this.daysInBsMonth(bsYear, bsMonth); bsDay = daysInBSMonth - (bsFirstDayOfMonthAdDate.getDate() - adDay) + 1; } else { bsDay = adDay - bsFirstDayOfMonthAdDate.getDate() + 1; } return this.format({ year: bsYear, month: bsMonth, day: bsDay }); }; /** * Calculate total number of days since minimum reference date */ BikramSambat.prototype.totalDaysSince = function () { var _a = this.date, bsYear = _a.year, bsMonth = _a.month, bsDay = _a.day; var totalDays = 0; var diffInYears = bsYear - Config.minBSYear; for (var i = 1; i <= 12; i++) { totalDays += this.totalMonthDaysSince(i, i < bsMonth ? diffInYears + 1 : diffInYears); } if (bsYear > 2085 && bsYear < 2088) { totalDays += bsDay - 2; } else if (bsYear === 2085 && bsMonth > 5) { totalDays += bsDay - 2; } else if (bsYear > 2088) { totalDays += bsDay - 4; } else if (bsYear === 2088 && bsMonth > 5) { totalDays += bsDay - 4; } else { totalDays += bsDay; } return totalDays; }; /** * Total number of bsMonth days since minimum reference date * @param bsMonth * @param diffInYear */ BikramSambat.prototype.totalMonthDaysSince = function (bsMonth, diffInYear) { if (diffInYear === 0) { return 0; } var monthData = Config.monthReferences[bsMonth - 1]; var calculated = monthData.slice(0).reduce(function (total, monthDataItem, monthIndex, arr) { if (monthDataItem === 0) { return total; } var monthTotalDaysCountIndex = monthIndex % 2; if (diffInYear > total.year + monthDataItem) { total.year += monthDataItem; total.month += Config.bsMonthTotalDaysCount[bsMonth - 1][monthTotalDaysCountIndex] * monthDataItem; } else { total.month += Config.bsMonthTotalDaysCount[bsMonth - 1][monthTotalDaysCountIndex] * (diffInYear - total.year); total.year = diffInYear - total.year; arr.splice(1); // break; dont forget slice(0) before reducing https://stackoverflow.com/questions/36144406/how-to-break-on-reduce-method } return total; }, { month: 0, year: 0 }); return calculated.month; }; // noinspection JSMethodCanBeStatic /** * Returns total number of days in bsMonth for bsYear * @param bsYear * @param bsMonth */ BikramSambat.prototype.daysInBsMonth = function (bsYear, bsMonth) { var totalYears = bsYear + 1 - Config.minBSYear; var monthData = Config.monthReferences[bsMonth - 1]; var yearCount = 0; for (var i = 0; i < monthData.length; i++) { if (monthData[i] === 0) { continue; } var monthTotalDaysCountIndex = i % 2; yearCount += monthData[i]; if (totalYears <= yearCount) { if ((bsYear === 2085 && bsMonth === 5) || (bsYear === 2088 && bsMonth === 5)) { return Config.bsMonthTotalDaysCount[bsMonth - 1][monthTotalDaysCountIndex] - 2; } else { return Config.bsMonthTotalDaysCount[bsMonth - 1][monthTotalDaysCountIndex]; } } } return 0; }; BikramSambat.prototype.splitDate = function (date) { // tslint:disable-next-line:radix var _a = date .replace(/\//g, "-") .split("-") .map(function (d) { return Math.floor(Number(d)); }), year = _a[0], month = _a[1], day = _a[2]; return { year: year, month: month, day: day }; }; // noinspection JSMethodCanBeStatic BikramSambat.prototype.splitAdDate = function (date) { if (typeof date === "string") { date = new Date(date); } var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); Validator.adYear(year); Validator.adMonth(month); Validator.adDay(day); return { year: year, month: month, day: day }; }; BikramSambat.prototype.splitBsDate = function (date) { var _a = this.splitDate(date), year = _a.year, month = _a.month, day = _a.day; Validator.bsYear(year); Validator.bsMonth(month); Validator.bsDay(day); return { year: year, month: month, day: day }; }; BikramSambat.prototype.format = function (date) { var separator = Config.outputSeparator; var year = date.year, month = date.month, day = date.day; return "" + year + separator + this.zeroPad(month) + separator + this.zeroPad(day); }; // noinspection JSMethodCanBeStatic BikramSambat.prototype.zeroPad = function (x) { return x > 9 ? "" + x : "0" + x; }; return BikramSambat; }()); var BSToAD = function (date) { return new BikramSambat().setDate(date, "BS").toAD(); }; var ADToBS = function (date) { return new BikramSambat().setDate(date, "AD").toBS(); }; var months = { en: [ "Baisakh", "Jestha", "Asar", "Shrawan", "Bhadra", "Asoj", "Kartik", "Mangsir", "Pouse", "Magh", "Falgun", "Chaitra", ], ne: ["बैशाख", "जेठ", "असार", "सावन", "भदौ", "असोज", "कार्तिक", "मंसिर", "पौष", "माघ", "फागुन", "चैत"], }; var weeks = { en: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], ne: ["आईत", "सोम", "मंगल", "बुध", "बिही", "शुक्र", "शनि"], }; var minBSYear = 1970; var bsMonthMaxDays = [ [30, 31], [31, 32], [31, 32], [31, 32], [31, 32], [30, 31], [29, 30], [29, 30], [29, 30], [29, 30], [29, 30], [30, 31], ]; var bsMonthCalculatedData = [ [0, 1, 1, 22, 1, 3, 1, 1, 1, 3, 1, 22, 1, 3, 1, 3, 1, 22, 1, 3, 1, 19, 1, 3, 1, 1, 3, 1, 2, 2, 1, 3, 1], [ 1, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 3, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 1, 3, 1, 1, 2, ], [ 0, 1, 2, 1, 3, 1, 3, 1, 2, 2,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 3, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 2, 2, 2, 15, 2, 4, ], [ 1, 1, 3, 1, 3, 1, 14, 1, 3, 1, 1, 1, 3, 1, 14, 1, 3, 1, 3, 1, 3, 1, 18, 1, 3, 1, 3, 1, 3, 1, 14, 1, 3, 15, 1, 2, 1, 1, ], [ 0, 1, 1, 3, 1, 3, 1, 10, 1, 3, 1, 3, 1, 1, 1, 3, 1, 3, 1, 10, 1, 3, 1, 3, 1, 3, 1, 3, 1, 14, 1, 3, 1, 3, 1, 3, 1, 3, 1, 10, 1, 20, 1, 1, 1, ], [ 1, 2, 2, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 2, 2, 2, 2, 2, 2, 2, 1, 3, 1, 3, 1, 20, 3, ], ]; var CalenderConfig = { maxBSYear: 2100, minBSYear: 1970, outputSeparator: "-", }; var validateAdYear = function (year) { var minAdYear = CalenderConfig.minBSYear - 57; var maxAdYear = CalenderConfig.maxBSYear - 57; if (year < minAdYear || year > maxAdYear) { throw new RangeError("AD year should be in range of " + minAdYear + " to " + maxAdYear); } }; var validateAdMonth = function (month) { if (month < 1 || month > 12) { throw new RangeError("AD month should be in range of 1 to 12"); } }; var validateAdDay = function (day) { if (day < 1 || day > 31) { throw new RangeError("AD day should be in range of 1 to 31"); } }; var validateBsYear = function (year) { var midBsYear = CalenderConfig.minBSYear; var maxBsYear = CalenderConfig.maxBSYear; if (year < midBsYear || year > maxBsYear) { throw new RangeError("BS year should be in range of " + midBsYear + " to " + maxBsYear); } }; var validateBsMonth = function (month) { if (month < 1 || month > 12) { throw new RangeError("BS month should be in range of 1 to 12"); } }; var validateBsDay = function (day) { if (day < 1 || day > 32) { throw new RangeError("BS day should be in range of 1 to 32"); } }; var range = function (start, end, step) { if (step === void 0) { step = 1; } var list = []; for (var i = start; i <= end; i = i + step) { list.push(i); } return list; }; var zeroPad = function (num) { return "" + (num > 9 ? num : "0" + num); }; var splitDate = function (date, separator) { if (separator === void 0) { separator = "-"; } var _a = date.split(separator), year = _a[0], month = _a[1], day = _a[2]; return { day: parseInt(day, 10), month: parseInt(month, 10), year: parseInt(year, 10), }; }; var stitchDate = function (date, separator) { if (separator === void 0) { separator = "-"; } return "" + date.year + separator + zeroPad(date.month) + separator + zeroPad(date.day); }; var validateDateObject = function (date, type) { if (type === void 0) { type = "BS"; } var year = date.year, month = date.month, day = date.day; if (type === "BS") { validateBsYear(year); validateBsMonth(month); validateBsDay(day); return; } validateAdYear(year); validateAdMonth(month); validateAdDay(day); }; var getNumberOfDaysInBSMonth = function (yearMonth) { var year = yearMonth.year, month = yearMonth.month; validateBsYear(year); validateBsMonth(month); var yearCount = 0; var totalYears = year + 1 - minBSYear; var bsMonthData = bsMonthCalculatedData[month - 1]; return bsMonthData.reduce(function (numberOfDays, monthData, index) { if (monthData === 0 || numberOfDays !== 0) { return numberOfDays; } var bsMonthUpperDaysIndex = index % 2; yearCount += monthData; if (totalYears > yearCount) { return numberOfDays; } if ((year === 2085 && month === 5) || (year === 2088 && month === 5)) { return bsMonthMaxDays[month - 1][bsMonthUpperDaysIndex] - 2; } return bsMonthMaxDays[month - 1][bsMonthUpperDaysIndex]; }, 0); }; var parseBSDate = function (date, separator) { if (separator === void 0) { separator = "-"; } var _a = splitDate(date, separator), year = _a.year, month = _a.month, day = _a.day; validateDateObject({ year: year, month: month, day: day }); var adDate = new Date(BSToAD(date)); var firstAdDateInBSMonth = new Date(BSToAD(stitchDate({ year: year, month: month, day: 1 }, separator))); var numberOfDaysInMonth = getNumberOfDaysInBSMonth({ year: year, month: month }); return { adDate: adDate, bsDay: day, bsMonth: month, bsYear: year, firstAdDayInBSMonth: firstAdDateInBSMonth, numberOfDaysInBSMonth: numberOfDaysInMonth, weekDay: adDate.getDay(), }; }; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } var SET_CONFIG = "set_config"; var initialState = { currentLocale: "en", }; var reducer = function (state, action) { var _a; if (state === void 0) { state = initialState; } if (action.type === SET_CONFIG) { return __assign(__assign({}, state), (_a = {}, _a[action.key] = action.value, _a)); } return state; }; var ConfigContext = React.createContext({ dispatch: function () { return null; }, state: initialState, }); var ConfigProvider = function (_a) { var children = _a.children; var _b = React.useReducer(reducer, initialState), state = _b[0], dispatch = _b[1]; var contextValue = { state: state, dispatch: dispatch }; return React__default.createElement(ConfigContext.Provider, { value: contextValue }, children); }; var useConfig = function () { var _a = React.useContext(ConfigContext), state = _a.state, dispatch = _a.dispatch; function setConfig(key, value) { dispatch({ key: key, type: SET_CONFIG, value: value, }); } function getConfig(key) { return state[key]; } return { getConfig: getConfig, setConfig: setConfig, }; }; var parsedDateInitialValue = { adDate: new Date(), bsDay: 0, bsMonth: 0, bsYear: 0, firstAdDayInBSMonth: new Date(), numberOfDaysInBSMonth: 0, weekDay: 0, }; var IconBase = function (props) { var size = props.size, viewBoxSize = props.viewBoxSize, options = __rest(props, ["size", "viewBoxSize"]); return (React__default.createElement("svg", __assign({ xmlns: "http://www.w3.org/2000/svg", xmlnsXlink: "http://www.w3.org/1999/xlink", width: props.size, height: props.size }, options, { viewBox: "0 0 " + props.viewBoxSize + " " + props.viewBoxSize }), props.children)); }; IconBase.defaultProps = { color: "#6b6b6b", size: "16", viewBoxSize: "24", }; var Next = function (props) { var rotationOrigin = parseInt(props.size || "24", 10) / 2; return (React__default.createElement(IconBase, __assign({}, props), React__default.createElement("path", { transform: "rotate(180 " + rotationOrigin + " " + rotationOrigin + ")", fill: "none", fillRule: "evenodd", stroke: "currentcolor", strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2.4", d: "M16.283 18.907L9.6 12l6.683-5.438" }))); }; var Previous = function (props) { return (React__default.createElement(IconBase, __assign({}, props), React__default.createElement("path", { fill: "none", fillRule: "evenodd", stroke: "currentcolor", strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2.4", d: "M16.283 18.907L9.6 12l6.683-5.438" }))); }; var Today = function (props) { props = __assign(__assign({}, props), { viewBoxSize: "512", size: "15" }); return (React__default.createElement(IconBase, __assign({}, props), React__default.createElement("defs", null, React__default.createElement("path", { id: "a", d: "M468 160H44c-6.6 0-12-5.4-12-12v-36c0-26.5 21.5-48 48-48h48V12c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v52h128V12c0-6.6 5.4-12 12-12h40c6.6 0 12 5.4 12 12v52h48c26.5 0 48 21.5 48 48v36c0 6.6-5.4 12-12 12zM44 192h424c6.6 0 12 5.4 12 12v260c0 26.5-21.5 48-48 48H80c-26.5 0-48-21.5-48-48V204c0-6.6 5.4-12 12-12zm116 204c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12v-40zm0-128c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12v-40zm128 128c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12v-40zm0-128c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12v-40zm128 128c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12v-40zm0-128c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v40c0 6.6 5.4 12 12 12h40c6.6 0 12-5.4 12-12v-40z" })), React__default.createElement("g", { fill: "none", fillRule: "evenodd" }, React__default.createElement("mask", { id: "b", fill: "#fff" }, React__default.createElement("use", { xlinkHref: "#a" })), React__default.createElement("use", { fill: "#000", fillRule: "nonzero", xlinkHref: "#a" }), React__default.createElement("g", { fill: "currentcolor", mask: "url(#b)" }, React__default.createElement("path", { d: "M0 0h512v512H0z" }))))); }; var DropDown = function (_a) { var options = _a.options, value = _a.value, onSelect = _a.onSelect; React.useLayoutEffect(function () { var elem = document.querySelector(".active"); if (elem) { elem.scrollIntoView(); } }); return (React__default.createElement("div", { className: "drop-down" }, React__default.createElement("div", { className: "option-wrapper" }, React__default.createElement("ul", null, options.map(function (option, index) { return (React__default.createElement("li", { key: index, className: option.value === value ? "active" : "", onClick: function () { onSelect(option); } }, option.label)); }))))); }; var MonthPicker = function (_a) { var date = _a.date, onSelect = _a.onSelect; var _b = React.useState(false), showDropdown = _b[0], setShowDropdown = _b[1]; var getConfig = useConfig().getConfig; var currentLocale = React.useMemo(function () { return getConfig("currentLocale"); }, [getConfig]); var currentMonth = React.useMemo(function () { var month = date.bsMonth; return { label: months[currentLocale][month - 1], value: month, }; }, [date, currentLocale]); var monthList = React.useMemo(function () { return months[currentLocale].map(function (month, index) { return ({ label: month, value: index + 1, }); }); }, [currentLocale]); var handleDropdownView = function (selected) { setShowDropdown(!showDropdown); onSelect(selected.value); }; return (React__default.createElement("div", { className: "control month" }, React__default.createElement("span", { className: "current-month", onClick: function () { return setShowDropdown(!showDropdown); } }, currentMonth.label), showDropdown && React__default.createElement(DropDown, { options: monthList, value: currentMonth.value, onSelect: handleDropdownView }))); }; var YearPicker = function (_a) { var date = _a.date, onSelect = _a.onSelect; var _b = React.useState(false), showDropdown = _b[0], setShowDropdown = _b[1]; var currentYear = React.useMemo(function () { var year = date.bsYear; return { label: year.toString(), value: year, }; }, [date]); var years = React.useMemo(function () { return range(1970, 2099).map(function (year) { return ({ label: year.toString(), value: year, }); }); }, []); var handleDropdownView = function (selected) { setShowDropdown(!showDropdown); onSelect(selected.value); }; return (React__default.createElement("div", { className: "control year" }, React__default.createElement("span", { className: "current-year", onClick: function () { return setShowDropdown(!showDropdown); } }, currentYear.label), showDropdown && React__default.createElement(DropDown, { options: years, value: currentYear.value, onSelect: handleDropdownView }))); }; var CalenderController = function (props) { var onNextMonth = props.onNextMonth, onPreviousMonth = props.onPreviousMonth, calenderDate = props.calenderDate, onToday = props.onToday, onYearSelect = props.onYearSelect, onMonthSelect = props.onMonthSelect; return (React__default.createElement("div", { className: "calendar-controller" }, React__default.createElement("span", { className: "control icon", title: "prev", onClick: onPreviousMonth }, React__default.createElement(Previous, null)), React__default.createElement("div", { className: "date-indicator" }, React__default.createElement(MonthPicker, { date: calenderDate, onSelect: onMonthSelect }), React__default.createElement(YearPicker, { date: calenderDate, onSelect: onYearSelect })), React__default.createElement("span", { className: "control icon icon-today", title: "today", onClick: onToday }, React__default.createElement(Today, { color: "#2096f5" })), React__default.createElement("span", { className: "control icon", title: "next", onClick: onNextMonth }, React__default.createElement(Next, null)))); }; var DayPickerBody = function (_a) { var selectedDate = _a.selectedDate, date = _a.calenderDate, onSelect = _a.onSelect; var weeksInMonth = React.useMemo(function () { return Math.ceil((date.firstAdDayInBSMonth.getDay() + date.numberOfDaysInBSMonth) / 7) - 1; }, [date]); var previousMonth = React.useMemo(function () { return (date.bsMonth - 1 !== 0 ? date.bsMonth - 1 : 12); }, [date]); var previousYear = React.useMemo(function () { return (previousMonth === 12 ? date.bsYear - 1 : date.bsYear); }, [previousMonth, date]); var previousMonthDays = React.useMemo(function () { return previousYear >= minBSYear ? getNumberOfDaysInBSMonth({ month: previousMonth, year: previousYear, }) : 30; }, [previousYear]); var getDayInfo = React.useCallback(function (weekNum, weekDayNum) { var day = weekNum * 7 + weekDayNum - date.firstAdDayInBSMonth.getDay(); var month = date.bsMonth; var year = date.bsYear; var isCurrentMonth = true; if (day <= 0) { day = previousMonthDays + day; isCurrentMonth = false; } else if (day > date.numberOfDaysInBSMonth) { day = day - date.numberOfDaysInBSMonth; isCurrentMonth = false; } var today = splitDate(ADToBS(new Date())); var isToday = isCurrentMonth ? today.day === day && today.month === date.bsMonth && today.year === date.bsYear : false; var isSelected = isCurrentMonth ? selectedDate.bsDay === day && selectedDate.bsMonth === date.bsMonth && selectedDate.bsYear === date.bsYear : false; return { day: day, month: month, year: year, isCurrentMonth: isCurrentMonth, isToday: isToday, isSelected: isSelected }; }, [date, selectedDate, previousMonthDays]); var onDateSelectHandler = React.useCallback(function (dayInfo) { if (dayInfo.isCurrentMonth) { onSelect({ year: dayInfo.year, month: dayInfo.month, day: dayInfo.day }); } }, [onSelect]); return (React__default.createElement("tbody", null, range(0, weeksInMonth).map(function (weekNum) { return (React__default.createElement("tr", { key: weekNum }, range(1, 7).map(function (weekDayNum) { var dayInfo = getDayInfo(weekNum, weekDayNum); return (React__default.createElement("td", { key: weekDayNum, className: "month-day " + (dayInfo.isCurrentMonth ? "current" : "disabled") + " " + (dayInfo.isToday ? "today" : "") + " " + (dayInfo.isSelected ? "selected" : ""), onClick: function () { return onDateSelectHandler(dayInfo); } }, dayInfo.day)); }))); }))); }; var DayPickerHeader = function () { var getConfig = useConfig().getConfig; var currentLocale = React.useMemo(function () { return getConfig("currentLocale"); }, [getConfig]); return (React__default.createElement("thead", null, React__default.createElement("tr", null, weeks[currentLocale].map(function (weekDay, index) { return (React__default.createElement("td", { key: index }, weekDay)); })))); }; var DayPicker = function (_a) { var selectedDate = _a.selectedDate, calenderDate = _a.calenderDate, onDaySelect = _a.onDaySelect; return (React__default.createElement("table", null, React__default.createElement(DayPickerHeader, null), React__default.createElement(DayPickerBody, { selectedDate: selectedDate, calenderDate: calenderDate, onSelect: onDaySelect }))); }; var Calender = function (_a) { var value = _a.value, onChange = _a.onChange; var setConfig = useConfig().setConfig; var _b = React.useState(false), isInitialized = _b[0], setIsInitialized = _b[1]; var _c = React.useState(parsedDateInitialValue), selectedDate = _c[0], setSelectedDate = _c[1]; var _d = React.useState(parsedDateInitialValue), calenderDate = _d[0], setCalenderDate = _d[1]; var _e = React.useState(false), daySelected = _e[0], setDaySelected = _e[1]; React.useEffect(function () { if (isInitialized) { onChange(stitchDate({ year: selectedDate.bsYear, month: selectedDate.bsMonth, day: selectedDate.bsDay }), daySelected); } }, [selectedDate, isInitialized]); React.useEffect(function () { setConfig("currentLocale", "ne"); var parsedDateValue = parseBSDate(value); setSelectedDate(parsedDateValue); setCalenderDate(parsedDateValue); setIsInitialized(true); }, [value]); var onPreviousMonthHandler = React.useCallback(function () { setCalenderDate(function (date) { var year = date.bsYear; var month = date.bsMonth - 1; if (month < 1) { month = 12; year--; } return parseBSDate(stitchDate({ day: date.bsDay, month: month, year: year, }, "-")); }); }, []); var onNextMonthClickHandler = React.useCallback(function () { setCalenderDate(function (date) { var year = date.bsYear; var month = date.bsMonth + 1; if (month > 12) { month = 1; year++; } return parseBSDate(stitchDate({ day: date.bsDay, month: month, year: year, }, "-")); }); }, []); var onTodayClickHandler = React.useCallback(function () { var today = parseBSDate(ADToBS(new Date())); setCalenderDate(today); setSelectedDate(today); }, []); var onYearSelectHandler = React.useCallback(function (year) { setCalenderDate(parseBSDate(stitchDate({ year: year, month: calenderDate.bsMonth, day: calenderDate.bsDay }))); }, [calenderDate]); var onMonthSelectHandler = React.useCallback(function (month) { setCalenderDate(parseBSDate(stitchDate({ year: calenderDate.bsYear, month: month, day: calenderDate.bsDay }))); }, [calenderDate]); var onDaySelectHandler = React.useCallback(function (date) { var newDate = parseBSDate(stitchDate(date)); setCalenderDate(newDate); setSelectedDate(newDate); setDaySelected(true); }, []); return (React__default.createElement("div", { className: "nepali-date-picker" }, React__default.createElement("div", { className: "calendar-wrapper" }, isInitialized && (React__default.createElement(React.Fragment, null, React__default.createElement(CalenderController, { onPreviousMonth: onPreviousMonthHandler, onNextMonth: onNextMonthClickHandler, calenderDate: calenderDate, onToday: onTodayClickHandler, onYearSelect: onYearSelectHandler, onMonthSelect: onMonthSelectHandler }), React__default.createElement(DayPicker, { selectedDate: selectedDate, calenderDate: calenderDate, onDaySelect: onDaySelectHandler })))))); }; function styleInject(css, ref) { if ( ref === void 0 ) ref = {}; var insertAt = ref.insertAt; if (!css || typeof document === 'undefined') { return; } var head = document.head || document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css'; if (insertAt === 'top') { if (head.firstChild) { head.insertBefore(style, head.firstChild); } else { head.appendChild(style); } } else { head.appendChild(style); } if (style.styleSheet) { style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } } var css_248z = ".nepali-date-picker {\n background: #fff none repeat scroll 0 0;\n border-radius: 6px;\n box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.1);\n box-sizing: border-box;\n color: #8c8c8c;\n display: block;\n font-family: NotoSans, sans-serif;\n font-size: 14px;\n font-style: normal;\n font-weight: normal;\n height: auto;\n letter-spacing: 0.2px;\n line-height: 1.25em;\n padding: 15px;\n position: absolute;\n text-align: right;\n user-select: none;\n z-index: 9999; }\n .nepali-date-picker .calendar-controller {\n align-items: center;\n display: flex;\n justify-content: space-between;\n position: relative; }\n .nepali-date-picker .calendar-controller .date-indicator {\n display: flex; }\n .nepali-date