2016-05-28 23:04:00 +03:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-05-21 21:52:48 +03:00
|
|
|
import classnames from 'classnames';
|
2016-05-28 23:04:00 +03:00
|
|
|
import time from '../utils/time.js';
|
2016-04-22 07:24:37 +03:00
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
const factory = (Dialog, Calendar) => {
|
|
|
|
class CalendarDialog extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
active: PropTypes.bool,
|
|
|
|
autoOk: PropTypes.bool,
|
|
|
|
className: PropTypes.string,
|
|
|
|
maxDate: PropTypes.object,
|
|
|
|
minDate: PropTypes.object,
|
2016-06-06 20:37:51 +03:00
|
|
|
name: PropTypes.string,
|
2016-05-28 23:04:00 +03:00
|
|
|
onDismiss: PropTypes.func,
|
2016-07-10 14:42:35 +03:00
|
|
|
onEscKeyDown: PropTypes.func,
|
|
|
|
onOverlayClick: PropTypes.func,
|
2016-05-28 23:04:00 +03:00
|
|
|
onSelect: PropTypes.func,
|
|
|
|
theme: PropTypes.shape({
|
2016-06-04 00:44:33 +03:00
|
|
|
button: PropTypes.string,
|
|
|
|
calendarWrapper: PropTypes.string,
|
|
|
|
date: PropTypes.string,
|
|
|
|
dialog: PropTypes.string,
|
|
|
|
header: PropTypes.string,
|
|
|
|
monthsDisplay: PropTypes.string,
|
|
|
|
year: PropTypes.string,
|
|
|
|
yearsDisplay: PropTypes.string
|
2016-05-28 23:04:00 +03:00
|
|
|
}),
|
|
|
|
value: PropTypes.object
|
|
|
|
};
|
2016-04-22 07:24:37 +03:00
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
static defaultProps = {
|
|
|
|
active: false,
|
|
|
|
className: '',
|
|
|
|
value: new Date()
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
display: 'months',
|
|
|
|
date: this.props.value
|
|
|
|
};
|
|
|
|
|
|
|
|
componentWillMount () {
|
|
|
|
this.updateStateDate(this.props.value);
|
2016-04-22 07:24:37 +03:00
|
|
|
|
2015-11-16 18:54:05 +03:00
|
|
|
}
|
2016-05-28 23:04:00 +03:00
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
this.updateStateDate(nextProps.value);
|
2016-03-09 17:56:10 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
handleCalendarChange = (value, dayClick) => {
|
|
|
|
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);
|
|
|
|
if (dayClick && this.props.autoOk && this.props.onSelect) {
|
|
|
|
this.props.onSelect(value);
|
|
|
|
}
|
|
|
|
};
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
handleSelect = (event) => {
|
|
|
|
if (this.props.onSelect) this.props.onSelect(this.state.date, event);
|
|
|
|
};
|
2015-09-07 02:59:11 +03:00
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
handleSwitchDisplay = (display) => {
|
|
|
|
this.setState({ display });
|
|
|
|
};
|
|
|
|
|
|
|
|
updateStateDate = (date) => {
|
|
|
|
if (Object.prototype.toString.call(date) === '[object Date]') {
|
|
|
|
this.setState({
|
|
|
|
date
|
|
|
|
});
|
|
|
|
}
|
2016-04-22 07:24:37 +03:00
|
|
|
}
|
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
actions = [
|
|
|
|
{ label: 'Cancel', className: this.props.theme.button, onClick: this.props.onDismiss },
|
2016-06-06 20:37:51 +03:00
|
|
|
{ label: 'Ok', className: this.props.theme.button, name: this.props.name, onClick: this.handleSelect }
|
2016-05-28 23:04:00 +03:00
|
|
|
];
|
2015-09-07 02:59:11 +03:00
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
render () {
|
|
|
|
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-10-11 20:07:09 +03:00
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
return (
|
2016-07-10 14:42:35 +03:00
|
|
|
<Dialog
|
|
|
|
actions={this.actions}
|
|
|
|
active={this.props.active}
|
|
|
|
className={className}
|
|
|
|
onEscKeyDown={this.props.onEscKeyDown}
|
|
|
|
onOverlayClick={this.props.onOverlayClick}
|
|
|
|
type="custom"
|
|
|
|
>
|
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')}>
|
2015-11-16 20:37:29 +03:00
|
|
|
{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}
|
2016-05-28 23:04:00 +03:00
|
|
|
selectedDate={this.state.date}
|
|
|
|
theme={this.props.theme} />
|
2015-09-07 02:59:11 +03:00
|
|
|
</div>
|
2016-05-28 23:04:00 +03:00
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
}
|
2015-09-07 02:59:11 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
return CalendarDialog;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default factory;
|