simplify date time initial value set

This commit is contained in:
Shawn Erquhart 2017-11-27 21:13:02 -05:00
parent b5b7fab2a7
commit 1a7c999f12

View File

@ -6,27 +6,22 @@ import moment from 'moment';
export default class DateControl extends React.Component { export default class DateControl extends React.Component {
componentDidMount() { componentDidMount() {
const { value, field, onChange } = this.props; const { value, field, onChange } = this.props;
this.format = field.get('format');
if (!value) { if (!value) {
const format = field.get('format'); this.handleChange(new Date());
const newValue = format
? moment(new Date()).format(format)
: new Date();
onChange(newValue);
} }
} }
handleChange = (datetime) => { handleChange = datetime => {
const { onChange, field } = this.props; const newValue = this.format
const format = field.get('format'); ? moment(datetime).format(this.format)
const newValue = format
? moment(datetime).format(format)
: datetime; : datetime;
onChange(newValue); onChange(newValue);
}; };
render() { render() {
const { field, includeTime, value } = this.props; const { includeTime, value } = this.props;
const format = field.get('format', moment.defaultFormat); const format = this.format || moment.defaultFormat;
return (<DateTime return (<DateTime
timeFormat={!!includeTime} timeFormat={!!includeTime}
value={moment(value, format)} value={moment(value, format)}