Skip to content

Commit d9066b9

Browse files
Actually fix element blurring bug (#60)
Was caused by the highlighted choice being un-set.
1 parent b1cf051 commit d9066b9

File tree

3 files changed

+7
-17
lines changed

3 files changed

+7
-17
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.3.3",
3+
"version": "0.3.4",
44
"description": "A modern, flexible, Angular 1.x autocomplete library with limited assumptions.",
55
"main": "dist/ngc-omnibox.js",
66
"scripts": {

spec/tests/angularComponent/ngcOmniboxControllerSpec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ describe('ngcOmnibox.angularComponent.ngcOmniboxController', () => {
1010
querySelector() {},
1111
querySelectorAll() {},
1212
focus() {},
13-
blur() {}
13+
blur() {},
14+
contains() {}
1415
};
1516

1617
beforeEach(() => {
@@ -534,13 +535,15 @@ describe('ngcOmnibox.angularComponent.ngcOmniboxController', () => {
534535
});
535536

536537
it('should focus the component when highlighting a choice', () => {
538+
omniboxController.doc.activeElement = omniboxController.element;
537539
spyOn(omniboxController.element, 'focus');
538540

539541
omniboxController.highlightedChoice = 'one';
540542
expect(omniboxController.element.focus).toHaveBeenCalled();
541543
});
542544

543545
it('should focus the field when highlighting no choice', () => {
546+
omniboxController.doc.activeElement = omniboxController.element;
544547
spyOn(omniboxController.fieldElement, 'focus');
545548

546549
omniboxController.highlightedChoice = null;

src/angularComponent/ngcOmniboxController.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,6 @@ export default class NgcOmniboxController {
5656
$scope.$apply();
5757
}
5858

59-
// Focus/blur events are finicky, especially when the blur event fires when there's no other
60-
// focus target (such as hitting the ESC key, manually calling blur(), or on mobile when
61-
// hitting "Done" on the keyboard): the field can still be the focused element. This is a
62-
// problem since it means if the user then immediately tries to focus the field again, the
63-
// focus event won't fire (since technically the field is already focused). To combat this,
64-
// we check here to make absolutely certain that the field isn't currently focused and if it
65-
// is, we'll focus our main component element which a.) has a tabindex so we know it's
66-
// focuseable, and b.) allows us to keep listening for more events if we need to. Also,
67-
// since the element that kept focus is a child of our component element, focusing it won't
68-
// call the focus event listener again, avoiding an infinite loop.
69-
if (this.doc.activeElement === this.fieldElement) {
70-
this.element.focus();
71-
}
72-
7359
this.onBlur({event});
7460
}, SUGGESTIONS_BLUR_THRESHOLD);
7561
}, true);
@@ -149,7 +135,8 @@ export default class NgcOmniboxController {
149135
set highlightedChoice(choice) {
150136
this._highlightedChoice = choice;
151137

152-
if (this._fieldElement) {
138+
const focusedEl = this.doc.activeElement;
139+
if (this._fieldElement && (focusedEl === this.element || this.element.contains(focusedEl))) {
153140
if (choice) {
154141
this.element.focus();
155142
} else {

0 commit comments

Comments
 (0)