react-toolbox/components/date_picker/DatePickerDialog.js

113 lines
3.4 KiB
JavaScript
Raw Normal View History

import React from 'react';
2016-05-21 21:52:48 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2015-11-23 00:13:27 +03:00
import Calendar from './Calendar';
import Dialog from '../dialog';
import time from '../utils/time';
2015-09-07 02:59:11 +03:00
2015-10-22 02:31:17 +03:00
class CalendarDialog extends React.Component {
static propTypes = {
2015-11-10 16:27:00 +03:00
active: React.PropTypes.bool,
2016-03-09 17:56:10 +03:00
autoOk: React.PropTypes.bool,
className: React.PropTypes.string,
2015-11-16 11:56:05 +03:00
maxDate: React.PropTypes.object,
minDate: React.PropTypes.object,
2015-11-11 22:34:19 +03:00
onDismiss: React.PropTypes.func,
onSelect: React.PropTypes.func,
2016-05-21 21:52:48 +03:00
theme: React.PropTypes.shape({
button: React.PropTypes.string.isRequired,
calendarWrapper: React.PropTypes.string.isRequired,
date: React.PropTypes.string.isRequired,
dialog: React.PropTypes.string.isRequired,
header: React.PropTypes.string.isRequired,
monthsDisplay: React.PropTypes.string.isRequired,
year: React.PropTypes.string.isRequired,
yearsDisplay: React.PropTypes.string.isRequired
}),
2015-11-11 22:34:19 +03:00
value: React.PropTypes.object
};
2015-09-07 02:59:11 +03:00
static defaultProps = {
2015-11-10 16:27:00 +03:00
active: false,
className: '',
2015-11-11 22:34:19 +03:00
value: new Date()
};
2015-09-07 02:59:11 +03:00
state = {
display: 'months',
date: this.props.value
};
2015-09-07 02:59:11 +03:00
componentWillMount () {
this.updateStateDate(this.props.value);
}
componentWillReceiveProps (nextProps) {
this.updateStateDate(nextProps.value);
}
2016-03-09 17:56:10 +03:00
handleCalendarChange = (value, dayClick) => {
2015-11-16 20:02:32 +03:00
const state = {display: 'months', date: value};
if (time.dateOutOfRange(value, this.props.minDate, this.props.maxDate)) {
state.date = this.props.maxDate || this.props.minDate;
}
this.setState(state);
2016-03-09 17:56:10 +03:00
if (dayClick && this.props.autoOk && this.props.onSelect) {
this.props.onSelect(value);
}
2015-10-22 02:31:17 +03:00
};
handleSelect = (event) => {
if (this.props.onSelect) this.props.onSelect(this.state.date, event);
2015-10-22 02:31:17 +03:00
};
2015-11-11 22:34:19 +03:00
handleSwitchDisplay = (display) => {
this.setState({ display });
2015-10-22 02:31:17 +03:00
};
2015-09-07 02:59:11 +03:00
updateStateDate = (date) => {
if (Object.prototype.toString.call(date) === '[object Date]') {
this.setState({
date
});
}
}
2015-11-11 22:34:19 +03:00
actions = [
2016-05-21 21:52:48 +03:00
{ label: 'Cancel', className: this.props.theme.button, onClick: this.props.onDismiss },
{ label: 'Ok', className: this.props.theme.button, onClick: this.handleSelect }
2015-11-11 22:34:19 +03:00
];
2015-09-07 02:59:11 +03:00
render () {
2016-05-21 21:52:48 +03:00
const { theme } = this.props;
const display = `${this.state.display}Display`;
const className = classnames(theme.dialog, this.props.className);
const headerClassName = classnames(theme.header, theme[display]);
2015-09-07 02:59:11 +03:00
return (
<Dialog active={this.props.active} type="custom" className={className} actions={this.actions}>
2015-10-10 21:06:16 +03:00
<header className={headerClassName}>
2016-05-21 21:52:48 +03:00
<span className={theme.year} onClick={this.handleSwitchDisplay.bind(this, 'years')}>
2015-11-16 20:02:32 +03:00
{this.state.date.getFullYear()}
2015-09-07 02:59:11 +03:00
</span>
2016-05-21 21:52:48 +03:00
<h3 className={theme.date} onClick={this.handleSwitchDisplay.bind(this, 'months')}>
{time.getShortDayOfWeek(this.state.date.getDay())}, {time.getShortMonth(this.state.date)} {this.state.date.getDate()}
</h3>
2015-09-07 02:59:11 +03:00
</header>
2016-05-21 21:52:48 +03:00
<div className={theme.calendarWrapper}>
2015-09-07 02:59:11 +03:00
<Calendar
display={this.state.display}
2015-11-16 11:56:05 +03:00
maxDate={this.props.maxDate}
minDate={this.props.minDate}
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
2016-05-21 21:52:48 +03:00
export default themr('ToolboxDatePicker')(CalendarDialog);