From ee871602724c991f858ce25f4f7ff72e17782098 Mon Sep 17 00:00:00 2001 From: Daniel Zimmermann Date: Thu, 19 Mar 2015 12:51:50 +1100 Subject: [PATCH] fix(typeahead): reset 'parse' validation key When typeahead-editable="false" and we select a perfect match from the results list, the $parsers aren't called, which results in the 'parse' error key not being reset. As with the 'editable' key, we should reset this once an item is selected because we know we have a valid model matching the view value. Closes #3166 --- src/typeahead/test/typeahead.spec.js | 12 +++++++++++- src/typeahead/typeahead.js | 5 +++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 8f2c7323c1..d83571a7c8 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -630,6 +630,16 @@ describe('typeahead tests', function () { $(match).click(); $scope.$digest(); }); + + it('issue #3166 - should set \'parse\' key as valid when selecting a perfect match and not editable', function () { + var element = prepareInputEl('
'); + var inputEl = findInput(element); + + changeInputValueTo(element, 'Alaska'); + triggerKeyDown(element, 13); + + expect($scope.test.typeahead.$error.parse).toBeUndefined(); + }); }); describe('input formatting', function () { @@ -741,7 +751,7 @@ describe('typeahead tests', function () { }; var element = prepareInputEl('
'); changeInputValueTo(element, 'b'); - + // enter key should not be captured when nothing is focused triggerKeyDown(element, 13); expect($scope.keyDownEvent.isDefaultPrevented()).toBeFalsy(); diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 06baa1be81..cc4b889c0b 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -172,7 +172,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap //we need to propagate user's query so we can higlight matches scope.query = undefined; - //Declare the timeout promise var outside the function scope so that stacked calls can be cancelled later + //Declare the timeout promise var outside the function scope so that stacked calls can be cancelled later var timeoutPromise; var scheduleSearchWithTimeout = function(inputValue) { @@ -252,6 +252,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap model = parserResult.modelMapper(originalScope, locals); $setModelValue(originalScope, model); modelCtrl.$setValidity('editable', true); + modelCtrl.$setValidity('parse', true); onSelectCallback(originalScope, { $item: item, @@ -326,7 +327,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap }); var $popup = $compile(popUpEl)(scope); - + if (appendToBody) { $document.find('body').append($popup); } else {