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

Commit c7be087

Browse files
committed
feat(timepicker): add pad-hours support
- Add support for optionally padding hours Closes #4288 Closes #5633
1 parent 1b888d4 commit c7be087

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/timepicker/docs/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ A lightweight & configurable timepicker directive.
5757
<i class="glyphicon glyphicon-eye-open"></i> -
5858
Date object that provides the time state.
5959

60+
* `pad-hours`
61+
<small class="badge">$</small>
62+
_(Default: true)_ -
63+
Whether the hours column is padded with a 0.
64+
6065
* `readonly-input`
6166
<small class="badge">$</small>
6267
<small class="badge">C</small>

src/timepicker/test/timepicker.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,37 @@ describe('timepicker directive', function() {
986986
});
987987
});
988988

989+
describe('`pad-hours` attribute', function() {
990+
function triggerInput(elem, val) {
991+
elem.val(val);
992+
elem.trigger('input');
993+
}
994+
995+
it('should pad the hours by default', function() {
996+
element = $compile('<uib-timepicker ng-model="time"></uib-timepicker>')($rootScope);
997+
$rootScope.$digest();
998+
999+
var inputs = element.find('input');
1000+
var hoursInput = inputs.eq(0);
1001+
triggerInput(hoursInput, 4);
1002+
hoursInput.blur();
1003+
1004+
expect(hoursInput.val()).toBe('04');
1005+
});
1006+
1007+
it('should not pad the hours', function() {
1008+
element = $compile('<uib-timepicker ng-model="time" pad-hours="false"></uib-timepicker>')($rootScope);
1009+
$rootScope.$digest();
1010+
1011+
var inputs = element.find('input');
1012+
var hoursInput = inputs.eq(0);
1013+
triggerInput(hoursInput, 4);
1014+
hoursInput.blur();
1015+
1016+
expect(hoursInput.val()).toBe('4');
1017+
});
1018+
});
1019+
9891020
describe('setting uibTimepickerConfig steps', function() {
9901021
var originalConfig = {};
9911022
beforeEach(inject(function(_$compile_, _$rootScope_, uibTimepickerConfig) {

src/timepicker/timepicker.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ angular.module('ui.bootstrap.timepicker', [])
1818
var selected = new Date(),
1919
watchers = [],
2020
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl
21-
meridians = angular.isDefined($attrs.meridians) ? $scope.$parent.$eval($attrs.meridians) : timepickerConfig.meridians || $locale.DATETIME_FORMATS.AMPMS;
21+
meridians = angular.isDefined($attrs.meridians) ? $scope.$parent.$eval($attrs.meridians) : timepickerConfig.meridians || $locale.DATETIME_FORMATS.AMPMS,
22+
padHours = angular.isDefined($attrs.padHours) ? $scope.$parent.$eval($attrs.padHours) : true;
2223

2324
$scope.tabindex = angular.isDefined($attrs.tabindex) ? $attrs.tabindex : 0;
2425
$element.removeAttr('tabindex');
@@ -190,12 +191,12 @@ angular.module('ui.bootstrap.timepicker', [])
190191
return seconds >= 0 && seconds < 60 ? seconds : undefined;
191192
}
192193

193-
function pad(value) {
194+
function pad(value, noPad) {
194195
if (value === null) {
195196
return '';
196197
}
197198

198-
return angular.isDefined(value) && value.toString().length < 2 ?
199+
return angular.isDefined(value) && value.toString().length < 2 && !noPad ?
199200
'0' + value : value.toString();
200201
}
201202

@@ -326,7 +327,7 @@ angular.module('ui.bootstrap.timepicker', [])
326327
invalidate(true);
327328
} else if (!$scope.invalidHours && $scope.hours < 10) {
328329
$scope.$apply(function() {
329-
$scope.hours = pad($scope.hours);
330+
$scope.hours = pad($scope.hours, !padHours);
330331
});
331332
}
332333
});
@@ -435,7 +436,7 @@ angular.module('ui.bootstrap.timepicker', [])
435436
hours = hours === 0 || hours === 12 ? 12 : hours % 12; // Convert 24 to 12 hour system
436437
}
437438

438-
$scope.hours = keyboardChange === 'h' ? hours : pad(hours);
439+
$scope.hours = keyboardChange === 'h' ? hours : pad(hours, !padHours);
439440
if (keyboardChange !== 'm') {
440441
$scope.minutes = pad(minutes);
441442
}

0 commit comments

Comments
 (0)