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

Commit

Permalink
fix(timepicker): leave view alone if either input is invalid
Browse files Browse the repository at this point in the history
- Change to not auto-update inputs if either input is invalid

Closes #4160
Fixes #3825
  • Loading branch information
wesleycho committed Aug 10, 2015
1 parent 11ecfd5 commit 818f7e5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
33 changes: 32 additions & 1 deletion src/timepicker/test/timepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,38 @@ describe('timepicker directive', function () {
expect(element.hasClass('ng-invalid-time')).toBe(false);
});

it('leaves view alone when hours are invalid and minutes are updated', function() {
var hoursEl = getHoursInputEl(),
minutesEl = getMinutesInputEl();

changeInputValueTo(hoursEl, '25');
hoursEl.blur();
$rootScope.$digest();
expect(getTimeState()).toEqual(['25', '40', 'PM']);

changeInputValueTo(minutesEl, '2');
minutesEl.blur();
$rootScope.$digest();
expect(getTimeState()).toEqual(['25', '2', 'PM']);
});

it('leaves view alone when minutes are invalid and hours are updated', function() {
var hoursEl = getHoursInputEl(),
minutesEl = getMinutesInputEl();

changeInputValueTo(minutesEl, '61');
minutesEl.blur();
$rootScope.$digest();
expect($rootScope.time).toBe(null);
expect(getTimeState()).toEqual(['02', '61', 'PM']);

changeInputValueTo(hoursEl, '2');
hoursEl.blur();
$rootScope.$digest();
expect($rootScope.time).toBe(null);
expect(getTimeState()).toEqual(['2', '61', 'PM']);
});

it('handles 12/24H mode change', function() {
$rootScope.meridian = true;
element = $compile('<timepicker ng-model="time" show-meridian="meridian"></timepicker>')($rootScope);
Expand Down Expand Up @@ -1661,4 +1693,3 @@ describe('timepicker directive', function () {
});
});
});

14 changes: 8 additions & 6 deletions src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ angular.module('ui.bootstrap.timepicker', [])
};

$scope.updateHours = function() {
var hours = getHoursFromTemplate();
var hours = getHoursFromTemplate(),
minutes = getMinutesFromTemplate();

if ( angular.isDefined(hours) ) {
if (angular.isDefined(hours) && angular.isDefined(minutes)) {
selected.setHours( hours );
if (selected < min || selected > max) {
invalidate(true);
Expand All @@ -240,9 +241,10 @@ angular.module('ui.bootstrap.timepicker', [])
});

$scope.updateMinutes = function() {
var minutes = getMinutesFromTemplate();
var minutes = getMinutesFromTemplate(),
hours = getHoursFromTemplate();

if ( angular.isDefined(minutes) ) {
if (angular.isDefined(minutes) && angular.isDefined(hours)) {
selected.setMinutes( minutes );
if (selected < min || selected > max) {
invalidate(undefined, true);
Expand Down Expand Up @@ -324,10 +326,10 @@ angular.module('ui.bootstrap.timepicker', [])
selected = addMinutes( selected, minutes );
refresh();
}

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

$scope.incrementHours = function() {
if (!$scope.noIncrementHours()) {
addMinutesToSelected(hourStep * 60);
Expand Down

0 comments on commit 818f7e5

Please sign in to comment.