diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e4758b15466..b57b2b3fd01e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ Click to see more. + ## v1 + + --- + + ## v2 + * (possibly breaking): return date object from date/datetime widgets if no format set ([@erquhart](https://github.com/erquhart) in [#1296](https://github.com/netlify/netlify-cms/pull/1296)) ## 1.8.2 (May 24, 2018) diff --git a/src/components/EditorWidgets/Date/DateControl.js b/src/components/EditorWidgets/Date/DateControl.js index 63d5d4365dc8..6b758bf6ff91 100644 --- a/src/components/EditorWidgets/Date/DateControl.js +++ b/src/components/EditorWidgets/Date/DateControl.js @@ -20,7 +20,7 @@ export default class DateControl extends React.Component { includeTime: PropTypes.bool, }; - format = this.props.field.get('format') || (this.props.includeTime ? DEFAULT_DATETIME_FORMAT : DEFAULT_DATE_FORMAT); + format = this.props.field.get('format'); componentDidMount() { const { value } = this.props; @@ -41,22 +41,34 @@ export default class DateControl extends React.Component { handleChange = datetime => { const { onChange } = this.props; - // Set the date only if the format is valid - if (this.isValidDate(datetime)) { + /** + * Set the date only if it is valid. + */ + if (!this.isValidDate(datetime)) { + return; + } + + /** + * Produce a formatted string only if a format is set in the config. + * Otherwise produce a date object. + */ + if (this.format) { const formattedValue = moment(datetime).format(this.format); onChange(formattedValue); + } else { + const value = moment.isMoment(datetime) ? datetime.toDate() : datetime; + onChange(value); } }; onBlur = datetime => { - const { setInactiveStyle, onChange } = this.props; + const { setInactiveStyle } = this.props; if (!this.isValidDate(datetime)) { const parsedDate = moment(datetime); if (parsedDate.isValid()) { - const formattedValue = parsedDate.format(this.format); - onChange(formattedValue); + this.handleChange(datetime); } else { window.alert('The date you entered is invalid.'); } @@ -67,11 +79,10 @@ export default class DateControl extends React.Component { render() { const { includeTime, value, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props; - const format = this.format; return (