Fixes for date formatting PR
This commit is contained in:
parent
688332410c
commit
d6c0505c49
@ -3,54 +3,44 @@ import React from 'react';
|
||||
import DateTime from 'react-datetime';
|
||||
import moment from 'moment';
|
||||
|
||||
function format(format, value) {
|
||||
if (format) {
|
||||
return moment(value).format(format || moment.defaultFormat);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function toDate(format, value) {
|
||||
if (format) {
|
||||
return moment(value, format);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export default class DateTimeControl extends React.Component {
|
||||
export default class DateControl extends React.Component {
|
||||
componentDidMount() {
|
||||
const {value, field, onChange} = this.props;
|
||||
const { value, field, onChange } = this.props;
|
||||
if (!value) {
|
||||
if (field.get('format')) {
|
||||
onChange(format(field.get('format'), new Date()));
|
||||
} else {
|
||||
onChange(new Date());
|
||||
}
|
||||
const format = field.get('format');
|
||||
const newValue = format
|
||||
? moment(new Date()).format(format)
|
||||
: new Date();
|
||||
onChange(newValue);
|
||||
}
|
||||
}
|
||||
|
||||
handleChange = (datetime) => {
|
||||
this.props.onChange(format(this.props.field.get('format'), datetime));
|
||||
const { onChange, field } = this.props;
|
||||
const format = field.get('format');
|
||||
const newValue = format
|
||||
? moment(datetime).format(format)
|
||||
: datetime;
|
||||
onChange(newValue);
|
||||
};
|
||||
|
||||
render() {
|
||||
return (
|
||||
<DateTime
|
||||
timeFormat={this.props.includeTime || false}
|
||||
value={toDate(this.props.field.get('format'), this.props.value)}
|
||||
onChange={this.handleChange}
|
||||
const { field, includeTime, value } = this.props;
|
||||
const format = field.get('format', moment.defaultFormat);
|
||||
return (<DateTime
|
||||
timeFormat={includeTime}
|
||||
value={moment(value, format)}
|
||||
onChange={this.handleChange}
|
||||
/>);
|
||||
}
|
||||
}
|
||||
|
||||
DateTimeControl.propTypes = {
|
||||
DateControl.propTypes = {
|
||||
onChange: PropTypes.func.isRequired,
|
||||
value: PropTypes.oneOfType([
|
||||
PropTypes.object,
|
||||
PropTypes.string,
|
||||
]),
|
||||
includeTime: PropTypes.bool,
|
||||
field: PropTypes.object
|
||||
field: PropTypes.object,
|
||||
};
|
||||
|
||||
|
||||
|
@ -3,7 +3,7 @@ import DateControl from './DateControl';
|
||||
|
||||
export default class DateTimeControl extends React.Component {
|
||||
render() {
|
||||
const {onChange, format, value, field} = this.props;
|
||||
return <DateControl onChange={onChange} format={format} value={value} field={field} includeTime={true}/>;
|
||||
const { field, format, onChange, value } = this.props;
|
||||
return <DateControl onChange={onChange} format={format} value={value} field={field} includeTime />;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user