-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateInput.js
More file actions
68 lines (57 loc) · 1.73 KB
/
DateInput.js
File metadata and controls
68 lines (57 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var moment = require('moment');
var Pikaday = require('pikaday');
var React = require('react');
module.exports = React.createClass({
displayName: 'DateInput',
// set default properties
getDefaultProps: function() {
return {
format: 'YYYY-MM-DD'
};
},
getInitialState: function() {
return {
value: this.props.value,
id: Math.round(Math.random() * 100000)
};
},
componentWillReceiveProps: function(newProps) {
if (newProps.value === this.state.value) return;
this.setState({
value: newProps.value
});
this.picker.setMoment(moment(newProps.value, this.props.format));
},
componentDidMount: function() {
this.picker = new Pikaday({
field: this.getDOMNode(),
format: this.props.format,
yearRange: this.props.yearRange,
onSelect: (date) => { // eslint-disable-line no-unused-vars
if (this.props.onChange && this.picker.toString() !== this.props.value) {
this.props.onChange(this.picker.toString());
}
}
});
},
componentWillUnmount: function() {
this.picker.destroy();
},
handleChange: function(e) {
if (e.target.value === this.state.value) return;
this.setState({ value: e.target.value });
},
handleBlur: function(e) { // eslint-disable-line no-unused-vars
if (this.state.value === this.props.value) return;
var newValue = moment(this.state.value, this.props.format);
if (newValue.isValid()) {
this.picker.setMoment(newValue);
} else {
this.picker.setDate(null);
if (this.props.onChange) this.props.onChange('');
}
},
render: function() {
return <input type="text" name={this.props.name} value={this.state.value} placeholder={this.props.placeholder} onChange={this.handleChange} onBlur={this.handleBlur} autoComplete="off" className="form-control" />;
}
});