2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-01 13:02:53 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
2015-06-05 11:06:36 +02:00
|
|
|
import DatePicker from 'react-datepicker/dist/react-datepicker';
|
2015-06-01 13:02:53 +02:00
|
|
|
|
|
|
|
let InputDate = React.createClass({
|
2015-06-09 16:10:38 +02:00
|
|
|
propTypes: {
|
|
|
|
submitted: React.PropTypes.bool,
|
2015-08-06 10:58:09 +02:00
|
|
|
placeholderText: React.PropTypes.string,
|
2015-08-12 13:53:17 +02:00
|
|
|
onChange: React.PropTypes.func,
|
2015-08-26 18:01:50 +02:00
|
|
|
defaultValue: React.PropTypes.object,
|
|
|
|
|
|
|
|
// DatePicker implements the disabled attribute
|
|
|
|
// https://github.com/Hacker0x01/react-datepicker/blob/master/src/datepicker.jsx#L30
|
|
|
|
disabled: React.PropTypes.bool
|
2015-06-09 16:10:38 +02:00
|
|
|
},
|
2015-06-01 13:02:53 +02:00
|
|
|
|
|
|
|
getInitialState() {
|
2015-12-18 15:00:37 +01:00
|
|
|
return this.getStateFromMoment(this.props.defaultValue);
|
2015-06-01 13:02:53 +02:00
|
|
|
},
|
2015-06-01 14:49:13 +02:00
|
|
|
|
2015-08-24 11:22:44 +02:00
|
|
|
// InputDate needs to support setting a defaultValue from outside.
|
|
|
|
// If this is the case, we need to call handleChange to propagate this
|
|
|
|
// to the outer Property
|
2015-08-12 13:53:17 +02:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if(!this.state.value && !this.state.value_moment && nextProps.defaultValue) {
|
2015-12-18 15:00:37 +01:00
|
|
|
this.handleChange(nextProps.defaultValue);
|
2015-08-12 13:53:17 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-12-18 15:00:37 +01:00
|
|
|
getStateFromMoment(date) {
|
|
|
|
const state = {};
|
|
|
|
|
|
|
|
if (date) {
|
|
|
|
state.value = date.format('YYYY-MM-DD');
|
|
|
|
state.value_moment = date;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
2015-06-01 14:49:13 +02:00
|
|
|
handleChange(date) {
|
2015-12-18 15:00:37 +01:00
|
|
|
const newState = this.getStateFromMoment(date);
|
|
|
|
|
|
|
|
this.setState(newState);
|
2015-07-15 14:48:51 +02:00
|
|
|
|
2015-12-18 15:00:37 +01:00
|
|
|
// Propagate change up by faking event
|
2015-07-15 14:48:51 +02:00
|
|
|
this.props.onChange({
|
|
|
|
target: {
|
2015-12-18 15:00:37 +01:00
|
|
|
value: newState.value
|
2015-07-15 14:48:51 +02:00
|
|
|
}
|
|
|
|
});
|
2015-06-01 13:02:53 +02:00
|
|
|
},
|
2015-06-01 14:49:13 +02:00
|
|
|
|
2015-09-10 13:24:32 +02:00
|
|
|
reset() {
|
|
|
|
this.setState(this.getInitialState());
|
|
|
|
},
|
|
|
|
|
2015-08-26 18:01:50 +02:00
|
|
|
render() {
|
2015-06-01 13:02:53 +02:00
|
|
|
return (
|
2015-07-29 14:49:36 +02:00
|
|
|
<div>
|
2015-06-02 18:43:37 +02:00
|
|
|
<DatePicker
|
2015-08-26 18:01:50 +02:00
|
|
|
disabled={this.props.disabled}
|
2015-06-02 18:43:37 +02:00
|
|
|
dateFormat="YYYY-MM-DD"
|
2015-07-15 14:48:51 +02:00
|
|
|
selected={this.state.value_moment}
|
2015-06-02 18:43:37 +02:00
|
|
|
onChange={this.handleChange}
|
|
|
|
placeholderText={this.props.placeholderText}/>
|
|
|
|
</div>
|
2015-06-01 13:02:53 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-01 14:49:13 +02:00
|
|
|
export default InputDate;
|