react-toolbox/components/date_picker/dialog.jsx

83 lines
2.2 KiB
React
Raw Normal View History

import React from 'react';
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';
2015-10-20 21:10:47 +03:00
import Calendar from './calendar';
2015-09-09 23:34:56 +03:00
import Dialog from '../dialog';
2015-09-07 02:59:11 +03:00
2015-10-22 02:31:17 +03:00
class CalendarDialog extends React.Component {
static propTypes = {
2015-09-07 02:59:11 +03:00
initialDate: React.PropTypes.object,
onDateSelected: React.PropTypes.func
};
2015-09-07 02:59:11 +03:00
static defaultProps = {
initialDate: new Date()
};
2015-09-07 02:59:11 +03:00
state = {
date: this.props.initialDate,
display: 'months',
actions: [
2015-10-22 02:31:17 +03:00
{ label: 'Cancel', className: style.button, onClick: this.onDateCancel.bind(this) },
{ label: 'Ok', className: style.button, onClick: this.onDateSelected.bind(this) }
]
};
2015-09-07 02:59:11 +03:00
2015-10-22 02:31:17 +03:00
handleCalendarChange = (date) => {
this.setState({date, display: 'months'});
2015-10-22 02:31:17 +03:00
};
displayMonths = () => {
this.setState({display: 'months'});
};
displayYears = () => {
this.setState({display: 'years'});
};
2015-09-07 02:59:11 +03:00
onDateCancel () {
this.refs.dialog.hide();
}
2015-09-07 02:59:11 +03:00
onDateSelected () {
if (this.props.onDateSelected) this.props.onDateSelected(this.state.date);
this.refs.dialog.hide();
}
2015-09-07 02:59:11 +03:00
show () {
this.refs.dialog.show();
}
2015-09-07 02:59:11 +03:00
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}
2015-10-22 02:31:17 +03:00
onChange={this.handleCalendarChange}
selectedDate={this.state.date} />
2015-09-07 02:59:11 +03:00
</div>
</Dialog>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default CalendarDialog;