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

feat(timepicker): added ability to clear timepicker control #5299

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 === '') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a tip, the empty string is falsey so this and all your other checks of this nature can be simplified.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine - valid is a boolean, as can be seen from the two lines prior. I did do a double take too, but it checks out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh. thanks.

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