2016-04-18 10:03:53 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-07-07 12:48:35 +02:00
|
|
|
import DateTimePicker from 'react-datetime';
|
2016-11-26 02:46:55 +08:00
|
|
|
// import moment from 'moment';
|
2016-04-18 10:03:53 +09:00
|
|
|
|
|
|
|
class DateTime extends Component {
|
2017-01-10 11:17:16 +01:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.updateDate = this.updateDate.bind(this);
|
|
|
|
}
|
2017-01-11 18:02:12 +01:00
|
|
|
|
2017-01-10 11:17:16 +01:00
|
|
|
// when the datetime picker has mounted, SmartForm will catch the date value (no formsy mixin in this component)
|
|
|
|
componentDidMount() {
|
|
|
|
this.updateDate(this.props.value || new Date());
|
|
|
|
}
|
2017-01-11 18:02:12 +01:00
|
|
|
|
2017-01-10 11:17:16 +01:00
|
|
|
updateDate(date) {
|
2017-01-23 15:50:55 +01:00
|
|
|
this.context.updateCurrentValues({[this.props.name]: date});
|
2016-07-07 12:48:35 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 10:03:53 +09:00
|
|
|
render() {
|
2017-02-21 19:08:14 +01:00
|
|
|
|
|
|
|
const date = typeof this.props.value === 'string' ? new Date(this.props.value) : this.props.value;
|
|
|
|
|
2016-04-18 10:03:53 +09:00
|
|
|
return (
|
|
|
|
<div className="form-group row">
|
|
|
|
<label className="control-label col-sm-3">{this.props.label}</label>
|
2016-07-06 15:53:14 +02:00
|
|
|
<div className="col-sm-9">
|
2016-11-26 02:46:55 +08:00
|
|
|
<DateTimePicker
|
2017-02-21 19:08:14 +01:00
|
|
|
value={date || new Date()}
|
2016-07-07 12:48:35 +02:00
|
|
|
// newDate argument is a Moment object given by react-datetime
|
2017-01-10 11:17:16 +01:00
|
|
|
onChange={newDate => this.updateDate(newDate._d)}
|
2016-11-26 02:46:55 +08:00
|
|
|
format={"x"}
|
2016-07-06 15:53:14 +02:00
|
|
|
inputProps={{name: this.props.name}}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-04-18 10:03:53 +09:00
|
|
|
</div>
|
2016-07-06 15:53:14 +02:00
|
|
|
);
|
2016-04-18 10:03:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DateTime.propTypes = {
|
2016-07-06 15:53:14 +02:00
|
|
|
control: React.PropTypes.any,
|
|
|
|
datatype: React.PropTypes.any,
|
|
|
|
group: React.PropTypes.any,
|
2016-04-18 10:03:53 +09:00
|
|
|
label: React.PropTypes.string,
|
2016-07-06 15:53:14 +02:00
|
|
|
name: React.PropTypes.string,
|
|
|
|
value: React.PropTypes.any,
|
2016-09-07 14:35:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
DateTime.contextTypes = {
|
|
|
|
addToAutofilledValues: React.PropTypes.func,
|
2017-01-23 15:50:55 +01:00
|
|
|
updateCurrentValues: React.PropTypes.func,
|
2016-09-07 14:35:49 +02:00
|
|
|
};
|
2016-04-18 10:03:53 +09:00
|
|
|
|
2016-09-27 07:30:58 +02:00
|
|
|
export default DateTime;
|