diff --git a/CHANGELOG.md b/CHANGELOG.md index 30eef1dfab87..7c27dfccdd8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Changes that have landed in master but are not yet released. Click to see more. + + * 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.6.0 (April 19, 2018) ([demo](https://5ad8e1ebb31274466632d026--cms-demo.netlify.com/#/)) diff --git a/src/components/EditorWidgets/Date/DateControl.js b/src/components/EditorWidgets/Date/DateControl.js index 63d5d4365dc8..cd5c0f93ab57 100644 --- a/src/components/EditorWidgets/Date/DateControl.js +++ b/src/components/EditorWidgets/Date/DateControl.js @@ -20,7 +20,8 @@ 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); + formatOutput = this.props.field.get('format'); + formatDisplay = this.formatOutput || (this.props.includeTime ? DEFAULT_DATETIME_FORMAT : DEFAULT_DATE_FORMAT); componentDidMount() { const { value } = this.props; @@ -41,22 +42,33 @@ 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)) { - const formattedValue = moment(datetime).format(this.format); + /** + * 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.formatOutput) { + const formattedValue = moment(datetime).format(this.formatOutput); onChange(formattedValue); + } else { + onChange(datetime); } }; 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,7 +79,7 @@ export default class DateControl extends React.Component { render() { const { includeTime, value, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props; - const format = this.format; + const format = this.formatDisplay; return (