@@ -8,34 +8,39 @@ moment = require('moment');
8
8
9
9
Glyphicon = require ( 'react-bootstrap' ) . Glyphicon ;
10
10
11
+ Constants = require ( './Constants' ) ;
12
+
11
13
DateTimeField = React . createClass ( {
12
14
propTypes : {
13
15
dateTime : React . PropTypes . string ,
14
16
onChange : React . PropTypes . func ,
15
17
format : React . PropTypes . string ,
16
- inputFormat : React . PropTypes . string ,
17
18
inputProps : React . PropTypes . object ,
19
+ inputFormat : React . PropTypes . string ,
18
20
defaultText : React . PropTypes . string ,
21
+ mode : React . PropTypes . oneOf ( [ Constants . MODE_DATE , Constants . MODE_DATETIME , Constants . MODE_TIME ] ) ,
19
22
minDate : React . PropTypes . object ,
20
23
maxDate : React . PropTypes . object
21
24
} ,
22
25
getDefaultProps : function ( ) {
23
26
return {
24
27
dateTime : moment ( ) . format ( 'x' ) ,
25
28
format : 'x' ,
26
- inputFormat : "MM/DD/YY h:mm A" ,
27
29
showToday : true ,
28
30
viewMode : 'days' ,
29
31
daysOfWeekDisabled : [ ] ,
32
+ mode : Constants . MODE_DATETIME ,
30
33
onChange : function ( x ) {
31
34
console . log ( x ) ;
32
35
}
33
36
} ;
34
37
} ,
35
38
getInitialState : function ( ) {
36
39
return {
37
- showDatePicker : true ,
38
- showTimePicker : false ,
40
+ showDatePicker : this . props . mode !== Constants . MODE_TIME ,
41
+ showTimePicker : this . props . mode === Constants . MODE_TIME ,
42
+ inputFormat : this . resolvePropsInputFormat ( ) ,
43
+ buttonIcon : this . props . mode === Constants . MODE_TIME ? "time" : "calendar" ,
39
44
widgetStyle : {
40
45
display : 'block' ,
41
46
position : 'absolute' ,
@@ -44,7 +49,7 @@ DateTimeField = React.createClass({
44
49
} ,
45
50
viewDate : moment ( this . props . dateTime , this . props . format , true ) . startOf ( "month" ) ,
46
51
selectedDate : moment ( this . props . dateTime , this . props . format , true ) ,
47
- inputValue : typeof this . props . defaultText != 'undefined' ? this . props . defaultText : moment ( this . props . dateTime , this . props . format , true ) . format ( this . props . inputFormat )
52
+ inputValue : typeof this . props . defaultText != 'undefined' ? this . props . defaultText : moment ( this . props . dateTime , this . props . format , true ) . format ( this . resolvePropsInputFormat ( ) )
48
53
} ;
49
54
} ,
50
55
componentWillReceiveProps : function ( nextProps ) {
@@ -55,32 +60,53 @@ DateTimeField = React.createClass({
55
60
inputValue : moment ( nextProps . dateTime , nextProps . format , true ) . format ( nextProps . inputFormat )
56
61
} ) ;
57
62
}
63
+ if ( nextProps . inputFormat !== this . props . inputFormat ) {
64
+ return this . setState ( {
65
+ inputFormat : nextProps . inputFormat
66
+ } ) ;
67
+ }
68
+ } ,
69
+ resolvePropsInputFormat : function ( ) {
70
+ if ( this . props . inputFormat ) return this . props . inputFormat ;
71
+ switch ( this . props . mode ) {
72
+ case Constants . MODE_TIME :
73
+ return "h:mm A" ;
74
+ case Constants . MODE_DATE :
75
+ return "MM/DD/YY" ;
76
+ default :
77
+ return "MM/DD/YY h:mm A" ;
78
+ }
58
79
} ,
59
80
onChange : function ( event ) {
60
81
var value = event . target == null ? event : event . target . value ;
61
- if ( moment ( value , this . props . inputFormat , true ) . isValid ( ) ) {
82
+ if ( moment ( value , this . state . inputFormat , true ) . isValid ( ) ) {
62
83
this . setState ( {
63
- selectedDate : moment ( value , this . props . inputFormat , true ) ,
64
- viewDate : moment ( value , this . props . inputFormat , true ) . startOf ( "month" )
84
+ selectedDate : moment ( value , this . state . inputFormat , true ) ,
85
+ viewDate : moment ( value , this . state . inputFormat , true ) . startOf ( "month" )
65
86
} ) ;
66
87
}
67
88
68
89
return this . setState ( {
69
90
inputValue : value
70
91
} , function ( ) {
71
- return this . props . onChange ( moment ( this . state . inputValue , this . props . inputFormat , true ) . format ( this . props . format ) ) ;
92
+ return this . props . onChange ( moment ( this . state . inputValue , this . state . inputFormat , true ) . format ( this . props . format ) ) ;
72
93
} ) ;
73
94
74
95
} ,
75
96
setSelectedDate : function ( e ) {
76
- if ( e . target . className && ! e . target . className . match ( / d i s a b l e d / g) ) {
97
+ var target = e . target ;
98
+ if ( target . className && ! target . className . match ( / d i s a b l e d / g) ) {
99
+ var month ;
100
+ if ( target . className . includes ( "new" ) ) month = this . state . viewDate . month ( ) + 1 ;
101
+ else if ( target . className . includes ( "old" ) ) month = this . state . viewDate . month ( ) - 1 ;
102
+ else month = this . state . viewDate . month ( ) ;
77
103
return this . setState ( {
78
- selectedDate : this . state . viewDate . clone ( ) . date ( parseInt ( e . target . innerHTML ) ) . hour ( this . state . selectedDate . hours ( ) ) . minute ( this . state . selectedDate . minutes ( ) )
79
- } , function ( ) {
104
+ selectedDate : this . state . viewDate . clone ( ) . month ( month ) . date ( parseInt ( e . target . innerHTML ) ) . hour ( this . state . selectedDate . hours ( ) ) . minute ( this . state . selectedDate . minutes ( ) )
105
+ } , function ( ) {
80
106
this . closePicker ( ) ;
81
107
this . props . onChange ( this . state . selectedDate . format ( this . props . format ) ) ;
82
108
return this . setState ( {
83
- inputValue : this . state . selectedDate . format ( this . props . inputFormat )
109
+ inputValue : this . state . selectedDate . format ( this . state . inputFormat )
84
110
} ) ;
85
111
} ) ;
86
112
}
@@ -92,7 +118,7 @@ DateTimeField = React.createClass({
92
118
this . closePicker ( ) ;
93
119
this . props . onChange ( this . state . selectedDate . format ( this . props . format ) ) ;
94
120
return this . setState ( {
95
- inputValue : this . state . selectedDate . format ( this . props . inputFormat )
121
+ inputValue : this . state . selectedDate . format ( this . state . inputFormat )
96
122
} ) ;
97
123
} ) ;
98
124
} ,
@@ -103,7 +129,7 @@ DateTimeField = React.createClass({
103
129
this . closePicker ( ) ;
104
130
this . props . onChange ( this . state . selectedDate . format ( this . props . format ) ) ;
105
131
return this . setState ( {
106
- inputValue : this . state . selectedDate . format ( this . props . inputFormat )
132
+ inputValue : this . state . selectedDate . format ( this . state . inputFormat )
107
133
} ) ;
108
134
} ) ;
109
135
} ,
@@ -123,7 +149,7 @@ DateTimeField = React.createClass({
123
149
} , function ( ) {
124
150
this . props . onChange ( this . state . selectedDate . format ( this . props . format ) ) ;
125
151
return this . setState ( {
126
- inputValue : this . state . selectedDate . format ( this . props . inputFormat )
152
+ inputValue : this . state . selectedDate . format ( this . resolvePropsInputFormat ( ) )
127
153
} ) ;
128
154
} ) ;
129
155
} ,
@@ -133,7 +159,7 @@ DateTimeField = React.createClass({
133
159
} , function ( ) {
134
160
this . props . onChange ( this . state . selectedDate . format ( this . props . format ) ) ;
135
161
return this . setState ( {
136
- inputValue : this . state . selectedDate . format ( this . props . inputFormat )
162
+ inputValue : this . state . selectedDate . format ( this . resolvePropsInputFormat ( ) )
137
163
} ) ;
138
164
} ) ;
139
165
} ,
@@ -158,7 +184,7 @@ DateTimeField = React.createClass({
158
184
} , function ( ) {
159
185
this . props . onChange ( this . state . selectedDate . format ( this . props . format ) ) ;
160
186
return this . setState ( {
161
- inputValue : this . state . selectedDate . format ( this . props . inputFormat )
187
+ inputValue : this . state . selectedDate . format ( this . resolvePropsInputFormat ( ) )
162
188
} ) ;
163
189
} ) ;
164
190
} ,
@@ -168,7 +194,7 @@ DateTimeField = React.createClass({
168
194
} , function ( ) {
169
195
this . props . onChange ( this . state . selectedDate . format ( this . props . format ) ) ;
170
196
return this . setState ( {
171
- inputValue : this . state . selectedDate . format ( this . props . inputFormat )
197
+ inputValue : this . state . selectedDate . format ( this . resolvePropsInputFormat ( ) )
172
198
} ) ;
173
199
} ) ;
174
200
} ,
@@ -189,9 +215,9 @@ DateTimeField = React.createClass({
189
215
} ,
190
216
togglePeriod : function ( ) {
191
217
if ( this . state . selectedDate . hour ( ) > 12 ) {
192
- return this . onChange ( this . state . selectedDate . clone ( ) . subtract ( 12 , 'hours' ) . format ( this . props . inputFormat ) ) ;
218
+ return this . onChange ( this . state . selectedDate . clone ( ) . subtract ( 12 , 'hours' ) . format ( this . state . inputFormat ) ) ;
193
219
} else {
194
- return this . onChange ( this . state . selectedDate . clone ( ) . add ( 12 , 'hours' ) . format ( this . props . inputFormat ) ) ;
220
+ return this . onChange ( this . state . selectedDate . clone ( ) . add ( 12 , 'hours' ) . format ( this . state . inputFormat ) ) ;
195
221
}
196
222
} ,
197
223
togglePicker : function ( ) {
@@ -284,6 +310,7 @@ DateTimeField = React.createClass({
284
310
showToday = { this . props . showToday }
285
311
viewMode = { this . props . viewMode }
286
312
daysOfWeekDisabled = { this . props . daysOfWeekDisabled }
313
+ mode = { this . props . mode }
287
314
minDate = { this . props . minDate }
288
315
maxDate = { this . props . maxDate }
289
316
addDecade = { this . addDecade }
@@ -306,7 +333,7 @@ DateTimeField = React.createClass({
306
333
/>
307
334
< div className = "input-group date" ref = "datetimepicker" >
308
335
< input type = "text" className = "form-control" onChange = { this . onChange } value = { this . state . inputValue } { ...this . props . inputProps } />
309
- < span className = "input-group-addon" onClick = { this . onClick } onBlur = { this . onBlur } ref = "dtpbutton" > < Glyphicon glyph = "calendar" /> </ span >
336
+ < span className = "input-group-addon" onClick = { this . onClick } onBlur = { this . onBlur } ref = "dtpbutton" > < Glyphicon glyph = { this . state . buttonIcon } /> </ span >
310
337
</ div >
311
338
</ div >
312
339
) ;
0 commit comments