From 253c49ff26c1bb6c324a0549bc51ee4f45f66429 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sun, 27 Oct 2013 19:22:17 +0100 Subject: [PATCH] fix(typeahead): prevent accidental form submission on ENTER --- src/typeahead/test/typeahead.spec.js | 8 ++++++++ src/typeahead/typeahead.js | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/typeahead/test/typeahead.spec.js b/src/typeahead/test/typeahead.spec.js index b5ddb13c06..5fd8155b5f 100644 --- a/src/typeahead/test/typeahead.spec.js +++ b/src/typeahead/test/typeahead.spec.js @@ -60,6 +60,7 @@ describe('typeahead tests', function () { var e = $.Event("keydown"); e.which = keyCode; inputEl.trigger(e); + return e; }; //custom matchers @@ -438,6 +439,13 @@ describe('typeahead tests', function () { expect($scope.isLoading).toBeFalsy(); }); + it('pr 1165 - prevent default on ENTER to avoid accidental form submission', function () { + var element = prepareInputEl("
"); + var e = triggerKeyDown(element, 13); + + expect(e.isDefaultPrevented()).toBeTruthy(); + }); + it('does not close matches popup on click in input', function () { var element = prepareInputEl("
"); var inputEl = findInput(element); diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index cc3ee79a82..ab52005c1f 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -228,6 +228,9 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap //typeahead is open and an "interesting" key was pressed if (scope.matches.length === 0 || HOT_KEYS.indexOf(evt.which) === -1) { + if (evt.which === 13) { + evt.preventDefault(); + } return; }