Skip to content

Commit 6ce2f0e

Browse files
Fixes bugs with highlighting a choice when didn't mean to (#39)
There was a weird issue where if you type in a bunch of text in the input field, then select all and hit backspace, it would delete the text, and then highlight a choice. This was because we were only checking that selection start was the same between key down and key up, which it would be if we selected all and deleted. To combat this, I'm now also checking selection end to make sure it's the same, which would be the case when the text is deleted since the selection will have collapsed. Additionally, I added using the DELETE key to to navigate when in the field (so it works as the opposite of BACKSPACE), and restricted using DELETE and BACKSPACE to select choices to only when the field is empty.
1 parent 4a471b6 commit 6ce2f0e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngc-omnibox",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "A modern, flexible, Angular 1.x autocomplete library with limited assumptions.",
55
"main": "dist/ngc-omnibox.js",
66
"scripts": {

src/angularComponent/ngcOmniboxController.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ export default class NgcOmniboxController {
446446
_handleKeyDown({keyCode}) {
447447
if (this.doc.activeElement === this._fieldElement) {
448448
this.selectionStartKeyDown = this.doc.activeElement.selectionStart;
449+
this.selectionEndKeyDown = this.doc.activeElement.selectionEnd;
449450
}
450451

451452
if (this.shouldShowSuggestions()) {
@@ -480,16 +481,19 @@ export default class NgcOmniboxController {
480481
if (this.hasChoices) {
481482
if (this.doc.activeElement === this._fieldElement) {
482483
this.selectionStartKeyUp = this.doc.activeElement.selectionStart;
484+
this.selectionEndKeyUp = this.doc.activeElement.selectionEnd;
483485

484486
// We should now consider navigating out of the input field. We only want to trigger this
485487
// when we are already at the beginning or end of the field and hit Left or Right *again*.
486-
if (this.selectionStartKeyDown === this.selectionStartKeyUp) {
488+
if (this.selectionStartKeyDown === this.selectionStartKeyUp && this.selectionEndKeyDown ===
489+
this.selectionEndKeyUp) {
487490
const inputLength = this._fieldElement.value.length;
488491

489-
if ((keyCode === KEY.LEFT || keyCode === KEY.BACKSPACE) &&
492+
if ((keyCode === KEY.LEFT || (keyCode === KEY.BACKSPACE && !this.query)) &&
490493
this.selectionStartKeyUp === 0) {
491494
this.highlightLastChoice();
492-
} else if (keyCode === KEY.RIGHT && this.selectionStartKeyUp === inputLength) {
495+
} else if ((keyCode === KEY.RIGHT || keyCode === KEY.DELETE && !this.query) &&
496+
this.selectionStartKeyUp === inputLength) {
493497
this.highlightFirstChoice();
494498
}
495499
}

0 commit comments

Comments
 (0)