-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Datetime format #842
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa396c6
Make date and datetime widget respect format attribute
biilmann 257ad3d
Keep old behevior if no format attribute is set on date widgets
biilmann 47ab2f5
Fixes for date formatting PR
Benaiah 54a617c
Coerce includeTime to a boolean
Benaiah c87660d
simplify date time initial value set
erquhart 4c35b66
allow empty default date value
erquhart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
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, | ||
]), | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DateControl.js
as written exportsDateTimeControl
, notDateControl
. (This is the cause of the PR being currently broken).There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch
There was a problem hiding this comment.
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).