Skip to content

Commit 62fd2f6

Browse files
committed
Accepts invalid values for the input. onBlur is not triggered when the calendar is closed anymore.
1 parent 7d10673 commit 62fd2f6

File tree

11 files changed

+76
-41
lines changed

11 files changed

+76
-41
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Changelog
22
=========
3+
## 1.1.0
4+
* Datepicker can have an empty value. If the value in the input is not valid, `onChange` and `onBlur` will return input value.
5+
* `onBlur` is not triggered anymore if the calendar is not open.
36

47
## 1.0.0-rc.2
58
* Added travis CI

DateTime.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var Datetime = React.createClass({
4242
var nof = function(){};
4343
return {
4444
className: '',
45-
defaultValue: new Date(),
45+
defaultValue: '',
4646
viewMode: 'days',
4747
inputProps: {},
4848
input: true,
@@ -65,19 +65,27 @@ var Datetime = React.createClass({
6565
getStateFromProps: function( props ){
6666
var formats = this.getFormats( props ),
6767
date = props.value || props.defaultValue,
68-
selectedDate
68+
selectedDate, viewDate
6969
;
7070

71-
if( typeof date == 'string' )
71+
if( date && typeof date == 'string' )
7272
selectedDate = this.localMoment( date, formats.datetime );
73-
else
73+
else if( date )
7474
selectedDate = this.localMoment( date );
7575

76+
if( selectedDate && !selectedDate.isValid() )
77+
selectedDate = null;
78+
79+
viewDate = selectedDate ?
80+
selectedDate.clone().startOf("month") :
81+
this.localMoment().startOf("month")
82+
;
83+
7684
return {
7785
inputFormat: formats.datetime,
78-
viewDate: selectedDate.clone().startOf("month"),
86+
viewDate: viewDate,
7987
selectedDate: selectedDate,
80-
inputValue: selectedDate.format( formats.datetime )
88+
inputValue: selectedDate ? selectedDate.format( formats.datetime ) : (date || '')
8189
};
8290
},
8391

@@ -129,10 +137,12 @@ var Datetime = React.createClass({
129137
update.selectedDate = localMoment;
130138
update.viewDate = localMoment.clone().startOf("month");
131139
}
140+
else {
141+
update.selectedDate = null;
142+
}
132143

133144
return this.setState( update, function() {
134-
if( localMoment.isValid() )
135-
return this.props.onChange( localMoment );
145+
return this.props.onChange( localMoment.isValid() ? localMoment : this.state.inputValue );
136146
});
137147
},
138148

@@ -183,7 +193,8 @@ var Datetime = React.createClass({
183193
allowedSetTime: ['hours','minutes','seconds', 'milliseconds'],
184194
setTime: function( type, value ){
185195
var index = this.allowedSetTime.indexOf( type ) + 1,
186-
date = this.state.selectedDate.clone(),
196+
state = this.state,
197+
date = (state.selectedDate || state.viewDate).clone(),
187198
nextType
188199
;
189200

@@ -198,7 +209,7 @@ var Datetime = React.createClass({
198209
if( !this.props.value ){
199210
this.setState({
200211
selectedDate: date,
201-
inputValue: date.format( this.state.inputFormat )
212+
inputValue: date.format( state.inputFormat )
202213
});
203214
}
204215
this.props.onChange( date );
@@ -207,7 +218,8 @@ var Datetime = React.createClass({
207218
updateSelectedDate: function( e ) {
208219
var target = e.target,
209220
modifier = 0,
210-
currentDate = this.state.selectedDate,
221+
viewDate = this.state.viewDate,
222+
currentDate = this.state.selectedDate || viewDate,
211223
date
212224
;
213225

@@ -216,8 +228,8 @@ var Datetime = React.createClass({
216228
else if(target.className.indexOf("old") != -1)
217229
modifier = -1;
218230

219-
date = this.state.viewDate.clone()
220-
.month( this.state.viewDate.month() + modifier )
231+
date = viewDate.clone()
232+
.month( viewDate.month() + modifier )
221233
.date( parseInt( target.getAttribute('data-value') ) )
222234
.hours( currentDate.hours() )
223235
.minutes( currentDate.minutes() )
@@ -241,9 +253,10 @@ var Datetime = React.createClass({
241253
},
242254

243255
handleClickOutside: function(){
244-
this.props.onBlur( this.state.selectedDate );
245-
if( this.props.input && this.state.open )
256+
if( this.props.input && this.state.open ){
246257
this.setState({ open: false });
258+
this.props.onBlur( this.state.selectedDate || this.state.inputValue );
259+
}
247260
},
248261

249262
localMoment: function( date, format ){

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ API
4242
| **timeFormat** | `bool` or `string` | `true` | Defines the format for the time. It accepts any [moment.js time format](http://momentjs.com/docs/#/displaying/format/). If `true` the time will be displayed using the defaults for the current locale. If `false` the timepicker is disabled and the component can be used as datepicker. |
4343
| **input** | boolean | true | Wether to show an input field to edit the date manually. |
4444
| **locale** | string | null | Manually set the locale for the react-datetime instance. Moment.js locale needs to be loaded to be used, see [i18n docs](#i18n).
45-
| **onChange** | function | empty function | Callback trigger when the date changes. The callback receives the selected `moment` object as only parameter. |
46-
| **onBlur** | function | empty function | Callback trigger for when the user clicks outside of the input, simulating a regular onBlur. The callback receives the selected `moment` object as only parameter. |
45+
| **onChange** | function | empty function | Callback trigger when the date changes. The callback receives the selected `moment` object as only parameter, if the date in the input is valid. If it isn't, the value of the input (a string) is returned. |
46+
| **onBlur** | function | empty function | Callback trigger for when the user clicks outside of the input, simulating a regular onBlur. The callback receives the selected `moment` object as only parameter, if the date in the input is valid. If it isn't, the value of the input (a string) is returned. |
4747
| **viewMode** | string or number | 'days' | The default view to display when the picker is shown. ('years', 'months', 'days', 'time') |
4848
| **className** | string | `""` | Extra class names for the component markup. |
4949
| **inputProps** | object | undefined | Defines additional attributes for the input element of the component. |

dist/react-datetime.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)