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

Commit

Permalink
feat(timepicker): add model state support
Browse files Browse the repository at this point in the history
- Adds support for the timepicker model $touched and $dirty states

Closes #3527
Closes #4835
  • Loading branch information
wesleycho committed Nov 7, 2015
1 parent 703432b commit fe69386
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
55 changes: 53 additions & 2 deletions src/timepicker/test/timepicker.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe('timepicker directive', function() {
var $rootScope, $compile, $templateCache, element;
var $rootScope, $compile, $templateCache, element, modelCtrl;

beforeEach(module('ui.bootstrap.timepicker'));
beforeEach(module('template/timepicker/timepicker.html'));
Expand All @@ -11,6 +11,8 @@ describe('timepicker directive', function() {

element = $compile('<uib-timepicker ng-model="time"></uib-timepicker>')($rootScope);
$rootScope.$digest();

modelCtrl = element.controller('ngModel');
}));

function newTime(hours, minutes, seconds) {
Expand Down Expand Up @@ -117,7 +119,11 @@ describe('timepicker directive', function() {
});

it('should be pristine', function() {
expect(element.controller('ngModel').$pristine).toBe(true);
expect(modelCtrl.$pristine).toBe(true);
});

it('should be untouched', function() {
expect(modelCtrl.$untouched).toBe(true);
});

it('has `selected` current time when model is initially cleared', function() {
Expand Down Expand Up @@ -192,6 +198,51 @@ describe('timepicker directive', function() {
expect(getModelState()).toEqual([14, 40, 24]);
});

it('should be dirty when input changes', function() {
var upHours = getHoursButton(true);
var upMinutes = getMinutesButton(true);
var upSeconds = getSecondsButton(true);

doClick(upHours);
expect(modelCtrl.$dirty).toBe(true);

modelCtrl.$setPristine();

doClick(upMinutes);
expect(modelCtrl.$dirty).toBe(true);

modelCtrl.$setPristine();

doClick(upSeconds);
expect(modelCtrl.$dirty).toBe(true);
});

it('should be touched when input blurs', function() {
var inputs = element.find('input');
var hoursInput = inputs.eq(0),
minutesInput = inputs.eq(1),
secondsInput = inputs.eq(2);

hoursInput.val(12);
$rootScope.$digest();
hoursInput.blur();
expect(modelCtrl.$touched).toBe(true);

modelCtrl.$setUntouched();

minutesInput.val(20);
$rootScope.$digest();
hoursInput.blur();
expect(modelCtrl.$touched).toBe(true);

modelCtrl.$setUntouched();

secondsInput.val(9);
$rootScope.$digest();
hoursInput.blur();
expect(modelCtrl.$touched).toBe(true);
});

it('meridian button has correct type', function() {
var button = getMeridianButton();
expect(button.attr('type')).toBe('button');
Expand Down
10 changes: 10 additions & 0 deletions src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ angular.module('ui.bootstrap.timepicker', [])
var hours = getHoursFromTemplate(),
minutes = getMinutesFromTemplate();

ngModelCtrl.$setDirty();

if (angular.isDefined(hours) && angular.isDefined(minutes)) {
selected.setHours(hours);
selected.setMinutes(minutes);
Expand Down Expand Up @@ -329,6 +331,8 @@ angular.module('ui.bootstrap.timepicker', [])
var minutes = getMinutesFromTemplate(),
hours = getHoursFromTemplate();

ngModelCtrl.$setDirty();

if (angular.isDefined(minutes) && angular.isDefined(hours)) {
selected.setHours(hours);
selected.setMinutes(minutes);
Expand Down Expand Up @@ -356,6 +360,8 @@ angular.module('ui.bootstrap.timepicker', [])
$scope.updateSeconds = function() {
var seconds = getSecondsFromTemplate();

ngModelCtrl.$setDirty();

if (angular.isDefined(seconds)) {
selected.setSeconds(seconds);
refresh('s');
Expand Down Expand Up @@ -505,6 +511,10 @@ angular.module('ui.bootstrap.timepicker', [])
}
}
};

$scope.blur = function() {
ngModelCtrl.$setTouched();
};
}])

.directive('uibTimepicker', function() {
Expand Down
6 changes: 3 additions & 3 deletions template/timepicker/timepicker.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
</tr>
<tr>
<td class="form-group" ng-class="{'has-error': invalidHours}">
<input style="width:50px;" type="text" placeholder="HH" ng-model="hours" ng-change="updateHours()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}" ng-disabled="disabled">
<input style="width:50px;" type="text" placeholder="HH" ng-model="hours" ng-change="updateHours()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}" ng-disabled="disabled" ng-blur="blur()">
</td>
<td>:</td>
<td class="form-group" ng-class="{'has-error': invalidMinutes}">
<input style="width:50px;" type="text" placeholder="MM" ng-model="minutes" ng-change="updateMinutes()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}" ng-disabled="disabled">
<input style="width:50px;" type="text" placeholder="MM" ng-model="minutes" ng-change="updateMinutes()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}" ng-disabled="disabled" ng-blur="blur()">
</td>
<td ng-show="showSeconds">:</td>
<td class="form-group" ng-class="{'has-error': invalidSeconds}" ng-show="showSeconds">
<input style="width:50px;" type="text" ng-model="seconds" ng-change="updateSeconds()" class="form-control text-center" ng-readonly="readonlyInput" maxlength="2" tabindex="{{::tabindex}}" ng-disabled="disabled">
<input style="width:50px;" type="text" ng-model="seconds" ng-change="updateSeconds()" class="form-control text-center" ng-readonly="readonlyInput" maxlength="2" tabindex="{{::tabindex}}" ng-disabled="disabled" ng-blur="blur()">
</td>
<td ng-show="showMeridian"><button type="button" ng-class="{disabled: noToggleMeridian()}" class="btn btn-default text-center" ng-click="toggleMeridian()" ng-disabled="noToggleMeridian()" tabindex="{{::tabindex}}">{{meridian}}</button></td>
</tr>
Expand Down

0 comments on commit fe69386

Please sign in to comment.