diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index 9e85ab3f88..0ccb243220 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -259,6 +259,42 @@ describe('typeahead tests', function() { expect($scope.form.input.$error.editable).toBeFalsy(); }); + it('should clear view value after blur for typeahead-editable="false"', function () { + var element = prepareInputEl('
'); + var inputEl = findInput(element); + + changeInputValueTo(element, 'not in matches'); + expect($scope.result).toEqual(undefined); + expect(inputEl.val()).toEqual('not in matches'); + inputEl.blur(); // input loses focus + expect($scope.result).toEqual(undefined); + expect(inputEl.val()).toEqual(''); + }); + + it('should clear view value when no value selected for typeahead-editable="false" typeahead-select-on-blur="false"', function () { + var element = prepareInputEl(''); + var inputEl = findInput(element); + + changeInputValueTo(element, 'b'); + expect($scope.result).toEqual(undefined); + expect(inputEl.val()).toEqual('b'); + inputEl.blur(); // input loses focus + expect($scope.result).toEqual(undefined); + expect(inputEl.val()).toEqual(''); + }); + + it('should not clear view value when there is match but no value selected for typeahead-editable="false" typeahead-select-on-blur="true"', function () { + var element = prepareInputEl(''); + var inputEl = findInput(element); + + changeInputValueTo(element, 'b'); + expect($scope.result).toEqual(undefined); + expect(inputEl.val()).toEqual('b'); + inputEl.blur(); // input loses focus + expect($scope.result).toEqual('bar'); + expect(inputEl.val()).toEqual('bar'); + }); + it('should bind loading indicator expression', inject(function($timeout) { $scope.isLoading = false; $scope.loadMatches = function(viewValue) { diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 17c7d01764..37b041a81e 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -336,6 +336,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position']) scope.select(scope.activeIdx); }); } + if (!isEditable && modelCtrl.$error.editable) { + element.val(''); + } hasFocus = false; selected = false; });