Skip to content

Commit

Permalink
fix(datepickerPopup): support ngModelOptions.allowInvalid angular-ui#…
Browse files Browse the repository at this point in the history
  • Loading branch information
npbenjohnson committed Apr 14, 2017
1 parent a2dee1b commit a710c87
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ lib-cov
*.swo
.DS_Store
.idea
.vscode

pids
logs
Expand Down
13 changes: 12 additions & 1 deletion src/datepickerPopup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,22 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $

$scope.date = dateParser.fromTimezone(value, ngModelOptions.getOption('timezone'));

if (ngModelOptions.getOption('allowInvalid') && isNaN($scope.date)) {
$scope.date = value;
return value;
}

return dateParser.filter($scope.date, dateFormat);
});
} else {
ngModel.$formatters.push(function(value) {
$scope.date = dateParser.fromTimezone(value, ngModelOptions.getOption('timezone'));

if (ngModelOptions.getOption('allowInvalid') && isNaN($scope.date)) {
$scope.date = value;
return value;
}

return value;
});
}
Expand Down Expand Up @@ -191,7 +202,7 @@ function($scope, $element, $attrs, $compile, $log, $parse, $window, $document, $
} else if (angular.isDate($scope.datepickerOptions[key])) {
dates[key] = new Date($scope.datepickerOptions[key]);
} else {
if ($datepickerPopupLiteralWarning) {
if ($datepickerPopupLiteralWarning && !ngModelOptions.getOption('allowInvalid')) {
$log.warn('Literal date support has been deprecated, please switch to date object usage');
}

Expand Down
25 changes: 25 additions & 0 deletions src/datepickerPopup/test/popup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,31 @@ describe('datepicker popup', function() {
});
});

describe('works with ngModelOptions allowInvalid', function() {
var $timeout, wrapElement;

beforeEach(inject(function(_$document_, _$sniffer_, _$timeout_) {
$document = _$document_;
$timeout = _$timeout_;
$rootScope.date = 'Invalid Date';
wrapElement = $compile('<div><input ng-model="date" ' +
'ng-model-options="{allowInvalid: true}" ' +
'uib-datepicker-popup><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
}));

it('should initially display invalid date', function() {
expect(inputEl.val()).toEqual('Invalid Date');
});

it('should display invalid date on change', function(){
$rootScope.date = 'Another Invalid Date';
$rootScope.$digest();
expect(inputEl.val()).toEqual('Another Invalid Date');
});
});

describe('attribute `datepickerOptions`', function() {
describe('show-weeks', function() {
beforeEach(function() {
Expand Down

0 comments on commit a710c87

Please sign in to comment.