From 366e0c8a1ff7fee8eb96e1c317a658957785f457 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Thu, 15 Aug 2013 18:09:30 +0200 Subject: [PATCH] fix(typeahead): set validity flag for non-editable inputs Closes #806 --- src/typeahead/test/typeahead.spec.js | 19 ++++++++++++++++++- src/typeahead/typeahead.js | 14 +++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index b1cee67618..e93d5e18b7 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -163,11 +163,28 @@ describe('typeahead tests', function () { }); it('should support the editable property to limit model bindings to matches only', function () { - var element = prepareInputEl("
"); + var element = prepareInputEl("
ng-model='result' typeahead='item for item in source | filter:$viewValue' typeahead-editable='false'>
"); changeInputValueTo(element, 'not in matches'); expect($scope.result).toEqual(undefined); }); + it('should set validation erros for non-editable inputs', function () { + + var element = prepareInputEl( + "
" + + "" + + "
"); + + changeInputValueTo(element, 'not in matches'); + expect($scope.result).toEqual(undefined); + expect($scope.form.input.$error.editable).toBeTruthy(); + + changeInputValueTo(element, 'foo'); + triggerKeyDown(element, 13); + expect($scope.result).toEqual('foo'); + expect($scope.form.input.$error.editable).toBeFalsy(); + }); + it('should bind loading indicator expression', inject(function ($timeout) { $scope.isLoading = false; diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index f03e7383bc..df55c8b2d4 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -29,7 +29,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position']) }; }]) - .directive('typeahead', ['$compile', '$parse', '$q', '$timeout', '$document', '$position', 'typeaheadParser', function ($compile, $parse, $q, $timeout, $document, $position, typeaheadParser) { + .directive('typeahead', ['$compile', '$parse', '$q', '$timeout', '$document', '$position', 'typeaheadParser', + function ($compile, $parse, $q, $timeout, $document, $position, typeaheadParser) { var HOT_KEYS = [9, 13, 27, 38, 40]; @@ -158,7 +159,12 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position']) } } - return isEditable ? inputValue : undefined; + if (isEditable) { + return inputValue; + } else { + modelCtrl.$setValidity('editable', false); + return undefined; + } }); modelCtrl.$formatters.push(function (modelValue) { @@ -192,6 +198,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position']) locals[parserResult.itemName] = item = scope.matches[activeIdx].model; model = parserResult.modelMapper(originalScope, locals); $setModelValue(originalScope, model); + modelCtrl.$setValidity('editable', true); onSelectCallback(originalScope, { $item: item, @@ -199,8 +206,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position']) $label: parserResult.viewMapper(originalScope, locals) }); - //return focus to the input element if a mach was selected via a mouse click event resetMatches(); + + //return focus to the input element if a mach was selected via a mouse click event element[0].focus(); };