Keep old behevior if no format attribute is set on date widgets

This commit is contained in:
Mathias Biilmann Christensen
2017-11-26 11:30:53 -08:00
committed by Shawn Erquhart
parent 704f1144ed
commit 688332410c

View File

@ -4,13 +4,28 @@ import DateTime from 'react-datetime';
import moment from 'moment'; import moment from 'moment';
function format(format, value) { function format(format, value) {
return moment(value).format(format || moment.defaultFormat); if (format) {
return moment(value).format(format || moment.defaultFormat);
}
return value;
}
function toDate(format, value) {
if (format) {
return moment(value, format);
}
return value;
} }
export default class DateTimeControl extends React.Component { export default class DateTimeControl extends React.Component {
componentDidMount() { componentDidMount() {
if (!this.props.value) { const {value, field, onChange} = this.props;
this.props.onChange(format(this.props.field.get('format'), new Date())); if (!value) {
if (field.get('format')) {
onChange(format(field.get('format'), new Date()));
} else {
onChange(new Date());
}
} }
} }
@ -22,7 +37,7 @@ export default class DateTimeControl extends React.Component {
return ( return (
<DateTime <DateTime
timeFormat={this.props.includeTime || false} timeFormat={this.props.includeTime || false}
value={moment(this.props.value, this.props.field.get('format'))} value={toDate(this.props.field.get('format'), this.props.value)}
onChange={this.handleChange} onChange={this.handleChange}
/>); />);
} }