react-toolbox/components/date_picker/Calendar.js

127 lines
3.8 KiB
JavaScript
Raw Normal View History

import React, { Component, PropTypes } from 'react';
import CssTransitionGroup from 'react-addons-css-transition-group';
2015-11-23 00:13:27 +03:00
import { SlideLeft, SlideRight } from '../animations';
import time from '../utils/time.js';
import utils from '../utils/utils.js';
import CalendarMonth from './CalendarMonth.js';
const DIRECTION_STEPS = { left: -1, right: 1 };
const factory = (IconButton) => {
class Calendar extends Component {
static propTypes = {
display: PropTypes.oneOf(['months', 'years']),
locale: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object
]),
maxDate: PropTypes.object,
minDate: PropTypes.object,
onChange: PropTypes.func,
selectedDate: PropTypes.object,
sundayFirstDayOfWeek: React.PropTypes.bool,
theme: PropTypes.shape({
active: PropTypes.string,
calendar: PropTypes.string,
next: PropTypes.string,
prev: PropTypes.string,
years: PropTypes.string
}),
viewDate: PropTypes.object
};
static defaultProps = {
display: 'months',
selectedDate: new Date()
};
state = {
viewDate: this.props.selectedDate
};
componentDidUpdate () {
if (this.refs.activeYear) {
this.scrollToActive();
}
2015-09-07 02:41:59 +03:00
}
scrollToActive () {
this.refs.years.scrollTop = this.refs.activeYear.offsetTop
2016-04-10 18:49:23 +03:00
- this.refs.years.offsetHeight / 2
+ this.refs.activeYear.offsetHeight / 2;
}
handleDayClick = (day) => {
this.props.onChange(time.setDay(this.state.viewDate, day), true);
};
handleYearClick = (event) => {
const year = parseInt(event.target.id);
const viewDate = time.setYear(this.props.selectedDate, year);
this.setState({viewDate});
this.props.onChange(viewDate, false);
};
2015-10-22 02:31:17 +03:00
changeViewMonth = (event) => {
const direction = event.target.id;
this.setState({
direction,
viewDate: time.addMonths(this.state.viewDate, DIRECTION_STEPS[direction])
});
2015-09-07 02:41:59 +03:00
};
renderYears () {
return (
<ul data-react-toolbox='years' ref="years" className={this.props.theme.years}>
{utils.range(1900, 2100).map(year => (
<li
children={year}
className={year === this.state.viewDate.getFullYear() ? this.props.theme.active : ''}
id={year}
key={year}
onClick={this.handleYearClick}
ref={year === this.state.viewDate.getFullYear() ? 'activeYear' : undefined}
/>
))}
</ul>
);
}
2015-09-07 02:41:59 +03:00
renderMonths () {
const { theme } = this.props;
const animation = this.state.direction === 'left' ? SlideLeft : SlideRight;
return (
<div data-react-toolbox='calendar'>
<IconButton id='left' className={theme.prev} icon='chevron_left' onClick={this.changeViewMonth} />
<IconButton id='right' className={theme.next} icon='chevron_right' onClick={this.changeViewMonth} />
<CssTransitionGroup transitionName={animation} transitionEnterTimeout={350} transitionLeaveTimeout={350}>
<CalendarMonth
key={this.state.viewDate.getMonth()}
2016-08-07 14:43:07 +03:00
locale={this.props.locale}
maxDate={this.props.maxDate}
minDate={this.props.minDate}
onDayClick={this.handleDayClick}
selectedDate={this.props.selectedDate}
2016-08-07 14:43:07 +03:00
sundayFirstDayOfWeek={this.props.sundayFirstDayOfWeek}
theme={this.props.theme}
viewDate={this.state.viewDate}
/>
</CssTransitionGroup>
</div>
);
}
2015-09-07 02:41:59 +03:00
render () {
return (
<div className={this.props.theme.calendar}>
{this.props.display === 'months' ? this.renderMonths() : this.renderYears()}
</div>
);
}
}
2015-09-07 02:41:59 +03:00
return Calendar;
};
2015-10-22 02:31:17 +03:00
export default factory;