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

Commit

Permalink
fix(typeahead): prevent accidental form submission on ENTER
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Oct 27, 2013
1 parent 85647c9 commit 253c49f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/typeahead/test/typeahead.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('typeahead tests', function () {
var e = $.Event("keydown");
e.which = keyCode;
inputEl.trigger(e);
return e;
};

//custom matchers
Expand Down Expand Up @@ -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("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue'></div>");
var e = triggerKeyDown(element, 13);

expect(e.isDefaultPrevented()).toBeTruthy();
});

it('does not close matches popup on click in input', function () {
var element = prepareInputEl("<div><input ng-model='result' typeahead='item for item in source | filter:$viewValue'></div>");
var inputEl = findInput(element);
Expand Down
3 changes: 3 additions & 0 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

4 comments on commit 253c49f

@scamden
Copy link
Contributor

@scamden scamden commented on 253c49f Dec 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkozlowski-opensource this commit breaks our use of typeahead, could help me understand why you would want to prevent enter when the typeahead is closed? wouldn't you actually want to allow the user to submit the form in that case?

@pkozlowski-opensource
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scamden this commit originally came from a pull request and I guess I wasn't careful enough while working on it, my bad.
The idea in the original PR was to prevent accidental forms submission when a popup with matches is open (which kind of makes sense as you wouldn't probably like to prevent form submission when people are selecting a match).

Discussion about this issue is in #1298
Based on all the discussion I'm close to reversing it now.

@scamden
Copy link
Contributor

@scamden scamden commented on 253c49f Dec 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh ok thanks!

@alonisser
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for reversing

Please sign in to comment.