wix-style-react
Version:
wix-style-react
338 lines (337 loc) • 13.4 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _focus = require("@react-aria/focus");
var _addMonths = _interopRequireDefault(require("date-fns/addMonths"));
var _subMonths = _interopRequireDefault(require("date-fns/subMonths"));
var _startOfMonth = _interopRequireDefault(require("date-fns/startOfMonth"));
var _isSameDay = _interopRequireDefault(require("date-fns/isSameDay"));
var _wixDesignSystemsLocaleUtils = require("wix-design-systems-locale-utils");
var _utils = require("./utils");
var _LocaleUtils = _interopRequireDefault(require("../common/LocaleUtils/LocaleUtils"));
var _BaseCalendar = _interopRequireDefault(require("./BaseCalendar/BaseCalendar"));
var _DatePickerHead = _interopRequireDefault(require("./DatePickerHead"));
var _context = require("../WixStyleReactEnvironmentProvider/context");
var _CalendarSt = require("./Calendar.st.css");
var _constants = require("./constants");
var _jsxFileName = "/home/builduser/work/a9c1ac8876d5057c/packages/wix-style-react/dist/cjs/Calendar/Calendar.js",
_Calendar;
class Calendar extends _react.default.PureComponent {
constructor(props) {
super(props);
this._setMonth = month => {
this.setState({
month
});
var {
onMonthChange
} = this.props;
if (onMonthChange) {
onMonthChange(month);
}
};
this._getLocaleUtilsFactory = (locale, firstDayOfWeek) => {
return (0, _LocaleUtils.default)(locale, firstDayOfWeek);
};
this._createCaptionElement = () => {
var {
showMonthDropdown,
showYearDropdown,
leftArrowAriaLabel,
leftArrowAriaLabelledBy,
rightArrowAriaLabel,
rightArrowAriaLabelledBy,
monthDropdownAriaLabel,
monthDropdownAriaLabelledBy,
yearDropdownAriaLabel,
yearDropdownAriaLabelledBy,
size
} = this.props;
var {
month
} = this.state;
var locale = this._getLocale();
var localeUtils = this._getLocaleUtilsFactory(locale);
return /*#__PURE__*/_react.default.createElement(_DatePickerHead.default, {
className: _CalendarSt.classes.header,
date: month,
showYearDropdown,
showMonthDropdown,
locale: typeof locale === 'string' ? locale : '',
localeUtils,
onChange: this._setMonth,
onLeftArrowClick: () => this._setMonth((0, _startOfMonth.default)((0, _addMonths.default)(month, -1))),
onRightArrowClick: () => this._setMonth((0, _startOfMonth.default)((0, _addMonths.default)(month, 1))),
leftArrowAriaLabel,
leftArrowAriaLabelledBy,
rightArrowAriaLabel,
rightArrowAriaLabelledBy,
monthDropdownAriaLabel,
monthDropdownAriaLabelledBy,
yearDropdownAriaLabel,
yearDropdownAriaLabelledBy,
size,
__self: this,
__source: {
fileName: _jsxFileName,
lineNumber: 176,
columnNumber: 7
}
});
};
var initialMonth = Calendar.getUpdatedMonth(props.value, props.numOfMonths);
this.state = {
month: initialMonth || new Date()
};
}
UNSAFE_componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.props.value) {
var month = Calendar.getUpdatedMonth(nextProps.value, nextProps.numOfMonths, this.state.month);
if (month) {
this.setState({
month
});
}
}
}
static areValuesEqual() {
var value1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var value2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!Boolean(value1) && !Boolean(value2)) {
return true;
}
if (Calendar.isRangeValue(value1) && Calendar.isRangeValue(value2)) {
return (0, _isSameDay.default)(value1.from, value2.from) && (0, _isSameDay.default)(value1.to, value2.to);
}
return (0, _isSameDay.default)(value1, value2);
}
static isSingleDay(value) {
return value instanceof Date;
}
static isRangeValue(value) {
return Boolean(value.from || value.to);
}
_getLocale() {
return this.props.locale || this.context.locale || 'en';
}
render() {
var {
dataHook,
className,
autoFocus,
numOfMonths,
firstDayOfWeek,
onChange,
onClose,
excludePastDates,
filterDate,
value,
selectionMode,
shouldCloseOnSelect,
locale,
rtl,
dateIndication,
today,
size,
onKeyDown,
containFocus,
fixedWeeks
} = this.props;
var {
month
} = this.state;
return /*#__PURE__*/_react.default.createElement("div", {
__self: this,
__source: {
fileName: _jsxFileName,
lineNumber: 233,
columnNumber: 7
}
}, /*#__PURE__*/_react.default.createElement(_focus.FocusScope, {
contain: autoFocus && containFocus,
restoreFocus: containFocus,
__self: this,
__source: {
fileName: _jsxFileName,
lineNumber: 234,
columnNumber: 9
}
}, /*#__PURE__*/_react.default.createElement(_BaseCalendar.default, {
dataHook: dataHook || _constants.dataHooks.baseCalendar,
size: size,
className: (0, _CalendarSt.st)(_CalendarSt.classes.root, className),
autoFocus: autoFocus,
numOfMonths: numOfMonths,
firstDayOfWeek: firstDayOfWeek,
onChange: onChange,
onClose: onClose,
excludePastDates: excludePastDates,
filterDate: filterDate,
value: value,
selectionMode: selectionMode,
shouldCloseOnSelect: shouldCloseOnSelect,
locale: locale,
rtl: rtl,
dateIndication: dateIndication,
today: today,
onDisplayedViewChange: this._setMonth,
displayedMonth: month,
captionElement: this._createCaptionElement(),
onKeyDown: onKeyDown,
fixedWeeks: fixedWeeks,
__self: this,
__source: {
fileName: _jsxFileName,
lineNumber: 238,
columnNumber: 11
}
})));
}
}
exports.default = Calendar;
_Calendar = Calendar;
Calendar.displayName = 'Calendar';
Calendar.defaultProps = {
className: '',
filterDate: () => true,
dateIndication: () => null,
shouldCloseOnSelect: true,
onClose: () => {},
autoFocus: true,
excludePastDates: false,
selectionMode: 'day',
showMonthDropdown: false,
showYearDropdown: false,
numOfMonths: 1,
size: 'medium',
fixedWeeks: true
};
Calendar.optionalParse = _BaseCalendar.default.optionalParse;
/** Return a value in which all string-dates are parsed into Date objects */
Calendar.parseValue = _BaseCalendar.default.parseValue;
Calendar.nextDay = _BaseCalendar.default.nextDay;
Calendar.prevDay = _BaseCalendar.default.prevDay;
Calendar.getUpdatedMonth = (nextPropsValue, numOfMonths, currentMonthDate) => {
var nextValue = _Calendar.parseValue(nextPropsValue);
if (!currentMonthDate) {
return _Calendar.isSingleDay(nextValue) ? nextValue : nextValue.from || nextValue.to;
}
var view = new _utils.CalendarView(currentMonthDate, numOfMonths);
if (_Calendar.isSingleDay(nextValue)) {
if (!view.isContained(nextValue)) {
return nextValue;
}
} else {
var {
from,
to
} = nextValue;
if (from && view.isAfterView(from)) {
// F--- => F---
// VVVVV => VVVVV
return from;
} else if (to && view.isBeforeView(to)) {
if (view.isRangeFits(from, to)) {
// F-T => F-T
// VVVVV => VVVVV
return from;
} else {
// F-----T => F-----T
// VVVVV => VVVVV
return (0, _subMonths.default)(to, numOfMonths - 1);
}
} else if (from && view.isBeforeView(from) && to && view.isAfterView(to)) {
// F-------T => F-------T
// VVVVV => VVVVV
return from; // choose the 'from' anchor arbitrarly
}
}
/*
* We only changed the month if the day (or range.edges) are outside the view.
* This is to avoid changing the month right after a user clicks on the calendar.
*/
return null;
};
Calendar.contextType = _context.WixStyleReactEnvironmentContext;
Calendar.propTypes = {
/** Applies as data-hook HTML attribute that can be used in the tests */
dataHook: _propTypes.default.string,
/** Focus selected day automatically when component mounts or updates */
autoFocus: _propTypes.default.bool,
/** Allows to display multiple months at once. Currently it shows 1 or 2 months only. */
numOfMonths: _propTypes.default.oneOf([1, 2]),
/** First day of the week, allowing only from 0 to 6 (Sunday to Saturday). The default value is 1 which means Monday. */
firstDayOfWeek: _propTypes.default.oneOf([0, 1, 2, 3, 4, 5, 6]),
/** A single CSS class name to be appended to the root element. */
className: _propTypes.default.string,
/** Provides a callback function when day in selected in the calendar */
onChange: _propTypes.default.func.isRequired,
/** Defines a callback function that is called whenever a user presses escape, clicks outside of the element or a date is selected and `shouldCloseOnSelect` is set. Receives an event as a first argument. */
onClose: _propTypes.default.func,
/** Provides a callback function when any key is clicked in the calendar */
onKeyDown: _propTypes.default.func,
/** Defines a callback function that is called with the date of the first day of the month whenever the user selects a month in the calendar */
onMonthChange: _propTypes.default.func,
/** Specify whether past dates should be selectable or not */
excludePastDates: _propTypes.default.bool,
/**
* ##### Specify selectable dates:
* * `param` {Date} `date` - a date to check
* * `return` {boolean} - true if `date` should be selectable, false otherwise
*/
filterDate: _propTypes.default.func,
/** Defines the selected date */
value: _propTypes.default.oneOfType([_propTypes.default.instanceOf(Date), _propTypes.default.shape({
from: _propTypes.default.instanceOf(Date),
to: _propTypes.default.instanceOf(Date)
})]),
/** Whether the user should be able to select a date range, or just a single day */
selectionMode: _propTypes.default.oneOf(['day', 'range']),
/** Displays a selectable yearDropdown */
showYearDropdown: _propTypes.default.bool,
/** Displays a selectable monthDropdown */
showMonthDropdown: _propTypes.default.bool,
/** Specify whether the calendar closes on day selection */
shouldCloseOnSelect: _propTypes.default.bool,
/** Specify date picker instance locale */
locale: _propTypes.default.oneOf(_wixDesignSystemsLocaleUtils.SupportedWixLocales),
/** Specify whether RTL mode is enabled or not. When true, the keyboard navigation will be changed means pressing on the right arrow will navigate to the previous day, and pressing on the left arrow will navigate to the next day. */
rtl: _propTypes.default.bool,
/**
##### Add an indication under a specific date.
Function returns the indication node of a specific date or null if this day doesn't have an indication.
* - `param` {date: Date, isSelected: boolean }
* - `date` - a date
* - `isSelected` - whether this date is the selected date
* - `return` {React.node} - the indication node of a specific date or null if this day doesn't have an indication.
*/
dateIndication: _propTypes.default.func,
/** Sets today's date. The today indication is added automatically according to the user timezone but in some cases, we need the ability to add the today indication based on the business timezone. */
today: _propTypes.default.instanceOf(Date),
/** Defines a string value that labels the left arrow in calendar header */
leftArrowAriaLabel: _propTypes.default.string,
/** Identifies the element that labels the left arrow in calendar header */
leftArrowAriaLabelledBy: _propTypes.default.string,
/** Defines a string value that labels the right arrow in calendar header */
rightArrowAriaLabel: _propTypes.default.string,
/** Identifies the element that labels the right arrow in calendar header */
rightArrowAriaLabelledBy: _propTypes.default.string,
/** Defines a string value that labels the months dropdown in calendar header */
monthDropdownAriaLabel: _propTypes.default.string,
/** Identifies the element that labels the months dropdown in calendar header */
monthDropdownAriaLabelledBy: _propTypes.default.string,
/** Defines a string value that labels the years dropdown in calendar header */
yearDropdownAriaLabel: _propTypes.default.string,
/** Identifies the element that labels the years dropdown in calendar header */
yearDropdownAriaLabelledBy: _propTypes.default.string,
/** Defines calendar size property. It can be either small or medium (default) */
size: _propTypes.default.oneOf(['small', 'medium']),
/** Use FocusScope to contain focus within calendar */
containFocus: _propTypes.default.bool,
/** Use the fixedWeeks prop to display 6 weeks per month. */
fixedWeeks: _propTypes.default.bool
};
//# sourceMappingURL=Calendar.js.map