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

fix(datepickerPopup): support ngModelOptions.allowInvalid #6519

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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