react-dates
Version:
A responsive and accessible date range picker component built with React
545 lines (454 loc) • 15.8 kB
JSX
import React, { PropTypes } from 'react';
import shallowCompare from 'react-addons-shallow-compare';
import ReactDOM from 'react-dom';
import { forbidExtraProps, nonNegativeInteger } from 'airbnb-prop-types';
import moment from 'moment';
import cx from 'classnames';
import { DayPickerPhrases } from '../defaultPhrases';
import getPhrasePropTypes from '../utils/getPhrasePropTypes';
import OutsideClickHandler from './OutsideClickHandler';
import CalendarMonthGrid from './CalendarMonthGrid';
import DayPickerNavigation from './DayPickerNavigation';
import getTransformStyles from '../utils/getTransformStyles';
import getCalendarMonthWidth from '../utils/getCalendarMonthWidth';
import ScrollableOrientationShape from '../shapes/ScrollableOrientationShape';
import {
HORIZONTAL_ORIENTATION,
VERTICAL_ORIENTATION,
VERTICAL_SCROLLABLE,
DAY_SIZE,
} from '../../constants';
const MONTH_PADDING = 23;
const DAY_PICKER_PADDING = 9;
const PREV_TRANSITION = 'prev';
const NEXT_TRANSITION = 'next';
const propTypes = forbidExtraProps({
// calendar presentation props
enableOutsideDays: PropTypes.bool,
numberOfMonths: PropTypes.number,
orientation: ScrollableOrientationShape,
withPortal: PropTypes.bool,
onOutsideClick: PropTypes.func,
hidden: PropTypes.bool,
initialVisibleMonth: PropTypes.func,
renderCalendarInfo: PropTypes.func,
daySize: nonNegativeInteger,
// navigation props
navPrev: PropTypes.node,
navNext: PropTypes.node,
onPrevMonthClick: PropTypes.func,
onNextMonthClick: PropTypes.func,
// day props
modifiers: PropTypes.object,
renderDay: PropTypes.func,
onDayClick: PropTypes.func,
onDayMouseEnter: PropTypes.func,
onDayMouseLeave: PropTypes.func,
isFocused: PropTypes.bool,
onBlur: PropTypes.func,
// internationalization
monthFormat: PropTypes.string,
phrases: PropTypes.shape(getPhrasePropTypes(DayPickerPhrases)),
});
export const defaultProps = {
// calendar presentation props
enableOutsideDays: false,
numberOfMonths: 2,
orientation: HORIZONTAL_ORIENTATION,
withPortal: false,
onOutsideClick() {},
hidden: false,
initialVisibleMonth: () => moment(),
renderCalendarInfo: null,
daySize: DAY_SIZE,
// navigation props
navPrev: null,
navNext: null,
onPrevMonthClick() {},
onNextMonthClick() {},
// day props
modifiers: {},
renderDay: null,
onDayClick() {},
onDayMouseEnter() {},
onDayMouseLeave() {},
isFocused: false,
onBlur() {},
// internationalization
monthFormat: 'MMMM YYYY',
phrases: DayPickerPhrases,
};
function applyTransformStyles(el, transform, opacity = '') {
const transformStyles = getTransformStyles(transform);
transformStyles.opacity = opacity;
Object.keys(transformStyles).forEach((styleKey) => {
// eslint-disable-next-line no-param-reassign
el.style[styleKey] = transformStyles[styleKey];
});
}
export function calculateDimension(el, axis, borderBox = false, withMargin = false) {
if (!el) {
return 0;
}
const axisStart = axis === 'width' ? 'Left' : 'Top';
const axisEnd = axis === 'width' ? 'Right' : 'Bottom';
// Only read styles if we need to
const style = (!borderBox || withMargin) ? window.getComputedStyle(el) : null;
// Offset includes border and padding
const { offsetWidth, offsetHeight } = el;
let size = axis === 'width' ? offsetWidth : offsetHeight;
// Get the inner size
if (!borderBox) {
size -= (
parseFloat(style[`padding${axisStart}`]) +
parseFloat(style[`padding${axisEnd}`]) +
parseFloat(style[`border${axisStart}Width`]) +
parseFloat(style[`border${axisEnd}Width`])
);
}
// Apply margin
if (withMargin) {
size += (parseFloat(style[`margin${axisStart}`]) + parseFloat(style[`margin${axisEnd}`]));
}
return size;
}
function getMonthHeight(el) {
const caption = el.querySelector('.js-CalendarMonth__caption');
const grid = el.querySelector('.js-CalendarMonth__grid');
// Need to separate out table children for FF
// Add an additional +1 for the border
return (
calculateDimension(caption, 'height', true, true) + calculateDimension(grid, 'height') + 1
);
}
export default class DayPicker extends React.Component {
constructor(props) {
super(props);
this.hasSetInitialVisibleMonth = !props.hidden;
this.state = {
currentMonth: props.hidden ? moment() : props.initialVisibleMonth(),
monthTransition: null,
translationValue: 0,
scrollableMonthMultiple: 1,
calendarMonthWidth: getCalendarMonthWidth(props.daySize),
};
this.onPrevMonthClick = this.onPrevMonthClick.bind(this);
this.onNextMonthClick = this.onNextMonthClick.bind(this);
this.multiplyScrollableMonths = this.multiplyScrollableMonths.bind(this);
this.updateStateAfterMonthTransition = this.updateStateAfterMonthTransition.bind(this);
}
componentDidMount() {
if (this.isHorizontal()) {
this.adjustDayPickerHeight();
this.initializeDayPickerWidth();
}
if (this.props.isFocused) {
this.container.focus();
}
}
componentWillReceiveProps(nextProps) {
if (!nextProps.hidden) {
if (!this.hasSetInitialVisibleMonth) {
this.hasSetInitialVisibleMonth = true;
this.setState({
currentMonth: nextProps.initialVisibleMonth(),
});
}
if (!this.dayPickerWidth && this.isHorizontal()) {
this.initializeDayPickerWidth();
this.adjustDayPickerHeight();
}
}
if (nextProps.daySize !== this.props.daySize) {
this.setState({
calendarMonthWidth: getCalendarMonthWidth(nextProps.daySize),
});
}
}
shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
componentDidUpdate(prevProps, prevState) {
if (this.state.monthTransition || !this.state.currentMonth.isSame(prevState.currentMonth)) {
if (this.isHorizontal()) {
this.adjustDayPickerHeight();
}
}
if (!prevProps.isFocused && this.props.isFocused) {
this.container.focus();
}
}
onPrevMonthClick(e) {
if (e) e.preventDefault();
if (this.props.onPrevMonthClick) {
this.props.onPrevMonthClick(e);
}
const translationValue =
this.isVertical() ? this.getMonthHeightByIndex(0) : this.dayPickerWidth;
// The first CalendarMonth is always positioned absolute at top: 0 or left: 0
// so we need to transform it to the appropriate location before the animation.
// This behavior is because we would otherwise need a double-render in order to
// adjust the container position once we had the height the first calendar
// (ie first draw all the calendar, then in a second render, use the first calendar's
// height to position the container). Variable calendar heights, amirite? <3 Maja
this.translateFirstDayPickerForAnimation(translationValue);
this.setState({
monthTransition: PREV_TRANSITION,
translationValue,
});
}
onNextMonthClick(e) {
if (e) e.preventDefault();
if (this.props.onNextMonthClick) {
this.props.onNextMonthClick(e);
}
const translationValue =
this.isVertical() ? -this.getMonthHeightByIndex(1) : -this.dayPickerWidth;
this.setState({
monthTransition: NEXT_TRANSITION,
translationValue,
});
}
getMonthHeightByIndex(i) {
return getMonthHeight(this.transitionContainer.querySelectorAll('.CalendarMonth')[i]);
}
multiplyScrollableMonths(e) {
if (e) e.preventDefault();
this.setState({
scrollableMonthMultiple: this.state.scrollableMonthMultiple + 1,
});
}
isHorizontal() {
return this.props.orientation === HORIZONTAL_ORIENTATION;
}
isVertical() {
return this.props.orientation === VERTICAL_ORIENTATION ||
this.props.orientation === VERTICAL_SCROLLABLE;
}
initializeDayPickerWidth() {
this.dayPickerWidth = calculateDimension(
// eslint-disable-next-line react/no-find-dom-node
ReactDOM.findDOMNode(this.calendarMonthGrid).querySelector('.CalendarMonth'),
'width',
true,
);
}
updateStateAfterMonthTransition() {
const { currentMonth, monthTransition } = this.state;
let newMonth = currentMonth;
if (monthTransition === PREV_TRANSITION) {
newMonth = currentMonth.clone().subtract(1, 'month');
} else if (monthTransition === NEXT_TRANSITION) {
newMonth = currentMonth.clone().add(1, 'month');
}
// clear the previous transforms
applyTransformStyles(
// eslint-disable-next-line react/no-find-dom-node
ReactDOM.findDOMNode(this.calendarMonthGrid).querySelector('.CalendarMonth'),
'none',
);
this.setState({
currentMonth: newMonth,
monthTransition: null,
translationValue: 0,
});
}
adjustDayPickerHeight() {
const heights = [];
Array.prototype.forEach.call(this.transitionContainer.querySelectorAll('.CalendarMonth'),
(el) => {
if (el.getAttribute('data-visible') === 'true') {
heights.push(getMonthHeight(el));
}
},
);
const newMonthHeight = Math.max(...heights) + MONTH_PADDING;
if (newMonthHeight !== calculateDimension(this.transitionContainer, 'height')) {
this.monthHeight = newMonthHeight;
this.transitionContainer.style.height = `${newMonthHeight}px`;
}
}
translateFirstDayPickerForAnimation(translationValue) {
const transformType = this.isVertical() ? 'translateY' : 'translateX';
const transformValue = `${transformType}(-${translationValue}px)`;
applyTransformStyles(
this.transitionContainer.querySelector('.CalendarMonth'),
transformValue,
1,
);
}
renderNavigation() {
const {
navPrev,
navNext,
orientation,
} = this.props;
let onNextMonthClick;
if (orientation === VERTICAL_SCROLLABLE) {
onNextMonthClick = this.multiplyScrollableMonths;
} else {
onNextMonthClick = this.onNextMonthClick;
}
return (
<DayPickerNavigation
onPrevMonthClick={this.onPrevMonthClick}
onNextMonthClick={onNextMonthClick}
navPrev={navPrev}
navNext={navNext}
orientation={orientation}
/>
);
}
renderWeekHeader(index) {
const { daySize, orientation } = this.props;
const { calendarMonthWidth } = this.state;
const verticalScrollable = orientation === VERTICAL_SCROLLABLE;
const horizontalStyle = {
left: index * calendarMonthWidth,
};
const verticalStyle = {
marginLeft: -calendarMonthWidth / 2,
};
let style = {}; // no styles applied to the vertical-scrollable orientation
if (this.isHorizontal()) {
style = horizontalStyle;
} else if (this.isVertical() && !verticalScrollable) {
style = verticalStyle;
}
const header = [];
for (let i = 0; i < 7; i += 1) {
header.push(
<li key={i} style={{ width: daySize }}>
<small>{moment().weekday(i).format('dd')}</small>
</li>,
);
}
return (
<div
className="DayPicker__week-header"
key={`week-${index}`}
style={style}
>
<ul>
{header}
</ul>
</div>
);
}
render() {
const {
currentMonth,
monthTransition,
translationValue,
scrollableMonthMultiple,
} = this.state;
const {
enableOutsideDays,
numberOfMonths,
orientation,
modifiers,
withPortal,
onDayClick,
onDayMouseEnter,
onDayMouseLeave,
renderDay,
renderCalendarInfo,
onOutsideClick,
monthFormat,
daySize,
} = this.props;
const { calendarMonthWidth } = this.state;
const numOfWeekHeaders = this.isVertical() ? 1 : numberOfMonths;
const weekHeaders = [];
for (let i = 0; i < numOfWeekHeaders; i += 1) {
weekHeaders.push(this.renderWeekHeader(i));
}
let firstVisibleMonthIndex = 1;
if (monthTransition === PREV_TRANSITION) {
firstVisibleMonthIndex -= 1;
} else if (monthTransition === NEXT_TRANSITION) {
firstVisibleMonthIndex += 1;
}
const verticalScrollable = this.props.orientation === VERTICAL_SCROLLABLE;
const dayPickerClassNames = cx('DayPicker', {
'DayPicker--horizontal': this.isHorizontal(),
'DayPicker--vertical': this.isVertical(),
'DayPicker--vertical-scrollable': verticalScrollable,
'DayPicker--portal': withPortal,
});
const transitionContainerClasses = cx('transition-container', {
'transition-container--horizontal': this.isHorizontal(),
'transition-container--vertical': this.isVertical(),
});
const horizontalWidth = (calendarMonthWidth * numberOfMonths) + (2 * DAY_PICKER_PADDING);
// this is a kind of made-up value that generally looks good. we'll
// probably want to let the user set this explicitly.
const verticalHeight = 1.75 * calendarMonthWidth;
const dayPickerStyle = {
width: this.isHorizontal() && horizontalWidth,
// These values are to center the datepicker (approximately) on the page
marginLeft: this.isHorizontal() && withPortal && -horizontalWidth / 2,
marginTop: this.isHorizontal() && withPortal && -calendarMonthWidth / 2,
};
const transitionContainerStyle = {
width: this.isHorizontal() && horizontalWidth,
height: this.isVertical() && !verticalScrollable && !withPortal && verticalHeight,
};
const isCalendarMonthGridAnimating = monthTransition !== null;
const transformType = this.isVertical() ? 'translateY' : 'translateX';
const transformValue = `${transformType}(${translationValue}px)`;
return (
<div
className={dayPickerClassNames}
style={dayPickerStyle}
>
<OutsideClickHandler onOutsideClick={onOutsideClick}>
<div
className="DayPicker__week-headers"
aria-hidden="true"
role="presentation"
>
{weekHeaders}
</div>
<div // eslint-disable-line jsx-a11y/no-static-element-interactions
className="DayPicker__focus-region"
ref={(ref) => { this.container = ref; }}
onClick={(e) => { e.stopPropagation(); }}
role="region"
tabIndex={-1}
>
{!verticalScrollable && this.renderNavigation()}
<div
className={transitionContainerClasses}
ref={(ref) => { this.transitionContainer = ref; }}
style={transitionContainerStyle}
>
<CalendarMonthGrid
ref={(ref) => { this.calendarMonthGrid = ref; }}
transformValue={transformValue}
enableOutsideDays={enableOutsideDays}
firstVisibleMonthIndex={firstVisibleMonthIndex}
initialMonth={currentMonth}
isAnimating={isCalendarMonthGridAnimating}
modifiers={modifiers}
orientation={orientation}
numberOfMonths={numberOfMonths * scrollableMonthMultiple}
onDayClick={onDayClick}
onDayMouseEnter={onDayMouseEnter}
onDayMouseLeave={onDayMouseLeave}
renderDay={renderDay}
onMonthTransitionEnd={this.updateStateAfterMonthTransition}
monthFormat={monthFormat}
daySize={daySize}
/>
{verticalScrollable && this.renderNavigation()}
</div>
{renderCalendarInfo && renderCalendarInfo()}
</div>
</OutsideClickHandler>
</div>
);
}
}
DayPicker.propTypes = propTypes;
DayPicker.defaultProps = defaultProps;