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

Commit 1d9294c

Browse files
chenyuzhcywesleycho
authored andcommitted
fix(typeahead): clear typeahead input when editable is false
Closes #1620 Closes #4265 Closes #4752
1 parent 623e564 commit 1d9294c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/typeahead/test/typeahead.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,42 @@ describe('typeahead tests', function() {
259259
expect($scope.form.input.$error.editable).toBeFalsy();
260260
});
261261

262+
it('should clear view value after blur for typeahead-editable="false"', function () {
263+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false"></div>');
264+
var inputEl = findInput(element);
265+
266+
changeInputValueTo(element, 'not in matches');
267+
expect($scope.result).toEqual(undefined);
268+
expect(inputEl.val()).toEqual('not in matches');
269+
inputEl.blur(); // input loses focus
270+
expect($scope.result).toEqual(undefined);
271+
expect(inputEl.val()).toEqual('');
272+
});
273+
274+
it('should clear view value when no value selected for typeahead-editable="false" typeahead-select-on-blur="false"', function () {
275+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false" typeahead-select-on-blur="false"></div>');
276+
var inputEl = findInput(element);
277+
278+
changeInputValueTo(element, 'b');
279+
expect($scope.result).toEqual(undefined);
280+
expect(inputEl.val()).toEqual('b');
281+
inputEl.blur(); // input loses focus
282+
expect($scope.result).toEqual(undefined);
283+
expect(inputEl.val()).toEqual('');
284+
});
285+
286+
it('should not clear view value when there is match but no value selected for typeahead-editable="false" typeahead-select-on-blur="true"', function () {
287+
var element = prepareInputEl('<div><input ng-model="result" uib-typeahead="item for item in source | filter:$viewValue" typeahead-editable="false" typeahead-select-on-blur="true"></div>');
288+
var inputEl = findInput(element);
289+
290+
changeInputValueTo(element, 'b');
291+
expect($scope.result).toEqual(undefined);
292+
expect(inputEl.val()).toEqual('b');
293+
inputEl.blur(); // input loses focus
294+
expect($scope.result).toEqual('bar');
295+
expect(inputEl.val()).toEqual('bar');
296+
});
297+
262298
it('should bind loading indicator expression', inject(function($timeout) {
263299
$scope.isLoading = false;
264300
$scope.loadMatches = function(viewValue) {

src/typeahead/typeahead.js

+3
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position'])
336336
scope.select(scope.activeIdx);
337337
});
338338
}
339+
if (!isEditable && modelCtrl.$error.editable) {
340+
element.val('');
341+
}
339342
hasFocus = false;
340343
selected = false;
341344
});

0 commit comments

Comments
 (0)