1
1
'use strict' ;
2
2
3
- var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ; // See http://jszen.blogspot.com/2007/03/how-to-build-simple-calendar-with.html for calendar logic.
4
-
5
3
Object . defineProperty ( exports , "__esModule" , {
6
4
value : true
7
5
} ) ;
8
6
7
+ var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ; // See http://jszen.blogspot.com/2007/03/how-to-build-simple-calendar-with.html for calendar logic.
8
+
9
9
var _react = require ( 'react' ) ;
10
10
11
11
var _react2 = _interopRequireDefault ( _react ) ;
@@ -28,19 +28,12 @@ var _OverlayTrigger2 = _interopRequireDefault(_OverlayTrigger);
28
28
29
29
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
30
30
31
- var makeInputValueString = function makeInputValueString ( date ) {
32
- var month = date . getMonth ( ) + 1 ;
33
- var day = date . getDate ( ) ;
34
- return ( month > 9 ? month : "0" + month ) + "/" + ( day > 9 ? day : "0" + day ) + "/" + date . getFullYear ( ) ;
35
- } ;
36
-
37
31
var CalendarHeader = _react2 . default . createClass ( {
38
32
displayName : "DatePickerHeader" ,
39
33
propTypes : {
40
34
displayDate : _react2 . default . PropTypes . object . isRequired ,
41
35
onChange : _react2 . default . PropTypes . func . isRequired ,
42
36
monthLabels : _react2 . default . PropTypes . array . isRequired ,
43
- onDateClick : _react2 . default . PropTypes . func . isRequired ,
44
37
previousButtonElement : _react2 . default . PropTypes . oneOfType ( [ _react2 . default . PropTypes . string , _react2 . default . PropTypes . object ] ) . isRequired ,
45
38
nextButtonElement : _react2 . default . PropTypes . oneOfType ( [ _react2 . default . PropTypes . string , _react2 . default . PropTypes . object ] ) . isRequired
46
39
} ,
@@ -65,7 +58,7 @@ var CalendarHeader = _react2.default.createClass({
65
58
) ,
66
59
_react2 . default . createElement (
67
60
'span' ,
68
- { onClick : this . props . onDateClick } ,
61
+ null ,
69
62
this . props . monthLabels [ this . props . displayDate . getMonth ( ) ] ,
70
63
' ' ,
71
64
this . props . displayDate . getFullYear ( )
@@ -182,35 +175,40 @@ exports.default = _react2.default.createClass({
182
175
propTypes : {
183
176
value : _react2 . default . PropTypes . string ,
184
177
cellPadding : _react2 . default . PropTypes . string ,
178
+ placeholder : _react2 . default . PropTypes . string ,
185
179
dayLabels : _react2 . default . PropTypes . array ,
186
180
monthLabels : _react2 . default . PropTypes . array ,
187
181
onChange : _react2 . default . PropTypes . func ,
188
182
clearButtonElement : _react2 . default . PropTypes . oneOfType ( [ _react2 . default . PropTypes . string , _react2 . default . PropTypes . object ] ) ,
189
183
previousButtonElement : _react2 . default . PropTypes . oneOfType ( [ _react2 . default . PropTypes . string , _react2 . default . PropTypes . object ] ) ,
190
184
nextButtonElement : _react2 . default . PropTypes . oneOfType ( [ _react2 . default . PropTypes . string , _react2 . default . PropTypes . object ] ) ,
191
- calendarPlacement : _react2 . default . PropTypes . string
185
+ calendarPlacement : _react2 . default . PropTypes . string ,
186
+ dateFormat : _react2 . default . PropTypes . oneOf ( [ 'MM/DD/YYYY' , 'DD/MM/YYYY' , 'YYYY/MM/DD' ] )
192
187
} ,
193
188
getDefaultProps : function getDefaultProps ( ) {
189
+ var language = ( window . navigator . userLanguage || window . navigator . language || '' ) . toLowerCase ( ) ;
190
+ var dateFormat = ! language || language === "en-us" ? 'MM/DD/YYYY' : 'DD/MM/YYYY' ;
194
191
return {
195
192
cellPadding : "5px" ,
196
193
dayLabels : [ 'Sun' , 'Mon' , 'Tue' , 'Wed' , 'Thu' , 'Fri' , 'Sat' ] ,
197
194
monthLabels : [ 'January' , 'February' , 'March' , 'April' , 'May' , 'June' , 'July' , 'August' , 'September' , 'October' , 'November' , 'December' ] ,
198
- placeholder : "MM/DD/YYYY" ,
199
195
clearButtonElement : "×" ,
200
196
previousButtonElement : "<" ,
201
197
nextButtonElement : ">" ,
202
- calendarPlacement : "bottom"
198
+ calendarPlacement : "bottom" ,
199
+ dateFormat : dateFormat
203
200
} ;
204
201
} ,
205
202
getInitialState : function getInitialState ( ) {
206
- var state = this . makeDateValues ( this . props . value ? this . props . value : this . props . valueLink ? this . props . valueLink . value : null ) ;
203
+ var state = this . makeDateValues ( this . props . value ) ;
207
204
state . focused = false ;
205
+ state . placeholder = this . props . placeholder || this . props . dateFormat ;
208
206
return state ;
209
207
} ,
210
208
makeDateValues : function makeDateValues ( isoString ) {
211
- var displayDate = undefined ;
209
+ var displayDate = void 0 ;
212
210
var selectedDate = isoString ? new Date ( isoString ) : null ;
213
- var inputValue = isoString ? makeInputValueString ( selectedDate ) : null ;
211
+ var inputValue = isoString ? this . makeInputValueString ( selectedDate ) : null ;
214
212
if ( selectedDate ) {
215
213
displayDate = new Date ( selectedDate ) ;
216
214
} else {
@@ -232,9 +230,6 @@ exports.default = _react2.default.createClass({
232
230
if ( this . props . onChange ) {
233
231
this . props . onChange ( null ) ;
234
232
}
235
- if ( this . props . valueLink && this . props . valueLink . requestChange ) {
236
- this . props . valueLink . requestChange ( null ) ;
237
- }
238
233
} ,
239
234
handleHide : function handleHide ( e ) {
240
235
if ( document . activeElement === this . refs . input . getInputDOMNode ( ) ) {
@@ -269,24 +264,52 @@ exports.default = _react2.default.createClass({
269
264
this . props . onBlur ( e ) ;
270
265
}
271
266
} ,
272
- handleHeaderDateClick : function handleHeaderDateClick ( e ) { } ,
273
267
getValue : function getValue ( ) {
274
268
return this . state . selectedDate ? this . state . selectedDate . toISOString ( ) : null ;
275
269
} ,
270
+ makeInputValueString : function makeInputValueString ( date ) {
271
+ var month = date . getMonth ( ) + 1 ;
272
+ var day = date . getDate ( ) ;
273
+ if ( this . props . dateFormat === "MM/DD/YYYY" ) {
274
+ return ( month > 9 ? month : "0" + month ) + "/" + ( day > 9 ? day : "0" + day ) + "/" + date . getFullYear ( ) ;
275
+ } else if ( this . props . dateFormat === "DD/MM/YYYY" ) {
276
+ return ( day > 9 ? day : "0" + day ) + "/" + ( month > 9 ? month : "0" + month ) + "/" + date . getFullYear ( ) ;
277
+ } else {
278
+ return date . getFullYear ( ) + "/" + ( month > 9 ? month : "0" + month ) + "/" + ( day > 9 ? day : "0" + day ) ;
279
+ }
280
+ } ,
276
281
handleInputChange : function handleInputChange ( e ) {
277
282
var inputValue = this . refs . input . getValue ( ) ;
278
283
inputValue = inputValue . replace ( / ( - | \/ \/ ) / g, '/' ) ;
279
- var month = inputValue . slice ( 0 , 2 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
280
- var day = inputValue . slice ( 3 , 5 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
281
- var year = inputValue . slice ( 6 , 10 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
284
+ var month = void 0 ,
285
+ day = void 0 ,
286
+ year = void 0 ;
287
+ if ( this . props . dateFormat === "MM/DD/YYYY" ) {
288
+ month = inputValue . slice ( 0 , 2 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
289
+ day = inputValue . slice ( 3 , 5 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
290
+ year = inputValue . slice ( 6 , 10 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
291
+ } else if ( this . props . dateFormat === "DD/MM/YYYY" ) {
292
+ day = inputValue . slice ( 0 , 2 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
293
+ month = inputValue . slice ( 3 , 5 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
294
+ year = inputValue . slice ( 6 , 10 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
295
+ } else {
296
+ year = inputValue . slice ( 0 , 4 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
297
+ month = inputValue . slice ( 5 , 7 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
298
+ day = inputValue . slice ( 8 , 10 ) . replace ( / [ ^ 0 - 9 ] / g, '' ) ;
299
+ }
300
+
282
301
var monthInteger = parseInt ( month , 10 ) ;
283
302
var dayInteger = parseInt ( day , 10 ) ;
284
303
var yearInteger = parseInt ( year , 10 ) ;
285
304
if ( ! isNaN ( monthInteger ) && ! isNaN ( dayInteger ) && ! isNaN ( yearInteger ) && monthInteger <= 12 && dayInteger <= 31 && yearInteger > 999 ) {
286
305
var selectedDate = new Date ( ) ;
287
- selectedDate . setDate ( dayInteger ) ;
288
- selectedDate . setMonth ( monthInteger - 1 ) ;
306
+ selectedDate . setHours ( 12 ) ;
307
+ selectedDate . setMinutes ( 0 ) ;
308
+ selectedDate . setSeconds ( 0 ) ;
309
+ selectedDate . setMilliseconds ( 0 ) ;
289
310
selectedDate . setYear ( yearInteger ) ;
311
+ selectedDate . setMonth ( monthInteger - 1 ) ;
312
+ selectedDate . setDate ( dayInteger ) ;
290
313
this . setState ( {
291
314
selectedDate : selectedDate ,
292
315
displayDate : selectedDate ,
@@ -295,19 +318,34 @@ exports.default = _react2.default.createClass({
295
318
if ( this . props . onChange ) {
296
319
this . props . onChange ( selectedDate . toISOString ( ) ) ;
297
320
}
298
- if ( this . props . valueLink && this . props . valueLink . requestChange ) {
299
- this . props . valueLink . requestChange ( selectedDate . toISOString ( ) ) ;
300
- }
301
321
}
302
- inputValue = month + inputValue . slice ( 2 , 3 ) . replace ( / [ ^ \/ ] / g, '' ) + day + inputValue . slice ( 5 , 6 ) . replace ( / [ ^ \/ ] / g, '' ) + year ;
303
- if ( this . state . inputValue && inputValue . length > this . state . inputValue . length ) {
304
- if ( inputValue . length == 2 ) {
305
- inputValue += "/" ;
322
+ if ( this . props . dateFormat === "MM/DD/YYYY" ) {
323
+ inputValue = month + inputValue . slice ( 2 , 3 ) . replace ( / [ ^ \/ ] / g, '' ) + day + inputValue . slice ( 5 , 6 ) . replace ( / [ ^ \/ ] / g, '' ) + year ;
324
+ } else if ( this . props . dateFormat === "DD/MM/YYYY" ) {
325
+ inputValue = day + inputValue . slice ( 2 , 3 ) . replace ( / [ ^ \/ ] / g, '' ) + month + inputValue . slice ( 5 , 6 ) . replace ( / [ ^ \/ ] / g, '' ) + year ;
326
+ } else {
327
+ inputValue = year + inputValue . slice ( 4 , 5 ) . replace ( / [ ^ \/ ] / g, '' ) + month + inputValue . slice ( 7 , 8 ) . replace ( / [ ^ \/ ] / g, '' ) ;
328
+ }
329
+ if ( this . props . dateFormat === "YYYY/MM/DD" ) {
330
+ if ( this . state . inputValue && inputValue . length > this . state . inputValue . length ) {
331
+ if ( inputValue . length == 4 ) {
332
+ inputValue += "/" ;
333
+ }
334
+ if ( inputValue . length == 7 ) {
335
+ inputValue += "/" ;
336
+ }
337
+ inputValue = inputValue . slice ( 0 , 10 ) ;
306
338
}
307
- if ( inputValue . length == 5 ) {
308
- inputValue += "/" ;
339
+ } else {
340
+ if ( this . state . inputValue && inputValue . length > this . state . inputValue . length ) {
341
+ if ( inputValue . length == 2 ) {
342
+ inputValue += "/" ;
343
+ }
344
+ if ( inputValue . length == 5 ) {
345
+ inputValue += "/" ;
346
+ }
347
+ inputValue = inputValue . slice ( 0 , 10 ) ;
309
348
}
310
- inputValue = inputValue . slice ( 0 , 10 ) ;
311
349
}
312
350
this . setState ( {
313
351
inputValue : inputValue
@@ -320,7 +358,7 @@ exports.default = _react2.default.createClass({
320
358
} ,
321
359
onChangeDate : function onChangeDate ( newSelectedDate ) {
322
360
this . setState ( {
323
- inputValue : makeInputValueString ( newSelectedDate ) ,
361
+ inputValue : this . makeInputValueString ( newSelectedDate ) ,
324
362
selectedDate : newSelectedDate ,
325
363
displayDate : newSelectedDate ,
326
364
value : newSelectedDate . toISOString ( )
@@ -329,12 +367,9 @@ exports.default = _react2.default.createClass({
329
367
if ( this . props . onChange ) {
330
368
this . props . onChange ( newSelectedDate . toISOString ( ) ) ;
331
369
}
332
- if ( this . props . valueLink && this . props . valueLink . requestChange ) {
333
- this . props . valueLink . requestChange ( newSelectedDate . toISOString ( ) ) ;
334
- }
335
370
} ,
336
371
componentWillReceiveProps : function componentWillReceiveProps ( newProps ) {
337
- var value = newProps . value ? newProps . value : newProps . valueLink ? newProps . valueLink . value : null ;
372
+ var value = newProps . value ;
338
373
if ( this . getValue ( ) !== value ) {
339
374
this . setState ( this . makeDateValues ( value ) ) ;
340
375
}
@@ -344,9 +379,9 @@ exports.default = _react2.default.createClass({
344
379
previousButtonElement : this . props . previousButtonElement ,
345
380
nextButtonElement : this . props . nextButtonElement ,
346
381
displayDate : this . state . displayDate ,
347
- onDateClick : this . handleHeaderDateClick ,
348
382
onChange : this . onChangeMonth ,
349
- monthLabels : this . props . monthLabels } ) ;
383
+ monthLabels : this . props . monthLabels ,
384
+ dateFormat : this . props . dateFormat } ) ;
350
385
var popOver = _react2 . default . createElement (
351
386
_Popover2 . default ,
352
387
{ id : 'calendar' , title : calendarHeader } ,
@@ -360,16 +395,15 @@ exports.default = _react2.default.createClass({
360
395
) ;
361
396
return _react2 . default . createElement (
362
397
'div' ,
363
- null ,
398
+ { id : this . props . id ? this . props . id + "_container" : null } ,
364
399
_react2 . default . createElement (
365
400
_OverlayTrigger2 . default ,
366
401
{ ref : 'overlay' , trigger : 'click' , rootClose : true , placement : this . props . calendarPlacement , overlay : popOver , delayHide : 100 } ,
367
402
_react2 . default . createElement ( _Input2 . default , _extends ( { } , this . props , {
368
- value : this . state . inputValue ,
403
+ value : this . state . inputValue || '' ,
369
404
ref : 'input' ,
370
405
type : 'text' ,
371
- valueLink : null ,
372
- placeholder : this . state . focused ? "MM/DD/YYYY" : this . props . placeholder ,
406
+ placeholder : this . state . focused ? this . props . dateFormat : this . state . placeholder ,
373
407
onFocus : this . handleFocus ,
374
408
onBlur : this . handleBlur ,
375
409
onChange : this . handleInputChange ,
@@ -378,7 +412,7 @@ exports.default = _react2.default.createClass({
378
412
id : null
379
413
} ) )
380
414
) ,
381
- _react2 . default . createElement ( 'input' , { type : 'hidden' , id : this . props . id , name : this . props . name , value : this . state . value } )
415
+ _react2 . default . createElement ( 'input' , { type : 'hidden' , id : this . props . id , name : this . props . name , value : this . state . value || '' } )
382
416
) ;
383
417
}
384
418
} ) ;
0 commit comments