UNPKG

lucid-ui

Version:

A UI component library from Xandr.

160 lines 7.14 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 react_day_picker_1 = __importDefault(require("react-day-picker")); var style_helpers_1 = require("../../util/style-helpers"); var DateUtils = react_day_picker_1.default.DateUtils; var cx = style_helpers_1.lucidClassNames.bind('&-CalendarMonth'); var bool = prop_types_1.default.bool, instanceOf = prop_types_1.default.instanceOf, number = prop_types_1.default.number, oneOf = prop_types_1.default.oneOf, string = prop_types_1.default.string; var CalendarMonth = /** @class */ (function (_super) { __extends(CalendarMonth, _super); function CalendarMonth() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.modifierRange = function (day) { var _a = _this.props, cursor = _a.cursor, from = _a.from, to = _a.to, selectMode = _a.selectMode; if (cursor) { if (selectMode === 'day') { return DateUtils.isSameDay(day, new Date(cursor)); } else if (from || to) { return DateUtils.isDayInRange(day, { from: new Date(selectMode === 'to' ? from : to), to: new Date(cursor), }); } return DateUtils.isSameDay(day, new Date(cursor)); } if (from && to) { return DateUtils.isDayInRange(day, { from: new Date(from), to: new Date(to), }); } return false; }; _this.modifierFrom = function (day) { var from = _this.props.from; return DateUtils.isSameDay(day, new Date(from)); }; _this.modifierTo = function (day) { var to = _this.props.to; return DateUtils.isSameDay(day, new Date(to)); }; return _this; } CalendarMonth.prototype.shouldComponentUpdate = function () { return this.props.shouldComponentUpdate; }; CalendarMonth.prototype.render = function () { var _a = this.props, className = _a.className, monthOffset = _a.monthOffset, initialMonth = _a.initialMonth, passThroughs = __rest(_a, ["className", "monthOffset", "initialMonth"]); // It can be tricky to increment months using JavaScript dates, this should // handle the edge cases. // http://stackoverflow.com/questions/499838/javascript-date-next-month var monthDate = new Date(initialMonth.getFullYear(), initialMonth.getMonth() + monthOffset, 1); return ( /**typescript boundary with this component is tricky to get right with the way passthrough works * the component is being rewritten in typescript. Going to punt on this for now * https://github.com/gpbl/react-day-picker/issues/942 */ //@ts-ignore react_1.default.createElement(react_day_picker_1.default, __assign({ key: monthOffset, className: cx('&', className), initialMonth: monthDate, canChangeMonth: false, weekdaysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] }, passThroughs, { modifiers: __assign({ range: this.modifierRange, from: this.modifierFrom, to: this.modifierTo }, passThroughs.modifiers) }))); }; CalendarMonth._isPrivate = true; CalendarMonth.displayName = 'CalendarMonth'; CalendarMonth.peek = { description: "A single calendar month based on `react-day-picker`.", categories: ['helpers'], }; CalendarMonth.propTypes = { /** Appended to the component-specific class names set on the root element. */ className: string, /** The offset of the rendered month, where 0 is the \`initialMonth\`. Negative values will show previous months. */ monthOffset: number, /** Sets the month of the calendar. The 0 value for the \`monthOffset\` prop refers to this month. */ initialMonth: instanceOf(Date), /** Set the cursor to target date. Primarily used to preview expected ranges when the cursor is on a target date. */ cursor: instanceOf(Date), /** Sets the start date in a date range. */ from: instanceOf(Date), /** Sets the end date in a date range. */ to: instanceOf(Date), /** The next selection that is expected. Primarily used to preview expected ranges when the cursor is on a target date. */ selectMode: oneOf(['day', 'from', 'to']), /** Used to skip re-rendering of this component when true. Primarily used for CalendarMonths which are rendered out of view. */ shouldComponentUpdate: bool, }; CalendarMonth.defaultProps = { monthOffset: 0, initialMonth: new Date(), cursor: null, from: null, to: null, selectMode: 'day', shouldComponentUpdate: true, }; return CalendarMonth; }(react_1.default.Component)); exports.default = CalendarMonth; //# sourceMappingURL=CalendarMonth.js.map