From 1d9294c8e6b0d888fd77317cb634dd37bb9b5094 Mon Sep 17 00:00:00 2001 From: Chenyu Zhang Date: Tue, 27 Oct 2015 17:49:20 -0400 Subject: [PATCH] fix(typeahead): clear typeahead input when editable is false Closes #1620 Closes #4265 Closes #4752 --- src/typeahead/test/typeahead.spec.js | 36 ++++++++++++++++++++++++++++ src/typeahead/typeahead.js | 3 +++ 2 files changed, 39 insertions(+) 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; });