import React from 'react'; import CssTransitionGroup from 'react-addons-css-transition-group'; import { SlideLeft, SlideRight } from '../animations'; import { IconButton } from '../button'; import CalendarMonth from './CalendarMonth'; import time from '../utils/time'; import utils from '../utils/utils'; import style from './style.calendar'; class Calendar extends React.Component { static propTypes = { display: React.PropTypes.oneOf(['months', 'years']), maxDate: React.PropTypes.object, minDate: React.PropTypes.object, onChange: React.PropTypes.func, selectedDate: React.PropTypes.object, viewDate: React.PropTypes.object }; static defaultProps = { display: 'months', selectedDate: new Date() }; state = { viewDate: this.props.selectedDate }; componentDidUpdate () { if (this.refs.activeYear) { this.scrollToActive(); } } scrollToActive () { this.refs.years.scrollTop = this.refs.activeYear.offsetTop - this.refs.years.offsetHeight / 2 + this.refs.activeYear.offsetHeight / 2; } handleDayClick = (day) => { this.props.onChange(time.setDay(this.state.viewDate, day), true); }; handleYearClick = (year) => { const viewDate = time.setYear(this.props.selectedDate, year); this.setState({viewDate}); this.props.onChange(viewDate, false); }; changeViewMonth = (direction, step) => { this.setState({ direction, viewDate: time.addMonths(this.state.viewDate, step) }); }; renderYear (year) { const props = { className: year === this.state.viewDate.getFullYear() ? style.active : '', key: year, onClick: this.handleYearClick.bind(this, year) }; if (year === this.state.viewDate.getFullYear()) { props.ref = 'activeYear'; } return
  • {year}
  • ; } renderYears () { return ( ); } renderMonths () { const animation = this.state.direction === 'left' ? SlideLeft : SlideRight; return (
    ); } render () { return (
    {this.props.display === 'months' ? this.renderMonths() : this.renderYears()}
    ); } } export default Calendar;