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

Commit

Permalink
feat(timepicker): add ability to clear timepicker control
Browse files Browse the repository at this point in the history
Closes #5293
  • Loading branch information
asurinov committed Jan 19, 2016
1 parent 38ba6ec commit 406cae1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/timepicker/test/timepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,29 @@ describe('timepicker directive', function() {
expect(element.hasClass('ng-invalid-time')).toBe(false);
});

it('should not be invalid when the model is cleared', function() {
var elH = getHoursInputEl();
var elM = getMinutesInputEl();
var elS = getSecondsInputEl();

$rootScope.time = newTime(10, 20, 30);
$rootScope.$digest();

expect(getModelState()).toEqual([10, 20, 30]);

changeInputValueTo(elH, '');
elH.blur();
$rootScope.$digest();
changeInputValueTo(elM, '');
elM.blur();
$rootScope.$digest();
changeInputValueTo(elS, '');
elS.blur();
$rootScope.$digest();

expect(element.hasClass('ng-invalid-time')).toBe(false);
});

it('timepicker1 leaves view alone when hours are invalid and minutes are updated', function() {
var hoursEl = getHoursInputEl(),
minutesEl = getMinutesInputEl();
Expand Down
26 changes: 21 additions & 5 deletions src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ angular.module('ui.bootstrap.timepicker', [])
var hours = +$scope.hours;
var valid = $scope.showMeridian ? hours > 0 && hours < 13 :
hours >= 0 && hours < 24;
if (!valid) {
if (!valid || $scope.hours === '') {
return undefined;
}

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

function getMinutesFromTemplate() {
var minutes = +$scope.minutes;
return minutes >= 0 && minutes < 60 ? minutes : undefined;
var valid = minutes >= 0 && minutes < 60;
if (!valid || $scope.minutes === '') {
return undefined;
}
return minutes;
}

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

hoursInputEl.bind('blur', function(e) {
ngModelCtrl.$setTouched();
if ($scope.hours === null || $scope.hours === '') {
if (modelIsEmpty()) {
makeValid();
} else if ($scope.hours === null || $scope.hours === '') {
invalidate(true);
} else if (!$scope.invalidHours && $scope.hours < 10) {
$scope.$apply(function() {
Expand Down Expand Up @@ -352,7 +358,9 @@ angular.module('ui.bootstrap.timepicker', [])

minutesInputEl.bind('blur', function(e) {
ngModelCtrl.$setTouched();
if ($scope.minutes === null) {
if (modelIsEmpty()) {
makeValid();
} else if ($scope.minutes === null) {
invalidate(undefined, true);
} else if (!$scope.invalidMinutes && $scope.minutes < 10) {
$scope.$apply(function() {
Expand All @@ -375,7 +383,9 @@ angular.module('ui.bootstrap.timepicker', [])
};

secondsInputEl.bind('blur', function(e) {
if (!$scope.invalidSeconds && $scope.seconds < 10) {
if (modelIsEmpty()) {
makeValid();
} else if (!$scope.invalidSeconds && $scope.seconds < 10) {
$scope.$apply( function() {
$scope.seconds = pad($scope.seconds);
});
Expand Down Expand Up @@ -464,6 +474,12 @@ angular.module('ui.bootstrap.timepicker', [])
return newDate;
}

function modelIsEmpty() {
return ($scope.hours === null || $scope.hours === '') &&
($scope.minutes === null || $scope.minutes === '') &&
(!$scope.showSeconds || $scope.showSeconds && ($scope.seconds === null || $scope.seconds === ''));
}

$scope.showSpinners = angular.isDefined($attrs.showSpinners) ?
$scope.$parent.$eval($attrs.showSpinners) : timepickerConfig.showSpinners;

Expand Down

0 comments on commit 406cae1

Please sign in to comment.