Skip to content

Commit 07ac585

Browse files
committed
fix(timepicker): Always pad the hours and minutes
The timepicker only padded the minutes and hours when the template first loaded. The hours and minutes are now also padded when the model changes. Fixes angular-ui#2314.
1 parent 5cd6f4a commit 07ac585

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/timepicker/test/timepicker.spec.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -714,11 +714,39 @@ describe('timepicker directive', function () {
714714
expect(getModelState()).toEqual([14, 40]);
715715
});
716716

717+
it('always pads hours & minutes', function() {
718+
// Make sure hours and minutes are padded in 12 hour format
719+
$rootScope.meridian = true;
720+
element = $compile('<timepicker ng-model="time" show-meridian="meridian"></timepicker>')($rootScope);
721+
$rootScope.$digest();
722+
723+
var hours = getHoursInputEl();
724+
var minutes = getMinutesInputEl();
725+
726+
// Make sure the time is padded when the template is first loaded
727+
expect(getTimeState()).toEqual(['02', '40', 'PM']);
728+
expect(getModelState()).toEqual([14, 40]);
729+
730+
changeInputValueTo(hours, 5);
731+
changeInputValueTo(minutes, 7);
732+
$rootScope.$digest();
733+
expect(getTimeState()).toEqual(['05', '07', 'PM']);
734+
expect(getModelState()).toEqual([17, 7]);
735+
736+
// Make sure hours and minutes are padded in 24 hour format
737+
$rootScope.meridian = false;
738+
changeInputValueTo(hours, 0);
739+
changeInputValueTo(minutes, 0);
740+
$rootScope.$digest();
741+
expect(getTimeState()).toEqual(['00', '00', 'AM']);
742+
expect(getModelState()).toEqual([0, 0]);
743+
});
744+
717745
it('updates hours & pads on input change & pads on blur', function() {
718746
var el = getHoursInputEl();
719747

720748
changeInputValueTo(el, 5);
721-
expect(getTimeState()).toEqual(['5', '40', 'PM']);
749+
expect(getTimeState()).toEqual(['05', '40', 'PM']);
722750
expect(getModelState()).toEqual([17, 40]);
723751

724752
el.blur();
@@ -730,7 +758,7 @@ describe('timepicker directive', function () {
730758
var el = getMinutesInputEl();
731759

732760
changeInputValueTo(el, 9);
733-
expect(getTimeState()).toEqual(['02', '9', 'PM']);
761+
expect(getTimeState()).toEqual(['02', '09', 'PM']);
734762
expect(getModelState()).toEqual([14, 9]);
735763

736764
el.blur();

src/timepicker/timepicker.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ angular.module('ui.bootstrap.timepicker', [])
188188
};
189189

190190
// Call internally when we know that model is valid.
191-
function refresh( keyboardChange ) {
191+
function refresh() {
192192
makeValid();
193193
ngModelCtrl.$setViewValue( new Date(selected) );
194-
updateTemplate( keyboardChange );
194+
updateTemplate();
195195
}
196196

197197
function makeValid() {
@@ -200,15 +200,15 @@ angular.module('ui.bootstrap.timepicker', [])
200200
$scope.invalidMinutes = false;
201201
}
202202

203-
function updateTemplate( keyboardChange ) {
203+
function updateTemplate() {
204204
var hours = selected.getHours(), minutes = selected.getMinutes();
205205

206206
if ( $scope.showMeridian ) {
207207
hours = ( hours === 0 || hours === 12 ) ? 12 : hours % 12; // Convert 24 to 12 hour system
208208
}
209209

210-
$scope.hours = keyboardChange === 'h' ? hours : pad(hours);
211-
$scope.minutes = keyboardChange === 'm' ? minutes : pad(minutes);
210+
$scope.hours = pad( hours );
211+
$scope.minutes = pad( minutes );
212212
$scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1];
213213
}
214214

0 commit comments

Comments
 (0)