2017-09-09 19:39:10 -06:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-11-17 04:08:37 -08:00
|
|
|
import DateTime from 'react-datetime';
|
2017-11-23 22:44:56 -08:00
|
|
|
import moment from 'moment';
|
2016-11-17 04:08:37 -08:00
|
|
|
|
2017-11-23 22:44:56 -08:00
|
|
|
function format(format, value) {
|
2017-11-26 11:30:53 -08:00
|
|
|
if (format) {
|
|
|
|
return moment(value).format(format || moment.defaultFormat);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toDate(format, value) {
|
|
|
|
if (format) {
|
|
|
|
return moment(value, format);
|
|
|
|
}
|
|
|
|
return value;
|
2017-11-23 22:44:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class DateTimeControl extends React.Component {
|
2016-12-01 16:28:33 -02:00
|
|
|
componentDidMount() {
|
2017-11-26 11:30:53 -08:00
|
|
|
const {value, field, onChange} = this.props;
|
|
|
|
if (!value) {
|
|
|
|
if (field.get('format')) {
|
|
|
|
onChange(format(field.get('format'), new Date()));
|
|
|
|
} else {
|
|
|
|
onChange(new Date());
|
|
|
|
}
|
2016-12-01 16:28:33 -02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 04:08:37 -08:00
|
|
|
handleChange = (datetime) => {
|
2017-11-23 22:44:56 -08:00
|
|
|
this.props.onChange(format(this.props.field.get('format'), datetime));
|
2016-11-17 04:08:37 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2017-11-23 22:44:56 -08:00
|
|
|
return (
|
|
|
|
<DateTime
|
|
|
|
timeFormat={this.props.includeTime || false}
|
2017-11-26 11:30:53 -08:00
|
|
|
value={toDate(this.props.field.get('format'), this.props.value)}
|
2017-11-23 22:44:56 -08:00
|
|
|
onChange={this.handleChange}
|
2016-11-17 04:08:37 -08:00
|
|
|
/>);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:44:56 -08:00
|
|
|
DateTimeControl.propTypes = {
|
2016-11-17 04:08:37 -08:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2017-11-23 22:44:56 -08:00
|
|
|
value: PropTypes.oneOfType([
|
|
|
|
PropTypes.object,
|
|
|
|
PropTypes.string,
|
|
|
|
]),
|
|
|
|
includeTime: PropTypes.bool,
|
|
|
|
field: PropTypes.object
|
2016-11-17 04:08:37 -08:00
|
|
|
};
|
2017-11-23 22:44:56 -08:00
|
|
|
|
|
|
|
|