UNPKG

office-ui-fabric-react

Version:

Reusable React components for building experiences for Office 365.

213 lines • 12.3 kB
import * as tslib_1 from "tslib"; import * as React from 'react'; import { DayOfWeek, FirstWeekOfYear, DateRangeType } from '../../utilities/dateValues/DateValues'; import { CalendarDay } from './CalendarDay'; import { CalendarMonth } from './CalendarMonth'; import { compareDates, getDateRangeArray } from '../../utilities/dateMath/DateMath'; import { css, BaseComponent, createRef } from '../../Utilities'; import * as stylesImport from './Calendar.scss'; var styles = stylesImport; var leftArrow = 'Up'; var rightArrow = 'Down'; var closeIcon = 'CalculatorMultiply'; var iconStrings = { leftNavigation: leftArrow, rightNavigation: rightArrow, closeIcon: closeIcon }; var defaultWorkWeekDays = [ DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday ]; var dateTimeFormatterCallbacks = { formatMonthDayYear: function (date, strings) { return strings.months[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear(); }, formatMonthYear: function (date, strings) { return strings.months[date.getMonth()] + ' ' + date.getFullYear(); }, formatDay: function (date) { return date.getDate().toString(); }, formatYear: function (date) { return date.getFullYear().toString(); } }; var Calendar = /** @class */ (function (_super) { tslib_1.__extends(Calendar, _super); function Calendar(props) { var _this = _super.call(this, props) || this; _this._dayPicker = createRef(); _this._monthPicker = createRef(); _this._navigateDayPickerDay = function (date) { _this.setState({ navigatedDayDate: date, navigatedMonthDate: date }); }; _this._navigateMonthPickerDay = function (date) { _this.setState({ navigatedMonthDate: date }); }; _this._onNavigateDayDate = function (date, focusOnNavigatedDay) { _this._navigateDayPickerDay(date); _this._focusOnUpdate = focusOnNavigatedDay; }; _this._onNavigateMonthDate = function (date, focusOnNavigatedDay) { if (!focusOnNavigatedDay) { _this._navigateMonthPickerDay(date); _this._focusOnUpdate = focusOnNavigatedDay; return; } var monthPickerOnly = !_this.props.showMonthPickerAsOverlay && !_this.props.isDayPickerVisible; if (monthPickerOnly) { _this._onSelectDate(date); } _this._navigateDayPickerDay(date); }; _this._onSelectDate = function (date, selectedDateRangeArray) { var onSelectDate = _this.props.onSelectDate; _this.setState({ selectedDate: date }); if (onSelectDate) { onSelectDate(date, selectedDateRangeArray); } }; _this._onHeaderSelect = function (focus) { _this.setState({ isDayPickerVisible: !_this.state.isDayPickerVisible, isMonthPickerVisible: !_this.state.isMonthPickerVisible }); if (focus) { _this._focusOnUpdate = true; } }; _this._onGotoToday = function () { var _a = _this.props, dateRangeType = _a.dateRangeType, firstDayOfWeek = _a.firstDayOfWeek, today = _a.today, workWeekDays = _a.workWeekDays, selectDateOnClick = _a.selectDateOnClick; if (selectDateOnClick) { // When using Defaultprops, TypeScript doesn't know that React is going to inject defaults // so we use exclamation mark as a hint to the type checker (see link below) // https://decembersoft.com/posts/error-ts2532-optional-react-component-props-in-typescript/ var dates = getDateRangeArray(today, dateRangeType, firstDayOfWeek, workWeekDays); _this._onSelectDate(today, dates); } _this._navigateDayPickerDay(today); }; _this._onGotoTodayClick = function (ev) { _this._onGotoToday(); }; _this._onGotoTodayKeyDown = function (ev) { if (ev.which === 13 /* enter */) { ev.preventDefault(); _this._onGotoToday(); } }; _this._onDatePickerPopupKeyDown = function (ev) { switch (ev.which) { case 13 /* enter */: ev.preventDefault(); break; case 8 /* backspace */: ev.preventDefault(); break; case 27 /* escape */: _this._handleEscKey(ev); break; default: break; } }; _this._handleEscKey = function (ev) { if (_this.props.onDismiss) { _this.props.onDismiss(); } }; var currentDate = props.value && !isNaN(props.value.getTime()) ? props.value : props.today || new Date(); _this.state = { selectedDate: currentDate, navigatedDayDate: currentDate, navigatedMonthDate: currentDate, /** When showMonthPickerAsOverlay is active it overrides isMonthPickerVisible/isDayPickerVisible props (These props permanently set the visibility of their respective calendars). */ isMonthPickerVisible: _this.props.showMonthPickerAsOverlay ? false : _this.props.isMonthPickerVisible, isDayPickerVisible: _this.props.showMonthPickerAsOverlay ? true : _this.props.isDayPickerVisible }; _this._focusOnUpdate = false; return _this; } Calendar.prototype.componentWillReceiveProps = function (nextProps) { var autoNavigateOnSelection = nextProps.autoNavigateOnSelection, value = nextProps.value, _a = nextProps.today, today = _a === void 0 ? new Date() : _a; // Make sure auto-navigation is supported for programmatic changes to selected date, i.e., // if selected date is updated via props, we may need to modify the navigated date var overrideNavigatedDate = autoNavigateOnSelection && !compareDates(value, this.props.value); if (overrideNavigatedDate) { this.setState({ navigatedMonthDate: value, navigatedDayDate: value }); } this.setState({ selectedDate: value || today }); }; Calendar.prototype.componentDidUpdate = function () { if (this._focusOnUpdate) { this.focus(); this._focusOnUpdate = false; } }; Calendar.prototype.render = function () { var rootClass = 'ms-DatePicker'; var _a = this.props, firstDayOfWeek = _a.firstDayOfWeek, dateRangeType = _a.dateRangeType, strings = _a.strings, showMonthPickerAsOverlay = _a.showMonthPickerAsOverlay, autoNavigateOnSelection = _a.autoNavigateOnSelection, showGoToToday = _a.showGoToToday, highlightCurrentMonth = _a.highlightCurrentMonth, highlightSelectedMonth = _a.highlightSelectedMonth, navigationIcons = _a.navigationIcons, minDate = _a.minDate, maxDate = _a.maxDate, className = _a.className, showCloseButton = _a.showCloseButton; var _b = this.state, selectedDate = _b.selectedDate, navigatedDayDate = _b.navigatedDayDate, navigatedMonthDate = _b.navigatedMonthDate, isMonthPickerVisible = _b.isMonthPickerVisible, isDayPickerVisible = _b.isDayPickerVisible; var onHeaderSelect = showMonthPickerAsOverlay ? this._onHeaderSelect : undefined; var monthPickerOnly = !showMonthPickerAsOverlay && !isDayPickerVisible; var overlayedWithButton = showMonthPickerAsOverlay && showGoToToday; return (React.createElement("div", { className: css(rootClass, styles.root, className), role: "application" }, React.createElement("div", { className: css('ms-DatePicker-picker ms-DatePicker-picker--opened ms-DatePicker-picker--focused', styles.picker, styles.pickerIsOpened, styles.pickerIsFocused, isMonthPickerVisible && 'ms-DatePicker-monthPickerVisible ' + styles.monthPickerVisible, isMonthPickerVisible && isDayPickerVisible && 'ms-DatePicker-calendarsInline ' + styles.calendarsInline, monthPickerOnly && 'ms-DatePicker-monthPickerOnly ' + styles.monthPickerOnly, showMonthPickerAsOverlay && 'ms-DatePicker-monthPickerAsOverlay ' + styles.monthPickerAsOverlay) }, React.createElement("div", { className: css('ms-DatePicker-holder ms-slideDownIn10', styles.holder, overlayedWithButton && styles.holderWithButton), onKeyDown: this._onDatePickerPopupKeyDown }, React.createElement("div", { className: css('ms-DatePicker-frame', styles.frame) }, React.createElement("div", { className: css('ms-DatePicker-wrap', styles.wrap, showGoToToday && styles.goTodaySpacing) }, isDayPickerVisible && (React.createElement(CalendarDay, { selectedDate: selectedDate, navigatedDate: navigatedDayDate, today: this.props.today, onSelectDate: this._onSelectDate, onNavigateDate: this._onNavigateDayDate, onDismiss: this.props.onDismiss, firstDayOfWeek: firstDayOfWeek, dateRangeType: dateRangeType, autoNavigateOnSelection: autoNavigateOnSelection, strings: strings, onHeaderSelect: onHeaderSelect, navigationIcons: navigationIcons, showWeekNumbers: this.props.showWeekNumbers, firstWeekOfYear: this.props.firstWeekOfYear, dateTimeFormatter: this.props.dateTimeFormatter, showSixWeeksByDefault: this.props.showSixWeeksByDefault, minDate: minDate, maxDate: maxDate, workWeekDays: this.props.workWeekDays, componentRef: this._dayPicker, showCloseButton: showCloseButton })), isDayPickerVisible && isMonthPickerVisible && React.createElement("div", { className: styles.divider }), isMonthPickerVisible && (React.createElement(CalendarMonth, { navigatedDate: navigatedMonthDate, selectedDate: navigatedDayDate, strings: strings, onNavigateDate: this._onNavigateMonthDate, today: this.props.today, highlightCurrentMonth: highlightCurrentMonth, highlightSelectedMonth: highlightSelectedMonth, onHeaderSelect: onHeaderSelect, navigationIcons: navigationIcons, dateTimeFormatter: this.props.dateTimeFormatter, minDate: minDate, maxDate: maxDate, componentRef: this._monthPicker })), showGoToToday && (React.createElement("button", { role: "button", className: css('ms-DatePicker-goToday js-goToday', styles.goToday, (_c = {}, _c[styles.goTodayInlineMonth] = isMonthPickerVisible, _c)), onClick: this._onGotoTodayClick, onKeyDown: this._onGotoTodayKeyDown, tabIndex: 0 }, strings.goToToday)))))))); var _c; }; Calendar.prototype.focus = function () { if (this.state.isDayPickerVisible && this._dayPicker.current) { this._dayPicker.current.focus(); } else if (this.state.isMonthPickerVisible && this._monthPicker.current) { this._monthPicker.current.focus(); } }; Calendar.defaultProps = { onSelectDate: undefined, onDismiss: undefined, isMonthPickerVisible: true, isDayPickerVisible: true, showMonthPickerAsOverlay: false, value: undefined, today: new Date(), firstDayOfWeek: DayOfWeek.Sunday, dateRangeType: DateRangeType.Day, autoNavigateOnSelection: false, showGoToToday: true, strings: null, highlightCurrentMonth: false, highlightSelectedMonth: false, navigationIcons: iconStrings, showWeekNumbers: false, firstWeekOfYear: FirstWeekOfYear.FirstDay, dateTimeFormatter: dateTimeFormatterCallbacks, showSixWeeksByDefault: false, workWeekDays: defaultWorkWeekDays, showCloseButton: false }; return Calendar; }(BaseComponent)); export { Calendar }; //# sourceMappingURL=Calendar.js.map