react-toolbox/components/date_picker/Calendar.js

118 lines
3.4 KiB
JavaScript
Raw Normal View History

import React from 'react';
import CssTransitionGroup from 'react-addons-css-transition-group';
2016-05-21 21:52:48 +03:00
import { themr } from 'react-css-themr';
2015-11-23 00:13:27 +03:00
import { SlideLeft, SlideRight } from '../animations';
2015-11-25 00:58:50 +03:00
import { IconButton } from '../button';
2015-11-23 00:13:27 +03:00
import CalendarMonth from './CalendarMonth';
import time from '../utils/time';
import utils from '../utils/utils';
2015-09-09 23:34:56 +03:00
2015-10-22 02:31:17 +03:00
class Calendar extends React.Component {
static propTypes = {
2015-09-07 02:41:59 +03:00
display: React.PropTypes.oneOf(['months', 'years']),
2015-11-16 11:56:05 +03:00
maxDate: React.PropTypes.object,
minDate: React.PropTypes.object,
2015-09-07 02:41:59 +03:00
onChange: React.PropTypes.func,
selectedDate: React.PropTypes.object,
2016-05-21 21:52:48 +03:00
theme: React.PropTypes.shape({
active: React.PropTypes.string.isRequired,
calendar: React.PropTypes.string.isRequired,
next: React.PropTypes.string.isRequired,
prev: React.PropTypes.string.isRequired,
years: React.PropTypes.string.isRequired
}),
2015-09-07 02:41:59 +03:00
viewDate: React.PropTypes.object
};
2015-09-07 02:41:59 +03:00
static defaultProps = {
display: 'months',
selectedDate: new Date()
};
2015-09-07 02:41:59 +03:00
state = {
viewDate: this.props.selectedDate
};
2015-09-07 02:41:59 +03:00
componentDidUpdate () {
if (this.refs.activeYear) {
2015-09-08 01:03:17 +03:00
this.scrollToActive();
2015-09-07 02:41:59 +03:00
}
}
2015-09-07 02:41:59 +03:00
2015-10-22 02:31:17 +03:00
scrollToActive () {
2016-04-10 18:49:23 +03:00
this.refs.years.scrollTop = this.refs.activeYear.offsetTop
- this.refs.years.offsetHeight / 2
+ this.refs.activeYear.offsetHeight / 2;
2015-10-22 02:31:17 +03:00
}
handleDayClick = (day) => {
2016-03-09 17:56:10 +03:00
this.props.onChange(time.setDay(this.state.viewDate, day), true);
2015-10-22 02:31:17 +03:00
};
2015-09-07 02:41:59 +03:00
2015-10-22 02:31:17 +03:00
handleYearClick = (year) => {
const viewDate = time.setYear(this.props.selectedDate, year);
2015-11-11 22:34:19 +03:00
this.setState({viewDate});
2016-03-09 17:56:10 +03:00
this.props.onChange(viewDate, false);
2015-10-22 02:31:17 +03:00
};
2015-09-07 02:41:59 +03:00
2015-11-25 00:58:50 +03:00
changeViewMonth = (direction, step) => {
2015-09-07 02:41:59 +03:00
this.setState({
2015-11-25 00:58:50 +03:00
direction,
viewDate: time.addMonths(this.state.viewDate, step)
2015-09-07 02:41:59 +03:00
});
2015-10-22 11:02:36 +03:00
};
2015-09-07 02:41:59 +03:00
renderYear (year) {
const props = {
2016-05-21 21:52:48 +03:00
className: year === this.state.viewDate.getFullYear() ? this.props.theme.active : '',
key: year,
2015-10-22 02:31:17 +03:00
onClick: this.handleYearClick.bind(this, year)
2015-09-07 02:41:59 +03:00
};
if (year === this.state.viewDate.getFullYear()) {
props.ref = 'activeYear';
}
return <li {...props}>{year}</li>;
}
2015-09-07 02:41:59 +03:00
renderYears () {
return (
2016-05-21 21:52:48 +03:00
<ul data-react-toolbox='years' ref="years" className={this.props.theme.years}>
{utils.range(1900, 2100).map((i) => { return this.renderYear(i); })}
2015-09-07 02:41:59 +03:00
</ul>
);
}
2015-09-07 02:41:59 +03:00
renderMonths () {
2016-05-21 21:52:48 +03:00
const { theme } = this.props;
const animation = this.state.direction === 'left' ? SlideLeft : SlideRight;
2015-09-07 02:41:59 +03:00
return (
<div data-react-toolbox='calendar'>
2016-05-21 21:52:48 +03:00
<IconButton className={theme.prev} icon='chevron_left' onClick={this.changeViewMonth.bind(this, 'left', -1)} />
<IconButton className={theme.next} icon='chevron_right' onClick={this.changeViewMonth.bind(this, 'right', 1)} />
<CssTransitionGroup transitionName={animation} transitionEnterTimeout={350} transitionLeaveTimeout={350}>
2015-11-23 00:13:27 +03:00
<CalendarMonth
2015-09-07 02:41:59 +03:00
key={this.state.viewDate.getMonth()}
2015-11-16 11:56:05 +03:00
maxDate={this.props.maxDate}
minDate={this.props.minDate}
2015-09-07 02:41:59 +03:00
viewDate={this.state.viewDate}
2015-11-11 22:34:19 +03:00
selectedDate={this.props.selectedDate}
2015-11-23 00:13:27 +03:00
onDayClick={this.handleDayClick}
/>
</CssTransitionGroup>
2015-09-07 02:41:59 +03:00
</div>
);
}
2015-09-07 02:41:59 +03:00
render () {
return (
2016-05-21 21:52:48 +03:00
<div className={this.props.theme.calendar}>
{this.props.display === 'months' ? this.renderMonths() : this.renderYears()}
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
2016-05-21 21:52:48 +03:00
export default themr('ToolboxDatePicker')(Calendar);