UNPKG

wix-style-react

Version:
332 lines • 15.9 kB
import { st, classes, cssStates } from './BaseCalendar.st.css'; import React from 'react'; import PropTypes from 'prop-types'; import DayPicker from 'react-day-picker'; import localeUtilsFactory from '../../common/LocaleUtils/LocaleUtils'; import { WixStyleReactEnvironmentContext } from '../../WixStyleReactEnvironmentProvider/context'; import { SupportedWixLocales } from 'wix-design-systems-locale-utils'; class BaseCalendar extends React.PureComponent { constructor() { super(...arguments); this._renderDay = (day, modifiers) => { const { dateIndication } = this.props; const isOutsideDay = !!modifiers[cssStates({ outside: true })]; const isSelectedDay = !!modifiers[cssStates({ selected: true })]; const dateIndicationNode = dateIndication && dateIndication({ date: day, isSelected: isSelectedDay }); const shouldHasIndication = dateIndicationNode && !isOutsideDay; return (React.createElement("div", { className: st(classes.dayWrapper, { hasIndication: shouldHasIndication, }), "data-date": `${day.getFullYear()}-${day.getMonth()}-${day.getDate()}`, "data-outsideday": isOutsideDay }, React.createElement("div", { className: classes.dayText }, day.getDate()), shouldHasIndication ? (React.createElement("div", { className: classes.dayIndicationContainer }, dateIndicationNode)) : null)); }; this._handleDayClick = (value, modifiers = {}, event = null) => { this._preventActionEventDefault(event); const propsValue = this.props.value || {}; const { onChange, shouldCloseOnSelect } = this.props; if (this.props.selectionMode === 'range') { if ((!propsValue.from && !propsValue.to) || (propsValue.from && propsValue.to)) { onChange({ from: value }, modifiers); } else { const anchor = propsValue.from || propsValue.to; const newVal = anchor < value ? { from: anchor, to: value } : { from: value, to: anchor }; onChange(newVal, modifiers); shouldCloseOnSelect && this.props.onClose(event); } } else { onChange(value, modifiers); shouldCloseOnSelect && this.props.onClose(event); } }; this._preventActionEventDefault = (event = null) => { if (event && (!event.key || (event.key !== 'Escape' && event.key !== 'Tab'))) { event.preventDefault(); } }; this._createWeekdayElement = localeUtils => { return ({ className, weekday }) => { const weekdayShort = localeUtils.formatWeekdayShort(weekday); const weekdayLong = localeUtils.formatWeekdayLong(weekday); return (React.createElement("div", { className: className, "aria-label": weekdayLong, role: "columnheader" }, React.createElement("abbr", { "data-hook": "weekday-day" }, weekdayShort))); }; }; this._createDayPickerProps = () => { const { filterDate, excludePastDates, numOfMonths, firstDayOfWeek, rtl, today, onDisplayedViewChange, displayedMonth, captionElement, allowSelectingOutsideDays, size, fixedWeeks, } = this.props; const locale = this._getLocale(); const value = BaseCalendar.parseValue(this.props.value); const localeUtils = localeUtilsFactory(locale, firstDayOfWeek); const { from, to } = value || {}; const singleDay = !from && !to && value; const firstOfMonth = [ new Date(displayedMonth.getFullYear(), displayedMonth.getMonth(), 1), new Date(displayedMonth.getFullYear(), displayedMonth.getMonth() + 1, 1), ]; const lastOfMonth = [ new Date(displayedMonth.getFullYear(), displayedMonth.getMonth() + 1, 0), new Date(displayedMonth.getFullYear(), displayedMonth.getMonth() + 2, 0), ]; const selectedDays = this._getSelectedDays(value); const weekdayElement = this._createWeekdayElement(localeUtils); const modifiers = { [cssStates({ start: true })]: from, [cssStates({ end: true })]: to, [cssStates({ firstOfMonth: true })]: firstOfMonth, [cssStates({ lastOfMonth: true })]: lastOfMonth, [cssStates({ singleDay: true })]: singleDay, [cssStates({ past: true })]: { before: BaseCalendar.parseValue(today) }, ...this.props.modifiers, }; if (today) { modifiers[cssStates({ today: true })] = BaseCalendar.parseValue(today); } // We must add the dummy state since ReactDayPicker use it as a selector in their code const outsideCssState = allowSelectingOutsideDays ? cssStates({ dummyOutside: true }) : cssStates({ outside: true }); return { disabledDays: [ date => !filterDate(new Date(date)), excludePastDates ? { before: new Date() } : {}, ], initialMonth: displayedMonth, initialYear: displayedMonth, selectedDays, month: displayedMonth, year: displayedMonth, locale: typeof locale === 'string' ? locale : '', fixedWeeks, onKeyDown: this._handleKeyDown, onDayClick: this._handleDayClick, localeUtils, navbarElement: () => null, captionElement, onCaptionClick: this._preventActionEventDefault, onDayKeyDown: this._handleDayKeyDown, numberOfMonths: numOfMonths, modifiers, renderDay: this._renderDay, dir: rtl ? 'rtl' : 'ltr', tabIndex: 0, weekdayElement, classNames: { /* The classes: 'DayPicker', 'DayPicker-wrapper', 'DayPicker-Month', 'DayPicker-Day', 'disabled' are used as selectors for the elements at the drivers and at the e2e tests */ container: st(classes.container, { size }, 'DayPicker'), wrapper: 'DayPicker-wrapper', interactionDisabled: 'DayPicker--interactionDisabled', months: st(classes.months, { twoMonths: numOfMonths > 1 }), month: st(classes.month, { size }, 'DayPicker-Month'), weekdays: classes.weekdays, weekdaysRow: classes.weekdaysRow, weekday: st(classes.weekday, { size }), body: st(classes.body, { size }), week: classes.week, weekNumber: 'DayPicker-WeekNumber', day: st(classes.day, { size }, 'DayPicker-Day'), // default modifiers today: cssStates({ today: !today }), selected: cssStates({ selected: true }), disabled: st('disabled', cssStates({ disabled: true })), outside: outsideCssState, }, onMonthChange: onDisplayedViewChange, }; }; this._handleKeyDown = event => { const { onKeyDown } = this.props; if (onKeyDown) { onKeyDown(event); return; } if (event.key === 'Escape') { this.props.onClose(event); } }; this._toggleFirstDayTabIndex = tabIndex => { const firstDay = this.dayPickerRef.dayPicker.querySelector(`.DayPicker-Day[tabindex="${tabIndex}"]`); firstDay.tabIndex = tabIndex === 0 ? -1 : 0; }; this._focusSelectedDay = () => { if (this.dayPickerRef) { const selectedDay = this.dayPickerRef.dayPicker.querySelector(`.${cssStates({ selected: true })}`); if (selectedDay) { // The 'unfocused' class is used as a selector at the drivers and e2e test selectedDay.classList.add(cssStates({ unfocused: true }), 'unfocused'); selectedDay.tabIndex = 0; selectedDay.focus(); this._toggleFirstDayTabIndex(0); } else { this._toggleFirstDayTabIndex(-1); } } }; this._handleDayKeyDown = (_value, _modifiers, event = null) => { this._preventActionEventDefault(event); const unfocusedDay = this.dayPickerRef.dayPicker.querySelector(`.${cssStates({ unfocused: true })}`); if (unfocusedDay) { // The 'unfocused' class is used as a selector at the drivers and e2e test unfocusedDay.classList.remove(cssStates({ unfocused: true }), 'unfocused'); } }; } _getSelectedDays(value) { const { from, to } = value || {}; if (from && to) { return { from, to }; } else if (from) { return { after: BaseCalendar.prevDay(from) }; } else if (to) { return { before: BaseCalendar.nextDay(to) }; } else { // Single day OR empty value return value; } } _getLocale() { return this.props.locale || this.context.locale || 'en'; } componentDidMount() { this.props.autoFocus && this._focusSelectedDay(); if (this.dayPickerRef) { this.dayPickerRef.wrapper.tabIndex = -1; } } componentDidUpdate(prevProps) { if (!prevProps.autoFocus && this.props.autoFocus) { this._focusSelectedDay(); } } render() { const { dataHook, className, size } = this.props; return (React.createElement("div", { "data-hook": dataHook, "data-size": size, className: st(classes.root, className), onClick: this._preventActionEventDefault, role: "dialog", tabIndex: -1 }, React.createElement(DayPicker, { ref: ref => (this.dayPickerRef = ref), ...this._createDayPickerProps() }))); } } BaseCalendar.displayName = 'BaseCalendar'; BaseCalendar.defaultProps = { className: '', filterDate: () => true, dateIndication: () => null, shouldCloseOnSelect: true, onClose: () => { }, autoFocus: true, excludePastDates: false, selectionMode: 'day', showMonthDropdown: false, showYearDropdown: false, numOfMonths: 1, size: 'medium', allowSelectingOutsideDays: false, fixedWeeks: true, }; /** Return a value in which all string-dates are parsed into Date objects */ BaseCalendar.parseValue = value => { if (!value) { return new Date(); } if (value instanceof Date) { return value; } else { return { from: value.from, to: value.to, }; } }; BaseCalendar.nextDay = date => { const day = new Date(date); day.setDate(day.getDate() + 1); return day; }; BaseCalendar.prevDay = date => { const day = new Date(date); day.setDate(day.getDate() - 1); return day; }; export default BaseCalendar; BaseCalendar.contextType = WixStyleReactEnvironmentContext; BaseCalendar.propTypes = { /** Applies as data-hook HTML attribute that can be used in the tests */ dataHook: PropTypes.string, /** Focus selected day automatically when component mounts or updates */ autoFocus: PropTypes.bool, /** Allows to display multiple months at once. Currently it shows 1 or 2 months only. */ numOfMonths: PropTypes.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.oneOf([0, 1, 2, 3, 4, 5, 6]), /** A single CSS class name to be appended to the root element. */ className: PropTypes.string, /** Provides a callback function when day in selected in the calendar */ onChange: PropTypes.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.func, /** Provides a callback function when any key is clicked in the calendar */ onKeyDown: PropTypes.func, /** Specify whether past dates should be selectable or not */ excludePastDates: PropTypes.bool, /** * ##### Specify selectable dates: * * `param` {Date} `date` - a date to check * * `return` {boolean} - true if `date` should be selectable, false otherwise */ filterDate: PropTypes.func, /** Defines the selected date */ value: PropTypes.oneOfType([ PropTypes.instanceOf(Date), PropTypes.shape({ from: PropTypes.instanceOf(Date), to: PropTypes.instanceOf(Date), }), ]), /** Whether the user should be able to select a date range, or just a single day */ selectionMode: PropTypes.oneOf(['day', 'range']), /** Specify whether the calendar closes on day selection */ shouldCloseOnSelect: PropTypes.bool, /** Specify date picker instance locale */ locale: PropTypes.oneOfType([PropTypes.oneOf(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.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.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.instanceOf(Date), /** The current displayed month */ displayedMonth: PropTypes.instanceOf(Date).isRequired, /** * ##### A callback function that would be invoked every time that the displayed month / week would be changed. * ##### This would be passed as a prop (onMonthChange) for the ReactDayPicker component. * - `month` - The current displayed month * `return` void */ onDisplayedViewChange: PropTypes.func.isRequired, /** The Calendar head components which includes the navigation arrows, and the captions elements. */ captionElement: PropTypes.node.isRequired, /** Responsible for adding a new modifier for the day elements. For example: `hidden` for dates that shouldn’t be displayed. It should be passed as an object according to `ReactDayPicker` API. https://react-day-picker.js.org/docs/matching-days/ */ modifiers: PropTypes.object, /** Allow selecting dates that are outside of the current displayed month. */ allowSelectingOutsideDays: PropTypes.bool, }; //# sourceMappingURL=BaseCalendar.js.map