-
Notifications
You must be signed in to change notification settings - Fork 6.7k
typeahead-focus-first=false option to prevent first match from being focused #2916
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,8 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap | |
|
||
var appendToBody = attrs.typeaheadAppendToBody ? originalScope.$eval(attrs.typeaheadAppendToBody) : false; | ||
|
||
var focusFirst = originalScope.$eval(attrs.typeaheadFocusFirst) !== false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's safer and more consistent to do same check as above (on line 60, to check whether the attrs expression is truthy/not empty) as that would work with any falsey value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied this from isEditable on line 50 which is also default-true. Line 60 for appendToBody is default-false. If you prefer different style for default-true attributes, I would suggest updating both isEditable and focusFirst, but I think that's outside of the scope of this pull request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, in that case this is fine. |
||
|
||
//INTERNAL VARIABLES | ||
|
||
//model setter executed upon match selection | ||
|
@@ -131,7 +133,7 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap | |
if (onCurrentRequest && hasFocus) { | ||
if (matches.length > 0) { | ||
|
||
scope.activeIdx = 0; | ||
scope.activeIdx = focusFirst ? 0 : -1; | ||
scope.matches.length = 0; | ||
|
||
//transform labels | ||
|
@@ -272,14 +274,19 @@ angular.module('ui.bootstrap.typeahead', ['ui.bootstrap.position', 'ui.bootstrap | |
return; | ||
} | ||
|
||
// if there's nothing selected (i.e. focusFirst) and enter is hit, don't do anything | ||
if (scope.activeIdx == -1 && (evt.which === 13 || evt.which === 9)) { | ||
return; | ||
} | ||
|
||
evt.preventDefault(); | ||
|
||
if (evt.which === 40) { | ||
scope.activeIdx = (scope.activeIdx + 1) % scope.matches.length; | ||
scope.$digest(); | ||
|
||
} else if (evt.which === 38) { | ||
scope.activeIdx = (scope.activeIdx ? scope.activeIdx : scope.matches.length) - 1; | ||
scope.activeIdx = (scope.activeIdx > 0 ? scope.activeIdx : scope.matches.length) - 1; | ||
scope.$digest(); | ||
|
||
} else if (evt.which === 13 || evt.which === 9) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a test for when pressing enter or tab when
activeIdx == -1
, nothing should happen and nothing should be selected.