Skip to content

Commit 4151f73

Browse files
committed
Override Autocomplete.open instead of Autocomplete.onInputBlur
The original implementation inadvertently relied on a browser event property that is only present on firefox. After looking at the original source code in the library and some more experimentation, it looks like we can override a different function to set the library's `interactingWithList` variable to false when the dropdown is initially opened. We can then rely on the `onResultsMouseDown` function to switch that variable back to true when a result is clicked on and trigger the proper selection behavior.
1 parent ca692b9 commit 4151f73

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

app/javascript/auto-complete-oninputblur.js renamed to app/javascript/auto-complete-open.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
import Autocomplete from '@github/auto-complete-element/dist/autocomplete.js'
1818

19-
Autocomplete.prototype.onInputBlur = function(event) {
20-
let target = event.explicitOriginalTarget
21-
// Target will either be the <li> element or the text node of the typeahead result.
22-
// The text nodes are wrapped in a span, so we need to look at the grandparent element
23-
// to get the <li> role.
24-
if ('option' === (target.role || target.parentElement.parentElement.role) ) return;
25-
this.close();
19+
Autocomplete.prototype.open = function(event) {
20+
const isHidden = this.results.popover ? !this.results.matches(':popover-open') : this.results.hidden
21+
if (isHidden) {
22+
this.combobox.start()
23+
if (this.results.popover) {
24+
this.results.showPopover()
25+
} else {
26+
this.results.hidden = false
27+
}
28+
}
29+
this.container.open = true
30+
this.interactingWithList = false
2631
};

app/javascript/packs/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import Search from '../components/Search';
4040
import MediaObjectRamp from '../components/MediaObjectRamp';
4141
import ReactButtonContainer from '../components/ReactButtonContainer';
4242
import PlaylistRamp from '../components/PlaylistRamp';
43-
import '../auto-complete-oninputblur.js';
43+
import '../auto-complete-open.js';
4444
import '@github/auto-complete-element';
4545

4646
ReactOnRails.register({

0 commit comments

Comments
 (0)