Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 406cae1

Browse files
committed
feat(timepicker): add ability to clear timepicker control
Closes #5293
1 parent 38ba6ec commit 406cae1

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

Diff for: src/timepicker/test/timepicker.spec.js

+23
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,29 @@ describe('timepicker directive', function() {
12401240
expect(element.hasClass('ng-invalid-time')).toBe(false);
12411241
});
12421242

1243+
it('should not be invalid when the model is cleared', function() {
1244+
var elH = getHoursInputEl();
1245+
var elM = getMinutesInputEl();
1246+
var elS = getSecondsInputEl();
1247+
1248+
$rootScope.time = newTime(10, 20, 30);
1249+
$rootScope.$digest();
1250+
1251+
expect(getModelState()).toEqual([10, 20, 30]);
1252+
1253+
changeInputValueTo(elH, '');
1254+
elH.blur();
1255+
$rootScope.$digest();
1256+
changeInputValueTo(elM, '');
1257+
elM.blur();
1258+
$rootScope.$digest();
1259+
changeInputValueTo(elS, '');
1260+
elS.blur();
1261+
$rootScope.$digest();
1262+
1263+
expect(element.hasClass('ng-invalid-time')).toBe(false);
1264+
});
1265+
12431266
it('timepicker1 leaves view alone when hours are invalid and minutes are updated', function() {
12441267
var hoursEl = getHoursInputEl(),
12451268
minutesEl = getMinutesInputEl();

Diff for: src/timepicker/timepicker.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ angular.module('ui.bootstrap.timepicker', [])
165165
var hours = +$scope.hours;
166166
var valid = $scope.showMeridian ? hours > 0 && hours < 13 :
167167
hours >= 0 && hours < 24;
168-
if (!valid) {
168+
if (!valid || $scope.hours === '') {
169169
return undefined;
170170
}
171171

@@ -182,7 +182,11 @@ angular.module('ui.bootstrap.timepicker', [])
182182

183183
function getMinutesFromTemplate() {
184184
var minutes = +$scope.minutes;
185-
return minutes >= 0 && minutes < 60 ? minutes : undefined;
185+
var valid = minutes >= 0 && minutes < 60;
186+
if (!valid || $scope.minutes === '') {
187+
return undefined;
188+
}
189+
return minutes;
186190
}
187191

188192
function getSecondsFromTemplate() {
@@ -322,7 +326,9 @@ angular.module('ui.bootstrap.timepicker', [])
322326

323327
hoursInputEl.bind('blur', function(e) {
324328
ngModelCtrl.$setTouched();
325-
if ($scope.hours === null || $scope.hours === '') {
329+
if (modelIsEmpty()) {
330+
makeValid();
331+
} else if ($scope.hours === null || $scope.hours === '') {
326332
invalidate(true);
327333
} else if (!$scope.invalidHours && $scope.hours < 10) {
328334
$scope.$apply(function() {
@@ -352,7 +358,9 @@ angular.module('ui.bootstrap.timepicker', [])
352358

353359
minutesInputEl.bind('blur', function(e) {
354360
ngModelCtrl.$setTouched();
355-
if ($scope.minutes === null) {
361+
if (modelIsEmpty()) {
362+
makeValid();
363+
} else if ($scope.minutes === null) {
356364
invalidate(undefined, true);
357365
} else if (!$scope.invalidMinutes && $scope.minutes < 10) {
358366
$scope.$apply(function() {
@@ -375,7 +383,9 @@ angular.module('ui.bootstrap.timepicker', [])
375383
};
376384

377385
secondsInputEl.bind('blur', function(e) {
378-
if (!$scope.invalidSeconds && $scope.seconds < 10) {
386+
if (modelIsEmpty()) {
387+
makeValid();
388+
} else if (!$scope.invalidSeconds && $scope.seconds < 10) {
379389
$scope.$apply( function() {
380390
$scope.seconds = pad($scope.seconds);
381391
});
@@ -464,6 +474,12 @@ angular.module('ui.bootstrap.timepicker', [])
464474
return newDate;
465475
}
466476

477+
function modelIsEmpty() {
478+
return ($scope.hours === null || $scope.hours === '') &&
479+
($scope.minutes === null || $scope.minutes === '') &&
480+
(!$scope.showSeconds || $scope.showSeconds && ($scope.seconds === null || $scope.seconds === ''));
481+
}
482+
467483
$scope.showSpinners = angular.isDefined($attrs.showSpinners) ?
468484
$scope.$parent.$eval($attrs.showSpinners) : timepickerConfig.showSpinners;
469485

0 commit comments

Comments
 (0)