Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return date object from date/datetime widgets if no format set #1296

Merged
merged 2 commits into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
Click to see more.
</summary>

## 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))
</details>

## 1.8.2 (May 24, 2018)
Expand Down
27 changes: 19 additions & 8 deletions src/components/EditorWidgets/Date/DateControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.');
}
Expand All @@ -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 (
<DateTime
timeFormat={!!includeTime}
value={moment(value, format)}
value={moment(value, this.format)}
onChange={this.handleChange}
onFocus={setActiveStyle}
onBlur={this.onBlur}
Expand Down
2 changes: 1 addition & 1 deletion website/site/content/docs/widgets/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The date widget translates a date picker input to a date string. For saving date
- **Data type:** Moment.js-formatted date string
- **Options:**
- `default`: accepts a date string, or an empty string to accept blank input; otherwise defaults to current date
- `format`: accepts Moment.js [tokens](https://momentjs.com/docs/#/parsing/string-format/); defaults to ISO8601 format `YYYY-MM-DD`
- `format`: optional; accepts Moment.js [tokens](https://momentjs.com/docs/#/parsing/string-format/); defaults to raw Date object (if supported by output format)
- **Example:**

```yaml
Expand Down
2 changes: 1 addition & 1 deletion website/site/content/docs/widgets/datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The datetime widget translates a datetime picker to a datetime string. For savin
- **Data type:** Moment.js-formatted datetime string
- **Options:**
- `default`: accepts a datetime string, or an empty string to accept blank input; otherwise defaults to current datetime
- `format`: accepts Moment.js [tokens](https://momentjs.com/docs/#/parsing/string-format/); defaults to ISO8601 format `YYYY-MM-DDTHH:mm:ssZ`
- `format`: optional; accepts Moment.js [tokens](https://momentjs.com/docs/#/parsing/string-format/); defaults to raw Date object (if supported by output format)
- **Example:**

```yaml
Expand Down