1
- import DateInput from './date_input'
2
1
import Calendar from './calendar'
3
2
import React from 'react'
4
3
import TetherComponent from './tether_component'
5
4
import classnames from 'classnames'
6
- import { isSameDay , isDayDisabled , isDayInRange , getEffectiveMinDate , getEffectiveMaxDate } from './date_utils'
5
+ import { isSameDay , isDayDisabled , isDayInRange , getEffectiveMinDate , getEffectiveMaxDate , parseDate , safeDateFormat } from './date_utils'
7
6
import moment from 'moment'
8
7
import onClickOutside from 'react-onclickoutside'
9
8
@@ -24,13 +23,12 @@ var DatePicker = React.createClass({
24
23
children : React . PropTypes . node ,
25
24
className : React . PropTypes . string ,
26
25
customInput : React . PropTypes . element ,
27
- dateFormat : React . PropTypes . oneOfType ( [
26
+ dateFormat : React . PropTypes . oneOfType ( [ // eslint-disable-line react/no-unused-prop-types
28
27
React . PropTypes . string ,
29
28
React . PropTypes . array
30
29
] ) ,
31
30
dateFormatCalendar : React . PropTypes . string ,
32
31
disabled : React . PropTypes . bool ,
33
- disableDateAutoCorrection : React . PropTypes . bool ,
34
32
disabledKeyboardNavigation : React . PropTypes . bool ,
35
33
dropdownMode : React . PropTypes . oneOf ( [ 'scroll' , 'select' ] ) . isRequired ,
36
34
endDate : React . PropTypes . object ,
@@ -77,15 +75,16 @@ var DatePicker = React.createClass({
77
75
title : React . PropTypes . string ,
78
76
todayButton : React . PropTypes . string ,
79
77
utcOffset : React . PropTypes . number ,
78
+ value : React . PropTypes . string ,
80
79
withPortal : React . PropTypes . bool
81
80
} ,
82
81
83
82
getDefaultProps ( ) {
84
83
return {
84
+ dateFormat : 'L' ,
85
85
dateFormatCalendar : 'MMMM YYYY' ,
86
86
onChange ( ) { } ,
87
87
disabled : false ,
88
- disableDateAutoCorrection : false ,
89
88
disabledKeyboardNavigation : false ,
90
89
dropdownMode : 'scroll' ,
91
90
onFocus ( ) { } ,
@@ -184,6 +183,20 @@ var DatePicker = React.createClass({
184
183
if ( this . props . withPortal ) { event . preventDefault ( ) }
185
184
} ,
186
185
186
+ handleChange ( event ) {
187
+ if ( this . props . onChangeRaw ) {
188
+ this . props . onChangeRaw ( event )
189
+ if ( event . isDefaultPrevented ( ) ) {
190
+ return
191
+ }
192
+ }
193
+ this . setState ( { inputValue : event . target . value } )
194
+ const date = parseDate ( event . target . value , this . props )
195
+ if ( date || ! event . target . value ) {
196
+ this . setSelected ( date , event , true )
197
+ }
198
+ } ,
199
+
187
200
handleSelect ( date , event ) {
188
201
// Preventing onFocus event to fix issue
189
202
// https://github.com/Hacker0x01/react-datepicker/issues/628
@@ -197,14 +210,14 @@ var DatePicker = React.createClass({
197
210
this . setOpen ( false )
198
211
} ,
199
212
200
- setSelected ( date , event ) {
213
+ setSelected ( date , event , keepInput ) {
201
214
let changedDate = date
202
215
203
216
if ( changedDate !== null && isDayDisabled ( changedDate , this . props ) ) {
204
217
return
205
218
}
206
219
207
- if ( ! isSameDay ( this . props . selected , changedDate ) || this . props . disableDateAutoCorrection ) {
220
+ if ( ! isSameDay ( this . props . selected , changedDate ) ) {
208
221
if ( changedDate !== null ) {
209
222
if ( this . props . selected ) {
210
223
changedDate = moment ( changedDate ) . set ( {
@@ -217,11 +230,14 @@ var DatePicker = React.createClass({
217
230
preSelection : changedDate
218
231
} )
219
232
}
220
-
221
233
this . props . onChange ( changedDate , event )
222
234
}
223
235
224
236
this . props . onSelect ( changedDate , event )
237
+
238
+ if ( ! keepInput ) {
239
+ this . setState ( { inputValue : null } )
240
+ }
225
241
} ,
226
242
227
243
setPreSelection ( date ) {
@@ -349,35 +365,33 @@ var DatePicker = React.createClass({
349
365
var className = classnames ( this . props . className , {
350
366
[ outsideClickIgnoreClass ] : this . state . open
351
367
} )
352
- return < DateInput
353
- ref = "input"
354
- disableDateAutoCorrection = { this . props . disableDateAutoCorrection }
355
- id = { this . props . id }
356
- name = { this . props . name }
357
- autoFocus = { this . props . autoFocus }
358
- date = { this . props . selected }
359
- locale = { this . props . locale }
360
- minDate = { this . props . minDate }
361
- maxDate = { this . props . maxDate }
362
- excludeDates = { this . props . excludeDates }
363
- includeDates = { this . props . includeDates }
364
- filterDate = { this . props . filterDate }
365
- dateFormat = { this . props . dateFormat }
366
- onFocus = { this . handleFocus }
367
- onBlur = { this . handleBlur }
368
- onClick = { this . onInputClick }
369
- onChangeRaw = { this . props . onChangeRaw }
370
- onKeyDown = { this . onInputKeyDown }
371
- onChangeDate = { this . setSelected }
372
- placeholder = { this . props . placeholderText }
373
- disabled = { this . props . disabled }
374
- autoComplete = { this . props . autoComplete }
375
- className = { className }
376
- title = { this . props . title }
377
- readOnly = { this . props . readOnly }
378
- required = { this . props . required }
379
- tabIndex = { this . props . tabIndex }
380
- customInput = { this . props . customInput } />
368
+
369
+ const customInput = this . props . customInput || < input type = "text" />
370
+ const inputValue =
371
+ typeof this . props . value === 'string' ? this . props . value
372
+ : typeof this . state . inputValue === 'string' ? this . state . inputValue
373
+ : safeDateFormat ( this . props . selected , this . props )
374
+
375
+ return React . cloneElement ( customInput , {
376
+ ref : 'input' ,
377
+ value : inputValue ,
378
+ onBlur : this . handleBlur ,
379
+ onChange : this . handleChange ,
380
+ onClick : this . onInputClick ,
381
+ onFocus : this . handleFocus ,
382
+ onKeyDown : this . onInputKeyDown ,
383
+ id : this . props . id ,
384
+ name : this . props . name ,
385
+ autoFocus : this . props . autoFocus ,
386
+ placeholder : this . props . placeholderText ,
387
+ disabled : this . props . disabled ,
388
+ autoComplete : this . props . autoComplete ,
389
+ className : className ,
390
+ title : this . props . title ,
391
+ readOnly : this . props . readOnly ,
392
+ required : this . props . required ,
393
+ tabIndex : this . props . tabIndex
394
+ } )
381
395
} ,
382
396
383
397
renderClearButton ( ) {
0 commit comments