react-toolbox/components/date_picker/CalendarMonth.js

70 lines
2.2 KiB
JavaScript
Raw Normal View History

import React, { Component, PropTypes } from 'react';
import time from '../utils/time.js';
import utils from '../utils/utils.js';
import CalendarDay from './CalendarDay.js';
2015-09-07 02:41:59 +03:00
class Month extends Component {
static propTypes = {
maxDate: PropTypes.object,
minDate: PropTypes.object,
onDayClick: PropTypes.func,
selectedDate: PropTypes.object,
theme: PropTypes.shape({
days: PropTypes.string,
month: PropTypes.string,
title: PropTypes.string,
week: PropTypes.string
2016-05-21 21:52:48 +03:00
}),
viewDate: PropTypes.object,
locale: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object
]),
sundayFirstDayOfWeek: React.PropTypes.bool
};
2015-09-07 02:41:59 +03:00
2015-10-22 02:31:17 +03:00
handleDayClick = (day) => {
if (this.props.onDayClick) this.props.onDayClick(day);
2015-10-22 02:31:17 +03:00
};
2015-09-07 02:41:59 +03:00
renderWeeks () {
const days = utils.range(0, 7).map(d => time.getDayOfWeekLetter(d, this.props.locale));
const source = (this.props.sundayFirstDayOfWeek) ? days : [...days.slice(1), days[0]];
return source.map((d, i) => (<span key={i}>{d}</span>))
}
2015-09-07 02:41:59 +03:00
renderDays () {
return utils.range(1, time.getDaysInMonth(this.props.viewDate) + 1).map(i => {
const date = new Date(this.props.viewDate.getFullYear(), this.props.viewDate.getMonth(), i);
const disabled = time.dateOutOfRange(date, this.props.minDate, this.props.maxDate);
2015-11-16 11:56:05 +03:00
2015-09-07 02:41:59 +03:00
return (
2015-11-23 00:13:27 +03:00
<CalendarDay
key={i}
2015-09-07 02:41:59 +03:00
day={i}
disabled={disabled}
onClick={!disabled ? this.handleDayClick.bind(this, i) : null}
2015-09-07 02:41:59 +03:00
selectedDate={this.props.selectedDate}
theme={this.props.theme}
viewDate={this.props.viewDate}
sundayFirstDayOfWeek={this.props.sundayFirstDayOfWeek}
/>
2015-09-07 02:41:59 +03:00
);
});
}
2015-09-07 02:41:59 +03:00
render () {
return (
2016-05-21 21:52:48 +03:00
<div data-react-toolbox='month' className={this.props.theme.month}>
<span className={this.props.theme.title}>
{time.getFullMonth(this.props.viewDate, this.props.locale)} {this.props.viewDate.getFullYear()}
2015-09-07 02:41:59 +03:00
</span>
2016-05-21 21:52:48 +03:00
<div className={this.props.theme.week}>{this.renderWeeks()}</div>
<div className={this.props.theme.days}>{this.renderDays()}</div>
2015-09-07 02:41:59 +03:00
</div>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Month;