react-toolbox/components/date_picker/dialog.jsx

90 lines
2.3 KiB
React
Raw Normal View History

import React from 'react';
import PureRenderMixin from 'react-addons-pure-render-mixin';
2015-10-10 21:06:16 +03:00
import style from './style';
2015-09-09 23:34:56 +03:00
import time from '../utils/time';
import Calendar from '../calendar';
import Dialog from '../dialog';
2015-09-07 02:59:11 +03:00
2015-09-09 23:34:56 +03:00
export default React.createClass({
mixins: [PureRenderMixin],
2015-09-08 01:04:45 +03:00
2015-09-07 02:59:11 +03:00
displayName: 'CalendarDialog',
propTypes: {
initialDate: React.PropTypes.object,
onDateSelected: React.PropTypes.func
},
getDefaultProps () {
return {
initialDate: new Date()
};
},
getInitialState () {
return {
date: this.props.initialDate,
display: 'months',
actions: [
{ label: 'Cancel', className: style.button, onClick: this.onDateCancel },
{ label: 'Ok', className: style.button, onClick: this.onDateSelected }
2015-09-07 02:59:11 +03:00
]
};
},
onCalendarChange (date) {
this.setState({date: date, display: 'months'});
},
onDateCancel () {
this.refs.dialog.hide();
},
onDateSelected () {
if (this.props.onDateSelected) this.props.onDateSelected(this.state.date);
this.refs.dialog.hide();
},
show () {
this.refs.dialog.show();
},
displayMonths () {
this.setState({display: 'months'});
},
displayYears () {
this.setState({display: 'years'});
},
render () {
2015-10-10 21:06:16 +03:00
const display = `display-${this.state.display}`;
const headerClassName = `${style.header} ${style[display]}`;
2015-09-07 02:59:11 +03:00
return (
2015-10-10 21:06:16 +03:00
<Dialog ref="dialog" type="custom" className={style.dialog} actions={this.state.actions}>
<header className={headerClassName}>
<span className={style.weekday}>
2015-09-07 02:59:11 +03:00
{time.getFullDayOfWeek(this.state.date.getDay())}
</span>
<div onClick={this.displayMonths}>
2015-10-10 21:06:16 +03:00
<span className={style.month}>{time.getShortMonth(this.state.date)}</span>
<span className={style.day}>{this.state.date.getDate()}</span>
2015-09-07 02:59:11 +03:00
</div>
2015-10-10 21:06:16 +03:00
<span className={style.year} onClick={this.displayYears}>
2015-09-07 02:59:11 +03:00
{this.state.date.getFullYear()}
</span>
</header>
2015-10-10 21:06:16 +03:00
<div className={style.wrapper}>
2015-09-07 02:59:11 +03:00
<Calendar
ref="calendar"
display={this.state.display}
onChange={this.onCalendarChange}
selectedDate={this.state.date} />
2015-09-07 02:59:11 +03:00
</div>
</Dialog>
);
}
});