wix-style-react
Version:
444 lines (375 loc) • 14.7 kB
JavaScript
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';
// This file is used for backward compatibility. Since the `locale` prop allows passing `date-fns locale object` as value we still need to support it!
import dateFnsLocaleUtilsFactory from '../../common/LocaleUtils/DateFnsLocaleUtils';
import legacyParse from '../../common/LocaleUtils/legacyParse';
import { WixStyleReactEnvironmentContext } from '../../WixStyleReactEnvironmentProvider/context';
import { supportedWixlocales } from 'wix-design-systems-locale-utils';
export default class BaseCalendar extends React.PureComponent {
static displayName = 'BaseCalendar';
static defaultProps = {
className: '',
filterDate: () => true,
dateIndication: () => null,
shouldCloseOnSelect: true,
onClose: () => {},
autoFocus: true,
excludePastDates: false,
selectionMode: 'day',
showMonthDropdown: false,
showYearDropdown: false,
numOfMonths: 1,
allowSelectingOutsideDays: false,
};
static optionalParse = dateOrString =>
typeof dateOrString === 'string' ? legacyParse(dateOrString) : dateOrString;
/** Return a value in which all string-dates are parsed into Date objects */
static parseValue = value => {
if (!value) {
return new Date();
}
if (typeof value === 'string') {
return legacyParse(value);
} else if (value instanceof Date) {
return value;
} else {
return {
from: BaseCalendar.optionalParse(value.from),
to: BaseCalendar.optionalParse(value.to),
};
}
};
static nextDay = date => {
const day = new Date(date);
day.setDate(day.getDate() + 1);
return day;
};
static prevDay = date => {
const day = new Date(date);
day.setDate(day.getDate() - 1);
return day;
};
_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 (
<div
className={st(classes.dayWrapper, {
hasIndication: shouldHasIndication,
})}
data-date={`${day.getFullYear()}-${day.getMonth()}-${day.getDate()}`}
data-outsideday={isOutsideDay}
>
<div className={classes.dayText}>{day.getDate()}</div>
{shouldHasIndication ? (
<div className={classes.dayIndicationContainer}>
{dateIndicationNode}
</div>
) : null}
</div>
);
};
_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);
}
};
_getSelectedDays(value) {
const { from, to } = value || {};
if (from && to) {
return { from: from, to: 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;
}
}
_preventActionEventDefault = (event = null) => {
// We should not prevent "TAB"/"ESC" key
if (event && (!event.key || !this.keyHandlers[event.key])) {
event.preventDefault();
}
};
_createWeekdayElement = localeUtils => {
return ({ className, weekday }) => {
const weekdayShort = localeUtils.formatWeekdayShort(weekday);
const weekdayLong = localeUtils.formatWeekdayLong(weekday);
return (
<div className={className} aria-label={weekdayLong} role="columnheader">
<abbr data-hook="weekday-day">{weekdayShort}</abbr>
</div>
);
};
};
_createDayPickerProps = () => {
const {
filterDate,
excludePastDates,
numOfMonths,
firstDayOfWeek,
rtl,
today,
onDisplayedViewChange,
displayedMonth,
captionElement,
allowSelectingOutsideDays,
} = this.props;
const locale = this._getLocale();
const value = BaseCalendar.parseValue(this.props.value);
const localeUtils = this._getLocaleUtilsFactory(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,
...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: true,
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',
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('DayPicker', classes.container),
wrapper: 'DayPicker-wrapper',
interactionDisabled: 'DayPicker--interactionDisabled',
months: st(classes.months, { twoMonths: numOfMonths > 1 }),
month: st('DayPicker-Month', classes.month),
weekdays: classes.weekdays,
weekdaysRow: classes.weekdaysRow,
weekday: classes.weekday,
body: classes.body,
week: classes.week,
weekNumber: 'DayPicker-WeekNumber',
day: st('DayPicker-Day', classes.day),
// default modifiers
today: cssStates({ today: !today }),
selected: cssStates({ selected: true }),
disabled: st('disabled', cssStates({ disabled: true })),
outside: outsideCssState,
},
onMonthChange: onDisplayedViewChange,
};
};
_handleKeyDown = event => {
const keyHandler = this.keyHandlers[event.key];
keyHandler && keyHandler(event);
};
keyHandlers = {
// escape
Escape: this.props.onClose,
// tab
Tab: this.props.onClose,
};
_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.focus();
}
}
};
_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',
);
}
};
_getLocale() {
return this.props.locale || this.context.locale || 'en';
}
_getLocaleUtilsFactory = (locale, firstDayOfWeek) => {
// The `dateFnsLocaleUtilsFactory` is used for backward compatibility.
// In case that the user passes an `date-fns locale object` we need to use our old `LocaleUtilsFactory`.
return typeof locale === 'string'
? localeUtilsFactory(locale, firstDayOfWeek)
: dateFnsLocaleUtilsFactory(locale, firstDayOfWeek);
};
componentDidMount() {
this.props.autoFocus && this._focusSelectedDay();
}
componentDidUpdate(prevProps) {
if (!prevProps.autoFocus && this.props.autoFocus) {
this._focusSelectedDay();
}
}
render() {
const { dataHook, className } = this.props;
return (
<div
data-hook={dataHook}
className={st(classes.root, className)}
onClick={this._preventActionEventDefault}
>
<DayPicker
ref={ref => (this.dayPickerRef = ref)}
{...this._createDayPickerProps()}
/>
</div>
);
}
}
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,
/** 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.string,
PropTypes.instanceOf(Date),
PropTypes.shape({
from: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
to: PropTypes.oneOfType([PropTypes.string, 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),
PropTypes.shape({
code: PropTypes.string,
formatDistance: PropTypes.func,
formatRelative: PropTypes.func,
localize: PropTypes.object,
formatLong: PropTypes.object,
match: PropTypes.object,
options: PropTypes.object,
}),
]),
/** 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.oneOfType([PropTypes.string, 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,
};