@@ -17,9 +17,9 @@ DateTimeField = React.createClass({
17
17
} ,
18
18
getDefaultProps : function ( ) {
19
19
return {
20
- dateTime : "1234567" ,
21
- format : 'X ' ,
22
- inputFormat : "MM/DD/YY H :mm A" ,
20
+ dateTime : moment ( ) . format ( 'x' ) ,
21
+ format : 'x ' ,
22
+ inputFormat : "MM/DD/YY h :mm A" ,
23
23
showToday : true ,
24
24
viewMode : 'days' ,
25
25
daysOfWeekDisabled : [ ] ,
@@ -38,31 +38,35 @@ DateTimeField = React.createClass({
38
38
left : - 9999 ,
39
39
zIndex : '9999 !important'
40
40
} ,
41
- viewDate : moment ( this . props . dateTime , this . props . format ) . startOf ( "month" ) ,
42
- selectedDate : moment ( this . props . dateTime , this . props . format ) ,
43
- inputValue : moment ( this . props . dateTime , this . props . format ) . format ( this . props . inputFormat )
41
+ viewDate : moment ( this . props . dateTime , this . props . format , true ) . startOf ( "month" ) ,
42
+ selectedDate : moment ( this . props . dateTime , this . props . format , true ) ,
43
+ inputValue : moment ( this . props . dateTime , this . props . format , true ) . format ( this . props . inputFormat )
44
44
} ;
45
45
} ,
46
46
componentWillReceiveProps : function ( nextProps ) {
47
- return this . setState ( {
48
- viewDate : moment ( nextProps . dateTime , nextProps . format ) . startOf ( "month" ) ,
49
- selectedDate : moment ( nextProps . dateTime , nextProps . format ) ,
50
- inputValue : moment ( nextProps . dateTime , nextProps . format ) . format ( nextProps . inputFormat )
51
- } ) ;
47
+ if ( moment ( nextProps . dateTime , nextProps . format , true ) . isValid ( ) ) {
48
+ return this . setState ( {
49
+ viewDate : moment ( nextProps . dateTime , nextProps . format , true ) . startOf ( "month" ) ,
50
+ selectedDate : moment ( nextProps . dateTime , nextProps . format , true ) ,
51
+ inputValue : moment ( nextProps . dateTime , nextProps . format , true ) . format ( nextProps . inputFormat )
52
+ } ) ;
53
+ }
52
54
} ,
53
55
onChange : function ( event ) {
54
- if ( moment ( event . target . value , this . props . format ) . isValid ( ) ) {
56
+ var value = event . target == null ? event : event . target . value ;
57
+ if ( moment ( value , this . props . inputFormat , true ) . isValid ( ) ) {
55
58
this . setState ( {
56
- selectedDate : moment ( event . target . value , this . props . format ) ,
57
- inputValue : moment ( event . target . value , this . props . format ) . format ( this . props . inputFormat )
59
+ selectedDate : moment ( value , this . props . inputFormat , true ) ,
60
+ viewDate : moment ( value , this . props . inputFormat , true ) . startOf ( "month" )
58
61
} ) ;
59
- } else {
60
- this . setState ( {
61
- inputValue : event . target . value
62
- } ) ;
63
- console . log ( "This is not a valid date" ) ;
64
62
}
65
- return this . props . onChange ( this . state . selectedDate . format ( this . props . format ) ) ;
63
+
64
+ return this . setState ( {
65
+ inputValue : value
66
+ } , function ( ) {
67
+ return this . props . onChange ( moment ( this . state . inputValue , this . props . inputFormat , true ) . format ( this . props . format ) ) ;
68
+ } ) ;
69
+
66
70
} ,
67
71
setSelectedDate : function ( e ) {
68
72
return this . setState ( {
@@ -167,13 +171,9 @@ DateTimeField = React.createClass({
167
171
} ,
168
172
togglePeriod : function ( ) {
169
173
if ( this . state . selectedDate . hour ( ) > 12 ) {
170
- return this . setState ( {
171
- selectedDate : this . state . selectedDate . clone ( ) . subtract ( 12 , 'hours' )
172
- } ) ;
174
+ return this . onChange ( this . state . selectedDate . clone ( ) . subtract ( 12 , 'hours' ) . format ( this . props . inputFormat ) ) ;
173
175
} else {
174
- return this . setState ( {
175
- selectedDate : this . state . selectedDate . clone ( ) . add ( 12 , 'hours' )
176
- } ) ;
176
+ return this . onChange ( this . state . selectedDate . clone ( ) . add ( 12 , 'hours' ) . format ( this . props . inputFormat ) ) ;
177
177
}
178
178
} ,
179
179
togglePicker : function ( ) {
@@ -285,7 +285,7 @@ DateTimeField = React.createClass({
285
285
togglePeriod = { this . togglePeriod }
286
286
/>
287
287
< div className = "input-group date" ref = "datetimepicker" >
288
- < input type = "text" className = "form-control" onChange = { this . onChange } value = { this . state . selectedDate . format ( this . props . inputFormat ) } />
288
+ < input type = "text" className = "form-control" onChange = { this . onChange } value = { this . state . inputValue } />
289
289
< span className = "input-group-addon" onClick = { this . onClick } onBlur = { this . onBlur } ref = "dtpbutton" > < Glyphicon glyph = "calendar" /> </ span >
290
290
</ div >
291
291
</ div >
0 commit comments