react-calendar
Version:
Ultimate calendar for your React app.
78 lines (77 loc) • 4.03 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(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);
};
var __rest = (this && this.__rest) || function (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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var prop_types_1 = __importDefault(require("prop-types"));
var date_utils_1 = require("@wojtekmaj/date-utils");
var TileGroup_1 = __importDefault(require("../TileGroup"));
var Day_1 = __importDefault(require("./Day"));
var dates_1 = require("../shared/dates");
var propTypes_1 = require("../shared/propTypes");
function Days(props) {
var activeStartDate = props.activeStartDate, calendarType = props.calendarType;
var showFixedNumberOfWeeks = props.showFixedNumberOfWeeks, showNeighboringMonth = props.showNeighboringMonth, otherProps = __rest(props, ["showFixedNumberOfWeeks", "showNeighboringMonth"]);
var year = (0, date_utils_1.getYear)(activeStartDate);
var monthIndex = (0, date_utils_1.getMonth)(activeStartDate);
var hasFixedNumberOfWeeks = showFixedNumberOfWeeks || showNeighboringMonth;
var dayOfWeek = (0, dates_1.getDayOfWeek)(activeStartDate, calendarType);
var offset = hasFixedNumberOfWeeks ? 0 : dayOfWeek;
/**
* Defines on which day of the month the grid shall start. If we simply show current
* month, we obviously start on day one, but if showNeighboringMonth is set to
* true, we need to find the beginning of the week the first day of the month is in.
*/
var start = (hasFixedNumberOfWeeks ? -dayOfWeek : 0) + 1;
/**
* Defines on which day of the month the grid shall end. If we simply show current
* month, we need to stop on the last day of the month, but if showNeighboringMonth
* is set to true, we need to find the end of the week the last day of the month is in.
*/
var end = (function () {
if (showFixedNumberOfWeeks) {
// Always show 6 weeks
return start + 6 * 7 - 1;
}
var daysInMonth = (0, date_utils_1.getDaysInMonth)(activeStartDate);
if (showNeighboringMonth) {
var activeEndDate = new Date();
activeEndDate.setFullYear(year, monthIndex, daysInMonth);
activeEndDate.setHours(0, 0, 0, 0);
var daysUntilEndOfTheWeek = 7 - (0, dates_1.getDayOfWeek)(activeEndDate, calendarType) - 1;
return daysInMonth + daysUntilEndOfTheWeek;
}
return daysInMonth;
})();
return (react_1.default.createElement(TileGroup_1.default, __assign({}, otherProps, { className: "react-calendar__month-view__days", count: 7, currentMonthIndex: monthIndex, dateTransform: function (day) {
var date = new Date();
date.setFullYear(year, monthIndex, day);
date.setHours(0, 0, 0, 0);
return date;
}, dateType: "day", end: end, offset: offset, start: start, tile: Day_1.default })));
}
exports.default = Days;
Days.propTypes = __assign({ calendarType: propTypes_1.isCalendarType, showFixedNumberOfWeeks: prop_types_1.default.bool, showNeighboringMonth: prop_types_1.default.bool }, propTypes_1.tileGroupProps);