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

Datetime format #842

Merged
merged 6 commits into from
Nov 28, 2017
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
11 changes: 11 additions & 0 deletions docs/widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,14 @@ collections:
searchFields: [name, twitterHandle]
valueField: name
```

### Date Widget / DateTime Widget

The date and datetime widgets provide date or datetime pickers when the widget is active. The resulting date string can be formatted (uses Moment.js), and accepts a default value. The initial value will be the current date unless `false` or an empty string are provided as the default value.

The following field configuration properties are specific to fields using the date or datetime widget:

Property | Accepted Values | Description
--- | --- | ---
`format` | string | format string uses Moment.js [tokens](https://momentjs.com/docs/#/parsing/string-format/)
`default` | boolean, string | can be a date string, or else an empty string - defaults to current date
32 changes: 25 additions & 7 deletions src/components/Widgets/DateControl.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
import PropTypes from 'prop-types';
import React from 'react';
import DateTime from 'react-datetime';
import moment from 'moment';

export default class DateControl extends React.Component {
componentDidMount() {
if (!this.props.value) {
this.props.onChange(new Date());
const { value, field, onChange } = this.props;
this.format = field.get('format');

/**
* Set the current date as default value if no default value is provided. An
* empty string means the value is intentionally blank.
*/
if (!value && value !== '') {
this.handleChange(new Date());
}
}

handleChange = (datetime) => {
this.props.onChange(datetime);
handleChange = datetime => {
const newValue = this.format
? moment(datetime).format(this.format)
: datetime;
this.props.onChange(newValue);
};

render() {
const { includeTime, value } = this.props;
const format = this.format || moment.defaultFormat;
return (<DateTime
timeFormat={false}
value={this.props.value}
timeFormat={!!includeTime}
value={moment(value, format)}
onChange={this.handleChange}
/>);
}
}

DateControl.propTypes = {
onChange: PropTypes.func.isRequired,
value: PropTypes.object, // eslint-disable-line
value: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
]),
includeTime: PropTypes.bool,
field: PropTypes.object,
};
24 changes: 3 additions & 21 deletions src/components/Widgets/DateTimeControl.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
import PropTypes from 'prop-types';
import React from 'react';
import DateTime from 'react-datetime';
import DateControl from './DateControl';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateControl.js as written exports DateTimeControl, not DateControl. (This is the cause of the PR being currently broken).

Copy link
Contributor

@tech4him1 tech4him1 Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Benaiah That's weird, a default export/import shouldn't break things if it's named differently. Am I mistaken there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tech4him1 it does seem like a default export should work regardless, but changing the name in DateControl.js fixed the broken CMS. (note: this is now fixed, even though this line hasn't actually changed).


export default class DateTimeControl extends React.Component {
componentDidMount() {
if (!this.props.value) {
this.props.onChange(new Date());
}
}

handleChange = (datetime) => {
this.props.onChange(datetime);
};

render() {
return <DateTime value={this.props.value} onChange={this.handleChange} />;
const { field, format, onChange, value } = this.props;
return <DateControl onChange={onChange} format={format} value={value} field={field} includeTime />;
}
}

DateTimeControl.propTypes = {
onChange: PropTypes.func.isRequired,
value: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
]),
};