Skip to content

Commit

Permalink
Accepts invalid values for the input. onBlur is not triggered when th…
Browse files Browse the repository at this point in the history
…e calendar is closed anymore.
  • Loading branch information
arqex committed Aug 20, 2015
1 parent 7d10673 commit 62fd2f6
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 41 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Changelog
=========
## 1.1.0
* Datepicker can have an empty value. If the value in the input is not valid, `onChange` and `onBlur` will return input value.
* `onBlur` is not triggered anymore if the calendar is not open.

## 1.0.0-rc.2
* Added travis CI
Expand Down
43 changes: 28 additions & 15 deletions DateTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var Datetime = React.createClass({
var nof = function(){};
return {
className: '',
defaultValue: new Date(),
defaultValue: '',
viewMode: 'days',
inputProps: {},
input: true,
Expand All @@ -65,19 +65,27 @@ var Datetime = React.createClass({
getStateFromProps: function( props ){
var formats = this.getFormats( props ),
date = props.value || props.defaultValue,
selectedDate
selectedDate, viewDate
;

if( typeof date == 'string' )
if( date && typeof date == 'string' )
selectedDate = this.localMoment( date, formats.datetime );
else
else if( date )
selectedDate = this.localMoment( date );

if( selectedDate && !selectedDate.isValid() )
selectedDate = null;

viewDate = selectedDate ?
selectedDate.clone().startOf("month") :
this.localMoment().startOf("month")
;

return {
inputFormat: formats.datetime,
viewDate: selectedDate.clone().startOf("month"),
viewDate: viewDate,
selectedDate: selectedDate,
inputValue: selectedDate.format( formats.datetime )
inputValue: selectedDate ? selectedDate.format( formats.datetime ) : (date || '')
};
},

Expand Down Expand Up @@ -129,10 +137,12 @@ var Datetime = React.createClass({
update.selectedDate = localMoment;
update.viewDate = localMoment.clone().startOf("month");
}
else {
update.selectedDate = null;
}

return this.setState( update, function() {
if( localMoment.isValid() )
return this.props.onChange( localMoment );
return this.props.onChange( localMoment.isValid() ? localMoment : this.state.inputValue );
});
},

Expand Down Expand Up @@ -183,7 +193,8 @@ var Datetime = React.createClass({
allowedSetTime: ['hours','minutes','seconds', 'milliseconds'],
setTime: function( type, value ){
var index = this.allowedSetTime.indexOf( type ) + 1,
date = this.state.selectedDate.clone(),
state = this.state,
date = (state.selectedDate || state.viewDate).clone(),
nextType
;

Expand All @@ -198,7 +209,7 @@ var Datetime = React.createClass({
if( !this.props.value ){
this.setState({
selectedDate: date,
inputValue: date.format( this.state.inputFormat )
inputValue: date.format( state.inputFormat )
});
}
this.props.onChange( date );
Expand All @@ -207,7 +218,8 @@ var Datetime = React.createClass({
updateSelectedDate: function( e ) {
var target = e.target,
modifier = 0,
currentDate = this.state.selectedDate,
viewDate = this.state.viewDate,
currentDate = this.state.selectedDate || viewDate,
date
;

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

date = this.state.viewDate.clone()
.month( this.state.viewDate.month() + modifier )
date = viewDate.clone()
.month( viewDate.month() + modifier )
.date( parseInt( target.getAttribute('data-value') ) )
.hours( currentDate.hours() )
.minutes( currentDate.minutes() )
Expand All @@ -241,9 +253,10 @@ var Datetime = React.createClass({
},

handleClickOutside: function(){
this.props.onBlur( this.state.selectedDate );
if( this.props.input && this.state.open )
if( this.props.input && this.state.open ){
this.setState({ open: false });
this.props.onBlur( this.state.selectedDate || this.state.inputValue );
}
},

localMoment: function( date, format ){
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ API
| **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. |
| **input** | boolean | true | Wether to show an input field to edit the date manually. |
| **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).
| **onChange** | function | empty function | Callback trigger when the date changes. The callback receives the selected `moment` object as only parameter. |
| **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. |
| **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. |
| **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. |
| **viewMode** | string or number | 'days' | The default view to display when the picker is shown. ('years', 'months', 'days', 'time') |
| **className** | string | `""` | Extra class names for the component markup. |
| **inputProps** | object | undefined | Defines additional attributes for the input element of the component. |
Expand Down
12 changes: 6 additions & 6 deletions dist/react-datetime.js

Large diffs are not rendered by default.

Loading

0 comments on commit 62fd2f6

Please sign in to comment.