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

Commit 449c0d1

Browse files
asurinovwesleycho
authored andcommitted
feat(timepicker): remove automatic padding when empty
- Removes automatic padding when input is empty to allow ability to manipulate inputs on user's side Closes #5293 Closes #5299 BREAKING CHANGE: This removes automatic padding done by the timepicker when the input is empty - if that behavior is desired, write a custom directive implementing it
1 parent c534cb4 commit 449c0d1

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
@@ -1271,6 +1271,29 @@ describe('timepicker directive', function() {
12711271
expect(element.hasClass('ng-invalid-time')).toBe(false);
12721272
});
12731273

1274+
it('should not be invalid when the model is cleared', function() {
1275+
var elH = getHoursInputEl();
1276+
var elM = getMinutesInputEl();
1277+
var elS = getSecondsInputEl();
1278+
1279+
$rootScope.time = newTime(10, 20, 30);
1280+
$rootScope.$digest();
1281+
1282+
expect(getModelState()).toEqual([10, 20, 30]);
1283+
1284+
changeInputValueTo(elH, '');
1285+
elH.blur();
1286+
$rootScope.$digest();
1287+
changeInputValueTo(elM, '');
1288+
elM.blur();
1289+
$rootScope.$digest();
1290+
changeInputValueTo(elS, '');
1291+
elS.blur();
1292+
$rootScope.$digest();
1293+
1294+
expect(element.hasClass('ng-invalid-time')).toBe(false);
1295+
});
1296+
12741297
it('timepicker1 leaves view alone when hours are invalid and minutes are updated', function() {
12751298
var hoursEl = getHoursInputEl(),
12761299
minutesEl = getMinutesInputEl();

Diff for: src/timepicker/timepicker.js

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

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

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

189193
function getSecondsFromTemplate() {
@@ -323,7 +327,9 @@ angular.module('ui.bootstrap.timepicker', [])
323327

324328
hoursInputEl.bind('blur', function(e) {
325329
ngModelCtrl.$setTouched();
326-
if ($scope.hours === null || $scope.hours === '') {
330+
if (modelIsEmpty()) {
331+
makeValid();
332+
} else if ($scope.hours === null || $scope.hours === '') {
327333
invalidate(true);
328334
} else if (!$scope.invalidHours && $scope.hours < 10) {
329335
$scope.$apply(function() {
@@ -353,7 +359,9 @@ angular.module('ui.bootstrap.timepicker', [])
353359

354360
minutesInputEl.bind('blur', function(e) {
355361
ngModelCtrl.$setTouched();
356-
if ($scope.minutes === null) {
362+
if (modelIsEmpty()) {
363+
makeValid();
364+
} else if ($scope.minutes === null) {
357365
invalidate(undefined, true);
358366
} else if (!$scope.invalidMinutes && $scope.minutes < 10) {
359367
$scope.$apply(function() {
@@ -376,7 +384,9 @@ angular.module('ui.bootstrap.timepicker', [])
376384
};
377385

378386
secondsInputEl.bind('blur', function(e) {
379-
if (!$scope.invalidSeconds && $scope.seconds < 10) {
387+
if (modelIsEmpty()) {
388+
makeValid();
389+
} else if (!$scope.invalidSeconds && $scope.seconds < 10) {
380390
$scope.$apply( function() {
381391
$scope.seconds = pad($scope.seconds);
382392
});
@@ -465,6 +475,12 @@ angular.module('ui.bootstrap.timepicker', [])
465475
return newDate;
466476
}
467477

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

0 commit comments

Comments
 (0)