@@ -17,9 +17,9 @@ DateTimeField = React.createClass({
1717 } ,
1818 getDefaultProps : function ( ) {
1919 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" ,
2323 showToday : true ,
2424 viewMode : 'days' ,
2525 daysOfWeekDisabled : [ ] ,
@@ -38,31 +38,35 @@ DateTimeField = React.createClass({
3838 left : - 9999 ,
3939 zIndex : '9999 !important'
4040 } ,
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 )
4444 } ;
4545 } ,
4646 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+ }
5254 } ,
5355 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 ( ) ) {
5558 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" )
5861 } ) ;
59- } else {
60- this . setState ( {
61- inputValue : event . target . value
62- } ) ;
63- console . log ( "This is not a valid date" ) ;
6462 }
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+
6670 } ,
6771 setSelectedDate : function ( e ) {
6872 return this . setState ( {
@@ -167,13 +171,9 @@ DateTimeField = React.createClass({
167171 } ,
168172 togglePeriod : function ( ) {
169173 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 ) ) ;
173175 } 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 ) ) ;
177177 }
178178 } ,
179179 togglePicker : function ( ) {
@@ -285,7 +285,7 @@ DateTimeField = React.createClass({
285285 togglePeriod = { this . togglePeriod }
286286 />
287287 < 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 } />
289289 < span className = "input-group-addon" onClick = { this . onClick } onBlur = { this . onBlur } ref = "dtpbutton" > < Glyphicon glyph = "calendar" /> </ span >
290290 </ div >
291291 </ div >
0 commit comments