UNPKG

react-native-ethiopian-calendar

Version:

A react native calendar component which is mainly intended for applications which require Ethiopian calendar.

178 lines (177 loc) 7.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EthiopianCalender = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _Convertor = require("../../utils/Calendar/Convertor"); var _header = require("./header"); var _day2 = require("./day"); var _generics = require("../../utils/generics"); var _styles = require("./styles"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const EthiopianCalender = props => { const { date = { day: new Date().getDate(), month: new Date().getMonth() + 1, year: new Date().getFullYear() }, locale, onDatePress, onModeChange, onLanguageChange, theme, hideHeaderButtons, selectedDate, setSelectedDate } = props; const styles = (0, _styles.makeStyle)(theme); const [_day, _setDay] = (0, _react.useState)(selectedDate ? selectedDate.ethiopian.date : (0, _Convertor.toEthiopic)(date.year, date.month, date.day).day); const [month, setMonth] = (0, _react.useState)(selectedDate ? selectedDate.ethiopian.month : (0, _Convertor.toEthiopic)(date.year, date.month, date.day).month); const [year, setYear] = (0, _react.useState)(selectedDate ? selectedDate.ethiopian.year : (0, _Convertor.toEthiopic)(date.year, date.month, date.day).year); const firstDayOfTheMonthIndex = (0, _react.useMemo)(() => { const { year: gregorianYear, month: gregorianMonth, day: gregorianDay } = (0, _Convertor.toGregorian)(year, month, 1); return new Date(gregorianYear, gregorianMonth - 1, gregorianDay).getDay(); }, [month, year]); const lastDayOfTheMonthIndex = (0, _react.useMemo)(() => { let lastDay = 30; if (month === 13) { if (_Convertor.ethiopicCalendar.isLeap(year)) { lastDay = 6; } else { lastDay = 5; } } const { year: gregorianYear, month: gregorianMonth, day: gregorianDay } = (0, _Convertor.toGregorian)(year, month, lastDay); return new Date(gregorianYear, gregorianMonth - 1, gregorianDay).getDay(); }, [month, year]); const nextDays = (0, _react.useMemo)(() => { return 7 - lastDayOfTheMonthIndex - 1; }, [lastDayOfTheMonthIndex]); const currentDay = (0, _react.useMemo)(() => { return (0, _Convertor.toEthiopic)(new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()).day; }, []); const currentMonthIndex = (0, _react.useMemo)(() => { return (0, _Convertor.toEthiopic)(new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()).month; }, []); const currentYear = (0, _react.useMemo)(() => { return (0, _Convertor.toEthiopic)(new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()).year; }, []); const gotoToday = (0, _react.useCallback)(() => { setMonth(currentMonthIndex); setYear(currentYear); }, [currentYear, currentMonthIndex]); const next = (0, _react.useCallback)(() => { const newMonth = month + 1; if (newMonth > 13) { setMonth(() => 1); setYear(previous => previous + 1); } else { setMonth(() => newMonth); } }, [month]); const prev = (0, _react.useCallback)(() => { const newMonth = month - 1; if (newMonth < 1) { setMonth(() => 13); setYear(previous => previous - 1); } else { setMonth(() => newMonth); } }, [month]); const today = iDate => { return iDate === currentDay && month === currentMonthIndex && year === currentYear; }; const selected = iDate => { if (selectedDate) { return selectedDate.ethiopian.date === iDate && selectedDate.ethiopian.month === month && selectedDate.ethiopian.year === year; } return false; }; const handleDayPress = pressedDay => { const toGregorianDate = (0, _Convertor.toGregorian)(year, month, pressedDay); setSelectedDate({ ethiopian: { date: pressedDay, month: month, year: year }, gregorian: { date: toGregorianDate.day, month: toGregorianDate.month, year: toGregorianDate.year } }); onDatePress({ ethiopian: { date: pressedDay, month: month, year: year }, gregorian: { date: toGregorianDate.day, month: toGregorianDate.month, year: toGregorianDate.year } }); }; return /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: styles.container }, /*#__PURE__*/_react.default.createElement(_header.Header, { currentDay: currentDay, today: gotoToday, next: next, prev: prev, month: month, year: year, locals: locale, mode: 'EC', theme: theme, onModeChange: onModeChange, onLanguageChange: onLanguageChange, hideHeaderButtons: hideHeaderButtons }), /*#__PURE__*/_react.default.createElement(_reactNative.View, { style: [styles.daysWrapper] }, (0, _generics.iterator)(firstDayOfTheMonthIndex).map((_item, i) => /*#__PURE__*/_react.default.createElement(_day2.Day, { key: i, dayNumber: 30 - firstDayOfTheMonthIndex + i + 1, extraDays: true, theme: theme })), month !== 13 ? (0, _generics.iterator)(30).map((_item, i) => /*#__PURE__*/_react.default.createElement(_day2.Day, { key: i, dayNumber: i + 1, today: today(i + 1), selected: selected(i + 1), onPress: () => handleDayPress(i + 1), theme: theme })) : // IF THE MONTH IS ጳጉሜ(13TH MONTH) // IF IT'S ETHIOPIAN LEAP YEAR, THE MONTH WILL 6 DAYS // ELSE IT WILL HAVE % DAYS (0, _generics.iterator)(_Convertor.ethiopicCalendar.isLeap(year) ? 6 : 5).map((_item, i) => /*#__PURE__*/_react.default.createElement(_day2.Day, { key: i, dayNumber: i + 1, today: today(i + 1), selected: selected(i + 1), onPress: () => handleDayPress(i + 1), theme: theme })), (0, _generics.iterator)(nextDays).map((_item, i) => /*#__PURE__*/_react.default.createElement(_day2.Day, { key: i, dayNumber: i + 1, extraDays: true, theme: theme })))); }; exports.EthiopianCalender = EthiopianCalender; //# sourceMappingURL=EthiopianCalendar.js.map