react-native-ethiopian-calendar-date-picker
Version:
A react native calendar component which is mainly intended for applications which require Ethiopian calendar.
176 lines (175 loc) • 6.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GregorianCalendar = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _array = require("../../utils/generics/array");
var _day = require("./day");
var _header = require("./header");
var _styles = require("./styles");
var _Calendar = require("../../utils/Calendar");
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 GregorianCalendar = props => {
const {
date = {
day: new Date().getDate(),
month: new Date().getMonth() + 1,
year: new Date().getFullYear()
},
onDatePress,
theme,
onModeChange,
hideHeaderButtons,
selectedDate,
setSelectedDate,
minDate,
maxDate
} = props;
const [day, _setDate] = (0, _react.useState)(1);
const [month, setMonth] = (0, _react.useState)(selectedDate ? selectedDate.gregorian.month : date.month);
const [year, setYear] = (0, _react.useState)(selectedDate ? selectedDate.gregorian.year : date.year);
const styles = (0, _styles.makeStyle)(theme);
const lastDayOfTheCurrentMonth = (0, _react.useMemo)(() => {
return new Date(year, month, 0).getDate();
}, [month, year]);
const lastDayOfPreviousMonth = (0, _react.useMemo)(() => {
return new Date(year, month - 1, 0).getDate();
}, [month, year]);
const firstDayOfTheMonthIndex = (0, _react.useMemo)(() => {
return new Date(year, month - 1, day).getDay();
}, [day, month, year]);
const lastDayOfTheMonthIndex = (0, _react.useMemo)(() => {
return new Date(year, month, 0).getDay();
}, [month, year]);
const nextDays = (0, _react.useMemo)(() => {
return 7 - lastDayOfTheMonthIndex - 1;
}, [lastDayOfTheMonthIndex]);
const currentYear = (0, _react.useMemo)(() => {
return new Date().getFullYear();
}, []);
const currentMonthIndex = (0, _react.useMemo)(() => {
return new Date().getMonth() + 1;
}, []);
const gotoToday = (0, _react.useCallback)(() => {
setMonth(currentMonthIndex);
setYear(currentYear);
}, [currentMonthIndex, currentYear]);
const next = (0, _react.useCallback)(() => {
const newMonth = month + 1;
if (newMonth > 12) {
setMonth(1);
setYear(previous => previous + 1);
} else {
setMonth(newMonth);
}
}, [month]);
const prev = (0, _react.useCallback)(() => {
const newMonth = month - 1;
if (newMonth < 1) {
setMonth(12);
setYear(previous => previous - 1);
} else {
setMonth(newMonth);
}
}, [month]);
const setYears = (0, _react.useCallback)(year => {
setYear(year);
}, []);
const setMonths = (0, _react.useCallback)(newMonth => {
setMonth(() => newMonth);
}, []);
const currentDay = (0, _react.useMemo)(() => {
return new Date().getDate();
}, []);
const isBeforeMinDate = day => {
if (!minDate) return false;
const selectedDate = new Date(year, month - 1, day);
const min = new Date(minDate.year, minDate.month - 1, minDate.day);
return selectedDate < min;
};
const isAfterMaxDate = day => {
if (!maxDate) return false;
const selectedDate = new Date(year, month - 1, day);
const max = new Date(maxDate.year, maxDate.month - 1, maxDate.day);
return selectedDate > max;
};
const today = iDate => {
return iDate === currentDay && month === currentMonthIndex && year === currentYear;
};
const selected = iDate => {
if (selectedDate) {
return selectedDate.gregorian.date === iDate && selectedDate.gregorian.month === month && selectedDate.gregorian.year === year;
}
return false;
};
const handleDayPress = pressedDay => {
const convertToEthiopic = (0, _Calendar.toEthiopic)(year, month, pressedDay);
if (isBeforeMinDate(pressedDay)) return;
setSelectedDate({
ethiopian: {
date: convertToEthiopic.day,
month: convertToEthiopic.month,
year: convertToEthiopic.year
},
gregorian: {
date: pressedDay,
month: month,
year: year
}
});
onDatePress({
ethiopian: {
date: convertToEthiopic.day,
month: convertToEthiopic.month,
year: convertToEthiopic.year
},
gregorian: {
date: pressedDay,
month: month,
year: 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,
setMonths: setMonths,
year: year,
setYears: setYears,
locals: 'ENG',
mode: "GC",
theme: theme,
onModeChange: onModeChange,
hideHeaderButtons: hideHeaderButtons
}), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
style: [styles.daysWrapper]
}, (0, _array.iterator)(firstDayOfTheMonthIndex).map((_item, i) => /*#__PURE__*/_react.default.createElement(_day.Day, {
key: i,
dayNumber: lastDayOfPreviousMonth - firstDayOfTheMonthIndex + i - 1,
extraDays: true,
theme: theme
})), (0, _array.iterator)(lastDayOfTheCurrentMonth).map((_item, i) => /*#__PURE__*/_react.default.createElement(_day.Day, {
key: i,
dayNumber: i + 1,
today: today(i + 1),
selected: selected(i + 1),
onPress: () => handleDayPress(i + 1),
theme: theme,
disabled: isBeforeMinDate(i + 1) || isAfterMaxDate(i + 1)
})), (0, _array.iterator)(nextDays).map((_item, i) => /*#__PURE__*/_react.default.createElement(_day.Day, {
key: i,
dayNumber: i + 1,
extraDays: true,
theme: theme
}))));
};
exports.GregorianCalendar = GregorianCalendar;
//# sourceMappingURL=GregorianCalender.js.map