Skip to content

Commit

Permalink
add ability to clear time
Browse files Browse the repository at this point in the history
Enables timepicker to be reset with clear or setTime(null)

ex.
$('.timepicker').timepicker('setTime', null);

or

$('.timepicker').timepicker('clear');

fixes #155
  • Loading branch information
jdewit committed Oct 6, 2013
1 parent 34d4dba commit a437bc5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
22 changes: 18 additions & 4 deletions js/bootstrap-timepicker.js
Expand Up @@ -90,6 +90,15 @@
this.updateFromElementVal();
},

clear: function() {
this.hour = null;
this.minute = null;
this.second = null;
this.meridian = null;

this.$element.val('');
},

decrementHour: function() {
if (this.showMeridian) {
if (this.hour === 1) {
Expand Down Expand Up @@ -228,11 +237,11 @@
},

formatTime: function(hour, minute, second, meridian) {
hour = hour < 10 ? '0' + hour : hour;
minute = minute < 10 ? '0' + minute : minute;
second = second < 10 ? '0' + second : second;
hour = hour !== null ? (hour < 10 ? '0' + hour : hour) : '';
minute = minute !== null ? (minute < 10 ? '0' + minute : minute) : '';
second = second !== null ? (second < 10 ? '0' + second : second) : '';

return hour + ':' + minute + (this.showSeconds ? ':' + second : '') + (this.showMeridian ? ' ' + meridian : '');
return hour + (hour !== '' ? ':' : '') + minute + (this.showSeconds && second !== '' ? ':' + second : '') + (this.showMeridian && meridian !== null ? ' ' + meridian : '');
},

getCursorPosition: function() {
Expand Down Expand Up @@ -666,6 +675,11 @@
var arr,
timeArray;

if (!time) {
this.clear();
return;
}

if (this.showMeridian) {
arr = time.split(' ');
timeArray = arr[0].split(':');
Expand Down
12 changes: 12 additions & 0 deletions spec/js/TimepickerSpec.js
Expand Up @@ -389,6 +389,18 @@ describe('Timepicker feature', function() {
expect(hideEvents).toBe(1);
});

it('should be able to reset time by using setTime 0/null', function() {
tp1.hour = 10;
tp1.minute = 30;
tp1.meridian = 'PM';
tp1.updateElement();

$input1.timepicker('setTime', null);
tp1.update();
expect(tp1.getTime()).toBe('');
});


it('should not have the widget in the DOM if remove method is called', function() {
expect($('body')).toContain('.bootstrap-timepicker-widget');
tp1.remove();
Expand Down

0 comments on commit a437bc5

Please sign in to comment.